-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppDebug.cpp
145 lines (130 loc) · 3.87 KB
/
AppDebug.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* ProductDebug.cpp
*
* Created on: 14.06.2016
* Author: nid
*/
#include <Arduino.h>
#include <SpinTimer.h>
#include <SerialCommand.h>
#include <DbgCliNode.h>
#include <DbgCliTopic.h>
#include <DbgCliCommand.h>
#include <DbgTraceContext.h>
#include <DbgTracePort.h>
#include <DbgTraceLevel.h>
#include <DbgPrintConsole.h>
#include <DbgTraceOut.h>
#if defined(ESP8266)
extern "C"
{
#include "user_interface.h"
}
#elif defined(ESP32)
#include "Esp.h"
#else
#include <RamUtils.h>
#endif
#include <AppDebug.h>
//-----------------------------------------------------------------------------
// Free Heap Logger
//-----------------------------------------------------------------------------
const unsigned long c_freeHeapLogIntervalMillis = 10000;
class FreeHeapLogTimerAction : public SpinTimerAction
{
private:
DbgTrace_Port* m_trPort;
public:
FreeHeapLogTimerAction()
: m_trPort(new DbgTrace_Port("heap", DbgTrace_Level::info))
{ }
void timeExpired()
{
int freeHeap = 0;
#if defined(ESP8266)
freeHeap = system_get_free_heap_size();
#elif defined(ESP32)
freeHeap = ESP.getFreeHeap();
#else
freeHeap = RamUtils::getFreeRam();
#endif
TR_PRINTF(m_trPort, DbgTrace_Level::debug, "%d bytes free", freeHeap);
}
};
//-----------------------------------------------------------------------------
const unsigned long int baudRate = 9600;
SerialCommand* AppDebug::m_sCmd = 0;
AppDebug::AppDebug(SerialCommand* sCmd)
{
m_sCmd = sCmd;
}
AppDebug::~AppDebug()
{ }
void AppDebug::setupDebugEnv()
{
//-----------------------------------------------------------------------------
// Serial Command Object for Debug CLI
//-----------------------------------------------------------------------------
Serial.begin(baudRate);
DbgCli_Node::AssignRootNode(new DbgCli_Topic(0, "dbg", "Debug CLI Root Node."));
// Setup callbacks for SerialCommand commands
if (0 != m_sCmd)
{
m_sCmd->addCommand("dbg", AppDebug::dbgCliExecute);
m_sCmd->addCommand("hello", AppDebug::sayHello); // Echos the string argument back
m_sCmd->setDefaultHandler(AppDebug::unrecognized); // Handler for command that isn't matched (says "What?")
}
//---------------------------------------------------------------------------
// Debug Trace
//---------------------------------------------------------------------------
new DbgTrace_Context(new DbgCli_Topic(DbgCli_Node::RootNode(), "tr", "Modify debug trace"));
new DbgTrace_Out(DbgTrace_Context::getContext(), "trConOut", new DbgPrint_Console());
//-----------------------------------------------------------------------------
// Free Heap Logger
//-----------------------------------------------------------------------------
new SpinTimer(c_freeHeapLogIntervalMillis, new FreeHeapLogTimerAction, SpinTimer::IS_RECURRING, SpinTimer::IS_AUTOSTART);
}
void AppDebug::dbgCliExecute()
{
if ((0 != m_sCmd) && (0 != DbgCli_Node::RootNode()))
{
const unsigned int firstArgToHandle = 1;
const unsigned int maxArgCnt = 10;
char* args[maxArgCnt];
char* arg = const_cast<char*>("dbg");
unsigned int arg_cnt = 0;
while ((maxArgCnt > arg_cnt) && (0 != arg))
{
args[arg_cnt] = arg;
arg = m_sCmd->next();
arg_cnt++;
}
DbgCli_Node::RootNode()->execute(static_cast<unsigned int>(arg_cnt), const_cast<const char**>(args), firstArgToHandle);
}
}
void AppDebug::sayHello()
{
char *arg;
if (0 != m_sCmd)
{
arg = m_sCmd->next(); // Get the next argument from the SerialCommand object buffer
}
else
{
arg = const_cast<char*>("");;
}
if (arg != NULL) // As long as it exists, take it
{
Serial.print("Hello ");
Serial.println(arg);
}
else
{
Serial.println("Hello, whoever you are");
}
}
// This is the default handler, and gets called when no other command matches.
void AppDebug::unrecognized(const char *command)
{
Serial.println("What?");
}