-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMeggyJr.cpp
More file actions
441 lines (351 loc) · 9.85 KB
/
Copy pathMeggyJr.cpp
File metadata and controls
441 lines (351 loc) · 9.85 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
/*
MeggyJr.cpp - Meggy Jr RGB library for Arduino
Version 1.4 - 4/17/2010
Copyright (c) 2010 Windell H. Oskay. All right reserved.
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library. If not, see <http://www.gnu.org/licenses/>.
Thanks to Arthur J. Dahm III and Jay Clegg for code written for the Peggy 2.0,
which was adapted to make this library.
*/
/******************************************************************************
* Includes
******************************************************************************/
extern "C" {
#include <inttypes.h>
#include <avr/interrupt.h>
#include <avr/io.h>
//#include <wiring.h>
}
#include "MeggyJr.h"
byte MeggyJr::MeggyFrame[DISP_BUFFER_SIZE];
byte MeggyJr::AuxLEDs;
byte MeggyJr::SoundAllowed;
byte MeggyJr::SoundEnabled;
byte MeggyJr::currentCol;
byte MeggyJr::currentBrightness;
byte* MeggyJr::currentColPtr;
//unsigned long MeggyJr::SoundStopTime;
unsigned int MeggyJr::ToneTimeRemaining;
#ifdef timingtest
unsigned int MeggyJr::testTime;
#endif
/******************************************************************************
* Constructor
******************************************************************************/
MeggyJr::MeggyJr(void)
{
// Initialization routine for Meggy Jr RGB hardware
AuxLEDs = 0;
currentColPtr = MeggyFrame;
currentCol=0;
currentBrightness=0;
PORTC = 255U; // Pull-ups on Port C (for detecting button presses)
DDRC = 0; //All inputs
DDRD = 254U; // All D Output except for Rx
PORTD = 254U;
DDRB = 63U;
PORTB = 255;
//If a button is pressed at startup, turn sound off.
if ((PINC & 63) != 63)
{
SoundAllowed = 0;
}
else
{
SoundAllowed = 1;
}
SoundEnabled = 0;
MeggyJr::ToneTimeRemaining = 0;
//Turn display off:
PORTD |= 252U;
PORTB |= 17U;
ClearMeggy();
SPSR = (1<<SPI2X);
/*
//ENABLE SPI, MASTER, CLOCK RATE fck/4:
SPCR = 80;// i.e., (1 << SPE) | ( 1 << MSTR );
SPDR = 0;
while (!(SPSR & (1<<SPIF))) { } // wait for last bitshift to complete
SPDR = 0;
while (!(SPSR & (1<<SPIF))) { } // wait for last bitshift to complete
SPDR = 0;
while (!(SPSR & (1<<SPIF))) { } // wait for last bitshift to complete
SPDR = 0;
while (!(SPSR & (1<<SPIF))) { } // wait for last bitshift to complete
PORTB |= 4; //Latch Pulse
PORTB &= 251;
*/
SPCR = 0; //turn off spi
// setup the interrupt.
TCCR2A = (1<<WGM21); // clear timer on compare match
TCCR2B = (1<<CS21); // timer uses main system clock with 1/8 prescale
OCR2A = (F_CPU >> 3) / 8 / 15 / FPS; // Frames per second * 15 passes for brightness * 8 rows
TIMSK2 = (1<<OCIE2A); // call interrupt on output compare match
sei( ); // Enable interrupts
#ifdef timingtest
MeggyJr::testTime = 0;
#endif
}
/******************************************************************************
* User API
******************************************************************************/
// Painfully Slow! Don't use this, if you can avoid it:
void MeggyJr::ClearMeggy (void)
{
for (byte i = 0; i < DISP_BUFFER_SIZE; i++)
{
MeggyFrame[i]= 0;
}
}
//Set Pixel Color: use an RGB array to specify the color.
// Very convenient for using color look-up tables.
void MeggyJr::SetPxClr(byte x, byte y, byte *rgb)
{
byte PixelPtr = 24*x + y;
MeggyFrame[PixelPtr] = rgb[2];
PixelPtr += 8;
MeggyFrame[PixelPtr] = rgb[1];
PixelPtr += 8;
MeggyFrame[PixelPtr] = rgb[0];
}
/*
void MeggyJr::SoundCheck(void)
{
// Obsolete with current version of library; sounds stop automatically.
// If your program contains "SoundCheck();" somewhere, please remove it.
}
*/
// Begin sound
void MeggyJr::StartTone(unsigned int Tone, unsigned int duration)
{
OCR1A = Tone;
SoundState(1);
MeggyJr::ToneTimeRemaining = duration;
}
byte MeggyJr::GetPixelR(byte x, byte y)
{
return MeggyFrame[24*x + y + 16];
}
byte MeggyJr::GetPixelG(byte x, byte y)
{
return MeggyFrame[24*x + y + 8];
}
byte MeggyJr::GetPixelB(byte x, byte y)
{
return MeggyFrame[24*x + y];
}
// Clear a single pixel. Not much better than writing "dark" to the pixel.
void MeggyJr::ClearPixel(byte x, byte y)
{
byte PixelPtr = 24*x + y;
MeggyFrame[PixelPtr] = 0;
PixelPtr += 8;
MeggyFrame[PixelPtr] = 0;
PixelPtr += 8;
MeggyFrame[PixelPtr] = 0;
}
// GetButtons returns a byte with a bit set for
// each of the buttons that is currently pressed.
byte MeggyJr::GetButtons(void)
{
return (~(PINC) & 63U);
}
// Set sound ON or OFF by calling
// SoundState(0) or SoundState(1).
void MeggyJr::SoundState(byte t)
{
if ((t) && (SoundAllowed))
{
TCCR1A = 65; // 0100 0001
//COM1A10 = 01 : Toggle output OC1A on compare match.
//WGM11,10: 01
TCCR1B = 17; // 0001 0001 // CS12..10 = 001: No clock prescaling.
// WGM13,12: 10
// CS12,CS11,CS10: 001. Count at CLKI/O/1 (no prescaling)
//WGM: 1001, phase+frequency correct PWM
// with top at OCR1A and update OCR1A at bottom.
SoundEnabled = 1;
DDRB |= 2;
}
else
{
SoundEnabled = 0;
TCCR1A = 0;
if (t)
TCCR1B = 128; // Harmless; can use (TCCR1B == 0) to check if sound is done.
else
TCCR1B = 0;
DDRB &= 253;
PORTB |= 2;
}
}
SIGNAL(TIMER2_COMPA_vect)
{
// there are 15 passes through this interrupt for each row per frame.
// ( 15 * 8) = 120 times per frame.
// during those 15 passes, a led can be on or off.
// if it is off the entire time, the perceived brightness is 0/15
// if it is on the entire time, the perceived brightness is 15/15
// giving a total of 16 average brightness levels from fully off to fully on.
// currentBrightness is a comparison variable, used to determine if a certain
// pixel is on or off during one of those 15 cycles.
//
// At 120 Hz refresh, this executes 15*8*120 = 14,400 times per second.
// Full routine takes about 340 cycles, 21 us.
//
// 14400*340/16000000 = ~0.3; so about 1/3 of the processor time is spent in
// this interrupt routine.
#ifdef timingtest
unsigned int soundTemp = OCR1A;
OCR1A = 65500;
TCNT1 = 0;
#endif
if (++MeggyJr::currentBrightness >= MAX_BRIGHTNESS)
{
MeggyJr::currentBrightness = 0;
if (++MeggyJr::currentCol > 7)
{
MeggyJr::currentCol = 0;
MeggyJr::currentColPtr = MeggyJr::MeggyFrame;
}
else
MeggyJr::currentColPtr += 24;
if (MeggyJr::ToneTimeRemaining > 0)
{
if (--MeggyJr::ToneTimeRemaining == 0)
{
TCCR1A = 0;
DDRB &= 253;
PORTB |= 2;
TCCR1B = 0; // High bit flags that the sound is done.
}
}
}
//////////////////// Parse a row of data and write out the bits via spi
byte *ptr = MeggyJr::currentColPtr + 23; // it is more convenient to work from right to left
byte p;
byte cb = MeggyJr::currentBrightness;
// Optimization: interleave waiting for SPI with other code, so the CPU can do something useful
// when waiting for each SPI transmission to complete
//Turn display off:
PORTD |= 252U;
PORTB |= 17U;
SPCR = 80;// i.e., (1 << SPE) | ( 1 << MSTR );
// First SPI word: Aux LED drive. Zero, except once per full screen redraw.
if ((cb + MeggyJr::currentCol ) == 0)
SPDR = MeggyJr::AuxLEDs;
else
SPDR = 0;
byte bits=0;
p = *ptr--;
if (p > cb)
bits |= 128;
p = *ptr--;
if (p > cb)
bits |= 64;
p = *ptr--;
if (p > cb)
bits |= 32;
p = *ptr--;
if (p > cb)
bits |= 16;
p = *ptr--;
if (p > cb)
bits |= 8;
p = *ptr--;
if (p > cb)
bits |= 4;
p = *ptr--;
if (p > cb)
bits |= 2;
p = *ptr--;
if (p > cb)
bits |= 1;
// while (!(SPSR & (1<<SPIF))) { } // wait for prior bitshift to complete
SPDR = bits;
bits=0;
p = *ptr--;
if (p > cb)
bits |= 128;
p = *ptr--;
if (p > cb)
bits |= 64;
p = *ptr--;
if (p > cb)
bits |= 32;
p = *ptr--;
if (p > cb)
bits |= 16;
p = *ptr--;
if (p > cb)
bits |= 8;
p = *ptr--;
if (p > cb)
bits |= 4;
p = *ptr--;
if (p > cb)
bits |= 2;
p = *ptr--;
if (p > cb)
bits |= 1;
// while (!(SPSR & (1<<SPIF))) { } // wait for prior bitshift to complete
SPDR = bits;
bits=0;
p = *ptr--;
if (p > cb)
bits |= 128;
p = *ptr--;
if (p > cb)
bits |= 64;
p = *ptr--;
if (p > cb)
bits |= 32;
p = *ptr--;
if (p > cb)
bits |= 16;
p = *ptr--;
if (p > cb)
bits |= 8;
p = *ptr--;
if (p > cb)
bits |= 4;
p = *ptr--;
if (p > cb)
bits |= 2;
p = *ptr--;
if (p > cb)
bits |= 1;
//asm("nop"); // short delay
while (!(SPSR & (1<<SPIF))) { } // wait for prior bitshift to complete
SPDR = bits;
//////////////////// Now set the row and latch the bits
byte portbTemp = 0;
byte portdTemp = 0;
if (MeggyJr::currentCol == 0)
portbTemp = 239U;
else if (MeggyJr::currentCol == 1)
portbTemp = 254U;
else
portdTemp = ~(1 << (9 - MeggyJr::currentCol));
while (!(SPSR & (1<<SPIF))) { } // wait for last bitshift to complete
PORTB |= 4; //Latch Pulse
if (MeggyJr::currentCol > 1)
PORTD &= portdTemp;
else
PORTB &= portbTemp;
PORTB &= 251; //End Latch Pulse
SPCR = 0; //turn off spi
#ifdef timingtest
if (TCNT1 > MeggyJr::testTime)
MeggyJr::testTime = TCNT1;
OCR1A = soundTemp;
#endif
}