-
Notifications
You must be signed in to change notification settings - Fork 0
/
LEDbyrinth.ino
129 lines (104 loc) · 3.09 KB
/
LEDbyrinth.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
/*
Copyright 2019 Zach Vonler
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "BoardController.hpp"
#include "Orientation.hpp"
#include "Screensaver.hpp"
#include <avr/sleep.h>
#include <avr/wdt.h>
PictureFrame pictureFrame;
BoardController controller(pictureFrame.matrix(), accelerometer, levels);
void setup() {
// Required because powering AREF pin - analogRead will short otherwise
analogReference(EXTERNAL);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
pictureFrame.enable();
controller.reset();
}
uint32_t lastBoardChangeTm = 0;
uint32_t screensaverTimeout = 30000; // millis
uint32_t sleepTimeout = 300000; // millis
void loop() {
bool didChange = controller.update();
if (didChange) {
lastBoardChangeTm = millis();
} else if (millis() - lastBoardChangeTm > screensaverTimeout) {
Orientation orientation(0.5);
while (!orientation.changed()) {
if (millis() - lastBoardChangeTm < sleepTimeout) {
pictureFrame.screenSaverUpdate();
} else {
// Deep sleep
pictureFrame.disable();
while (!orientation.changed()) {
powerSave();
}
pictureFrame.enable();
break;
}
}
controller.setLevel(1);
lastBoardChangeTm = millis();
}
}
/*
Power save code from http://www.gammon.com.au/power
*/
// watchdog intervals
// sleep bit patterns for WDTCSR
enum
{
WDT_16_MS = 0b000000,
WDT_32_MS = 0b000001,
WDT_64_MS = 0b000010,
WDT_128_MS = 0b000011,
WDT_256_MS = 0b000100,
WDT_512_MS = 0b000101,
WDT_1_SEC = 0b000110,
WDT_2_SEC = 0b000111,
WDT_4_SEC = 0b100000,
WDT_8_SEC = 0b100001,
}; // end of WDT intervals enum
// watchdog interrupt
ISR (WDT_vect)
{
wdt_disable(); // disable watchdog
} // end of WDT_vect
void powerSave() {
// clear various "reset" flags
MCUSR = 0;
// allow changes, disable reset
WDTCSR = bit (WDCE) | bit (WDE);
// set interrupt mode and interval
WDTCSR = bit (WDIE) | WDT_2_SEC;
wdt_reset(); // pat the dog
// disable ADC
byte old_ADCSRA = ADCSRA;
ADCSRA = 0;
// disable various modules
byte old_PRR = PRR;
PRR = 0xFF;
noInterrupts(); // timed sequence follows
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
sleep_enable();
// turn off brown-out enable in software
MCUCR = bit (BODS) | bit (BODSE);
MCUCR = bit (BODS);
interrupts(); // guarantees next instruction executed
sleep_cpu();
// cancel sleep as a precaution
sleep_disable();
PRR = old_PRR;
ADCSRA = old_ADCSRA;
}