SAE Teensy ECU
IIT SAE Microcontroller programming
Loading...
Searching...
No Matches
manager.cpp
1#include "manager.hpp"
2#include "messageModule.hpp"
3
4namespace Module {
5
6static bool modulesOrdered = false;
7static std::mutex uMux;
8bitmapVal_t s_id;
9Module_t *allModules[maxModules];
10
11// Should we be allowed to re-setup modules?
12bool Manager_t::setupModules() {
13 if (modulesOrdered)
14 return modulesOrdered;
15 Log.d(ID, "Ordering modules", s_id);
16 if (orderModules()) {
17 for (bitmapVal_t i = 0; i < s_id; i++) {
18 Module_t *m = allModules[i];
19 if (m == 0)
20 break;
21 m->setup();
22 }
23 modulesOrdered = true;
24 } else {
25 Log.e(ID, "Failed to order modules");
26 }
27 return modulesOrdered;
28}
29
30void Manager_t::startModules() {
31 std::lock_guard<std::mutex> lock(uMux);
32 if (setupModules()) {
33 Log.d(ID, "Starting modules");
34 for (bitmapVal_t i = 0; i < s_id; i++) {
35 Module_t *m = allModules[i];
36 if (m == 0) {
37 break;
38 } else if (m->getClassID() == ActiveModule_t::classID) {
39 ActiveModule_t *am = static_cast<ActiveModule_t *>(m);
40 if (!am->stackSize) {
41 Log.d(ID, "Not running module, stack size 0", i);
42 continue;
43 }
44 am->start();
45 }
46 }
47 } else {
48 Log.e(ID, "Failed to setup modules");
49 }
50}
51
52void Manager_t::stopModules() {
53 std::lock_guard<std::mutex> lock(uMux);
54 Log.d(ID, "Stopping modules");
55 for (bitmapVal_t i = 0; i < s_id; i++) {
56 Module_t *m = allModules[i];
57 if (m == 0) {
58 break;
59 } else if (m->getClassID() == ActiveModule_t::classID) {
60 ActiveModule_t *am = static_cast<ActiveModule_t *>(m);
61 am->stop();
62 }
63 }
64}
65
66void Manager_t::restartModules() {
67 stopModules();
68 startModules();
69}
70
71void Manager_t::printModules() {
72 for (bitmapVal_t i = 0; i < s_id; i++) {
73 Module_t *m = allModules[i];
74 if (m == 0)
75 break;
76 m->print();
77 }
78}
79
80void Manager_t::start() {
81 Log.d(ID, "Starting");
82 startModules();
83 Log.d(ID, "Starting Loop");
84 while (1) {
85#if CONF_LOGGING_ASCII_DEBUG
86 Serial.println(Thread::infoString());
87#endif
88 Thread::delay(500);
89 }
90}
91} // namespace Module
Logging::Log_t Log
The global logging object.
void d(LOG_TAG TAG, LOG_MSG message)
Log a string using a debug tag.
void e(LOG_TAG TAG, LOG_MSG message)
Log a string using an error tag.