-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
367 lines (309 loc) · 8.55 KB
/
main.c
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <stdlib.h>
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <ctype.h>
#include <string.h>
#include <util/delay.h>
#include <util/atomic.h>
#include <math.h>
#include "main.h"
#include "ADXL345.h"
#include "TLC5947DAP.h"
#include "console.h"
void ioinit(void);
void timerinit(void);
void audioinit(void);
void serialinit(void);
void accelinit(void);
uint8_t get_battery_percent();
void shutdown(int forever);
void banner();
void lowbat();
uint16_t get_ticks();
#define ITERS_PER_SECOND 20
#define ITERS_PER_MINUTE (ITERS_PER_SECOND * 60)
//check battery every 10 minutes
#define BATTERY_CHECK_ITERS (600 * ITERS_PER_SECOND)
// Idle config
#define IDLE_MINUTES_UNTIL_SHUTDOWN 10
#define IDLE_ACTIVITY_THRESHOLD 4
#define MINUTES_UNTIL_SHUTDOWN 15
#define BRI_MAX 255
#define BRI_MIN 32
#define HIST_SIZE (ITERS_PER_SECOND * 4)
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
static volatile uint16_t ticks; // updated async
static volatile uint32_t curAudioPower; // updated async
static volatile uint8_t curAudioPowerReady; // updated async
int main(void)
{
//LEDs
uint16_t hue = 0, brightness = BRI_LIMIT - 1, lastBrightness = brightness;
//motion
int16_t oldaccel[3], rawoldaccel[3], accel[3] = {0}, rawaccel[3] = {0};
int32_t dotp[3];
double flt_div, flt_acos;
int8_t int_acos;
//microphone
uint32_t histPower[HIST_SIZE] = {0}, histPowerIndex = 0;
uint32_t currentPower, avgPower = 0;
double scale;
//idle
uint16_t idleIters = 0, idleActivity = 0;
uint8_t idleMinutes = 0;
// Initialize io ports
ioinit();
// Initialize serial
serialinit();
stdout = &mystdout;
// print banner/battery check
banner();
// Initialize the timer
timerinit();
// Initialize audio capture
audioinit();
// Initialize accelerometer
accelinit();
// Enable interrupts
sei();
while(1)
{
// Wait until audio sample is ready
while(!curAudioPowerReady) {}
curAudioPowerReady = 0;
// Grab the current audio power
currentPower = curAudioPower;
//Read accelerometer, determine hue
oldaccel[0] = accel[0]; oldaccel[1] = accel[1]; oldaccel[2] = accel[2];
rawoldaccel[0] = rawaccel[0]; rawoldaccel[1] = rawaccel[1]; rawoldaccel[2] = rawaccel[2];
adxl345_getxyz(&rawaccel[0], &rawaccel[1], &rawaccel[2]);
//(lowpass filter the accelerometer data)
accel[0] = (rawoldaccel[0] + rawaccel[0]) / 2;
accel[1] = (rawoldaccel[1] + rawaccel[1]) / 2;
accel[2] = (rawoldaccel[2] + rawaccel[2]) / 2;
//theta = acos(a.b / |a||b|)
dotp[0] = accel[0] * oldaccel[0] + accel[1] * oldaccel[1] + accel[2] * oldaccel[2];
dotp[1] = accel[0] * accel[0] + accel[1] * accel[1] + accel[2] * accel[2];
dotp[2] = oldaccel[0] * oldaccel[0] + oldaccel[1] * oldaccel[1] + oldaccel[2] * oldaccel[2];
if(dotp[1] == 0) dotp[1] = 1;
if(dotp[2] == 0) dotp[2] = 1;
flt_div = dotp[0] / (sqrt(dotp[1]) * sqrt(dotp[2]));
flt_acos = acos(flt_div);
int_acos = (int8_t)(flt_acos * 180 / M_PI);
//the amount of motion varies pretty widely; it seems like the best
//aesthetic results are from motion-triggering
if(int_acos > 15)
{
hue += 15;
idleActivity++;
}
if(hue >= HUE_LIMIT) hue -= HUE_LIMIT;
//determine intensity (sliding scale)
avgPower -= histPower[histPowerIndex];
histPower[histPowerIndex] = currentPower;
avgPower += histPower[histPowerIndex];
histPowerIndex++;
if(histPowerIndex >= HIST_SIZE) histPowerIndex = 0;
//scale should vary between 0.5ish and 1.5ish... clamp to [0,1]
scale = (float)(currentPower * HIST_SIZE) / avgPower;
scale -= 0.5;
if(scale > 1.0) scale = 1.0;
if(scale < 0.0) scale = 0.0;
//perception mapping: y=x^4 to give it a concave shape
scale = scale*scale*scale*scale;
brightness = BRI_MIN + (BRI_MAX-BRI_MIN) * scale;
if(brightness < lastBrightness)
brightness = (brightness + lastBrightness) / 2;
lastBrightness = brightness;
//Update LED colors
send_hsb(hue, SATURATION, brightness);
//Handle console
dispatch_console();
// Idle check
idleIters++;
if(idleIters >= ITERS_PER_MINUTE) {
if(idleActivity >= IDLE_ACTIVITY_THRESHOLD) {
// Activity!!! Reset idle minutes
idleMinutes = 0;
} else {
// Not enough activity, mark minute as idle
idleMinutes++;
}
// Shutdown if idle for too long
if(idleMinutes >= IDLE_MINUTES_UNTIL_SHUTDOWN) {
shutdown(0);
}
// Reset iteration counts
idleIters = 0;
idleActivity = 0;
}
// Idle backstop check
if(get_ticks() >= MINUTES_UNTIL_SHUTDOWN * 60) {
shutdown(0);
}
}
}
void ioinit(void)
{
// Power latch
PORTC |= _BV(PWR_EN_CPU); // PWR_EN_CPU should default to high
DDRC |= (_BV(PWR_EN_CPU));
// Accelerometer
DDRC |= (_BV(ACCEL_SCK) | _BV(ACCEL_DI));
PORTD |= _BV(ACCEL_CS); // ADXL_CS should default to high
DDRD |= (_BV(ACCEL_CS));
// LEDs
DDRB |= (_BV(PWM_XL) | _BV(PWM_SI) | _BV(PWM_CL));
PORTD |= _BV(PWM_BL); // LED_BLANK should default to high
DDRD |= (_BV(PWM_BL));
}
void timerinit(void)
{
// Set TIMER1_CAPT interrupt for 1s interval
ICR1 = 15625;
TIMSK1 |= (_BV(ICIE1));
TCCR1B |= (_BV(WGM13) | _BV(WGM12) | _BV(CS12) | _BV(CS10)); // CTC, ICR1, CLK/1024
}
void audioinit(void)
{
// Free running at 19230 samples/s
ADMUX = (_BV(ADLAR) | MIC_ADC_MUX);
ADCSRA = (_BV(ADEN) | _BV(ADSC) | _BV(ADATE) | _BV(ADIE) | _BV(ADPS2) | _BV(ADPS1));
}
void serialinit(void)
{
// 115.2K, 8N1
UBRR0 = 16;
UCSR0A = (_BV(U2X0));
UCSR0B = (_BV(RXEN0) | _BV(TXEN0));
UCSR0C = (_BV(UCSZ01) | _BV(UCSZ00));
}
void accelinit(void)
{
// Interrupts
adxl345_write(THRESH_ACT, 10); // Threshold to detect activity
adxl345_write(ACT_INACT_CTL, (_BV(4) | _BV(5) | _BV(6) | _BV(7))); // X, Y, Z participate in activity detection, AC coupled
adxl345_write(DATA_FORMAT, INT_INVERT); // Needed for power control bootstrapping
adxl345_write(INT_MAP, ~(ACTIVITY)); // Only activity interrupt on INT1
adxl345_write(INT_ENABLE, ACTIVITY); // Enable the interrupts
// Sampling
adxl345_write(BW_RATE, RATE_25HZ); // Set sample rate to 25 Hz
adxl345_write(POWER_CTL, MEASURE); // Start measuring
}
int uart_putchar(char c, FILE *stream)
{
if(c == '\n') {
uart_putchar('\r', stream);
}
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
return 0;
}
int uart_charwaiting(void)
{
return !(UCSR0A & (1<<RXC0));
}
uint8_t uart_getchar(void)
{
while( !(UCSR0A & (1<<RXC0)) );
return UDR0;
}
uint8_t get_battery_percent()
{
ADMUX = BATT_ADC_MUX;
//10x multisample
uint16_t val = 0;
for(uint8_t i = 0; i < 10; ++i) {
ADCSRA = _BV(ADEN)|_BV(ADSC)|_BV(ADIF)|_BV(ADPS2)|_BV(ADPS1);
while(bit_is_clear(ADCSRA, ADIF));
val += ADC;
};
ADCSRA = 0;
val /= 10;
// Scale the voltage to percent
// 500 => 1.8V
// 800 => 3V
if(val < 500) { val = 500; }
if(val > 800) { val = 800; }
val -= 500;
val /= 3;
return val;
}
//blink LEDs and turn off (in case of low battery)
void lowbat()
{
send_rgb(100, 0, 0);
_delay_ms(200);
send_rgb(0, 0, 0);
_delay_ms(200);
send_rgb(100, 0, 0);
_delay_ms(200);
send_rgb(0, 0, 0);
_delay_ms(200);
send_rgb(100, 0, 0);
_delay_ms(200);
send_rgb(0, 0, 0);
shutdown(1);
}
void banner()
{
//console banner
printf_P(PSTR("Syzygryd Memorial Cube Firmware. Hello!\n"));
//give the user a visual indication of battery health...
uint8_t bat = get_battery_percent();
if(bat == 0) {
lowbat();
} else {
// Show the battery level
send_rgb(100 - bat, bat, 0);
_delay_ms(1000);
send_rgb(0, 0, 0);
}
}
void shutdown(int forever)
{
// Turn off the LEDs
send_rgb(0,0,0);
// Reset the ADXL threshold level
adxl345_write(THRESH_ACT, 50); // Threshold to detect activity
// If we don't want to turn back on turn off the accelerometer
if(forever)
adxl345_write(POWER_CTL, 0);
// Loop until we die
while(1)
{
// Read the ADXL interrupts to clear out PWR_EN_ADXL
adxl345_read(INT_SOURCE);
// Lower the power latch (PWR_EN_CPU)
PORTC &= ~(_BV(PORTC4));
_delay_ms(5000);
}
}
uint16_t get_ticks()
{
uint16_t result;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
result = ticks;
}
return ticks;
}
ISR(TIMER1_CAPT_vect, ISR_BLOCK)
{
ticks++;
}
ISR(ADC_vect, ISR_BLOCK)
{
static uint16_t count;
static uint32_t power;
count++;
if(count == 19230 / ITERS_PER_SECOND) {
curAudioPower = power;
curAudioPowerReady = 1;
power = 0;
count = 0;
}
int8_t sample = ADCH - 128;
power += (sample * sample);
}