-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnp.c
More file actions
282 lines (226 loc) · 6.34 KB
/
snp.c
File metadata and controls
282 lines (226 loc) · 6.34 KB
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
276
277
278
279
280
281
282
#define F_CPU 1000000UL
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/sleep.h>
#include <stdbool.h>
#include <stdint.h>
#define MEASURE
#define SECS_PER_MIN (uint32_t)60
#define SECS_PER_HOUR ((uint32_t)60 * SECS_PER_MIN)
#define SECS_PER_DAY ((uint32_t)24 * SECS_PER_HOUR)
volatile uint32_t secs = 12 * SECS_PER_HOUR;
volatile uint32_t millis = 0;
volatile uint32_t last_activity = 0;
volatile enum Mode { Display, Sleep, SetTime } mode = Display;
void disable_components() {
// disable Analog Digital Converter
ADCSRA &= ~(1 << ADEN);
// shut down Analog Digital Converter
PRR |= (1 << PRADC);
// disable Analog Comparator
ACSR |= (1 << ACD);
// shut down Two Wire Interface
PRR |= (1 << PRTWI);
// shut down Serial Peripheral Interface
PRR |= (1 << PRSPI);
// shut down Usart
PRR |= (1 << PRUSART0);
}
void setup_timer0_millis() {
// prescaler 8
TCCR0B |= (1 << CS01);
// enable ctc
TCCR0A |= (1 << WGM01);
// enable Timer0 compare interrupt A
TIMSK0 |= (1 << OCIE0A);
OCR0A = 125 - 1;
}
ISR(TIMER0_COMPA_vect) { millis += 1; }
void setup_timer1_pwm() {
// fast PWM 8-bit
TCCR1A |= (1 << WGM10);
TCCR1B |= (1 << WGM12);
// Use OC1A and OC1B pins
TCCR1A |= (1 << COM1A1) | (1 << COM1B1);
// prescaler 8
TCCR1B |= 1 << CS11;
// set OC1A, OC1B writeable
DDRB |= (1 << PB1) | (1 << PB2);
}
void setup_timer2_secs() {
// prescaler 128
TCCR2B |= (1 << CS22) | (1 << CS20);
// Asynchronous Counter2, with frequency 2^15
ASSR |= (1 << AS2);
// Run interrupt on Counter2 overflow
TIMSK2 |= (1 << TOIE2);
TIFR2 |= (1 << TOV2);
// measure output pin
DDRD |= (1 << PD5);
}
volatile uint16_t drift = 0;
// NUM_PERIODS = 1 / (2s - T)
#define NUM_PERIODS 22472
ISR(TIMER2_OVF_vect) {
secs += 1;
#ifdef MEASURE
if (secs % 2 == 0) {
PORTD |= (1 << PD5);
} else {
PORTD &= ~(1 << PD5);
}
#endif
drift += 1;
if (drift == NUM_PERIODS) {
// quartz is always a bit faster than the expected frequency
secs -= 1;
drift = 0;
}
}
volatile enum Intensity {
VeryLow = 0,
Low = 1,
Medium = 2,
High = 3,
VeryHigh = 5,
} intensity = Medium;
void apply_intensity(enum Intensity intensity) {
const uint8_t mapping[5] = {32, 64, 128, 192, 255};
uint8_t inverted = 255 - mapping[(uint8_t)intensity];
OCR1A = inverted;
OCR1B = inverted;
}
void increase_intensity() {
intensity += 1;
if (intensity > VeryHigh) {
intensity = VeryLow;
}
apply_intensity(intensity);
}
void decrease_intensity() {
intensity -= 1;
if (intensity > VeryHigh) {
intensity = VeryHigh;
}
apply_intensity(intensity);
}
const uint8_t mask_c =
(1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3) | (1 << PC4) | (1 << PC5);
const uint8_t mask_d = (1 << PD0) | (1 << PD1) | (1 << PD6) | (1 << PD7);
const uint8_t mask_b = (1 << PB0);
void setup_leds() {
DDRC |= mask_c;
DDRD |= mask_d;
DDRB |= mask_b;
apply_intensity(intensity);
}
uint16_t extract_bit(uint16_t bits, uint8_t index) {
return (bits >> index) & 0b1;
}
void display(uint16_t bits) {
PORTC &= ~mask_c;
PORTD &= ~mask_d;
PORTB &= ~mask_b;
PORTC |= (extract_bit(bits, 0) << PC0) | (extract_bit(bits, 1) << PC1) |
(extract_bit(bits, 2) << PC2) | (extract_bit(bits, 3) << PC3) |
(extract_bit(bits, 4) << PC4) | (extract_bit(bits, 5) << PC5);
PORTD |= (extract_bit(bits, 6) << PD0) | (extract_bit(bits, 7) << PD1) |
(extract_bit(bits, 8) << PD6) | (extract_bit(bits, 9) << PD7);
PORTB |= (extract_bit(bits, 10) << PB0);
}
void invert_display() {
PORTC ^= mask_c;
PORTD ^= mask_d;
PORTB ^= mask_b;
}
void setup_buttons() {
// enable external interrupts INT0 and INT1
EIMSK |= (1 << INT0) | (1 << INT1);
// trigger interrupt on a falling edge
EICRA |= (1 << ISC01) | (1 << ISC11);
// enable pin change interrupts PCINT23..16
PCICR |= (1 << PCIE2);
// enable pin change interrupt PCINT20
PCMSK2 |= (1 << PCINT20);
// enable internal pullup resistors
PORTD |= (1 << PD2) | (1 << PD3) | (1 << PD4);
}
int32_t difference(uint32_t a, uint32_t b) {
return (a > b) ? a - b : -(b - a);
}
bool debounce(uint32_t delay) {
bool result = difference(millis, last_activity) > delay;
if (result) {
last_activity = millis;
}
return result;
}
ISR(INT0_vect) {
if (!debounce(250)) return;
switch (mode) {
case Display:
mode = SetTime;
invert_display();
break;
case SetTime:
mode = Display;
invert_display();
break;
case Sleep:
sleep_disable();
mode = Display;
break;
}
}
ISR(INT1_vect) {
if (mode == Sleep) return;
if (!debounce(250)) return;
if (mode == Display) {
decrease_intensity();
return;
}
secs += SECS_PER_HOUR;
if (secs >= SECS_PER_DAY) {
secs -= SECS_PER_DAY;
}
}
ISR(PCINT2_vect) {
if (mode == Sleep) return;
if (!debounce(250)) return;
if (mode == Display) {
increase_intensity();
return;
}
secs += SECS_PER_MIN;
if (secs % SECS_PER_HOUR == 0) {
secs -= SECS_PER_HOUR;
}
}
int main() {
disable_components();
setup_timer0_millis();
setup_timer1_pwm();
setup_timer2_secs();
setup_leds();
setup_buttons();
set_sleep_mode(SLEEP_MODE_PWR_SAVE);
sei();
while (true) {
if (mode == Sleep) {
sleep_enable();
sleep_cpu();
continue;
}
// sleep after 10 seconds of inactivity
if (difference(millis, last_activity) >= 10000) {
display(0);
mode = Sleep;
continue;
}
// hours in the upper 5 bits
const uint16_t hours = secs / SECS_PER_HOUR;
// minutes in the lower 6 bits
const uint16_t mins = (secs % SECS_PER_HOUR) / SECS_PER_MIN;
display(hours << 6 | mins);
}
}