-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathglassesfunctions.h
455 lines (367 loc) · 11.2 KB
/
glassesfunctions.h
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
// Glasses-specific functions, may not work with other applications
typedef void (*functionList)(); // definition for list of effect function pointers
extern const byte numEffects;
// Buffers for graphics generation
byte GlassesBits[24][2] = {{0}}; // 24 column x 8 row bit arrays (on/off frame)
byte GlassesPWM[24][9] = {}; // 24 column x 8 row byte array (PWM frame)
// Read and smooth light sensor input
#define brightnessSmoothFactor 0.99
float smoothedBrightness = 0;
void readBrightness() {
smoothedBrightness = smoothedBrightness * brightnessSmoothFactor + analogRead(3) * (1.0 - brightnessSmoothFactor);
}
// Send contents of bit frame to correct AS1130 registers
// Rough, needs to be streamlined
void writeBitFrame(byte frame, byte bitbuffer) {
Wire.beginTransmission(0x37);
Wire.write(0xfd);
Wire.write(frame + memoryONOFFSTART);
Wire.endTransmission();
byte tempBits = 0;
Wire.beginTransmission(0x37);
Wire.write(0x00);
for (byte i = 0; i < 12; i++) {
tempBits = GlassesBits[i][bitbuffer];
Wire.write(tempBits << 2);
Wire.write(tempBits >> 6);
}
Wire.endTransmission();
Wire.beginTransmission(0x30);
Wire.write(0xfd);
Wire.write(frame + memoryONOFFSTART);
Wire.endTransmission();
Wire.beginTransmission(0x30);
Wire.write(0x00);
for (byte i = 0; i < 12; i++) {
tempBits = GlassesBits[i+12][bitbuffer];
Wire.write(tempBits << 2);
Wire.write(tempBits >> 6);
}
Wire.endTransmission();
}
// Send contents of PWM frame to correct AS1130 registers
// Rough, needs to be streamlined
void writePWMFrame(byte frame) {
Wire.beginTransmission(0x37);
Wire.write(0xfd);
Wire.write(frame + memoryBLINKPWMSTART);
Wire.endTransmission();
for (int x = 0; x < 12; x++) {
Wire.beginTransmission(0x37);
Wire.write(26+x*11);
for (int y = 0; y < 8; y++) {
Wire.write(GlassesPWM[x][y]);
}
Wire.endTransmission();
}
Wire.beginTransmission(0x30);
Wire.write(0xfd);
Wire.write(frame + memoryBLINKPWMSTART);
Wire.endTransmission();
for (int x = 0; x < 12; x++) {
Wire.beginTransmission(0x30);
Wire.write(26+x*11);
for (int y = 0; y < 8; y++) {
Wire.write(GlassesPWM[x+12][y]);
}
Wire.endTransmission();
}
}
// Send contents of bit frame to blink AS1130 registers
// Rough, needs to be streamlined
// Mostly used to clear blink frames, since blink is useless
void writeBlinkFrame(byte frame, byte bitbuffer) {
Wire.beginTransmission(0x37);
Wire.write(0xfd);
Wire.write(frame + memoryBLINKPWMSTART);
Wire.endTransmission();
Wire.beginTransmission(0x37);
Wire.write(0x00);
for (byte i = 0; i < 12; i++) {
Wire.write(GlassesBits[i][bitbuffer] << 2);
Wire.write(GlassesBits[i][bitbuffer] >> 6);
}
Wire.endTransmission();
Wire.beginTransmission(0x30);
Wire.write(0xfd);
Wire.write(frame + memoryBLINKPWMSTART);
Wire.endTransmission();
Wire.beginTransmission(0x30);
Wire.write(0x00);
for (byte i = 0; i < 12; i++) {
Wire.write(GlassesBits[i+12][bitbuffer] << 2);
Wire.write(GlassesBits[i+12][bitbuffer] >> 6);
}
Wire.endTransmission();
}
// Set PWM frame all to one value
// Usually used to clear or fill frame
// Bit and PWM frames interact, if you want to do bit graphics you must fill PWM frame
void fillPWMFrame(byte frame, byte value) {
for (int x = 0; x < 24; x++) {
for (int y = 0; y < 8; y++) {
GlassesPWM[x][y] = value;
}
}
}
// Set PWM frame all to one value
// Usually used to clear or fill frame
// Bit and PWM frames interact, if you want to do PWM graphics you must fill bit on/off frame
void fillBitFrame(byte frame, byte value) {
for (int x = 0; x < 24; x++) {
GlassesBits[x][0] = 255*(value > 0);
GlassesBits[x][1] = 255*(value > 0);
}
}
// Fill blink frame with known value
// Usually zero because no one actually wants to use blink
void fillBlinkFrame(byte frame, byte value) {
for (int x = 0; x < 24; x++) {
GlassesBits[x][0] = 255*(value > 0);
GlassesBits[x][1] = 255*(value > 0);
}
writeBlinkFrame(frame, 0);
}
// Configure AS1130 chips to ideal startup settings
void glassesInit() {
byte configOptions = 0;
// reset chip again
configOptions = (0 << AS1130_test_all) |
(0 << AS1130_auto_test) |
(0 << AS1130_manual_test) |
(0 << AS1130_init) |
(0 << AS1130_shdn);
setShutdownTest(glassesRight, configOptions);
setShutdownTest(glassesLeft, configOptions);
delay(5);
configOptions = (1 << AS1130_test_all) |
(1 << AS1130_auto_test) |
(0 << AS1130_manual_test) |
(1 << AS1130_init) |
(1 << AS1130_shdn);
setShutdownTest(glassesRight, configOptions);
setShutdownTest(glassesLeft, configOptions);
delay(5);
configOptions = (0 << AS1130_low_vdd_rst) |
(0 << AS1130_low_vdd_stat) |
(1 << AS1130_led_error_correction) |
(0 << AS1130_dot_corr) |
(0 << AS1130_common_addr);
setConfigs(glassesRight, configOptions, 1);
setConfigs(glassesLeft, configOptions, 1);
setMovie(glassesRight, 0, 0, 0, 0);
setMovie(glassesLeft, 0, 0, 0, 0);
setMovieOptions(glassesRight, 0, 0, 0, 0);
setMovieOptions(glassesLeft, 0, 0, 0, 0);
setMovieLooping(glassesRight, 1);
setMovieLooping(glassesLeft, 1);
setBrightness(glassesRight, startbrightness);
setBrightness(glassesLeft, startbrightness);
configOptions = (0 << AS1130_selected_pic) |
(0 << AS1130_watchdog) |
(0 << AS1130_por) |
(0 << AS1130_overtemp) |
(0 << AS1130_low_vdd) |
(0 << AS1130_open_err) |
(0 << AS1130_short_err) |
(0 << AS1130_movie_fin);
setInterruptMask(glassesRight, configOptions);
setInterruptMask(glassesLeft, configOptions);
setInterruptFrame(glassesRight, 0);
setInterruptFrame(glassesLeft, 0);
setI2CWatchdog(glassesRight, 64, 1);
setI2CWatchdog(glassesLeft, 64, 1);
setClockSync(glassesRight, AS1130_clock_speed_1MHz, AS1130_sync_IN);
setClockSync(glassesLeft, AS1130_clock_speed_1MHz, AS1130_sync_OUT);
fillPWMFrame(0, 255);
writePWMFrame(0);
fillBitFrame(0, 0);
writeBitFrame(0, 0);
fillBlinkFrame(0, 0);
writeBlinkFrame(0, 0);
setFrame(glassesRight, 0, 1);
setFrame(glassesLeft, 0, 1);
}
// When switching from on/off to PWM or vice versa
// Need to blank or fill respectively to correct the masks
void switchDrawType(byte frame, byte enablePWM) {
if (enablePWM == 0) {
fillBitFrame(frame,0); writeBitFrame(frame, 0);
fillPWMFrame(frame,255); writePWMFrame(frame);
} else if (enablePWM == 1) {
fillPWMFrame(frame,0); writePWMFrame(frame);
fillBitFrame(frame,1); writeBitFrame(frame, 0);
}
}
// Copy contents of PWM array one LED over
// Possible to do in hardware at some future date
void scrollPWM(byte dir) {
if (dir == 0) {
for (int i = 1; i < 24; i++) {
for (int j = 0; j < 8; j++) {
GlassesPWM[24 - i][j] = GlassesPWM[24-i-1][j];
}
}
}
else if (dir == 1) {
for (int i = 1; i < 24; i++) {
for (int j = 0; j < 8; j++) {
GlassesPWM[i-1][j] = GlassesPWM[i][j];
}
}
}
}
// Copy contents of bit array one LED over
// Possible to do in hardware at some future date
void scrollBits(byte dir, byte bitbuffer) {
if (dir == 0) {
for (int i = 1; i < 24; i++) {
GlassesBits[24 - i][bitbuffer] = GlassesBits[24-i-1][bitbuffer];
}
}
else if (dir == 1) {
for (int i = 1; i < 24; i++) {
GlassesBits[i-1][bitbuffer] = GlassesBits[i][bitbuffer];
}
}
}
// Fetch font character bitmap from flash
byte charBuffer[8] = {0};
void loadCharBuffer(byte character) {
for (int i = 0; i< 8; i++) {
charBuffer[i] = pgm_read_byte(Font[character]+i);
}
}
// Determine flash address of text string
unsigned int currentStringAddress = 0;
void selectFlashString(byte string) {
currentStringAddress = pgm_read_word(&stringArray[string]);
}
// Fetch a character value from a text string in flash
char loadStringChar(byte string, int character) {
return (char) pgm_read_byte(currentStringAddress + character);
}
byte getCIE (byte value) {
return pgm_read_byte(&cie[value]);
}
// Cycle through several brightness settings
byte brightness = startbrightness;
void cycleBrightness() {
if (brightness > 204) {
brightness = 51;
} else {
brightness += 51;
}
byte newBrightness = getCIE(brightness);
setBrightness(glassesRight, newBrightness);
setBrightness(glassesLeft, newBrightness);
}
byte qsine(int angle) {
int cangle = abs(angle)%359;
if (cangle < 90) {
return pgm_read_byte(&qsinetable[cangle]);
} else if (cangle < 180) {
return 255-pgm_read_byte(&qsinetable[179-cangle]);
} else if (cangle < 270) {
return 255-pgm_read_byte(&qsinetable[cangle-180]);
} else {
return pgm_read_byte(&qsinetable[359-cangle]);
}
}
// Anti-aliased line algorithm
// Adapted from Michael Abrash http://www.phatcode.net/res/224/files/html/ch42/42-02.html
void wuLine(int X0, int Y0, int X1, int Y1) {
uint16_t IntensityShift, ErrorAdj, ErrorAcc;
uint16_t ErrorAccTemp, Weighting, WeightingComplementMask;
int DeltaX, DeltaY, Temp, XDir;
// make sure line runs from top to bottom
if (Y0 > Y1) {
Temp = Y0; Y0 = Y1; Y1 = Temp;
Temp = X0; X0 = X1; X1 = Temp;
}
// first pixel
GlassesPWM[X0][Y0] = 255;
if ((DeltaX = X1 - X0) >= 0) {
XDir = 1;
} else {
XDir = -1;
DeltaX = -DeltaX;
}
if ((DeltaY = Y1 - Y0) == 0) {
// horizontal line
while (DeltaX-- != 0) {
X0 += XDir;
GlassesPWM[X0][Y0] = 255;
}
return;
}
if (DeltaX == 0) {
// vertical line
do {
Y0++;
GlassesPWM[X0][Y0] = 255;
} while (--DeltaY != 0);
return;
}
if (DeltaX == DeltaY) {
// diagonal line
do {
X0 += XDir;
Y0++;
GlassesPWM[X0][Y0] = 255;
} while (--DeltaY != 0);
return;
}
// need an anti-aliased line
ErrorAcc = 0;
IntensityShift = 16 - 8;
WeightingComplementMask = 256 - 1;
if (DeltaY > DeltaX) {
// y-major line
ErrorAdj = ((unsigned long) DeltaX << 16) / (unsigned long) DeltaY;
while (--DeltaY) {
ErrorAccTemp = ErrorAcc;
ErrorAcc += ErrorAdj;
if (ErrorAcc <= ErrorAccTemp) {
X0 += XDir;
}
Y0++;
Weighting = ErrorAcc >> IntensityShift;
GlassesPWM[X0][Y0] = getCIE(255 - Weighting);
GlassesPWM[X0][Y0 + 1] = getCIE(255 - (Weighting ^ WeightingComplementMask));
}
GlassesPWM[X1][Y1] = 255;
return;
}
ErrorAdj = ((unsigned long) DeltaY << 16) / (unsigned long) DeltaX;
while (--DeltaX) {
ErrorAccTemp = ErrorAcc;
ErrorAcc += ErrorAdj;
if (ErrorAcc <= ErrorAccTemp) {
Y0++;
}
X0 += XDir;
Weighting = ErrorAcc >> IntensityShift;
GlassesPWM[X0][Y0] = getCIE(255 - Weighting);
GlassesPWM[X0][Y0 + 1] = getCIE(255 - (Weighting ^ WeightingComplementMask));
}
GlassesPWM[X1][Y1] = 255;
}
void loadGraphicsFrame(int frame) {
for (int x = 0; x < 24; x++) {
GlassesBits[x][0] = pgm_read_byte(Graphics[frame]+x);
}
}
/*
readBrightness();
brightact++;
if (brightact > 10) {
brightact = 0;
//Serial.println(smoothedBrightness);
int autoBright = smoothedBrightness;
if (autoBright < 50) autoBright = 50;
if (autoBright > 255) autoBright = 255;
setBrightness(glassesRight, autoBright);
setBrightness(glassesLeft, autoBright);
}
*/