|
10 | 10 | // We stick them in the '.dtors' segment because it's always included by the linker scripts |
11 | 11 | // even though it never gets called. Who calls exit() in an embedded program anyways? |
12 | 12 | // If someone ever does, though, it'll explode as these aren't function pointers. |
13 | | -static Usermod * const _usermod_table_begin[0] __attribute__((__section__(".dynarray.usermods.0"), unused)) = {}; |
14 | | -static Usermod * const _usermod_table_end[0] __attribute__((__section__(".dynarray.usermods.99"), unused)) = {}; |
| 13 | +DECLARE_DYNARRAY(Usermod*, usermods); |
15 | 14 |
|
16 | 15 | static size_t getCount() { |
17 | | - return &_usermod_table_end[0] - &_usermod_table_begin[0]; |
| 16 | + return DYNARRAY_LENGTH(usermods); |
18 | 17 | } |
19 | 18 |
|
20 | 19 |
|
21 | 20 | //Usermod Manager internals |
22 | | -void UsermodManager::setup() { for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) (*mod)->setup(); } |
23 | | -void UsermodManager::connected() { for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) (*mod)->connected(); } |
24 | | -void UsermodManager::loop() { for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) (*mod)->loop(); } |
25 | | -void UsermodManager::handleOverlayDraw() { for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) (*mod)->handleOverlayDraw(); } |
26 | | -void UsermodManager::appendConfigData(Print& dest) { for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) (*mod)->appendConfigData(dest); } |
| 21 | +void UsermodManager::setup() { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->setup(); } |
| 22 | +void UsermodManager::connected() { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->connected(); } |
| 23 | +void UsermodManager::loop() { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->loop(); } |
| 24 | +void UsermodManager::handleOverlayDraw() { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->handleOverlayDraw(); } |
| 25 | +void UsermodManager::appendConfigData(Print& dest) { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->appendConfigData(dest); } |
27 | 26 | bool UsermodManager::handleButton(uint8_t b) { |
28 | 27 | bool overrideIO = false; |
29 | | - for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) { |
| 28 | + for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) { |
30 | 29 | if ((*mod)->handleButton(b)) overrideIO = true; |
31 | 30 | } |
32 | 31 | return overrideIO; |
33 | 32 | } |
34 | 33 | bool UsermodManager::getUMData(um_data_t **data, uint8_t mod_id) { |
35 | | - for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) { |
| 34 | + for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) { |
36 | 35 | if (mod_id > 0 && (*mod)->getId() != mod_id) continue; // only get data form requested usermod if provided |
37 | 36 | if ((*mod)->getUMData(data)) return true; // if usermod does provide data return immediately (only one usermod can provide data at one time) |
38 | 37 | } |
39 | 38 | return false; |
40 | 39 | } |
41 | | -void UsermodManager::addToJsonState(JsonObject& obj) { for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) (*mod)->addToJsonState(obj); } |
| 40 | +void UsermodManager::addToJsonState(JsonObject& obj) { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->addToJsonState(obj); } |
42 | 41 | void UsermodManager::addToJsonInfo(JsonObject& obj) { |
43 | 42 | auto um_id_list = obj.createNestedArray("um"); |
44 | | - for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) { |
| 43 | + for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) { |
45 | 44 | um_id_list.add((*mod)->getId()); |
46 | 45 | (*mod)->addToJsonInfo(obj); |
47 | 46 | } |
48 | 47 | } |
49 | | -void UsermodManager::readFromJsonState(JsonObject& obj) { for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) (*mod)->readFromJsonState(obj); } |
50 | | -void UsermodManager::addToConfig(JsonObject& obj) { for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) (*mod)->addToConfig(obj); } |
| 48 | +void UsermodManager::readFromJsonState(JsonObject& obj) { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->readFromJsonState(obj); } |
| 49 | +void UsermodManager::addToConfig(JsonObject& obj) { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->addToConfig(obj); } |
51 | 50 | bool UsermodManager::readFromConfig(JsonObject& obj) { |
52 | | - bool allComplete = true; |
53 | | - for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) { |
| 51 | + Serial.printf_P(PSTR("Mods: %d Begin: %08X End: %08X\n"), getCount(), (intptr_t) &DYNARRAY_BEGIN(usermods)[0], (intptr_t) &DYNARRAY_END(usermods)[0]); |
| 52 | + Usermod** volatile x = (Usermod**) DYNARRAY_BEGIN(usermods); |
| 53 | + Serial.printf_P(PSTR("X: %08X\n"), (intptr_t) x); |
| 54 | + Serial.printf_P(PSTR("*X: %08X\n"), (intptr_t) *x); |
| 55 | + |
| 56 | + bool allComplete = true; |
| 57 | + for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) { |
54 | 58 | if (!(*mod)->readFromConfig(obj)) allComplete = false; |
55 | 59 | } |
56 | 60 | return allComplete; |
57 | 61 | } |
58 | 62 | #ifndef WLED_DISABLE_MQTT |
59 | | -void UsermodManager::onMqttConnect(bool sessionPresent) { for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) (*mod)->onMqttConnect(sessionPresent); } |
| 63 | +void UsermodManager::onMqttConnect(bool sessionPresent) { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->onMqttConnect(sessionPresent); } |
60 | 64 | bool UsermodManager::onMqttMessage(char* topic, char* payload) { |
61 | | - for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) if ((*mod)->onMqttMessage(topic, payload)) return true; |
| 65 | + for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) if ((*mod)->onMqttMessage(topic, payload)) return true; |
62 | 66 | return false; |
63 | 67 | } |
64 | 68 | #endif |
65 | 69 | #ifndef WLED_DISABLE_ESPNOW |
66 | 70 | bool UsermodManager::onEspNowMessage(uint8_t* sender, uint8_t* payload, uint8_t len) { |
67 | | - for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) if ((*mod)->onEspNowMessage(sender, payload, len)) return true; |
| 71 | + for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) if ((*mod)->onEspNowMessage(sender, payload, len)) return true; |
68 | 72 | return false; |
69 | 73 | } |
70 | 74 | #endif |
71 | 75 | bool UsermodManager::onUdpPacket(uint8_t* payload, size_t len) { |
72 | | - for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) if ((*mod)->onUdpPacket(payload, len)) return true; |
| 76 | + for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) if ((*mod)->onUdpPacket(payload, len)) return true; |
73 | 77 | return false; |
74 | 78 | } |
75 | | -void UsermodManager::onUpdateBegin(bool init) { for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) (*mod)->onUpdateBegin(init); } // notify usermods that update is to begin |
76 | | -void UsermodManager::onStateChange(uint8_t mode) { for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) (*mod)->onStateChange(mode); } // notify usermods that WLED state changed |
| 79 | +void UsermodManager::onUpdateBegin(bool init) { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->onUpdateBegin(init); } // notify usermods that update is to begin |
| 80 | +void UsermodManager::onStateChange(uint8_t mode) { for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) (*mod)->onStateChange(mode); } // notify usermods that WLED state changed |
77 | 81 |
|
78 | 82 | /* |
79 | 83 | * Enables usermods to lookup another Usermod. |
80 | 84 | */ |
81 | 85 | Usermod* UsermodManager::lookup(uint16_t mod_id) { |
82 | | - for (auto mod = _usermod_table_begin; mod < _usermod_table_end; ++mod) { |
| 86 | + for (auto mod = DYNARRAY_BEGIN(usermods); mod < DYNARRAY_END(usermods); ++mod) { |
83 | 87 | if ((*mod)->getId() == mod_id) { |
84 | 88 | return *mod; |
85 | 89 | } |
|
0 commit comments