|
| 1 | +#include "usermod_sensor_sync.h" |
| 2 | +#ifdef USERMOD_MPR121 |
| 3 | + #include "../usermods/mpr121/usermod_mpr121.h" |
| 4 | +#endif |
| 5 | + |
| 6 | +// The largest single message must fit the receive buffer (oversized datagrams are flushed). |
| 7 | +static_assert(sizeof(SensorSyncHeader) + 255 * sizeof(SensorEdge) >= sizeof(SensorSyncHeader), |
| 8 | + "edge batch size sanity"); |
| 9 | + |
| 10 | +const char UsermodSensorSync::_name[] PROGMEM = "SensorSync"; |
| 11 | +const char UsermodSensorSync::_enabled[] PROGMEM = "enabled"; |
| 12 | + |
| 13 | +void UsermodSensorSync::setup() { |
| 14 | + deviceId = configId ? configId : deriveDeviceId(); |
| 15 | + initDone = true; |
| 16 | +} |
| 17 | + |
| 18 | +void UsermodSensorSync::connected() { |
| 19 | + udpStarted = false; // re-bind on (re)connection; lazy begin in loop() also covers SoftAP-only |
| 20 | +} |
| 21 | + |
| 22 | +void UsermodSensorSync::loop() { |
| 23 | + if (!enabled || strip.isUpdating()) return; |
| 24 | + |
| 25 | + if (!(Network.isConnected() || apActive)) { udpStarted = false; return; } |
| 26 | + if (!udpStarted) { |
| 27 | + if (!udp.begin(port)) return; |
| 28 | + udpStarted = true; |
| 29 | + } |
| 30 | + |
| 31 | + receiveLoop(); |
| 32 | + broadcastLocalState(); |
| 33 | +} |
| 34 | + |
| 35 | +bool UsermodSensorSync::popRemoteEvent(RemoteSensorEvent &out) { |
| 36 | + if (rxCount == 0) return false; |
| 37 | + out = rxQueue[rxTail]; |
| 38 | + rxTail = (rxTail + 1) % RX_QUEUE_LEN; |
| 39 | + rxCount--; |
| 40 | + return true; |
| 41 | +} |
| 42 | + |
| 43 | +uint16_t UsermodSensorSync::getDeviceId() const { return deviceId; } |
| 44 | + |
| 45 | +uint16_t UsermodSensorSync::getId() { return USERMOD_ID_SENSOR_SYNC; } |
| 46 | + |
| 47 | +void UsermodSensorSync::addToJsonInfo(JsonObject &root) { |
| 48 | + JsonObject user = root["u"]; |
| 49 | + if (user.isNull()) user = root.createNestedObject("u"); |
| 50 | + JsonArray arr = user.createNestedArray("Sensor Sync"); |
| 51 | + arr.add(udpStarted ? txCount : 0); |
| 52 | + arr.add(udpStarted ? " sent" : " (offline)"); |
| 53 | +} |
| 54 | + |
| 55 | +void UsermodSensorSync::addToConfig(JsonObject &root) { |
| 56 | + JsonObject top = root.createNestedObject(FPSTR(_name)); |
| 57 | + top[FPSTR(_enabled)] = enabled; |
| 58 | + top["port"] = port; |
| 59 | + top["id"] = configId; |
| 60 | +} |
| 61 | + |
| 62 | +bool UsermodSensorSync::readFromConfig(JsonObject &root) { |
| 63 | + JsonObject top = root[FPSTR(_name)]; |
| 64 | + if (top.isNull()) return false; |
| 65 | + uint16_t portPrev = port; |
| 66 | + getJsonValue(top[FPSTR(_enabled)], enabled); |
| 67 | + getJsonValue(top["port"], port); |
| 68 | + getJsonValue(top["id"], configId); |
| 69 | + if (initDone) { |
| 70 | + deviceId = configId ? configId : deriveDeviceId(); |
| 71 | + if (port != portPrev) udpStarted = false; // rebind on next loop |
| 72 | + } |
| 73 | + return true; |
| 74 | +} |
| 75 | + |
| 76 | +uint16_t UsermodSensorSync::deriveDeviceId() { |
| 77 | + uint8_t mac[6] = {0}; |
| 78 | + WiFi.macAddress(mac); |
| 79 | + uint16_t id = ((uint16_t)mac[4] << 8) | mac[5]; |
| 80 | + return id ? id : 1; // never 0 (0 means "auto") |
| 81 | +} |
| 82 | + |
| 83 | +void UsermodSensorSync::enqueue(uint8_t sensorType, uint8_t channel, uint8_t value, |
| 84 | + uint16_t dev, uint32_t ts) { |
| 85 | + RemoteSensorEvent &slot = rxQueue[rxHead]; |
| 86 | + slot.sensorType = sensorType; |
| 87 | + slot.channel = channel; |
| 88 | + slot.value = value; |
| 89 | + slot.deviceId = dev; |
| 90 | + slot.timestamp = ts; |
| 91 | + rxHead = (rxHead + 1) % RX_QUEUE_LEN; |
| 92 | + if (rxCount < RX_QUEUE_LEN) rxCount++; |
| 93 | + else rxTail = (rxTail + 1) % RX_QUEUE_LEN; // overwrite oldest |
| 94 | +} |
| 95 | + |
| 96 | +// Edges are derived at the producer, so the consumer just forwards each record to the queue. |
| 97 | +void UsermodSensorSync::dispatchMessage(const SensorSyncHeader &h, const uint8_t *data, int dataLen) { |
| 98 | + int avail = dataLen / (int)sizeof(SensorEdge); |
| 99 | + int n = (h.count < avail) ? h.count : avail; |
| 100 | + for (int i = 0; i < n; i++) { |
| 101 | + SensorEdge e; |
| 102 | + memcpy(&e, data + i * sizeof(SensorEdge), sizeof(e)); |
| 103 | + enqueue(h.sensorType, e.channel, e.value, h.deviceId, h.timestamp); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +void UsermodSensorSync::receiveLoop() { |
| 108 | + int sz = udp.parsePacket(); |
| 109 | + while (sz > 0) { |
| 110 | + if (sz >= (int)sizeof(SensorSyncHeader) && sz <= (int)RX_BUF_LEN) { |
| 111 | + uint8_t buf[RX_BUF_LEN]; |
| 112 | + int rd = udp.read(buf, sizeof(buf)); |
| 113 | + SensorSyncHeader h; |
| 114 | + if (rd >= (int)sizeof(h)) { |
| 115 | + memcpy(&h, buf, sizeof(h)); |
| 116 | + if (h.magic[0] == 'A' && h.magic[1] == 'M' && h.magic[2] == 'P' && h.magic[3] == 'S' && |
| 117 | + h.version == SENSOR_SYNC_VERSION && h.msgType == SENSOR_SYNC_MSG_EDGES && |
| 118 | + h.deviceId != deviceId) { |
| 119 | + dispatchMessage(h, buf + sizeof(h), rd - (int)sizeof(h)); |
| 120 | + } |
| 121 | + } |
| 122 | + } else { |
| 123 | + udp.flush(); |
| 124 | + } |
| 125 | + sz = udp.parsePacket(); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +void UsermodSensorSync::sendEdges(uint8_t sensorType, const SensorEdge *edges, uint8_t n) { |
| 130 | + if (n == 0) return; |
| 131 | + uint8_t buf[RX_BUF_LEN]; |
| 132 | + if (sizeof(SensorSyncHeader) + (size_t)n * sizeof(SensorEdge) > sizeof(buf)) return; |
| 133 | + SensorSyncHeader h; |
| 134 | + h.magic[0] = 'A'; h.magic[1] = 'M'; h.magic[2] = 'P'; h.magic[3] = 'S'; |
| 135 | + h.version = SENSOR_SYNC_VERSION; |
| 136 | + h.msgType = SENSOR_SYNC_MSG_EDGES; |
| 137 | + h.sensorType = sensorType; |
| 138 | + h.count = n; |
| 139 | + h.deviceId = deviceId; |
| 140 | + h.seq = txSeq++; |
| 141 | + h.timestamp = millis() + strip.timebase; |
| 142 | + memcpy(buf, &h, sizeof(h)); |
| 143 | + memcpy(buf + sizeof(h), edges, (size_t)n * sizeof(SensorEdge)); |
| 144 | + |
| 145 | + if (udp.beginPacket(IPAddress(255, 255, 255, 255), port)) { |
| 146 | + udp.write(buf, sizeof(h) + (size_t)n * sizeof(SensorEdge)); |
| 147 | + udp.endPacket(); |
| 148 | + txCount++; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +void UsermodSensorSync::broadcastLocalState() { |
| 153 | +#ifdef USERMOD_MPR121 |
| 154 | + UsermodMPR121 *mpr = (UsermodMPR121*) UsermodManager::lookup(USERMOD_ID_MPR121); |
| 155 | + if (!mpr || !mpr->isSensorFound()) { prevLocalTouched = 0; return; } |
| 156 | + |
| 157 | + uint16_t cur = 0; |
| 158 | + for (uint8_t e = 0; e < MPR121::TOTAL_SENSORS; e++) |
| 159 | + if (mpr->touched(e)) cur |= (1u << e); |
| 160 | + |
| 161 | + uint16_t changed = cur ^ prevLocalTouched; |
| 162 | + if (!changed) return; |
| 163 | + |
| 164 | + // Producer derives edges once and sends all changed channels in one message. |
| 165 | + SensorEdge edges[MPR121::TOTAL_SENSORS]; |
| 166 | + uint8_t n = 0; |
| 167 | + for (uint8_t e = 0; e < MPR121::TOTAL_SENSORS; e++) { |
| 168 | + if (!(changed & (1u << e))) continue; |
| 169 | + edges[n].channel = e; |
| 170 | + edges[n].value = (cur & (1u << e)) ? 1 : 0; |
| 171 | + n++; |
| 172 | + } |
| 173 | + sendEdges(SS_SENSOR_TOUCH, edges, n); |
| 174 | + prevLocalTouched = cur; |
| 175 | +#endif |
| 176 | +} |
0 commit comments