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 #ifndef MODULE_H_INCLUDED
00019 #define MODULE_H_INCLUDED
00020
00021 #include "common.h"
00022 #include "array.h"
00023
00024 class Socket;
00025 class Socket8;
00026 class Socket16;
00027 class SocketF;
00028 class PlugF;
00029
00030
00031 #define MAX_MODULES 20
00032
00034 struct SOCKETRECORD
00035 {
00036 Socket * socket;
00037 const char * name;
00038 };
00039
00041 class SocketArray : public GrowArray<SOCKETRECORD>
00042 {
00043 public:
00044 ~SocketArray();
00045 void Add(const char * name, Socket * socket);
00046 Socket * Get(const char * name, const char * type = NULL);
00047 bool Replace(Socket* oldSocket, Socket * newSocket);
00048 };
00049
00050
00053
00067 #define DECLARE_MODULE(Name,Parent)\
00068 public:\
00069 virtual const char * GetName(){return(#Name);}\
00070 virtual void PrintName(HardwareSerial& port){\
00071 port.print(#Name);\
00072 port.print(".");\
00073 Parent::PrintName(port);}
00074
00076
00077 class Module
00078 {
00079 public:
00080
00082
00084 Module();
00085
00087
00088 virtual void Init(){}
00089
00091
00098 virtual void Reference(){};
00099
00101
00104 virtual void ParamChanged(Socket * param){};
00105
00107
00110 virtual void Loop(const unsigned long& interval){};
00111
00112
00114
00116
00117 virtual const char * GetName()
00118 {
00119 return("Module");
00120 }
00121
00123
00124 virtual void PrintName(HardwareSerial& port)
00125 {
00126 port.print("Module");
00127 };
00128
00130
00131 bool Poll(const long& time);
00132
00134
00136 Socket * GetSocket(const char * name, const char * type = NULL)
00137 {
00138 return(m_sockets.Get(name,type));
00139 }
00140
00142
00143 bool ReplaceSocket(Socket* oldSocket, Socket * newSocket)
00144 {
00145 return(m_sockets.Replace(oldSocket, newSocket));
00146 }
00147
00148
00150 SocketArray& GetSockets()
00151 {
00152 return(m_sockets);
00153 }
00154
00156 inline bool IsRunning(){return(m_running);}
00157
00159 inline int8_t GetPriority(){return(m_priority);}
00160
00162 inline long GetMaxTime(){long t = m_cpuTime; m_cpuTime = 0; return(t);}
00163
00165
00166 int GetInterval(){return (m_interval / 1000);}
00167
00169 inline void CheckTime(long time)
00170 {
00171 if(time > m_cpuTime)
00172 {
00173 m_cpuTime = time;
00174 }
00175
00176 }
00177
00178 protected:
00179
00180
00182
00183 void SetInterval(unsigned int time);
00184
00186
00195 void SetPriority(const int8_t priority)
00196 {
00197 m_priority = priority;
00198 if(m_priority > 100)
00199 {
00200 m_priority = 100;
00201 }
00202 }
00203
00205
00206 void AddSocket(const char * name, Socket * socket)
00207 {
00208 m_sockets.Add(name,socket);
00209 }
00210
00212
00214 void AddSocketArray(const char * names[], Socket8 sockets[], int n);
00216
00218 void AddSocketArray(const char * names[], Socket16 sockets[], int n);
00220
00222 void AddSocketArray(const char * names[], SocketF sockets[], int n);
00223
00225
00227 void ConnectPlugArray(const char * names[], PlugF plugs[], int n);
00228
00230 inline float GetTime(const unsigned long& interval)
00231 {
00232 float result = interval;
00233 result /= 1000000;
00234 return (result);
00235 }
00236
00237 private:
00238 SocketArray m_sockets;
00239 long m_interval;
00240 long m_lastTime;
00241 volatile byte m_running;
00242 int8_t m_priority;
00243 long m_cpuTime;
00244 };
00245
00246
00247 #define MAX_THREADS 6
00248
00249 #define PRIORITY_NEVER -1
00250
00252
00255 class ModuleManager
00256 {
00257 public:
00258 ModuleManager();
00259
00261
00263 void Init();
00264
00265
00267
00268 Module * GetModule(const char * name);
00269
00271
00274 Socket * GetSocket(const char * name, const char * type = NULL, int * moduleNumber = NULL);
00275
00277
00278 bool ReplaceSocket(Socket* oldSocket, Socket * newSocket);
00279
00280
00282
00286 bool ParseParameter(char * name, char * string);
00287
00289
00291 bool WriteParameters();
00292
00294
00295 void Reference();
00296
00298
00299 void Add(Module * module){m_modules.Add(module);}
00300
00302 inline int GetCount(){return(m_modules.GetCount());}
00303
00305 inline Module * GetModule(int mod){return (m_modules[mod]);}
00306
00308 void PrintVersion(Print& print, const bool full);
00309
00311
00314 static void InterruptHandler();
00315
00317
00318 int GetOverloadCount(){int m = m_overloadCount; m_overloadCount = 0; return(m); }
00320
00321 int GetMaxThreads(){int m = m_maxThreads; m_maxThreads = 0; return(m); }
00323
00324 int GetHeartbeat(){return (m_heartbeat & 0x7fff);}
00325
00326 private:
00327
00328 enum READMODE{modeREAD,modeWRITE,modeCHECKSUM};
00329
00330
00331
00332 bool ReadParameters();
00333 bool ReadWriteParameters(const READMODE mode);
00334 void CreateChecksum();
00335
00336 int m_checksum;
00337 static GrowArray<Module *> m_modules;
00338 static volatile int m_threadCount;
00339 static int m_maxThreads;
00340 static int m_overloadCount;
00341 static int m_heartbeat;
00342 static int8_t m_threadStack[MAX_THREADS + 1];
00343 bool m_started;
00344
00345 };
00346
00347 extern ModuleManager g_moduleManager;
00348
00349 #endif //#define MODULE_H_INCLUDED
00350
00351