2 #include <util/delay.h>
3 #include <avr/pgmspace.h>
4 #include <avr/interrupt.h>
16 uart_init(UART_BAUD_SELECT_DOUBLE_SPEED(115201, F_CPU
));
17 //software PWM init FIXME
23 //this scary construct is in place to make the compiler happy. if you know a better way, feel free to improve.
24 uint8_t _frameBuffer
[] = {0,0,0,0};
25 uint8_t _secondFrameBuffer
[] = {0,0,0,0};
26 uint8_t* frameBuffer
= _frameBuffer
;
27 uint8_t* secondFrameBuffer
= _secondFrameBuffer
;
30 uint8_t switch_last_state
= 0;
31 int switch_debounce_timeout
= 0;
35 uint8_t pwm_cycle
= 0;
38 void swapBuffers(void){
39 uint8_t* tmp
= frameBuffer
;
40 frameBuffer
= secondFrameBuffer
;
41 secondFrameBuffer
= tmp
;
44 void setLED(int num
, int val
){
46 frameBuffer
[num
>>3] &= ~(1<<(num
&7));
48 frameBuffer
[num
>>3] |= 1<<(num
&7);
52 int parseHex(char* buf
){
55 for(int i
=0; i
<len
; i
++){
60 }else if(c
>='a' && c
<= 'f'){
62 }else if(c
>='A' && c
<= 'F'){
70 void loop(){ //one frame
71 static uint8_t escape_state
= 0;
72 uint16_t receive_status
= 1;
73 //primitive somewhat messy state machine of the uart interface
74 do{ //Always empty the receive buffer since there are _delay_xxs in the following code and thus this might not run all that often.
75 receive_status
= uart_getc();
76 char c
= receive_status
&0xFF;
77 receive_status
&= 0xFF00;
78 //eats three commands: 's' (0x73) led value sets led number [led] to [value]
79 // 'b' (0x62) buffer buffer buffer buffer sets the whole frame buffer
80 // 'a' (0x61) meter value sets analog meter number [meter] to [value]
83 receive_state
|= 0x02;
103 case 0: //Do not assume anything about the variables used
127 secondFrameBuffer
[(uint8_t) nbuf
] = c
;
137 if(nbuf
>= 4) //prevent array overflows
141 pwm_val
[(uint8_t) nbuf
] = c
;
145 }while(!receive_status
);
146 for(int i
=0; i
<8; i
++){
147 uint8_t Q
= 0x80>>i
; //select the currently active "row" of the matrix. On the protoboards I make, this actually corresponds to physical traces.
148 //de-packing of frame data: data is packed like this: [11111117][22222266][33333555][4444----]
150 Q
|= frameBuffer
[i
&3] >> i
;
152 Q
|= frameBuffer
[i
&3] & (0xFF << (i
&3));
153 //equivalent to the code above: <== revisiting this, I'm not quite sure about this anymore.
154 //Q |= frameBuffer[i&3]>>(i&(0x30>>(i&4))) & (0xFF<<(i&(0x30>>(!i&4))));
159 _delay_ms(1); //Should result in >100Hz refresh rate