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
|
/* (c) copyright N.C. 2011 */
// ATMEL ATMEGA8
//
// +-\/-+
// (RESET) PC6 1| |28 PC5 (ADC5/SCL)
// (RXD) PD0 2| |27 PC4 (ADC4/SDA)
// (TXD) PD1 3| |26 PC3 (ADC3)
// (INT0) PD2 4| |25 PC2 (ADC2)
// (INT1) PD3 5| |24 PC1 (ADC1)
// (XCK/T0) PD4 6| |23 PC0 (ADC0)
// VCC 7| |22 GND
// GND 8| |21 AREF
// (XTAL1/TOSC1) PB6 9| |20 AVCC
// (XTAL2/TOSC2) PB7 10| |19 PB5 (SCK)
// (T1) PD5 11| |18 PB4 (MISO)
// (AIN0) PD6 12| |17 PB3 (MOSI/OC2)
// (AIN1) PD7 13| |16 PB2 (SS/OC1B)
// (ICP1) PB0 14| |15 PB1 (OC1A)
// +----+
extern "C" {
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h> // rand
#include "main.h"
#include "effect.h"
#include "launch_effect.h"
//#include "draw.h"
}
//int myrand() { return rand(); }
#define CUBE_SIZE 8
//#define AXIS_X 1
//#define AXIS_Y 2
//#define AXIS_Z 3
volatile unsigned char cube[8][8];
//volatile unsigned char current_layer = 0;
extern volatile unsigned char current_layer;
volatile bool in_wait = false;
volatile unsigned char fb[CUBE_SIZE][CUBE_SIZE];
/*****************************************************************************
* TIME MANAGEMENT
*****************************************************************************/
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
// the prescaler is set so that timer0 ticks every 64 clock cycles, and the
// the overflow handler is called every 256 ticks.
#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
// the whole number of milliseconds per timer0 overflow
#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
// the fractional number of milliseconds per timer0 overflow. we shift right
// by three to fit these numbers into a byte. (for the clock speeds we care
// about - 8 and 16 MHz - this doesn't lose precision.)
#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
#define FRACT_MAX (1000 >> 3)
//volatile uint32_t timer0_overflow_count = 0;
volatile uint32_t timer0_millis = 0;
//static uint8_t timer0_fract = 0;
ISR(TIMER0_OVF_vect)
{
// copy these to local variables so they can be stored in registers
// (volatile variables must be read from memory on every access)
uint32_t m = timer0_millis;
//uint8_t f = timer0_fract;
static uint8_t timer0_fract = 0;
m += MILLIS_INC;
//f += FRACT_INC;
timer0_fract += FRACT_INC;
//if (f >= FRACT_MAX) {
if (timer0_fract >= FRACT_MAX) {
//f -= FRACT_MAX;
timer0_fract -= FRACT_MAX;
++m;
}
//timer0_fract = f;
timer0_millis = m;
//timer0_overflow_count++;
//static uint32_t last_time = 0;
//if (timer0_overflow_count & 0x1)
//if (m - last_time >= 5) {
//debounce_keys(); // called nearly each 2ms (0,002048s)
//last_time = m;
//}
}
/*
inline uint32_t millis()
{
uint32_t m;
uint8_t oldSREG = SREG;
// disable interrupts while we read timer0_millis or we might get an
// inconsistent value (e.g. in the middle of a write to timer0_millis)
cli();
m = timer0_millis;
SREG = oldSREG;
return m;
}
*/
inline uint32_t millis()
{
return timer0_millis;
}
void delay(uint32_t ms)
{
in_wait = true;
uint32_t time1 = millis();
while ((millis()) - time1 < ms);
in_wait = false;
}
//void delay_ms(uint16_t x)
//{
// in_wait = true;
// uint8_t y, z;
// for ( ; x > 0 ; x--){
// for ( y = 0 ; y < 90 ; y++){
// for ( z = 0 ; z < 6 ; z++){
// asm volatile ("nop");
// }
// }
// }
// in_wait = false;
//}
/*****************************************************************************
* ACCESSORS
*****************************************************************************/
unsigned char inrange(int x, int y, int z);
void set_led(unsigned char x, unsigned char y, unsigned char z, bool on)
{
if (!inrange(x, y, z)) {
return;
}
/*
assert(x >= 0 && x <= 7);
assert(y >= 0 && y <= 7);
assert(z >= 0 && z <= 7);
*/
if (on) {
cube[y][z] |= ((unsigned char)1) << x;
}
else {
cube[y][z] &= ~(((unsigned char)1) << x);
}
}
void clear_led()
{
for (unsigned char z = 0; z < 8; ++z) {
for (unsigned char y = 0; y < 8; ++y) {
cube[y][z] = 0;
}
}
}
/*****************************************************************************
* RENDER
*****************************************************************************/
ISR(TIMER2_COMP_vect)
{
//if (!in_wait) return;
PORTD &= ~0x20; // layer and latch low
unsigned char current_layer_ = current_layer;
for (unsigned char j = 0; j < 8; ++j) {
//for (char j = 0; j < 4; ++j) {
unsigned char val = cube[j][current_layer_];
PORTD &= ~0x10;
PORTC = val;
PORTD = (PORTD & ~0xC0) | (val & 0xC0);
PORTD |= 0x10;
}
PORTB = (PORTB & ~0x07) | current_layer_;
PORTD |= 0x20;
++current_layer_;
current_layer = current_layer_ & 0x07;
//if (current_layer_ > 7) current_layer_ = 0;
//current_layer = current_layer_;
//PORTC |= 0x28; // layer and latch high
}
void tmp2cube (void);
// Take input from a computer and load it onto the cube buffer
void rs232(void)
{
int tempval;
int x = 0;
int y = 0;
int escape = 0;
while (1)
{
// Switch state on red LED for debugging
// Should switch state every time the code
// is waiting for a byte to be received.
//LED_PORT ^= LED_RED;
// Wait until a byte has been received
while ( !(UCSRA & (1<<RXC)) );
// Load the received byte from rs232 into a buffer.
tempval = UDR;
// Uncommet this to echo data back to the computer
// for debugging purposes.
UDR = tempval;
// Every time the cube receives a 0xff byte,
// it goes into sync escape mode.
// if a 0x00 byte is then received, the x and y counters
// are reset to 0. This way the x and y counters are
// always the same on the computer and in the cube.
// To send an 0xff byte, you have to send it twice!
// Go into sync escape mode
if (tempval == 0xff)
{
// Wait for the next byte
while ( !(UCSRA & (1<<RXC)) );
// Get the next byte
tempval = UDR;
// Sync signal is received.
// Reset x and y counters to 0.
if (tempval == 0x00)
{
x = 0;
y = 0;
escape = 1;
}
// if no 0x00 byte is received, proceed with
// the byte we just received.
}
if (escape == 0)
{
// Load data into the current position in the buffer
fb[x][y] = tempval;
// Check if we have reached the limits of the buffer array.
if (y == 7)
{
if (x == 7)
{
// All data is loaded. Reset both counters
y = 0;
x = 0;
// Copy the data onto the cube.
tmp2cube();
} else
{
// A layer is loaded, reset y and increment x.
x++;
y = 0;
}
} else
{
// We are in the middle of loading a layer. increment y.
y++;
}
} else
{
escape = 0;
}
}
}
/*****************************************************************************
* MAIN
*****************************************************************************/
#define USART_BAUDRATE 115200
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
//#define BAUD_PRESCALE 51
int main()
{
/*
* =======================================================================
* Initialisation
* =======================================================================
*/
//*** init time management
TCNT0 = 0; // init timer count to 0
TCCR0 |= 0x03; // prescaler: 64
TIMSK |= 0x01; // enable timer 0 overflow interrupt
// Timer 2
// Frame buffer interrupt
// 14745600/128/11 = 10472.72 interrupts per second
// 10472.72/8 = 1309 frames per second
OCR2 = 11; // interrupt at counter = 10
TCCR2 |= (1 << CS20) | (0 << CS21) | (1 << CS22); // Prescaler = 128.
TCCR2 |= (1 << WGM21); // CTC mode. Reset counter when OCR2 is reached.
TCNT2 = 0x00; // initial counter value = 0;
TIMSK |= (1 << OCIE2); // Enable CTC interrupt
PORTD = 0;
PORTB = 0;
PORTC = 0;
DDRD = 0xff;
DDRB = 0xff;
DDRC = 0xff;
/*
* =======================================================================
* Serial port init
* =======================================================================
*/
// Initiate uart
// USART Baud rate is defined in MYUBRR
//UBRRH = BAUD_PRESCALE >> 8;
//UBRRL = BAUD_PRESCALE;
//// UCSRC - USART control register
//// bit 7-6 sync/ascyn 00 = async, 01 = sync
//// bit 5-4 parity 00 = disabled
//// bit 3 stop bits 0 = 1 bit 1 = 2 bits
//// bit 2-1 frame length 11 = 8
//// bit 0 clock polarity = 0
////UCSRC = 0b10000110;
//// Enable RS232, tx and rx
//UCSRB = (1<<RXEN)|(1<<TXEN);
//UCSRC=(1<<URSEL)|(3<<UCSZ0);
////UDR = 0x00; // send an empty byte to indicate powerup.
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)
// try again...
UBRRH = (uint8_t)(BAUD_PRESCALLER>>8);
UBRRL = (uint8_t)(BAUD_PRESCALLER);
UCSRC = (1<<URSEL)|(3<<UCSZ0);
UCSRB = (1<<RXEN)|(1<<TXEN);
#undef BAUDRATE
#undef BAUD_PRESCALLER
//*** set interupts
//sei();
/*
* =======================================================================
* MAIN LOOP
* =======================================================================
*/
rs232();
//while (1)
//{
// // Show the effects in a predefined order
// for (char i=0; i<EFFECTS_TOTAL; i++)
// launch_effect(i);
// // Show the effects in a random order.
// // Comment the two lines above and uncomment this
// // if you want the effects in a random order.
// //launch_effect(rand()%EFFECTS_TOTAL);
//}
for (;;) {
//clear_led();
//delay_ms(1000);
for (unsigned char z = 0; z < 8; ++z) {
for (unsigned char y = 0; y < 8; ++y) {
cube[y][z] = 0xFF;
}
}
//continue;
delay(5000);
clear_led();
for (char z = 0; z < 8; ++z) {
for (char y = 0; y < 8; ++y) {
for (char x = 0; x < 8; ++x) {
set_led(x, y, z, true);
//delay(5);
delay(100);
//delay(500);
//delay(1000);
//delay_ms(1000);
}
}
}
// Show the effects in a predefined order
//for (char i=0; i<EFFECTS_TOTAL; i++)
//launch_effect(i);
//sendvoxels_rand_z(20,220,2000);
//effect_rain(100);
//effect_random_filler(5,1);
//effect_z_updown(20,1000);
//effect_wormsqueeze (2, AXIS_Z, -1, 100, 1000);
//effect_blinky2();
// Show the effects in a random order.
// Comment the two lines above and uncomment this
// if you want the effects in a random order.
//launch_effect(rand()%EFFECTS_TOTAL);
//effect_boxside_randsend_parallel (AXIS_X, 0, 150, 1);
//effect_boxside_randsend_parallel (AXIS_X, 1, 150, 1);
//effect_boxside_randsend_parallel (AXIS_Y, 0, 150, 1);
//effect_boxside_randsend_parallel (AXIS_Y, 1, 150, 1);
//effect_boxside_randsend_parallel (AXIS_Z, 0, 150, 1);
//effect_boxside_randsend_parallel (AXIS_Z, 1, 150, 1);
//delay(1000);
//PORTB ^= 0x01;
}
return 0; // normally never return, just to be complient with c99 standard
}
|