3-phase PV router
Loading...
Searching...
No Matches
remote_loads_core.h
Go to the documentation of this file.
1
28
29#ifndef REMOTE_LOADS_CORE_H
30#define REMOTE_LOADS_CORE_H
31
32#include <stdint.h>
33
34#include "load_map.h"
35
36inline constexpr uint8_t REMOTE_REFRESH_CYCLES{ 5 };
37
47template< uint8_t NumIds >
48constexpr bool areValidNodeIds(const uint8_t (&nodeIds)[NumIds], uint8_t numUnits, uint8_t routerId)
49{
50 if (numUnits > NumIds)
51 {
52 return false;
53 }
54
55 for (uint8_t i = 0; i != numUnits; ++i)
56 {
57 if ((nodeIds[i] < 1) || (nodeIds[i] > 30) || (nodeIds[i] == routerId))
58 {
59 return false;
60 }
61
62 for (uint8_t j = 0; j < i; ++j)
63 {
64 if (nodeIds[i] == nodeIds[j])
65 {
66 return false;
67 }
68 }
69 }
70 return true;
71}
72
77{
78 uint8_t tx_data{ 0 };
79 uint8_t cyclesSinceLastUpdate{ 0 };
80 uint8_t previousBitmask{ 0 };
81 volatile bool pendingTransmission{ false };
82};
83
99template< uint8_t NumUnits >
101{
102public:
103 constexpr RemoteLoadCore() = default;
104
106 static constexpr uint8_t size()
107 {
108 return NumUnits;
109 }
110
125 template< const auto &Map, uint8_t NumLoads >
126 void updateLoads(const LoadStates (&loadStates)[NumLoads])
127 {
128 static_assert(sizeof(Map) == NumLoads, "one load state per entry of the load map");
129
130 if constexpr (NumUnits > 0)
131 {
132 updateUnit< Map, 1 >(loadStates);
133 }
134 }
135
149 bool takePending(uint8_t unitIdx, uint8_t &payload)
150 {
151 if ((unitIdx >= NumUnits) || !unitStates[unitIdx].pendingTransmission)
152 {
153 return false;
154 }
155
156 unitStates[unitIdx].pendingTransmission = false;
157 payload = unitStates[unitIdx].tx_data;
158
159 return true;
160 }
161
172 template< uint8_t NumIds, typename Send >
173 void sendPending(const uint8_t (&nodeIds)[NumIds], Send &&send)
174 {
175 static_assert(NumIds >= NumUnits, "one node ID per remote unit");
176
177 for (uint8_t idx = 0; idx != NumUnits; ++idx)
178 {
179 uint8_t payload{ 0 };
180
181 if (takePending(idx, payload))
182 {
183 send(nodeIds[idx], payload);
184 }
185 }
186 }
187
189 constexpr uint8_t payloadOf(uint8_t unitIdx) const
190 {
191 return (unitIdx < NumUnits) ? unitStates[unitIdx].tx_data : 0;
192 }
193
195 constexpr bool isPending(uint8_t unitIdx) const
196 {
197 return (unitIdx < NumUnits) && unitStates[unitIdx].pendingTransmission;
198 }
199
201 void reset()
202 {
203 for (uint8_t idx = 0; idx < NumUnits; ++idx)
204 {
206 }
207 }
208
209private:
213 template< const auto &Map, uint8_t Unit, uint8_t NumLoads >
214 void updateUnit(const LoadStates (&loadStates)[NumLoads])
215 {
216 schedule(Unit - 1, bitmaskOf< Map, Unit, 0 >(loadStates));
217
218 if constexpr (Unit < NumUnits)
219 {
220 updateUnit< Map, Unit + 1 >(loadStates);
221 }
222 }
223
227 template< const auto &Map, uint8_t Unit, uint8_t I, uint8_t NumLoads >
228 static uint8_t bitmaskOf(const LoadStates (&loadStates)[NumLoads])
229 {
230 if constexpr (I == NumLoads)
231 {
232 return 0;
233 }
234 else
235 {
236 uint8_t bitmask{ bitmaskOf< Map, Unit, I + 1 >(loadStates) };
237
238 if constexpr (Load::unitOf(Map[I]) == Unit)
239 {
240 constexpr uint8_t bit{ static_cast< uint8_t >(1U << Load::bitOf(Map, I)) };
241
242 if (loadStates[I] == LoadStates::LOAD_ON)
243 {
244 bitmask |= bit;
245 }
246 }
247
248 return bitmask;
249 }
250 }
251
255 void schedule(uint8_t unitIdx, uint8_t bitmask)
256 {
257 auto &unit{ unitStates[unitIdx] };
258
259 unit.tx_data = bitmask;
260
261 if (bitmask != unit.previousBitmask)
262 {
263 // state changed - transmit on the next pass through loop()
264 unit.previousBitmask = bitmask;
265 unit.cyclesSinceLastUpdate = 0;
266 unit.pendingTransmission = true;
267
268 return;
269 }
270
271 // state unchanged - refresh periodically
272 if (++unit.cyclesSinceLastUpdate >= REMOTE_REFRESH_CYCLES)
273 {
274 unit.cyclesSinceLastUpdate = 0;
275 unit.pendingTransmission = true;
276 }
277 }
278
279 RemoteUnitState unitStates[NumUnits ? NumUnits : 1]{};
280};
281
282#endif // REMOTE_LOADS_CORE_H
void updateUnit(const LoadStates(&loadStates)[NumLoads])
Build and schedule the payload of Unit, then of every unit after it.
void updateLoads(const LoadStates(&loadStates)[NumLoads])
Rebuild every unit's payload from the current load states.
constexpr RemoteLoadCore()=default
bool takePending(uint8_t unitIdx, uint8_t &payload)
Claim a pending transmission for one unit.
RemoteUnitState unitStates[NumUnits ? NumUnits :1]
static uint8_t bitmaskOf(const LoadStates(&loadStates)[NumLoads])
Payload of Unit, from map entry I onwards.
void reset()
Return every unit to its power-on state.
constexpr uint8_t payloadOf(uint8_t unitIdx) const
Current payload of a unit, whether or not it has been sent.
constexpr bool isPending(uint8_t unitIdx) const
True if a transmission is due for that unit.
void schedule(uint8_t unitIdx, uint8_t bitmask)
Record a unit's freshly computed payload and decide whether to send it.
void sendPending(const uint8_t(&nodeIds)[NumIds], Send &&send)
Hand every pending payload to send, addressed to its unit's node ID.
static constexpr uint8_t size()
Number of remote units handled.
uint8_t i
Encoding of the load map: one packed byte per dump load.
LoadStates
Definition load_map.h:32
constexpr uint8_t bitOf(const uint8_t(&map)[N], uint8_t loadNum)
Rank of a load among the loads of its own unit, in ascending map order.
Definition load_map.h:195
constexpr uint8_t unitOf(uint8_t entry)
The owning unit of the entry: 0 for a local load, 1..3 for a remote one.
Definition load_map.h:95
constexpr bool areValidNodeIds(const uint8_t(&nodeIds)[NumIds], uint8_t numUnits, uint8_t routerId)
Check the node IDs of the first numUnits remote units.
constexpr uint8_t REMOTE_REFRESH_CYCLES
Transmission bookkeeping for one remote unit.
volatile bool pendingTransmission
uint8_t cyclesSinceLastUpdate