Skip to content

Commit 797ff66

Browse files
more info queryable about state
1 parent bd687e4 commit 797ff66

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

slave.ino

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@
77
#define COMMAND_MILLIS 129
88
#define COMMAND_LASTWATCHDOGREBOOT 130
99
#define COMMAND_WATCHDOGREBOOTCOUNT 131
10+
#define COMMAND_LASTWATCHDOGPET 132
11+
#define COMMAND_LASTPETATBITE 133
1012
#define COMMAND_WATCHDOGPETBASE 200
1113

1214
// ---- STATE ----
1315
volatile long receivedValue = 0;
1416
volatile long dataToSend = -1;
15-
unsigned long lastWatchdogPet = 0;
16-
unsigned long lastWatchdogReboot = 0;
17+
volatile unsigned long lastWatchdogPet = 0;
18+
volatile unsigned long lastWatchdogReboot = 0;
19+
volatile unsigned int lastTimeoutScale = 0;
20+
volatile unsigned long lastPetAtBite = 0;
1721

18-
unsigned long watchdogTimeout = 200;
19-
unsigned int rebootCount = 0;
20-
unsigned long timeLastPrinted = 0;
22+
volatile unsigned long watchdogTimeout = 200;
23+
volatile unsigned int rebootCount = 0;
24+
volatile unsigned long timeLastPrinted = 0;
2125

2226
// forward declarations
2327
void receiveEvent(int howMany);
@@ -54,11 +58,15 @@ void loop() {
5458

5559
if ((now - lastWatchdogPet) > watchdogTimeout * 1000) {
5660
rebootCount++;
61+
//Serial.println("REBOOT!!");
5762
// timeout -> toggle REBOOT_PIN low then high
5863
digitalWrite(REBOOT_PIN, LOW);
5964
delay(100);
6065
digitalWrite(REBOOT_PIN, HIGH);
66+
lastPetAtBite = (now - lastWatchdogPet)/1000;
6167
lastWatchdogPet = now; // reset watchdog timer
68+
lastWatchdogReboot = now;
69+
6270
}
6371
}
6472

@@ -94,7 +102,7 @@ void receiveEvent(int howMany) {
94102
handleCommand(command, value);
95103

96104
// Always give master a safe response in case it does a requestFrom
97-
dataToSend = 0;
105+
//dataToSend = 0;
98106

99107
} else if (bytesRead > 0) {
100108
// ---- Write to pin ----
@@ -123,8 +131,7 @@ void receiveEvent(int howMany) {
123131

124132
// ---- Command handler ----
125133
void handleCommand(byte command, long value) {
126-
//
127-
Serial.print("command: ");
134+
//Serial.print("command: ");
128135
Serial.println(command);
129136
switch (command) {
130137
case COMMAND_REBOOT:
@@ -144,13 +151,25 @@ void handleCommand(byte command, long value) {
144151
dataToSend = rebootCount;
145152
break;
146153

154+
case COMMAND_LASTPETATBITE:
155+
dataToSend = lastPetAtBite;
156+
break;
157+
158+
case COMMAND_LASTWATCHDOGPET:
159+
dataToSend = lastWatchdogPet;
160+
break;
161+
147162
default:
148163
if(command > 199 && command < 210) {
149164

150165
byte watchdogTimingIndication = command - COMMAND_WATCHDOGPETBASE;
151-
watchdogTimeout = 1;
152-
for (byte i = 0; i < watchdogTimingIndication; i++) { //better than pow()
153-
watchdogTimeout *= 10;
166+
167+
if(lastTimeoutScale != watchdogTimingIndication && watchdogTimingIndication>2) {
168+
watchdogTimeout = 1;
169+
for (byte i = 0; i < watchdogTimingIndication; i++) { //better than pow()
170+
watchdogTimeout *= 10;
171+
}
172+
lastTimeoutScale = watchdogTimingIndication;
154173
}
155174
lastWatchdogPet = millis();
156175
}

0 commit comments

Comments
 (0)