blob: 3dc21d91e94656008696b984e491e454d7c2eec3 (
plain)
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
|
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <util/delay.h>
#include <stdlib.h>
#include "display.h"
#include "fecmodem.h"
#include "storage.h"
#include "system.h"
#define SHUTDOWN_THRESHOLD 2048
System rocket;
extern animation_t ohai;
uint8_t disp_buf[128];
uint8_t *rx_buf = disp_buf + 64;
void System::initialize()
{
// disable ADC to save power
PRR |= _BV(PRADC);
// dito
wdt_disable();
// Enable pull-ups on PC3 and PC7 (button pins)
PORTC |= _BV(PC3) | _BV(PC7);
display.enable();
modem.enable();
storage.enable();
sei();
}
// ! This function has not been tested yet
void System::receive(void)
{
static uint8_t rx_pos = 0;
static uint16_t remaining_bytes = 0;
uint8_t rx_byte = modem.buffer_get();
/*
* START* and PATTERN* are sync signals, everything else needs to be
* stored on the EEPROM.
* (Note that the C++ standard guarantees "rxExpect > PATTERN2" to match
* for HEADER*, META* and DATA since they are located after PATTERN2
* in the RxExpect enum declaration)
*/
if (rxExpect > PATTERN2) {
rx_buf[rx_pos] = modem.buffer_get();
}
switch(rxExpect) {
case START1:
if (rx_byte == 0x99)
rxExpect = START2;
else
rxExpect = START_OR_PATTERN;
break;
case START2:
if (rx_byte == 0x99) {
rxExpect = PATTERN1;
storage.reset();
} else {
rxExpect = START_OR_PATTERN;
}
break;
case START_OR_PATTERN:
if (rx_byte == 0x99)
rxExpect = START2;
else if (rx_byte == 0xa9)
rxExpect = PATTERN2;
else
rxExpect = START_OR_PATTERN;
break;
case PATTERN1:
if (rx_byte == 0xa9)
rxExpect = PATTERN2;
else
rxExpect = START_OR_PATTERN;
break;
case PATTERN2:
if (rx_byte == 0xa9)
rxExpect = HEADER1;
else
rxExpect = START_OR_PATTERN;
break;
case HEADER1:
rxExpect = HEADER2;
rx_pos = 0;
remaining_bytes = (rx_byte & 0x0f) << 8;
break;
case HEADER2:
rxExpect = META1;
remaining_bytes += rx_byte;
break;
case META1:
rxExpect = META2;
break;
case META2:
rxExpect = DATA_FIRSTBLOCK;
break;
case DATA_FIRSTBLOCK:
remaining_bytes--;
if (remaining_bytes == 0) {
rxExpect = START_OR_PATTERN;
storage.save(rx_buf);
} else if (rx_pos == 64) {
rxExpect = DATA;
rx_pos = 0;
storage.save(rx_buf);
}
break;
case DATA:
remaining_bytes--;
if (remaining_bytes == 0) {
rxExpect = START_OR_PATTERN;
storage.append(rx_buf);
} else if (rx_pos == 64) {
rx_pos = 0;
storage.append(rx_buf);
}
break;
}
/*
if (i == 127) {
i = 0;
} else if (modem_byte == 0) {
if (i > 1) { // workaround for trailing double null bytes
ohai.data = disp_buf;
ohai.length = i-1;
display.show(&ohai);
}
i = 0;
}
*/
}
void System::loop()
{
// both buttons are pressed
if ((PINC & (_BV(PC3) | _BV(PC7))) == 0) {
// naptime!
// But not before both buttons have been pressed for
// SHUTDOWN_THRESHOLD * 0.256 ms. And then, not before both have
// been released, because otherwise we'd go te sleep when
// they're pressed and wake up when they're released, which
// isn't really the point here.
if (want_shutdown < SHUTDOWN_THRESHOLD) {
want_shutdown++;
}
else {
shutdown();
want_shutdown = 0;
}
}
else {
want_shutdown = 0;
}
while (modem.buffer_available()) {
receive();
}
display.update();
}
void System::shutdown()
{
// turn off display to indicate we're about to shut down
display.disable();
modem.disable();
// wait until both buttons are released
while (!((PINC & _BV(PC3)) && (PINC & _BV(PC7)))) ;
// and some more to debounce the buttons
_delay_ms(10);
// actual naptime
// enable PCINT on PC3 (PCINT11) and PC7 (PCINT15) for wakeup
PCMSK1 |= _BV(PCINT15) | _BV(PCINT11);
PCICR |= _BV(PCIE1);
// go to power-down mode
SMCR = _BV(SM1) | _BV(SE);
asm("sleep");
// execution will resume here - disable PCINT again.
// Don't disable PCICR, something else might need it.
PCMSK1 &= ~(_BV(PCINT15) | _BV(PCINT11));
// turn on display
display.enable();
// ... and modem
modem.enable();
// also, reset state machine
rxExpect = START1;
}
ISR(PCINT1_vect)
{
// we use PCINT1 for wakeup, so we should catch it here (and do nothing)
}
|