-
Notifications
You must be signed in to change notification settings - Fork 67
/
RoadApplePi.ino
276 lines (248 loc) · 6.59 KB
/
RoadApplePi.ino
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
/*
* Simple example showing how to set the Sleepy Pi to wake on button press
* and then power up the Raspberry Pi. To switch the RPi off press the button
* again. If the button is held dwon the Sleepy Pi will cut the power to the
* RPi regardless of any handshaking.
*
* This is a modified version of ButtonOnOff adding the functionality
* of detecting whether the Rpi is running or not. If it detects that
* is has been shutdown (possibly manually by the User) then it will
* cut the power to the Rpi and go into a sleep state.
*
* Also, this version will turn on if it detects a HIGH signal on pin 14, and
* turns back off when the value returns to LOW. Pin 14 is ignored if turned on
* by the button
*/
#include "SleepyPi2.h"
#include <Time.h>
#include <LowPower.h>
#include <PCF8523.h>
#include <Wire.h>
#define kBUTTON_POWEROFF_TIME_MS2000
#define kBUTTON_FORCEOFF_TIME_MS8000
// States
typedef enum
{
eWAIT = 0,
eBUTTON_PRESSED,
eBUTTON_WAIT_ON_RELEASE,
eBUTTON_HELD,
eBUTTON_RELEASED
}eBUTTONSTATE;
typedef enum
{
ePI_OFF = 0,
ePI_BOOTING,
ePI_ON,
ePI_SHUTTING_DOWN
}ePISTATE;
const int LED_PIN = 13;
const int DTP_PIN = 14;
volatile bool buttonPressed = false;
eBUTTONSTATEbuttonState = eWAIT;
ePISTATE pi_state = ePI_OFF;
bool onByButton, state = LOW;
unsigned long time, timePress;
//Setup the Periodic Timer
//Use either eTB_SECOND or eTB_MINUTE or eTB_HOUR
eTIMER_TIMEBASE PeriodicTimer_Timebase = eTB_SECOND;// e.g. Timebase set to seconds
uint8_t PeriodicTimer_Value = 5; // Timer Interval in units of Timebase e.g 5 seconds
void button_isr()
{
// A handler for the Button interrupt.
buttonPressed = true;
}
void alarm_isr()
{
// A handler for the Alarm interrupt.
}
void setup()
{
// Don't actually shutdown
SleepyPi.simulationMode = false;
//Configure pins
pinMode(LED_PIN, OUTPUT);
pinMode(DTP_PIN, INPUT);
digitalWrite(LED_PIN,LOW);
SleepyPi.enablePiPower(false);
SleepyPi.enableExtPower(false);
// Allow wake up triggered by button press
attachInterrupt(1, button_isr, LOW);
SleepyPi.rtcInit(true);
}
void loop()
{
bool pi_running;
unsigned long buttonTime;
//Enter power down state with ADC and BOD module disabled.
//Wake up when wake button is pressed.
//Once button is pressed stay awake - this allows the timer to keep running
switch(buttonState)
{
case eWAIT:
SleepyPi.rtcClearInterrupts();
attachInterrupt(0, alarm_isr, FALLING); // Alarm pin
SleepyPi.setTimer1(PeriodicTimer_Timebase, PeriodicTimer_Value);
SleepyPi.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
if(buttonPressed == false)
{
//Begin routine checks
digitalWrite(LED_PIN,HIGH);
pi_running = SleepyPi.checkPiStatus(false);
//Power on pin 14 - turn on
if(pi_running == false && digitalRead(DTP_PIN) == 1)
{
SleepyPi.enablePiPower(true);
SleepyPi.enableExtPower(true);
pi_state = ePI_BOOTING;
onByButton = false;
}
//Power lost on pin 14 - turn off
if(pi_running == true && digitalRead(DTP_PIN) == 0 && onByButton == false)
{
SleepyPi.enableExtPower(false);
SleepyPi.piShutdown();
pi_state = ePI_SHUTTING_DOWN;
}
switch(pi_state)
{
case ePI_BOOTING:
// Check if we have finished booting
if(pi_running == true)
{
// We have booted up!
pi_state = ePI_ON;
}
else
{
// Still not completed booting so lets carry on waiting
pi_state = ePI_BOOTING;
}
break;
case ePI_ON:
// Check if it is still on?
if(pi_running == false)
{
// Shock horror! it's not running!!
// Assume it has been manually shutdown, so lets cut the power
// Force Pi Off
SleepyPi.enablePiPower(false);
SleepyPi.enableExtPower(false);
pi_state = ePI_OFF;
}
else
{
// Still on - all's well - keep this state
pi_state = ePI_ON;
}
break;
case ePI_SHUTTING_DOWN:
// Is it still shutting down?
if(pi_running == false)
{
// Finished a shutdown
// Force the Power Off
SleepyPi.enablePiPower(false);
SleepyPi.enableExtPower(false);
pi_state = ePI_OFF;
}
else
{
// Still shutting down - keep this state
pi_state = ePI_SHUTTING_DOWN;
}
break;
case ePI_OFF:
default:
// intentional drop thru
// RPi off, so we'll continue to wait
// for a button press to tell us to switch it on
delay(10);
pi_state = ePI_OFF;
break;
}
// Loop back around and go to sleep again
buttonState = eWAIT;
digitalWrite(LED_PIN,LOW);
// Disable external pin interrupt on wake up pin.
detachInterrupt(0);
SleepyPi.ackTimer1();
}
else
{
buttonPressed = false;
// This was a button press so change the button state (and stay awake)
// Disable the alarm interrupt
detachInterrupt(0);
// Disable external pin interrupt on wake up pin.
detachInterrupt(1);
buttonState = eBUTTON_PRESSED;
}
break;
case eBUTTON_PRESSED:
buttonPressed = false;
timePress = millis(); // Log Press time
pi_running = SleepyPi.checkPiStatus(false);
if(pi_running == false)
{
// Switch on the Pi
onByButton = true;
SleepyPi.enablePiPower(true);
SleepyPi.enableExtPower(true);
pi_state = ePI_BOOTING;
}
buttonState = eBUTTON_WAIT_ON_RELEASE;
digitalWrite(LED_PIN,HIGH);
attachInterrupt(1, button_isr, HIGH);
break;
case eBUTTON_WAIT_ON_RELEASE:
if(buttonPressed == true)
{
detachInterrupt(1);
buttonPressed = false;
time = millis(); // Log release time
buttonState = eBUTTON_RELEASED;
}
else
{
// Carry on waiting
buttonState = eBUTTON_WAIT_ON_RELEASE;
}
break;
case eBUTTON_RELEASED:
pi_running = SleepyPi.checkPiStatus(false);
if(pi_running == true)
{
// Check how long we have held button for
buttonTime = time - timePress;
if(buttonTime > kBUTTON_FORCEOFF_TIME_MS)
{
// Force Pi Off
SleepyPi.enablePiPower(false);
SleepyPi.enableExtPower(false);
pi_state = ePI_OFF;
}
else if (buttonTime > kBUTTON_POWEROFF_TIME_MS)
{
// Start a shutdown
pi_state = ePI_SHUTTING_DOWN;
SleepyPi.piShutdown(); // true
SleepyPi.enableExtPower(false);
}
else
{
// Button not held off long - Do nothing
}
}
else
{
// Pi not running
}
digitalWrite(LED_PIN,LOW);
attachInterrupt(1, button_isr, LOW); // button pin
buttonState = eWAIT;
break;
default:
break;
}
}