-
Notifications
You must be signed in to change notification settings - Fork 0
/
crosby_badge.ino
563 lines (474 loc) · 12.6 KB
/
crosby_badge.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#include <Arduino.h>
#include <stdint.h>
#include <Wire.h>
#include "mma8653.h"
//#include <ESP8266WiFi.h>
//#include <WiFiUdp.h>
#define LED_ROWS 8
#define LED_COLS 8
#define LED_COUNT (LED_ROWS*LED_COLS)
#define BUTTON 4
const int i2c_scl = 14;
const int i2c_sda = 12;
class Pixel {
/** Blend the new value in with the fraction specified 0-256*/
public:
uint8_t v;
void blend(int _new, int frac) {
*this = (v*(255-frac)+(_new*frac))/255;
};
uint8_t operator=(int in) {
v = in;
};
void draw() {
int v2 = v;
if (v2 == 255) v2=254;
Serial1.print(char(v2));
}
};
class Color {
Pixel r;
Pixel g;
Pixel b;
public:
void blend(int r, int g, int b, int frac) {
if (frac < 0) frac = 0;
if (frac > 255) frac = 255;
this->r.blend(r,frac);
this->b.blend(b,frac);
this->g.blend(g,frac);
}
void set(int r, int g, int b) {
this->r = r;
this->g = g;
this->b = b;
}
void draw() {
r.draw();
g.draw();
b.draw();
}
};
class Board {
Color *color = new Color[LED_COUNT]();
boolean isChanged = false;
uint32_t last_draw = 0;
public:
uint8_t brightness = 0;
void setup() {
// Drawing in matrix.
Serial1.begin( 230400 );
}
Color *cell(int x, int y) {
isChanged = true;
if (x < 0 || x > 7) return color;
if (y < 0 || y > 7) return color;
return color+8*x+y;
}
void clear(int r, int g, int b) {
isChanged = true;
for (int i = 0 ; i < LED_COUNT ; i++) {
(color+i)->set(r,g,b);
}
}
void clearRow(int row, int r, int g, int b) {
for (int i = 0 ; i < LED_COUNT ; i++) {
cell(row,i)->set(r,g,b);
}
}
void set(int x, int y, int r, int g, int b) {
cell(x,y)->set(r,g,b);
}
void blend(int x, int y, int r, int g, int b, int blend) {
cell(x,y)->blend(r,g,b,blend);
}
void setRow(int row, uint8_t val, int r, int g, int b) {
for (int ii = 0 ; ii < 8 ; ii++) {
int mask = 1 << ii;
if ((val & mask) != 0)
cell(row,ii)->set(r,g,b);
}
}
void setCol(int col, uint8_t val, int r, int g, int b) {
for (int ii = 0 ; ii < 8 ; ii++) {
int mask = 1 << ii;
if ((val & mask) != 0)
cell(ii,col)->set(r,g,b);
}
}
void blendRow(int row, uint8_t val, int r, int g, int b, int frac) {
for (int ii = 0 ; ii < 8 ; ii++) {
int mask = 1 << ii;
if ((val & mask) != 0)
cell(row,ii)->blend(r,g,b,frac);
}
}
void blendCol(int col, uint8_t val, int r, int g, int b, int frac) {
for (int ii = 0 ; ii < 8 ; ii++) {
int mask = 1 << ii;
if ((val & mask) != 0)
cell(ii,col)->blend(r,g,b,frac);
}
}
void draw(boolean force) {
const uint32_t now = millis();
if (!force && (!isChanged || now-last_draw < 40))
return;
last_draw = now;
isChanged = false;
// Make first pixel oscillate.
//Serial1.print((char(now>>3) & 0xfe));
//Serial1.print((char(now>>3) & 0xfe));
//Serial1.print((char(now>>3) & 0xfe));
for (int i = 0 ; i < LED_COUNT ; i++) {
Color *j= color+i;
j->draw();
}
Serial1.print(char(255));
// Command mode.
for(int i = 0; i < 9; i++) {
Serial1.print(char(255));
}
Serial1.print(char(1)); // 'Set brightness'
Serial1.print(char(brightness));
}
/** Valid from 0 -- 255 */
void setBrightness(int val) {
brightness = val;
isChanged = true;
}
};
class TheButton {
public:
boolean nowDown = false;
// If this is true, you can get durationDown.
boolean justCameUp = false;
uint32_t whenDown;
// Updated when down until it just came up. then Latched until next press.
uint32_t durationDown;
void setup () {
pinMode(BUTTON, INPUT_PULLUP);
}
void loop() {
boolean nowDown2 = isNowDown2();
if (nowDown2 && !nowDown) {
// Just going down the first time.
whenDown = millis();
}
justCameUp = nowDown && !nowDown2;
if (nowDown2 || justCameUp) {
durationDown = millis()-whenDown;
}
nowDown = nowDown2;
}
// milliseconds.
const int CLICK = 200; // Less than this.
const int SHORT_PRESS = 500; // Less than this.
const int MEDIUM_PRESS = 2000; // Less than this.
boolean inShortPress() {
return nowDown && durationDown > CLICK && durationDown < SHORT_PRESS;
}
boolean inMediumPress() {
return nowDown && durationDown > SHORT_PRESS && durationDown < MEDIUM_PRESS;
}
boolean inLongPress() {
return nowDown && durationDown > MEDIUM_PRESS;
}
boolean wasClick() {
return justCameUp && durationDown < CLICK;
}
boolean wasShortPress() {
return justCameUp && durationDown > CLICK && durationDown < SHORT_PRESS;
}
boolean wasMediumPress() {
return justCameUp && durationDown > SHORT_PRESS && durationDown < MEDIUM_PRESS;
}
boolean wasLongPress() {
return justCameUp && durationDown > MEDIUM_PRESS;
}
private:
boolean isNowDown2() {
return !digitalRead(BUTTON);
}
};
#define ACCEL_EMWA (1<<4)
class AccelRaw {
float x, y, z;
void update(float x,float y,float z) {
this->x = x;
this->y = y;
this->z = z;
}
};
class AccelEMWA {
//const float ACCEL_EMWA = (1<<4);
float x, y, z;
void update(float x,float y,float z) {
emwa(this->x,x);
emwa(this->y,y);
emwa(this->z,z);
}
float emwa(float &acc, float in) {
acc = (acc*(ACCEL_EMWA-1)+in)/ACCEL_EMWA;
}
};
class Accel {
MMA8653 chip;
public:
// Some notes:
// 1 g is about 16 as measured by this.
// az is negative 16.
// Accelerometer values at last read. Note, 45 degree orientation: '-x' is with twosigma down and on right.
float ax, ay, az;
// EMWA values of above.
float ex, ey, ez;
// EMWA, but displaced to the speed in x and y.
// dz is about -16 with 'two sigma' text on bottom.
// 'x' measures rows. 'y' measures columns.
float dx, dy;
// Shock accel value (detect bump)
float accel;
void setup() {
// Accelerometer setup.
Wire.begin(i2c_sda, i2c_scl);
chip.setup();
}
// Invoke each iteration.
void loop() {
chip.getXYZ(ax,ay,az);
emwa(ex,ax);
emwa(ey,ay);
emwa(ez,az);
accel = sqrt(ex*ex+ey*ey+ez*ez);
// From the diagram, sensor assumes 45 degree mounting. Rotate it.
const float mag = sqrt(ex*ex+ey*ey);
const float angle = atan2(ex, -ey);
dx = -sin(angle + 45 * M_PI/180) * mag;
dy = cos(angle + 45 * M_PI/180) * mag;
}
float emwa(float &acc, float in) {
acc = (acc*(ACCEL_EMWA-1)+in)/ACCEL_EMWA;
}
};
Board board = Board();
TheButton button = TheButton();
Accel accel = Accel();
class Demo {
public:
virtual void setup() {};
virtual void activate() {};
virtual void deactivate() {};
virtual void loop() = 0;
};
// Show clock, accel, and switch parameters.
class OrientationDemo : public Demo {
public:
virtual void loop() {
const uint32_t now = millis();
board.clear(0,0,16);
board.setRow(0,now >> 9,0,0,254);
//board.set(5,5,255,0,0);
//board.set(6,6,0,255,0);
//board.set(7,7,0,0,255);
showAccelNicely(2,8*accel.ex);
showAccelNicely(3,8*accel.ey);
showAccelNicely(4,8*accel.ez);
board.setRow(5,board.brightness,255,255,255);
showAccelNicely(6,8*accel.dx);
showAccelNicely(7,8*accel.dy);
if (button.nowDown)
board.set(1,0,255,128,128);
else
board.set(1,0,128,255,128);
if (button.justCameUp)
board.set(1,1,255,128,128);
else
board.set(1,1,128,255,128);
board.setRow(1,(button.durationDown>>6)<<2,255,255,255);
}
void showAccelNicely(int col, float accel) {
if (accel > 0)
board.setRow(col,accel,0,255,0);
else
board.setRow(col,-accel,255,0,0);
}
};
// Sliders track 16 bits internally, even though they only output/store 8 bits. That lets them handle variable accelerations.
class Slider {
uint16_t minVal;
uint16_t maxVal;
boolean wrap;
int currentVal;
public:
// Should we wrap when we get to max/min?
Slider(uint8_t minVal, uint8_t maxVal, uint8_t currentVal, boolean wrap) {
this->minVal = minVal << 8;
this->maxVal = (maxVal << 8) + 255;
this->wrap = wrap;
this->set(currentVal);
}
void set(uint8_t newVal) {
this->currentVal = ((int)(newVal))<<8 + 128;
}
// delta is a fixedpoint with 8 fractional bits.
void accountForDelta(int delta) {
int maxChange = maxVal-minVal;
if (delta > maxChange) delta = maxChange;
if (delta < -maxChange) delta = -maxChange;
currentVal = currentVal + delta;
if (!wrap) {
if (currentVal < minVal) currentVal = minVal;
if (currentVal > maxVal) currentVal = maxVal;
} else {
if (currentVal < minVal) currentVal += maxChange;
if (currentVal > maxVal) currentVal -= maxChange;
}
}
uint8_t value() {
return currentVal>>8;
}
};
class PickBrightness : public Demo {
Slider brightness = Slider(2,255,board.brightness,false);
public:
virtual void activate() {
brightness.set(board.brightness);
};
virtual void loop() {
board.clear(0,0,0);
board.setRow(0,board.brightness,255,255,255);
board.setRow(1,0xf,255,0,0);
board.setRow(2,0xf,0,255,0);
board.setRow(3,0xf,0,0,255);
board.setRow(4,0xff,255,255,255);
board.setRow(5,0xff,255,255,255);
board.setRow(6,0xff,255,255,255);
board.setRow(7,0xff,255,255,255);
if (abs(accel.dy*8)>12)
brightness.accountForDelta(accel.dy*16);
board.setBrightness(brightness.value());
}
};
class PickColor : public Demo {
Slider gSlider = Slider(1,255,85,false);
Slider bSlider = Slider(1,255,85,false);
public:
virtual void loop() {
if (abs(accel.dx*8)>12) // Give a dead zone.
gSlider.accountForDelta(accel.dx*8);
if (abs(accel.dy*8)>12) // Give a dead zone.
bSlider.accountForDelta(accel.dy*8);
int b = bSlider.value();
int g = gSlider.value();
int r = max(0,256-g-b);
board.clear(r,g,b);
board.clearRow(0,64,64,64);
board.clearRow(1,64,64,64);
board.clearRow(2,64,64,64);
board.setRow(0,r,255,0,0);
board.setRow(1,g,0,255,0);
board.setRow(2,b,0,0,255);
board.setRow(3,0b11110000,255,255,255);
board.setRow(3,0b11110000,r,g,b);
}
};
class OrangeStripes : public Demo {
virtual void loop() {
int clock = millis()>>7;
int tick = clock%48;
for (int i = 0 ; i < 8 ; i++)
board.blendRow(i,0xff,0,0,0,10);
int stripe;
if (tick < 8) { // From 0-7.
rowOrange(tick);
} else if (tick >=12 && tick < 24) {
board.setRow(0,0b00001000,10,190,40);
board.setRow(1,0b00011000,10,190,40);
board.setRow(2,0b00111100,178,60,0);
board.setRow(3,0b01111110,178,60,0);
board.setRow(4,0b01111110,178,60,0);
board.setRow(5,0b01111110,178,60,0);
board.setRow(6,0b00111100,178,60,0);
board.setRow(7,0b00000000,178,60,0);
board.setRow(2,0b00100100,120,40,0);
board.setRow(3,0b01000010,120,40,0);
board.setRow(4,0b01000010,120,40,0);
board.setRow(5,0b01000010,120,40,0);
board.setRow(6,0b00100100,168,60,0);
} else if (tick >= 36 && tick < 44) {
colOrange(tick-36);
}
}
void rowOrange(int row) {
board.setRow(row,0xff,178,60,0);
}
void colOrange(int col) {
board.setCol(col,0xff,178,60,0);
}
};
OrangeStripes demo4;
OrientationDemo demo1;
PickBrightness demo2;
PickColor demo3;
Demo *demos[] = {&demo4, &demo1,
&demo2,
&demo3};
int demo_size = sizeof(demos)/sizeof(*demos);
class DemoPicker {
int current = 0;
public:
void setup() {
for (int i = 0 ; i < demo_size ; i++)
demos[i]->setup();
demos[current]->activate();
}
void loop() {
if (button.inMediumPress()) {
// TODO: SHould have functionality to save state and restor.
board.clear(millis()&0xff,0,0);
return;
}
//board.clear(0,millis(),0);
if (button.wasMediumPress()) {
demos[current]->deactivate();
Serial.print("Switch Demo\r\n");
Serial.flush();
current++;
if (current == demo_size)
current = 0;
demos[current]->activate();
}
demos[current]->loop();
}
};
// PickBrightness pickBrightness = PickBrightness();
//};
DemoPicker picker;
OrientationDemo orientationDemo = OrientationDemo();
PickBrightness pickBrightness = PickBrightness();
void setup() {
// put your setup code here, to run once:
//WiFi.persistent(false);
//WiFi.mode(WIFI_STA);
//WiFi.begin("netname", "passwd");
// Communication to PC
Serial.begin ( 460800 );
board.setup();
button.setup();
accel.setup();
board.setBrightness(64);
picker.setup();
}
void loop() {
const uint32_t now = millis();
accel.loop();
button.loop();
if (false) {
orientationDemo.loop();
pickBrightness.loop();
} else {
picker.loop();
}
board.draw(false);
delay(10);
}