Skip to content

Commit 7d55a64

Browse files
add an offset and thus a global mesh time, schedule sleep and awake using global mesh time instead of local uptime
1 parent 22ad226 commit 7d55a64

File tree

4 files changed

+88
-5
lines changed

4 files changed

+88
-5
lines changed

src/Shell.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,16 @@ static StringBuffer uptimeReportHQ(void) {
223223
strncpy_P(reset, Scout.getLastResetCause(), sizeof(reset));
224224
reset[sizeof(reset) - 1] = 0; // ensure termination, strncpy is weird
225225

226-
report.appendSprintf("[%d,[%d,%d,%d,%d],[%ld,%ld,%d,",keyMap("uptime",0),
226+
report.appendSprintf("[%d,[%d,%d,%d,%d,%d],[%ld,%ld,%ld,%d,",
227+
keyMap("uptime",0),
227228
keyMap("total", 0),
228229
keyMap("sleep", 0),
230+
keyMap("meshsleep", 0),
229231
keyMap("random", 0),
230232
keyMap("reset", 0),
231233
SleepHandler::uptime().seconds,
232234
SleepHandler::sleeptime().seconds,
235+
SleepHandler::meshsleeptime().seconds,
233236
(int)random());
234237

235238
report.appendJsonString(reset, true);
@@ -291,9 +294,40 @@ static numvar uptimeStatus(void) {
291294
appendTime(out, SleepHandler::sleeptime());
292295
speol(out.c_str());
293296

297+
out = F("Global: ");
298+
appendTime(out, SleepHandler::meshtime());
299+
speol(out.c_str());
294300
return true;
295301
}
296302

303+
static numvar uptimeSetOffset(void) {
304+
if (!checkArgs(3, F("usage: uptime.setoffset(seconds, micros, inFuture)"))) {
305+
return 0;
306+
}
307+
308+
uint32_t seconds = getarg(1);
309+
uint32_t us = getarg(2);
310+
311+
Duration d;
312+
d.seconds = seconds;
313+
d.us = us;
314+
315+
bool future = getarg(3);
316+
317+
SleepHandler::setOffsetInFuture(future);
318+
SleepHandler::setOffset(d);
319+
320+
return 1;
321+
}
322+
323+
static numvar uptimeMeshOffsetMicros(void) {
324+
return SleepHandler::getOffset().us;
325+
}
326+
327+
static numvar uptimeMeshOffsetSeconds(void) {
328+
return SleepHandler::getOffset().seconds;
329+
}
330+
297331
static numvar uptimeMeshSleepingMicros(void) {
298332
return SleepHandler::meshsleeptime().us;
299333
}
@@ -2504,10 +2538,14 @@ void PinoccioShell::setup() {
25042538
addFunction("uptime.getlastreset", getLastResetCause);
25052539
addFunction("uptime.status", uptimeStatus);
25062540
addFunction("uptime", uptimeStatus);
2541+
addFunction("uptime.setoffset", uptimeSetOffset);
25072542

25082543
addFunction("uptime.meshsleeping.micros", uptimeMeshSleepingMicros);
25092544
addFunction("uptime.meshsleeping.seconds", uptimeMeshSleepingSeconds);
25102545

2546+
addFunction("uptime.meshoffset.micros", uptimeMeshOffsetMicros);
2547+
addFunction("uptime.meshoffset.seconds", uptimeMeshOffsetSeconds);
2548+
25112549
addFunction("led.on", ledTorch); // alias
25122550
addFunction("led.off", ledOff);
25132551
addFunction("led.red", ledRed);

src/SleepHandler.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,26 @@ uint32_t SleepHandler::meshSleepStart = 0;
1313
radio_state_t SleepHandler::radioState = RF_AWAKE;
1414
uint32_t SleepHandler::sleepPeriod = 100;
1515
uint32_t SleepHandler::wakePeriod = 100;
16+
Duration SleepHandler::meshOffset = {0, 0};
17+
boolean SleepHandler::offsetInFuture = true;
1618

1719
// Returns the time mesh slept since startup
1820
const Duration& SleepHandler::meshsleeptime() {
1921
return meshSleep;
2022
}
2123

24+
const Duration& SleepHandler::getOffset(){
25+
return meshOffset;
26+
}
27+
28+
void SleepHandler::setOffset(Duration d){
29+
meshOffset = d;
30+
}
31+
32+
void SleepHandler::setOffsetInFuture(bool future){
33+
offsetInFuture = future;
34+
}
35+
2236
Pbbe::LogicalPin::mask_t SleepHandler::pinWakeups = 0;
2337

2438
ISR(SCNT_CMP3_vect) {
@@ -116,11 +130,11 @@ void SleepHandler::loop() {
116130
switch (radioState) {
117131
case RF_SHOULD_SLEEP:
118132
sleepRadio();
119-
scheduleWakeRadio(uptime() + (uint64_t)(sleepPeriod * 1000));
133+
scheduleWakeRadio(meshtime() + (uint64_t)(sleepPeriod * 1000));
120134
break;
121135
case RF_SHOULD_WAKE:
122136
wakeRadio();
123-
scheduleSleepRadio(uptime() + (uint64_t)(wakePeriod * 1000));
137+
scheduleSleepRadio(meshtime() + (uint64_t)(wakePeriod * 1000));
124138
break;
125139
default:
126140
// RF_WILL_SLEEP, RF_WILL_WAKE, RF_SLEEPING, RF_AWAKE
@@ -231,15 +245,15 @@ void SleepHandler::scheduleSleepRadio(Duration future) {
231245
// todo handle microseconds too
232246
// todo calculate off of an uptime + offset
233247
radioState = RF_WILL_SLEEP;
234-
setTimer2((future.seconds - uptime().seconds) * 1000);
248+
setTimer2((future.seconds - meshtime().seconds) * 1000);
235249
}
236250

237251
void SleepHandler::scheduleWakeRadio(Duration future) {
238252
// todo check its in the future
239253
// todo handle microseconds too
240254
// todo calculate off of an uptime + offset
241255
radioState = RF_WILL_WAKE;
242-
setTimer2((future.seconds - uptime().seconds) * 1000);
256+
setTimer2((future.seconds - meshtime().seconds) * 1000);
243257
}
244258

245259
// TODO take a few ticks off the schedule as it takes us some amount of

src/SleepHandler.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ ISR(SCNT_OVFL_vect);
1212

1313
class SleepHandler {
1414
public:
15+
16+
static void setOffset(Duration d);
17+
static void setOffsetInFuture(bool future);
18+
19+
static const Duration& getOffset();
1520
static const Duration& meshsleeptime();
1621

1722
static bool getMatch();
@@ -94,7 +99,21 @@ class SleepHandler {
9499
return uptime() - totalSleep;
95100
}
96101

102+
// Returns the local authority's time
103+
static Duration meshtime() {
104+
if(offsetInFuture)
105+
return uptime() + meshOffset;
106+
else
107+
return uptime() - meshOffset;
108+
}
109+
97110
protected:
111+
112+
// The mesh offset
113+
static Duration meshOffset;
114+
// The offset direction, forward is true, backward is false
115+
static bool offsetInFuture;
116+
98117
static radio_state_t radioState;
99118
static uint32_t sleepPeriod;
100119
static uint32_t wakePeriod;

src/util/Duration.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ class Duration {
4040

4141
return result;
4242
}
43+
44+
Duration operator +(Duration d) {
45+
Duration result(*this);
46+
result.seconds += d.seconds;
47+
result.us += d.us;
48+
if (result.us > 1000000) {
49+
result.us -= 1000000;
50+
result.seconds++;
51+
}
52+
53+
return result;
54+
}
4355
};
4456

4557
#endif // LIB_PINOCCIO_DURATION_H

0 commit comments

Comments
 (0)