2 Copyright (C) 2012 jaseg <s@jaseg.de>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 3 as published by the Free Software Foundation.
10 #include <util/delay.h>
11 #include <avr/pgmspace.h>
12 #include <avr/interrupt.h>
24 uart_init(UART_BAUD_SELECT_DOUBLE_SPEED(115200, F_CPU
));
25 //s/w "BCM"(<== "Binary Code Modulation") timer setup
27 TCCR0A |= _BV(WGM00)|_BV(WGM01);
31 //FIXME set PWM output DDRs
32 //The DDRs of the led matrix outputs are set in the mux loop.
36 //this scary construct is in place to make the compiler happy. if you know a better way, feel free to improve.
37 uint8_t _frameBuffer
[] = {0,0,0,0};
38 uint8_t _secondFrameBuffer
[] = {0,0,0,0};
39 uint8_t* frameBuffer
= _frameBuffer
;
40 uint8_t* secondFrameBuffer
= _secondFrameBuffer
;
43 uint8_t switch_last_state
= 0;
44 int switch_debounce_timeout
= 0;
49 uint8_t pwm_cycle
= 0;
50 uint8_t pwm_val
[PWM_COUNT
];
52 uint8_t debounce_timeouts
[INPUT_COUNT
];
53 uint8_t switch_states
[INPUT_COUNT
];
55 void swapBuffers(void){
56 uint8_t* tmp
= frameBuffer
;
57 frameBuffer
= secondFrameBuffer
;
58 secondFrameBuffer
= tmp
;
61 void setLED(int num
, int val
){
63 frameBuffer
[num
>>3] &= ~(1<<(num
&7));
65 frameBuffer
[num
>>3] |= 1<<(num
&7);
69 int parseHex(char* buf
){
72 for(int i
=0; i
<len
; i
++){
77 }else if(c
>='a' && c
<= 'f'){
79 }else if(c
>='A' && c
<= 'F'){
87 inline char hex_nibble(uint8_t data
){
94 void put_hex(uint8_t data
){
95 uart_putc(hex_nibble(data
&15));
96 uart_putc(hex_nibble(data
>>4));
99 void loop(){ //one frame
100 static uint8_t escape_state
= 0;
101 uint16_t receive_state
= 1;
102 //primitive somewhat messy state machine of the uart interface
103 do{ //Always empty the receive buffer since there are _delay_xxs in the following code and thus this might not run all that often.
104 receive_state
= uart_getc();
105 char c
= receive_state
&0xFF;
106 receive_state
&= 0xFF00;
107 //escape code format:
111 //eats three commands: 's' (0x73) led value sets led number [led] to [value]
112 // 'b' (0x62) buffer buffer buffer buffer sets the whole frame buffer
113 // 'a' (0x61) meter value sets analog meter number [meter] to [value]
114 // 'r' (0x72) read the frame buffer
115 //this device will utter a "'c' (0x63) num state" when switch [num] changes state to [state]
116 //commands are terminated by \n
120 receive_state
|= 0x02;
123 receive_state
|= 0x02;
140 case 0: //Do not assume anything about the variables used
156 for(uint8_t i
=0; i
<sizeof(frameBuffer
); i
++){
157 put_hex(frameBuffer
[i
]);
172 secondFrameBuffer
[(uint8_t) nbuf
] = c
;
186 pwm_val
[(uint8_t) nbuf
] = c
;
190 }while(!receive_state
);
191 for(int i
=0; i
<8; i
++){
192 uint8_t Q
= 0x80>>i
; //select the currently active "row" of the matrix. On the protoboards I make, this actually corresponds to physical traces.
193 //uint8_t DDRQ = 0xFF>>i; //This is supposed to be an optimization. It is untested and will probably not work.
194 uint8_t DDRQ
= 0xFF; //Just for testing: reactivating the old behavioor
195 //unpacking of frame data: data is packed like this: [11111117][22222266][33333555][4444----]
197 Q
|= frameBuffer
[i
&3] >> i
;
199 Q
|= frameBuffer
[i
&3] & (0xFF << (i
&3));
200 //FIXME this whole mapping shit should be done in h/w!!1!
201 //FIXME this whole mapping is not even correct!!1!
202 //FIXME IT DOES NOT WORK!!1!
224 DDRB
|=(DDRQ
&0xC0)>>2;
227 /* second channel skeleton
230 Q |= frameBuffer[4+(i&3)] >> i;
232 Q |= frameBuffer[4+(i&3)] & (0xFF << (i&3));
243 switch_states[0] |= !!(PINH&0x02);
244 for(int i=0; i<INPUT_COUNT; i++){
245 debounce_timeouts[i]--;
246 //A #define for the debounce time would be great
247 if(debounce_timeouts[i] == 0){
248 uint8_t new_switch_state = switch_states[i]<<1;
249 if(!(switch_states[i]^new_switch_state)){
252 uart_putc(switch_states[i]&1);
253 debounce_timeouts[i] = 0xFF;
254 switch_states[i] = new_switch_state&3;
262 //Called every 256 cpu clks (i.e should not get overly long)
263 ISR(TIMER0_OVF_vect
){
265 Q
|= (pwm_val
[0] & pwm_cycle
);
266 Q
|= (pwm_val
[1] & pwm_cycle
)?2:0;
267 Q
|= (pwm_val
[2] & pwm_cycle
)?4:0;
268 Q
|= (pwm_val
[3] & pwm_cycle
)?8:0;
269 Q
|= (pwm_val
[4] & pwm_cycle
)?16:0;