-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswim_machine.cpp
More file actions
415 lines (360 loc) · 9.82 KB
/
Copy pathswim_machine.cpp
File metadata and controls
415 lines (360 loc) · 9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "swim_machine.h"
#include "UDPEventSender.h"
#include <vector>
/* ------------ internal state ----------------------------------- */
namespace
{
std::vector<SwimMachine::Segment> workout;
bool motorOn = false;
bool machineFound = false;
// Function pointer to push network event
void (*push_network_event_func)(const uint8_t *data, size_t len) = nullptr;
struct
{
int32_t idx = -1; // current segment
uint32_t segStart = 0; // ms at seg start
bool active = false;
bool paused = false;
} sim;
}
/* ------------ multicast/control via UDPEventSender -------------- */
const IPAddress multicastAddr(239, 255, 0, 1);
const uint16_t multicastPort = 45654;
static IPAddress peer_ip(255, 255, 255, 255);
static uint16_t PEER_PORT = 9750;
// Two UDPEventSender instances:
// - mcast: joined to multicast group for incoming status/acks
// - ctrl: unicast/broadcast for control commands to the swim machine
static UDPEventSender mcast;
static UDPEventSender ctrl;
/* ------------ helpers: CRC-32 & monotonic tick ------------------ */
static uint32_t crc32(const uint8_t *d, size_t n)
{
uint32_t c = 0xFFFFFFFF;
while (n--)
{
c ^= *d++;
for (int k = 0; k < 8; ++k)
c = (c & 1) ? (c >> 1) ^ 0xEDB88320 : (c >> 1);
}
return ~c;
}
static uint32_t monoTick()
{ // never repeats
static uint32_t last = 0, now;
now = millis();
if (now <= last)
now = last + 1;
last = now;
return last;
}
/* ------------ send queue and flow control ----------------------- */
static uint16_t messagequeue[16];
static uint16_t *nextmessagetosend = messagequeue;
static uint16_t *nextmessage = messagequeue;
static uint8_t lastSentIdx2 = 0xC5; // so first increment is 0x64
static uint8_t idx2Counter = 0xC5; // so first increment is 0x64
static bool readyToSendNext = true;
static bool resendLastPacket = false;
static bool s_dryRun = false; // true = build/report packets but never transmit
static uint8_t lastReceivedCommand = 0x00; // last command byte received from machine (data[3] of 111-byte packet)
static void queuePkt(uint16_t command, uint16_t param = 0)
{
*nextmessage = command;
nextmessage++;
*nextmessage = param;
nextmessage++;
if (nextmessage - messagequeue >= (int)(sizeof(messagequeue) / sizeof(*messagequeue)))
nextmessage = messagequeue;
}
static void confirmPacket(uint8_t idx2, uint8_t cmdByte)
{
// Update global last received command byte from machine status packet
lastReceivedCommand = cmdByte;
if (readyToSendNext)
return;
static uint8_t lastReceivedIdx2 = 0;
static int consecutiveMisses = 0;
// Track if we received the expected idx2 value
if (idx2 == lastSentIdx2)
{
consecutiveMisses = 0;
lastReceivedIdx2 = idx2;
readyToSendNext = true;
resendLastPacket = false;
nextmessagetosend += 2;
if (nextmessagetosend - messagequeue >= (int)(sizeof(messagequeue) / sizeof(*messagequeue)))
nextmessagetosend = messagequeue;
}
else
{
consecutiveMisses++;
if (consecutiveMisses >= 2)
{
// Resend the last packet
resendLastPacket = true;
consecutiveMisses = 0;
}
}
}
/* ------------ high-level opcodes ------------------------------- */
void setDuration(uint16_t s) { queuePkt(0x25, s); }
void setPace(uint16_t p)
{
static uint16_t lastpace = 0;
if (lastpace != p)
queuePkt(0x24, p);
lastpace = p;
}
void sendStart() { queuePkt(0x1F); }
void sendStop() { queuePkt(0x21); }
void motorStart()
{
if (!motorOn)
{
sendStart();
motorOn = true;
}
}
void motorStop()
{
if (motorOn)
{
sendStop();
motorOn = false;
}
}
/* ------------ raw packet emitter ------------------------------- */
static void sendPkt()
{
if (nextmessagetosend == nextmessage)
return;
// do not send message when machine's last received command indicates turning off or slowing down
if (lastReceivedCommand == 0x4E ||
lastReceivedCommand == 0x0E ||
lastReceivedCommand == 0x4A ||
lastReceivedCommand == 0x0A)
return;
// Only send if ready or if a resend is required
if (!readyToSendNext && !resendLastPacket)
return;
uint16_t command = *nextmessagetosend;
uint16_t param = *(nextmessagetosend + 1);
uint8_t idx2;
if (resendLastPacket)
{
// For resend, use the last sent idx2
idx2 = lastSentIdx2;
}
else
{
// For new packet, increment idx2Counter and wrap in [0x64, 0xC6]
idx2Counter++;
if (idx2Counter > 0xC6)
idx2Counter = 0x64;
if (idx2Counter < 0x64)
idx2Counter = 0x64;
idx2 = idx2Counter;
}
// Prepare packet
uint8_t b[44];
memset(b, 0, sizeof b);
b[0] = 0x0A; // header
b[1] = 0xF0;
b[36] = 0x97; // footer
b[37] = 0x01;
b[2] = idx2;
b[3] = command & 0xFF;
b[4] = param & 0xFF;
b[5] = param >> 8;
uint32_t t = monoTick();
memcpy(b + 32, &t, 4);
uint32_t c = crc32(b, 40);
memcpy(b + 40, &c, 4);
if (s_dryRun)
{
// Dry run: never transmit. Still surface the packet to the UDP monitor so
// you can see what *would* have been sent, and self-confirm it (there is no
// machine to ACK) so the queue keeps flowing instead of retrying forever.
if (push_network_event_func)
push_network_event_func(b, sizeof b);
lastSentIdx2 = idx2;
resendLastPacket = false;
readyToSendNext = true;
nextmessagetosend += 2;
if (nextmessagetosend - messagequeue >= (int)(sizeof(messagequeue) / sizeof(*messagequeue)))
nextmessagetosend = messagequeue;
return;
}
// Send via UDPEventSender (control socket)
(void)ctrl.sendBytes(b, sizeof b);
if (push_network_event_func)
push_network_event_func(b, sizeof b);
// Update state
lastSentIdx2 = idx2;
readyToSendNext = false;
resendLastPacket = false;
}
/* =================================================================
* P U B L I C A P I
* ================================================================= */
void SwimMachine::begin()
{
// Join multicast group to receive status/acks
mcast.onReceive([](const uint8_t *data, size_t len, const IPAddress &remote, uint16_t rport) {
// Process only expected 111-byte packets
if (len == 111)
{
confirmPacket(data[2], data[3]);
if (push_network_event_func)
push_network_event_func(data, len);
if (!machineFound)
{
lastSentIdx2 = data[2] + 1;
Serial.printf("Swim machine found at IP: %s\n", remote.toString().c_str());
machineFound = true;
}
}
});
// Initialize sockets (rebinding handled by connection changes)
mcast.begin(multicastAddr, multicastPort, 45654); // bind to group port
ctrl.begin(peer_ip, PEER_PORT, 40000); // distinct local port to avoid conflict
}
/* Set network event callback */
void SwimMachine::setPushNetworkEvent(void (*push_network_event)(const uint8_t *data, size_t len))
{
push_network_event_func = push_network_event;
}
/* deep-copy caller’s data so it can go out of scope */
void SwimMachine::loadWorkout(const std::vector<SwimMachine::Segment> &segs)
{
workout.clear();
workout.insert(workout.end(), segs.begin(), segs.end()); // append the rest
sim.idx = -1;
sim.active = false;
sim.paused = false;
}
/* --------------------------------------------------------------- */
bool SwimMachine::start()
{
if (workout.empty())
{
return false;
}
/* send TOTAL ACTIVE duration once */
uint16_t tot = 0;
for (auto &s : workout)
if (s.pace100s)
tot += s.durSec;
if (tot > 90 * 60 - 10)
{
return false; // too long workout.
}
setDuration(tot + 10);
sim.idx = -1; // will advance on first tick()
sim.segStart = millis();
sim.active = true;
sim.paused = false;
return true;
}
/* toggle pause */
void SwimMachine::pause()
{
if (!sim.active)
return;
sim.paused = !sim.paused;
if (sim.paused)
{
motorStop();
/* pause: keep elapsed time intact */
sim.segStart = millis() - sim.segStart;
}
else
{
/* resume: keep elapsed time intact */
sim.segStart = millis() - sim.segStart;
if (workout[sim.idx].pace100s)
motorStart();
}
}
/* force stop */
void SwimMachine::stop()
{
sim.active = false;
sim.paused = false;
sim.idx = -1;
motorStop();
}
/* --------------------------------------------------------------- */
void SwimMachine::tick()
{
static uint32_t prev =0;
uint32_t now = millis();
if(now > 250 && now<prev+250)
return;
prev = now;
// Process UDP (receives multicast acks and keeps sockets rebound)
mcast.loop();
ctrl.loop();
// Attempt send if ready
sendPkt();
if (!sim.active || sim.paused)
return;
/* start next segment if idx == -1 OR segment finished */
if (sim.idx == -1 ||
now - sim.segStart >= workout[sim.idx].durSec * 1000UL)
{
sim.idx++;
if (sim.idx >= (int)workout.size())
{
stop();
return;
}
Segment s = workout[sim.idx];
sim.segStart = now;
if (s.pace100s == 0)
{ // rest
motorStop();
// set future pace for faster transition
if (sim.idx + 1 < (int)workout.size() && workout[sim.idx + 1].pace100s)
{
setPace(workout[sim.idx + 1].pace100s);
}
}
else
{
setPace(s.pace100s);
motorStart();
}
}
}
/* --------------------------------------------------------------- */
SwimMachine::SwimStatus SwimMachine::getStatus()
{
SwimMachine::SwimStatus st;
st.active = sim.active;
st.found = machineFound;
st.paused = sim.paused;
st.idx = sim.idx;
const uint32_t lag = 1000;
st.elapsedMs = (sim.paused ? sim.segStart - lag : (millis() - sim.segStart - lag));
return st;
}
bool SwimMachine::isMachineFound()
{
return machineFound;
}
void SwimMachine::setDryRun(bool on)
{
s_dryRun = on;
}
bool SwimMachine::isDryRun()
{
return s_dryRun;
}
void SwimMachine::setPeerIP(IPAddress ip)
{
peer_ip = ip;
// Reconfigure control sender to new peer (keep distinct local port)
ctrl.begin(peer_ip, PEER_PORT, 40000);
}