Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 bool g_errFirst = true;
00019 static const char * g_errHdr = "ERROR:";
00020
00021 void _CheckError()
00022 {
00023 if (g_errFirst)
00024 {
00025 ERROR_PORT.print(g_errHdr);
00026 g_errFirst = false;
00027 }
00028 }
00029
00030 void Error(const char * src)
00031 {
00032 _CheckError();
00033 ERROR_PORT.print(src);
00034 }
00035
00036 void Errorln(const char * src)
00037 {
00038 _CheckError();
00039 ERROR_PORT.println(src);
00040 g_errFirst = true;
00041 }
00042
00043 void Error(const int src)
00044 {
00045 _CheckError();
00046 ERROR_PORT.print(src);
00047 }
00048
00049 void Errorln(const int src)
00050 {
00051 _CheckError();
00052 ERROR_PORT.println(src);
00053 g_errFirst = true;
00054 }
00055
00056 void Error(const float src)
00057 {
00058 _CheckError();
00059 ERROR_PORT.print(src);
00060 }
00061
00062 void Errorln(const float src)
00063 {
00064 _CheckError();
00065 ERROR_PORT.println(src);
00066 g_errFirst = true;
00067 }
00068
00069
00070
00071
00072 #define MAX_CMD_LENGTH 50
00073
00074 #define EOT '\n'
00075
00076
00078
00087 class SerialComms : public Module
00088 {
00089 DECLARE_MODULE(SerialComms,Module)
00090
00091 public:
00092 SerialComms(HardwareSerial& port, long int baud)
00093 {
00094 m_port = &port;
00095 m_baud = baud;
00096 }
00097
00098
00099 void Init()
00100 {
00101 m_port->begin(m_baud);
00102 SetInterval(20);
00103 SetPriority(0);
00104 m_bufIdx = 0;
00105
00106 m_port->println("#######################");
00107 m_port->println("# #");
00108 m_port->println("# Welcome to FlightOS #");
00109 m_port->println("# #");
00110 m_port->println("#######################");
00111 }
00112
00113
00114 virtual void Loop(const unsigned long& interval)
00115 {
00116 while (m_port->available() >0)
00117 {
00118 char c = m_port->read();
00119 if (c == EOT)
00120 {
00121 m_cmdBuffer[m_bufIdx] = 0;
00122 HandleCommand();
00123 return;
00124 }
00125 if (c < ' ')
00126 {
00127 continue;
00128 }
00129 if (m_bufIdx >= MAX_CMD_LENGTH -1)
00130 {
00131 m_bufIdx = 0;
00132 Errorln("Buffer overflow");
00133 }
00134 m_cmdBuffer[m_bufIdx++] = c;
00135 }
00136
00137 static int loopCount = 1;
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 }
00159
00160 private:
00161 void HandleCommand()
00162 {
00163 m_bufIdx = 0;
00164 bool stop = true;
00165 bool err = false;
00166 switch (m_cmdBuffer[0])
00167 {
00168 case 'g':
00169 PrintSocket();
00170 break;
00171
00172 case 's':
00173 SetParameter();
00174 break;
00175
00176 case 'l':
00177 ListModules();
00178 break;
00179
00180 case 'a':
00181 AppendList();
00182 break;
00183
00184 case 'c':
00185 m_list.Empty();
00186 break;
00187
00188 case 'r':
00189 m_port->print("*");
00190 for (int ct=0; ct< m_list.GetCount(); ct++)
00191 {
00192 if (ct !=0 )
00193 {
00194 m_port->print(", ");
00195 }
00196 m_list[ct]->Print(*m_port);
00197 }
00198 m_port->println("");
00199 err = true;
00200
00201
00202
00203 break;
00204
00205 case 'w':
00206 if (strcmp(m_cmdBuffer,"writeEEPROM") !=0)
00207 {
00208 Errorln("Invalid memory write command");
00209 }
00210 else
00211 {
00212 g_moduleManager.WriteParameters();
00213 }
00214 break;
00215
00216 case 'p':
00217 break;
00218
00219 case 'd':
00220 ShowDiagnostics();
00221 break;
00222
00223 case 'v':
00224 g_moduleManager.PrintVersion(*m_port,true);
00225 m_port->println();
00226 break;
00227
00228 default:
00229 err = true;
00230 Errorln("Unknown command");
00231 }
00232 if (!err)
00233 {
00234 m_port->println("ok");
00235 }
00236 if (stop)
00237 {
00238 m_run = false;
00239 }
00240 }
00241
00242 void PrintSocket()
00243 {
00244 Socket * val = g_moduleManager.GetSocket(m_cmdBuffer + 1);
00245 if (!val)
00246 {
00247 return;
00248 }
00249 val->Print(*m_port);
00250 m_port->println("");
00251 }
00252
00253 void SetParameter()
00254 {
00255 char * parPtr = m_cmdBuffer + 1;
00256 while (*parPtr !=0 && *parPtr != '=')
00257 {
00258 parPtr++;
00259 }
00260 if (*parPtr == 0)
00261 {
00262 Errorln("No number");
00263 return;
00264 }
00265 *parPtr=0;
00266 parPtr++;
00267
00268 g_moduleManager.ParseParameter(m_cmdBuffer + 1,parPtr);
00269 }
00270
00271 void ListModules()
00272 {
00273 for (int ct=0; ct< g_moduleManager.GetCount(); ct++)
00274 {
00275 Module * mod = g_moduleManager.GetModule(ct);
00276 mod->PrintName(*m_port);
00277 m_port->println();
00278 SocketArray& sockets = mod->GetSockets();
00279 for (int ct2=0; ct2 < sockets.GetCount(); ct2++)
00280 {
00281 SOCKETRECORD& rec = sockets[ct2];
00282 m_port->print(" ");
00283 #ifdef ARDUINO
00284 PrintProgString(m_port,rec.name);
00285 #else
00286 m_port->print(rec.name);
00287 #endif
00288 if (rec.socket->IsParameter())
00289 {
00290 m_port->print(" W ");
00291 }
00292 else
00293 {
00294 m_port->print(" R ");
00295 }
00296 m_port->print(rec.socket->GetType());
00297 m_port->print(" ");
00298 m_port->println((ct << 8) + ct2,HEX);
00299 }
00300 }
00301 }
00302
00303
00304 void ShowDiagnostics()
00305 {
00306
00307
00308 uint8_t *heapptr, *stackptr, *testptr;
00309 stackptr = (uint8_t *)malloc(4);
00310 heapptr = stackptr;
00311 free(stackptr);
00312 stackptr = (uint8_t *)(SP);
00313
00314 m_port->print("Heap ptr = ");
00315 m_port->println((long int)heapptr);
00316 m_port->print("Stack ptr = ");
00317 m_port->println((long int)stackptr);
00318
00319
00320
00321
00322 int ct=0;
00323 testptr = heapptr;
00324 while(testptr < stackptr)
00325 {
00326 if(*testptr++ !=0)
00327 {
00328 if(ct < 20)
00329 {
00330 ct=0;
00331 continue;
00332 }else
00333 {
00334 break;
00335 }
00336 }
00337 ct++;
00338 }
00339
00340 m_port->print("CPU load = ");
00341 m_port->print(GetCPULoad());
00342 m_port->println("%");
00343 m_port->print("Max number of threads = ");
00344 m_port->println(g_moduleManager.GetMaxThreads());
00345 m_port->print("Ran out of threads ");
00346 m_port->print(g_moduleManager.GetOverloadCount());
00347 m_port->println(" times");
00348 m_port->print("Heartbeat = ");
00349 m_port->println(g_moduleManager.GetHeartbeat());
00350 m_port->print("Current free memory = ");
00351 m_port->println(stackptr - heapptr);
00352 m_port->print("Minimum free memory = ");
00353 m_port->println(ct);
00354 for (int ct=0; ct< g_moduleManager.GetCount(); ct++)
00355 {
00356 Module * mod = g_moduleManager.GetModule(ct);
00357 m_port->print(mod->GetName());
00358 m_port->print(": Priority = ");
00359 m_port->print((int)mod->GetPriority());
00360 m_port->print(": Interval = ");
00361 m_port->print((int)mod->GetInterval());
00362 m_port->print(" Max execution time = ");
00363 m_port->println(mod->GetMaxTime());
00364 }
00365 m_port->println();
00366
00367 }
00368
00369 void AppendList()
00370 {
00371 Socket * val = g_moduleManager.GetSocket(m_cmdBuffer + 1);
00372 if (!val)
00373 {
00374 return;
00375 }
00376 m_list.Add(val);
00377 }
00378
00379
00380 private:
00381 char m_cmdBuffer[MAX_CMD_LENGTH];
00382 int m_bufIdx;
00383 bool m_run;
00384 GrowArray<Socket *> m_list;
00385
00386 HardwareSerial * m_port;
00387 long int m_baud;
00388
00389 };
00390