Ready to pull from altar.
[cerebrum.git] / led.c
bloba205074cffd53eb2fccf0a28e95815fce5114adc
2 #include "led.h"
3 #include <util/delay.h>
5 //The DDRs of the led matrix outputs are set in the mux loop.
6 void led_setup(void){}
8 #ifdef HAS_LED_SUPPORT
10 void swapBuffers(void){
11 uint8_t* tmp = frameBuffer;
12 frameBuffer = secondFrameBuffer;
13 secondFrameBuffer = tmp;
16 void setLED(int num, int val){
17 if(num<32){
18 frameBuffer[num>>3] &= ~(1<<(num&7));
19 if(val)
20 frameBuffer[num>>3] |= 1<<(num&7);
24 void led_loop(){
25 for(int i=0; i<8; i++){
26 uint8_t Q = 0x80>>i; //select the currently active "row" of the matrix. On the protoboards I make, this actually corresponds to physical traces.
27 //uint8_t DDRQ = 0xFF>>i; //This is supposed to be an optimization. It is untested and will probably not work.
28 uint8_t DDRQ = 0xFF; //Just for testing: reactivating the old behavioor
29 //unpacking of frame data: data is packed like this: [11111117][22222266][33333555][4444----]
30 if(!(i&4))
31 Q |= frameBuffer[i&3] >> (i+1);
32 else
33 Q |= frameBuffer[i&3] & (0xFF << ((i&3)+1));
34 //FIXME this whole mapping shit should be done in h/w!!1!
35 //FIXME this whole mapping is not even correct!!1!
36 //FIXME IT DOES NOT WORK!!1!
37 // Q&0x01 ==> PG5
38 // 02 ==> PE3
39 // 04 ==> PH3
40 // 08 ==> PH4
41 // 10 ==> PH5
42 // 20 ==> PH6
43 // 40 ==> PB4
44 // 80 ==> PB5
45 DDRG&=~(1<<5);
46 DDRG|=(DDRQ&1)<<5;
47 PORTG&=~(1<<5);
48 PORTG|=(Q&1)<<5;
49 DDRE&=~(1<<3);
50 DDRE|=(DDRQ&2)<<2;
51 PORTE&=~(1<<3);
52 PORTE|=(Q&2)<<2;
53 DDRH&=0xC3;
54 DDRH|=(DDRQ&0x3C);
55 PORTH&=0xC3;
56 PORTH|=(Q&0x3C);
57 DDRB&=0xCF;
58 DDRB|=(DDRQ&0xC0)>>2;
59 PORTB&=0xCF;
60 PORTB|=(Q&0xC0)>>2;
61 /* second channel skeleton
62 Q = 0x80>>i;
63 if(!(i&4))
64 Q |= frameBuffer[4+(i&3)] >> i;
65 else
66 Q |= frameBuffer[4+(i&3)] & (0xFF << (i&3));
67 DDRA = DDRQ;
68 PORTA = Q;
69 //PORTD &= 0x0F;
70 //PORTD |= Q&0xF0;
71 //PORTB &= 0xF0;
72 //PORTB |= Q&0x0F;
74 _delay_ms(1);
78 //this scary construct is in place to make the compiler happy. if you know a better way, feel free to improve.
79 uint8_t _frameBuffer[] = {0,0,0,0};
80 uint8_t _secondFrameBuffer[] = {0,0,0,0};
81 uint8_t* frameBuffer = _frameBuffer;
82 uint8_t* secondFrameBuffer = _secondFrameBuffer;
84 #endif//HAS_LED_SUPPORT