-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGLYPH.c
More file actions
452 lines (366 loc) · 11.7 KB
/
GLYPH.c
File metadata and controls
452 lines (366 loc) · 11.7 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
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
#include "GLYPH.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include "ASCII.h"
volatile uint32_t _millis = 0;
const __memx Tune *current_tune;
uint8_t current_note;
bool playing = FALSE;
uint8_t rngSEED = 5;
uint8_t rng( void )
{
rngSEED = (rngSEED*rngA +rngC) % rngM;
return rngSEED;
}
void delay_ms( uint32_t ms )
{
ms += _millis;
if (ms<_millis)
{
while(_millis > 0);
while(_millis < ms);
}
else
while(_millis < ms);
}
void initialise( void )
{
/* Configure Outputs */
//DDRA
DDRB = (1<<CS) | (1<<DC) | (1<<RST) | (1<<SCK);
PORTC = 0xFF; // Set pull-up resistors
DDRD = (1 << SNDA) | (1 << SNDB) | (1<<MOSI) | (1 << LED_1_PIN) | (1 << LED_2_PIN);
PORTD = 0x00;
/* Initialise "millis" Timer */
/* TCCR0A
7 6 5 4 3 2 1 0
.--------.--------.--------.--------.--------.--------.--------.--------.
| COM0A1 | COM0A0 | COM0B1 | COM0B0 | | | WGM01 | WGM00 |
'--------'--------'--------'--------'--------'--------'--------'--------' */
TCCR0A = 0b00000010; // OC0A disconnected, CTC Mode.
/* TCCR0B
7 6 5 4 3 2 1 0
.--------.--------.--------.--------.--------.--------------------------.
| FOC0A | FOC0B | | | WGM02 | CS0[2:0] |
'--------'--------'--------'--------'--------'--------------------------' */
//TCCR0B = 0b00000100; // 1/256 CLK Prescale.
TCCR0B = 0b00000011;
// F_CPU Prescale Timer frequency (1 ms)
//OCR0A = 125; // Set compare value (8000000Hz / 64) / 1000Hz
OCR0A = 250; // Set compare value (16000000Hz / 64) / 1000Hz
//OCR0A = 78; // Set compare value (20000000Hz / 256) / 1000Hz = 78.125
/* TIMSK0
7 6 5 4 3 2 1 0
.--------.--------.--------.--------.--------.--------.--------.--------.
| | | | | | OCIEB | OCIEA | TOIE |
'--------'--------'--------'--------'--------'--------'--------'--------' */
TIMSK0 = 0b00000010 ; // Enable OCR0A Compare Interrupt
/* Configure sound timers */
//TODO: Use Proper bit names
TCCR1A = 0b01000001; // phase correct pwm mode
TCCR1B = 0b00010010; // 1/8 Prescale
OCR1A = 0;
TCCR3A = 0b00000000;
TCCR3B = 0b00001101; // CTC Mode, 1/1024 prescale
TIMSK3 = 0x02;
OCR3A = 0;
/* Configure LED PWM Timers */
TCCR2A = 0b11110011;
TCCR2B = 0b00000111;
OCR2A = 255;
OCR2B = 255;
/* Configure ADC */
/* ADMUX
7 6 5 4 3 2 1 0
.--------.--------.--------.--------.--------.--------.--------.--------.
| REFS1 | REFS0 | ADLAR | MUX4 | MUX3 | MUX2 | MUX1 | MUX0 |
'--------'--------'--------'--------'--------'--------'--------'--------' */
ADMUX = 0b00000111; // BATMON connected to PA7
/* ADCSRA
7 6 5 4 3 2 1 0
.--------.--------.--------.--------.--------.--------.--------.--------.
| ADEN | ADSC | ADATE | ADIF | ADIE | ADPS2 | ADPS1 | ADPS0 |
'--------'--------'--------'--------'--------'--------'--------'--------' */
ADCSRA = 0b11100111; // Enable ADC; Start in free-running mode; Set Prescale to 128
/* Configure Harware SPI
USCZ01 = UDORD0 = 0 (MSBFIRST)
UCSZ00 = UCPHA0 = 0 (SPI MODE 0)
UCPOL0 = 0 (SPI MODE 0) */
UBRR0 = 0; // MAXIMUM BAUD RATE
UCSR0C = (1<<UMSEL01) | (1<<UMSEL00) | (0<<UCSZ01) | (0<<UCSZ00) | (0<<UCPOL0);
UCSR0B = (0<<RXEN0) | (1<<TXEN0);
UBRR0 = 0; // MAXIMUM BAUD RATE
sei(); // Enable interrupts
set_LED_brightness(BOTH, 200);
play_tune(&STARTUP_CHIME);
/* Setup Display */
initialise_oled();
draw_image(&LOGO, 16, 2);
draw();
for(uint8_t i=0 ; i<5 ; i++)
{
delay_ms(SPLASH_DELAY/5);
set_LED_brightness(BOTH, 200-50*i);
}
/* Get Battery Voltage */
draw_battery();
draw();
delay_ms(BATTERY_DELAY);
}
ISR(TIMER0_COMPA_vect)
{
_millis += 1;
}
ISR(TIMER3_COMPA_vect)
{
if (playing)
{
if (current_note < current_tune->length)
{
uint8_t note_index = current_tune->score[current_note] & 0x0f;
uint8_t beat_index = current_tune->score[current_note] >> 4;
OCR1A = NOTES[note_index];
TCNT3 = 0;
OCR3A = BEATS[beat_index] * BEAT_ATOM;
current_note += 1;
return;
}
else
{
playing = FALSE;
}
}
OCR3A = 0;
OCR1A = 0;
}
void note(uint8_t note, uint16_t duration)
{
if (!playing)
{
OCR1A = NOTES[note];
TCNT3 = 0;
OCR3A = duration * NOTE_DURATION_MULTIPLIER;
}
}
void play_tune(const __memx Tune *t)
{
if (!playing)
{
current_note = 0;
current_tune = t;
playing = TRUE;
TCNT3 = 0;
OCR3A = 1;
}
}
void stop_tune()
{
if (playing)
{
OCR3A = 0;
OCR1A = 0;
playing = FALSE;
}
}
uint32_t millis( void )
{
return _millis;
}
/* OLED Functions */
void shift_out_byte(uint8_t b)
{
while( !( UCSR0A & (1<<UDRE0) ) );
UDR0 = b;
}
/* Initiasation for SSD1306 OLED Controller */
void initialise_oled(void)
{
PORTB &= ~(1 << CS); // LOW (Enabled)
PORTB &= ~(1 << DC); // LOW (Command Mode)
PORTB |= 1 << RST; // HIGH
delay_ms(10);
PORTB &= ~(1 << RST); // LOW
delay_ms(10);
PORTB |= 1 << RST; // HIGH
delay_ms(10);
shift_out_byte(0xAE); // DISPLAYOFF
shift_out_byte(0xD5); // SETDISPLAYCLOCKDIV
shift_out_byte(0x80); // the suggested ratio 0x80
shift_out_byte(0xA8 ); // SSD1306_SETMULTIPLEX
shift_out_byte(SCREEN_HEIGHT - 1);
shift_out_byte(0xD3 ); // SETDISPLAYOFFSET
shift_out_byte(0x0); // no offset
shift_out_byte(0x40 | 0x0); // SETSTARTLINE line #0
shift_out_byte(0x8D); // CHARGEPUMP
shift_out_byte(0x14); // Not External Vcc
shift_out_byte(0x20 ); // MEMORYMODE
shift_out_byte(0x00); // 0x0 act like ks0108
shift_out_byte(0xA0 | 0x1); // SEGREMAP
shift_out_byte(0xC8 ); // COMSCANDEC
shift_out_byte(0xDA); // SETCOMPINS
shift_out_byte(0x12);
shift_out_byte(0x81 ); // SETCONTRAST
shift_out_byte(0xCF); // Not External Vcc
shift_out_byte(0xD9 ); // SETPRECHARGE
shift_out_byte(0xF1); // Not External Vcc
shift_out_byte(0xDB); // SETVCOMDETECT
shift_out_byte(0x40);
shift_out_byte(0xA4 ); // DISPLAYALLON_RESUME
shift_out_byte(0xA6 ); // NORMALDISPLAY
//shift_out_byte(0x2E ); // DEACTIVATE_SCROLL
shift_out_byte(0xAF); // DISPLAYON
shift_out_byte(0xB0 + 0); // PAGEADDR (0 = reset)
shift_out_byte(0 & 0x0F); // Column start address (0 = reset)
shift_out_byte(0x10 | (0 >> 4)); // LOW COL ADDR
PORTB |= 1 << CS; // HIGH (Disabled)
PORTB |= 1 << DC; // DATA
delay_ms(1);
PORTB &= ~(1 << CS); // LOW (Enabled)
}
void display_mode(mode_t mode)
{
PORTB &= ~(1 << DC); // LOW (Command Mode)
if (mode == INVERTED)
shift_out_byte(0xA7 );
else
shift_out_byte(0xA6 );
delay_ms(1);
PORTB |= 1 << DC; // DATA
}
void clear_buffer(void)
{
for (uint16_t i=0 ; i<SCREEN_WIDTH*SCREEN_ROWS ; i++)
buffer[i] = 0x00;
}
void draw( void )
{
for (uint16_t i=0 ; i<SCREEN_WIDTH*SCREEN_ROWS ; i++)
shift_out_byte(buffer[i]);
}
void draw_pixel(int16_t x, int16_t y)
{
buffer[ (y>>3) * SCREEN_WIDTH + x ] |= 1 << (y&7);
}
void draw_tile(const uint8_t __flash *tile, const uint8_t __flash *mask, int16_t x, int16_t y, bool flipped)
{
/* is the tile actually visible */
if (x < -7 || x >= SCREEN_WIDTH || y < -7 || y >= SCREEN_HEIGHT)
return;
int16_t y_ = y;
if (y < 0)
y_ = 0-y;
int16_t tile_start = ((y_ >> 3) * SCREEN_WIDTH) + x;
uint8_t y_offset_a = y & 7; // y % 8
uint8_t y_offset_b = 8-y_offset_a;
uint8_t tile_index = 0;
int8_t tile_dir = 1;
uint8_t tile_width = 8;
if (x < 0)
{
tile_start -= x;
tile_index = 0-x;
tile_width -= tile_index;
}
if (flipped)
{
tile_index = 7-tile_index;
tile_dir = -1;
}
if (x > SCREEN_WIDTH-8)
{
tile_width = SCREEN_WIDTH-x;
}
if (y < 0)
{
y_offset_a = 8;
y_offset_b = 0-y;
tile_start -= SCREEN_WIDTH;
}
if (y > SCREEN_HEIGHT-8)
{
y_offset_b = 8;
}
for(uint8_t tile_offset=0 ; tile_offset<tile_width ; tile_offset++, tile_index+=tile_dir)
{
if (y_offset_a < 8)
{
buffer[tile_start+tile_offset] &= (mask[tile_index] << y_offset_a) | ~(0xff << y_offset_a);
buffer[tile_start+tile_offset] |= tile[tile_index] << y_offset_a;
}
if (y_offset_b < 8)
{
buffer[tile_start+SCREEN_WIDTH+tile_offset] &= (mask[tile_index] >> y_offset_b) | ~(0xff >> y_offset_b);
buffer[tile_start+SCREEN_WIDTH+tile_offset] |= tile[tile_index] >> y_offset_b;
}
}
}
void draw_image(const __flash Image *img, int16_t x, int16_t y)
{
for(uint8_t img_y=0 ; img_y<(img->height>>3) ; img_y++)
for(uint8_t img_x=0 ; img_x<img->width ; img_x++)
buffer[(img_y+y)*SCREEN_WIDTH + (img_x+x)] = img->data[img_y*img->width + img_x];
}
void draw_string(const __memx char *string, int16_t x, int16_t y)
{
for(uint8_t i=0 ; string[i] != '\0' ; i++)
{
draw_tile(&ASCII[(string[i]-32)*8], &BLOCK_MASKS[OPAQUE], x+(i*8), y, FALSE);
}
}
void draw_int(int16_t n, uint8_t width, int16_t x, int16_t y)
{
//TODO: Negative numbers
int32_t n_;
for (uint8_t i=0 ; i<width ; i++)
{
n_ = (6554*(int32_t)n)>>16;
draw_tile(&DIGITS[(n - (n_*10))*8], &BLOCK_MASKS[OPAQUE], x+((width-i-1)*8), y, FALSE);
n = (int16_t)n_;
}
}
/* Sound Functions */
void click( void )
{
note(_C5, 15);
}
/* Battery */
// 3.6v = ~550
// 3.76v = ~585
// 3.90v = ~612
// 4.17v = ~647 (charging)
void draw_battery()
{
//ADCSRA |= (1 << ADSC); // Start Conversion
//while(ADCSRA & (1<<ADSC)); // Wait for conversion
uint8_t l = ADCL;
uint8_t h = ADCH;
uint16_t voltage = (h << 8) | l;
//uint8_t* glyph;
if (voltage >= 645)
draw_tile(&BATTERY_GLYPHS[BAT_CHG], &BLOCK_MASKS[OPAQUE], 15*8, 0, FALSE);
else if (voltage >= 610)
draw_tile(&BATTERY_GLYPHS[BAT_FUL], &BLOCK_MASKS[OPAQUE], 15*8, 0, FALSE);
else if (voltage >= 570)
draw_tile(&BATTERY_GLYPHS[BAT_FUL-8], &BLOCK_MASKS[OPAQUE], 15*8, 0, FALSE);
else if (voltage >= 540)
draw_tile(&BATTERY_GLYPHS[BAT_FUL-16], &BLOCK_MASKS[OPAQUE], 15*8, 0, FALSE);
else if (voltage >= 510)
draw_tile(&BATTERY_GLYPHS[BAT_EMT+8], &BLOCK_MASKS[OPAQUE], 15*8, 0, FALSE);
else if (voltage < 510)
draw_tile(&BATTERY_GLYPHS[BAT_EMT], &BLOCK_MASKS[OPAQUE], 15*8, 0, FALSE);
else //TODO: map values
draw_int(voltage, 3, 13*8, 0);
}
/* LEDs */
void set_LED_brightness(LED led, uint8_t value)
{
if (led == LEFT)
OCR2B = 255-value;
else if (led == RIGHT)
OCR2A = 255-value;
else
{
OCR2A = 255-value;
OCR2B = 255-value;
}
}