-
Notifications
You must be signed in to change notification settings - Fork 100
/
hexbright4.ino
executable file
·388 lines (351 loc) · 10.1 KB
/
hexbright4.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
/* Test firmware for HexBright
Notes:
Requires Arduino 1.0.1!
*/
#include <math.h>
#include <Wire.h>
// Settings
#define OVERTEMP 315
// Constants
#define ACC_ADDRESS 0x4C
#define ACC_REG_XOUT 0
#define ACC_REG_YOUT 1
#define ACC_REG_ZOUT 2
#define ACC_REG_TILT 3
#define ACC_REG_INTS 6
#define ACC_REG_MODE 7
// Pin assignments
#define DPIN_RLED_SW 2
#define DPIN_GLED 5
#define DPIN_PGOOD 7
#define DPIN_PWR 8
#define DPIN_DRV_MODE 9
#define DPIN_DRV_EN 10
#define DPIN_ACC_INT 3
#define APIN_TEMP 0
#define APIN_CHARGE 3
// Interrupts
#define INT_SW 0
#define INT_ACC 1
// Modes
#define MODE_POWERUP 0
#define MODE_OFF 1
#define MODE_LOW 2
#define MODE_HIGH 3
#define MODE_KNOBBING 4
#define MODE_KNOBBED 5
#define MODE_BLINKING 6
#define MODE_BLINKING_PREVIEW 7
#define MODE_DAZZLING 8
#define MODE_DAZZLING_PREVIEW 9
// State
byte mode = 0;
unsigned long btnTime = 0;
boolean btnDown = false;
void setup()
{
// We just powered on! That means either we got plugged
// into USB, or (more likely) the user is pressing the
// power button. We need to pull up the enable pin of
// the regulator very soon so we don't lose power.
pinMode(DPIN_PWR, INPUT);
digitalWrite(DPIN_PWR, LOW);
// pinMode(DPIN_PWR, OUTPUT);
// digitalWrite(DPIN_PWR, LOW);
// Initialize GPIO
pinMode(DPIN_RLED_SW, INPUT);
pinMode(DPIN_GLED, OUTPUT);
pinMode(DPIN_DRV_MODE, OUTPUT);
pinMode(DPIN_DRV_EN, OUTPUT);
pinMode(DPIN_ACC_INT, INPUT);
pinMode(DPIN_PGOOD, INPUT);
digitalWrite(DPIN_DRV_MODE, LOW);
digitalWrite(DPIN_DRV_EN, LOW);
digitalWrite(DPIN_ACC_INT, HIGH);
// Initialize serial busses
Serial.begin(9600);
Wire.begin();
// Configure accelerometer
byte config[] = {
ACC_REG_INTS, // First register (see next line)
0xE4, // Interrupts: shakes, taps
0x00, // Mode: not enabled yet
0x00, // Sample rate: 120 Hz
0x0F, // Tap threshold
0x10 // Tap debounce samples
};
Wire.beginTransmission(ACC_ADDRESS);
Wire.write(config, sizeof(config));
Wire.endTransmission();
// Enable accelerometer
byte enable[] = {ACC_REG_MODE, 0x01}; // Mode: active!
Wire.beginTransmission(ACC_ADDRESS);
Wire.write(enable, sizeof(enable));
Wire.endTransmission();
btnTime = millis();
btnDown = digitalRead(DPIN_RLED_SW);
mode = MODE_OFF;
Serial.println("Powered up!");
}
void loop()
{
static unsigned long lastTime, lastTempTime, lastAccTime;
static float lastKnobAngle, knob;
static byte blink;
unsigned long time = millis();
// Blink the indicator LED now and then
digitalWrite(DPIN_GLED, (time&0x03FF)?LOW:HIGH);
// Check the serial port
if (Serial.available())
{
char c = Serial.read();
switch (c)
{
case 's':
{
int temperature = analogRead(APIN_TEMP);
Serial.print("Temperature = ");
Serial.println(temperature);
char accel[3];
readAccel(accel);
Serial.print("Acceleration = ");
Serial.print(accel[0], DEC);
Serial.print(", ");
Serial.print(accel[1], DEC);
Serial.print(", ");
Serial.println(accel[2], DEC);
byte pgood = digitalRead(DPIN_PGOOD);
Serial.print("LED driver power good = ");
Serial.println(pgood?"Yes":"No");
}
break;
}
}
// Check the temperature sensor
if (time-lastTempTime > 1000)
{
lastTempTime = time;
int temperature = analogRead(APIN_TEMP);
Serial.print("Temperature = ");
Serial.println(temperature);
if (temperature > OVERTEMP)
{
Serial.println("Overheat shutdown!");
mode = MODE_OFF;
digitalWrite(DPIN_DRV_MODE, LOW);
digitalWrite(DPIN_DRV_EN, LOW);
digitalWrite(DPIN_PWR, LOW);
}
}
// Check if the accelerometer wants to interrupt
byte tapped = 0, shaked = 0;
if (!digitalRead(DPIN_ACC_INT))
{
Wire.beginTransmission(ACC_ADDRESS);
Wire.write(ACC_REG_TILT);
Wire.endTransmission(false); // End, but do not stop!
Wire.requestFrom(ACC_ADDRESS, 1); // This one stops.
byte tilt = Wire.read();
if (time-lastAccTime > 500)
{
lastAccTime = time;
tapped = !!(tilt & 0x20);
shaked = !!(tilt & 0x80);
if (tapped) Serial.println("Tap!");
if (shaked) Serial.println("Shake!");
}
}
// Do whatever this mode does
switch (mode)
{
case MODE_KNOBBING:
{
if (time-lastTime < 100) break;
lastTime = time;
float angle = readAccelAngleXZ();
float change = angle - lastKnobAngle;
lastKnobAngle = angle;
if (change > PI) change -= 2.0*PI;
if (change < -PI) change += 2.0*PI;
knob += -change * 40.0;
if (knob < 0) knob = 0;
if (knob > 255) knob = 255;
// Make apparent brightness changes linear by squaring the
// value and dividing back down into range. This gives us
// a gamma correction of 2.0, which is close enough.
byte bright = (long)(knob * knob) >> 8;
// Avoid ever appearing off in this mode!
if (bright < 8) bright = 8;
analogWrite(DPIN_DRV_EN, bright);
Serial.print("Ang = ");
Serial.print(angle);
Serial.print("\tChange = ");
Serial.print(change);
Serial.print("\tKnob = ");
Serial.print(knob);
Serial.print("\tBright = ");
Serial.println(bright);
}
break;
case MODE_BLINKING:
case MODE_BLINKING_PREVIEW:
if (time-lastTime < 250) break;
lastTime = time;
blink = !blink;
digitalWrite(DPIN_DRV_EN, blink);
break;
case MODE_DAZZLING:
case MODE_DAZZLING_PREVIEW:
if (time-lastTime < 10) break;
lastTime = time;
digitalWrite(DPIN_DRV_EN, random(4)<1);
break;
}
// Check for mode changes
byte newMode = mode;
byte newBtnDown = digitalRead(DPIN_RLED_SW);
switch (mode)
{
case MODE_OFF:
if (btnDown && !newBtnDown) // Button released
newMode = MODE_LOW;
if (btnDown && newBtnDown && (time-btnTime)>500) // Held
newMode = MODE_KNOBBING;
break;
case MODE_LOW:
if (btnDown && !newBtnDown) // Button released
newMode = MODE_HIGH;
if (btnDown && newBtnDown && (time-btnTime)>500) // Held
newMode = MODE_KNOBBING;
break;
case MODE_HIGH:
if (btnDown && !newBtnDown) // Button released
newMode = MODE_OFF;
if (btnDown && newBtnDown && (time-btnTime)>500) // Held
newMode = MODE_KNOBBING;
break;
case MODE_KNOBBING:
if (btnDown && !newBtnDown) // Button released
newMode = MODE_KNOBBED;
if (btnDown && newBtnDown && tapped)
newMode = MODE_BLINKING_PREVIEW;
break;
case MODE_KNOBBED:
if (btnDown && !newBtnDown) // Button released
newMode = MODE_OFF;
if (btnDown && newBtnDown && (time-btnTime)>500) // Held
newMode = MODE_KNOBBING;
break;
case MODE_BLINKING:
if (btnDown && !newBtnDown) // Button released
newMode = MODE_OFF;
if (btnDown && newBtnDown && (time-btnTime)>500) // Held
newMode = MODE_BLINKING_PREVIEW;
break;
case MODE_BLINKING_PREVIEW:
if (btnDown && !newBtnDown) // Button released
newMode = MODE_BLINKING;
if (btnDown && newBtnDown && tapped)
newMode = MODE_DAZZLING_PREVIEW;
break;
case MODE_DAZZLING:
if (btnDown && !newBtnDown) // Button released
newMode = MODE_OFF;
if (btnDown && newBtnDown && (time-btnTime)>500) // Held
newMode = MODE_DAZZLING_PREVIEW;
break;
case MODE_DAZZLING_PREVIEW:
if (btnDown && !newBtnDown) // Button released
newMode = MODE_DAZZLING;
if (btnDown && newBtnDown && tapped)
newMode = MODE_BLINKING_PREVIEW;
break;
}
// Do the mode transitions
if (newMode != mode)
{
switch (newMode)
{
case MODE_OFF:
Serial.println("Mode = off");
pinMode(DPIN_PWR, OUTPUT);
digitalWrite(DPIN_PWR, LOW);
digitalWrite(DPIN_DRV_MODE, LOW);
digitalWrite(DPIN_DRV_EN, LOW);
break;
case MODE_LOW:
Serial.println("Mode = low");
pinMode(DPIN_PWR, OUTPUT);
digitalWrite(DPIN_PWR, HIGH);
digitalWrite(DPIN_DRV_MODE, LOW);
analogWrite(DPIN_DRV_EN, 255);
break;
case MODE_HIGH:
Serial.println("Mode = high");
pinMode(DPIN_PWR, OUTPUT);
digitalWrite(DPIN_PWR, HIGH);
digitalWrite(DPIN_DRV_MODE, HIGH);
analogWrite(DPIN_DRV_EN, 255);
break;
case MODE_KNOBBING:
Serial.println("Mode = knobbing");
pinMode(DPIN_PWR, OUTPUT);
digitalWrite(DPIN_PWR, HIGH);
lastKnobAngle = readAccelAngleXZ();
knob = (mode==MODE_OFF) ? 0 : 255;
break;
case MODE_KNOBBED:
Serial.println("Mode = knobbed");
break;
case MODE_BLINKING:
case MODE_BLINKING_PREVIEW:
Serial.println("Mode = blinking");
pinMode(DPIN_PWR, OUTPUT);
digitalWrite(DPIN_PWR, HIGH);
digitalWrite(DPIN_DRV_MODE, LOW);
break;
case MODE_DAZZLING:
case MODE_DAZZLING_PREVIEW:
Serial.println("Mode = dazzling");
pinMode(DPIN_PWR, OUTPUT);
digitalWrite(DPIN_PWR, HIGH);
digitalWrite(DPIN_DRV_MODE, HIGH);
break;
}
mode = newMode;
}
// Remember button state so we can detect transitions
if (newBtnDown != btnDown)
{
btnTime = time;
btnDown = newBtnDown;
delay(50);
}
}
void readAccel(char *acc)
{
while (1)
{
Wire.beginTransmission(ACC_ADDRESS);
Wire.write(ACC_REG_XOUT);
Wire.endTransmission(false); // End, but do not stop!
Wire.requestFrom(ACC_ADDRESS, 3); // This one stops.
for (int i = 0; i < 3; i++)
{
if (!Wire.available())
continue;
acc[i] = Wire.read();
if (acc[i] & 0x40) // Indicates failed read; redo!
continue;
if (acc[i] & 0x20) // Sign-extend
acc[i] |= 0xC0;
}
break;
}
}
float readAccelAngleXZ()
{
char acc[3];
readAccel(acc);
return atan2(acc[0], acc[2]);
}