Rev 287 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 287 | Rev 289 | ||
|---|---|---|---|
| 1 | /** |
1 | /** |
| 2 | * CAN RGB LED. This application is used to control a RGB LED. |
2 | * CAN RGB LED. This application is used to control a RGB LED. |
| 3 | * Still under heavy development |
3 | * Still under heavy development |
| 4 | * |
4 | * |
| 5 | * @date 2007-02-04 |
5 | * @date 2007-02-04 |
| 6 | * @author Martin Nordén |
6 | * @author Martin Nordén |
| 7 | * |
7 | * |
| 8 | */ |
8 | */ |
| 9 | 9 | ||
| 10 | /*----------------------------------------------------------------------------- |
10 | /*----------------------------------------------------------------------------- |
| 11 | * Includes |
11 | * Includes |
| 12 | *---------------------------------------------------------------------------*/ |
12 | *---------------------------------------------------------------------------*/ |
| 13 | /* system files */ |
13 | /* system files */ |
| 14 | #include <avr/io.h> |
14 | #include <avr/io.h> |
| 15 | #include <avr/interrupt.h> |
15 | #include <avr/interrupt.h> |
| 16 | #include <avr/wdt.h> |
16 | #include <avr/wdt.h> |
| 17 | #include <stdio.h> |
17 | #include <stdio.h> |
| 18 | /* lib files */ |
18 | /* lib files */ |
| 19 | #include <can.h> |
19 | #include <can.h> |
| 20 | #include <serial.h> |
20 | #include <serial.h> |
| 21 | #include <timebase.h> |
21 | #include <timebase.h> |
| 22 | #include <math.h> |
22 | #include <math.h> |
| 23 | 23 | ||
| 24 | /*----------------------------------------------------------------------------- |
24 | /*----------------------------------------------------------------------------- |
| 25 | * Variables |
25 | * Variables |
| 26 | *---------------------------------------------------------------------------*/ |
26 | *---------------------------------------------------------------------------*/ |
| 27 | uint32_t timeStamp; |
27 | uint32_t timeStamp; //TimeStamp to keep track of time |
| - | 28 | uint32_t dimStamp; //Timestamp to keep track of dimmer changes |
|
| 28 | 29 | ||
| 29 | uint8_t currR; //Current Red |
30 | uint8_t currR; //Current Red 0->255 |
| 30 | uint8_t currG; //Current Green |
31 | uint8_t currG; //Current Green 0->255 |
| 31 | uint8_t currB; //Current Blue |
32 | uint8_t currB; //Current Blue 0->255 |
| 32 | 33 | ||
| 33 | uint8_t destR; //Destination Red |
34 | uint8_t destR; //Destination Red |
| 34 | uint8_t destG; //Destination Green |
35 | uint8_t destG; //Destination Green |
| 35 | uint8_t destB; //Destination Blue |
36 | uint8_t destB; //Destination Blue |
| 36 | 37 | ||
| 37 | uint8_t |
38 | uint8_t currIntens; //Current intensity. 0->255 |
| - | 39 | uint8_t destIntens; //Destination intensity. 0->255 |
|
| 38 | 40 | ||
| 39 | uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading |
41 | uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange. |
| - | 42 | uint8_t dimSpeed; //Dimmer speed. Sets the speed of intensitychange. |
|
| - | 43 | ||
| 40 | uint8_t program; //This variable keeps track of what program is currently running. |
44 | uint8_t program; //This variable keeps track of what program is currently running. |
| 41 | uint8_t programstate; //This variable keeps track of where in a program we currently are. |
45 | uint8_t programstate; //This variable keeps track of where in a program we currently are. Will probably be removed later on. |
| 42 | 46 | ||
| 43 | /*---------------------------------------------------------------------------- |
47 | /*---------------------------------------------------------------------------- |
| 44 | * Functions |
48 | * Functions |
| 45 | * -------------------------------------------------------------------------*/ |
49 | * -------------------------------------------------------------------------*/ |
| 46 | void updateLED(); |
50 | void updateLED(); |
| 47 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B); |
51 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B); |
| - | 52 | uint8_t fade(uint8_t current, uint8_t destination); |
|
| 48 | 53 | ||
| 49 | 54 | ||
| 50 | /*----------------------------------------------------------------------------- |
55 | /*----------------------------------------------------------------------------- |
| 51 | * Main Program |
56 | * Main Program |
| 52 | *---------------------------------------------------------------------------*/ |
57 | *---------------------------------------------------------------------------*/ |
| 53 | int main(void) { |
58 | int main(void) { |
| 54 | Timebase_Init(); |
59 | Timebase_Init(); |
| 55 | Serial_Init(); |
60 | Serial_Init(); |
| 56 | Can_Init(); |
61 | Can_Init(); |
| 57 | 62 | ||
| 58 | //Setting up OC1A, 16 bit counter used as 8 bit. |
63 | //Setting up OC1A, 16 bit counter used as 8 bit. |
| 59 | /* Use OC1A */ |
64 | /* Use OC1A */ |
| 60 | TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */ |
65 | TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */ |
| 61 | TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS11); /* Timer uses main system clock with 1/8 prescale */ |
66 | TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS11); /* Timer uses main system clock with 1/8 prescale */ |
| 62 | ICR1 = 256; //8 bit precision to match the other two counters. |
67 | ICR1 = 256; //8 bit precision to match the other two counters. |
| 63 | OCR1A = 128; //50% as standard |
68 | OCR1A = 128; //50% as standard |
| 64 | DDRB |= (1<<PB1); /* Enable pin as output */ |
69 | DDRB |= (1<<PB1); /* Enable pin as output */ |
| 65 | 70 | ||
| 66 | //Initialize variables |
71 | //Initialize variables |
| 67 | fadeSpeed = 250; |
72 | fadeSpeed = 250; |
| - | 73 | dimSpeed = 255; |
|
| 68 | timeStamp = 0; |
74 | timeStamp = 0; |
| - | 75 | dimStamp = 0; |
|
| 69 | 76 | ||
| 70 | Can_Message_t rxMsg; |
77 | Can_Message_t rxMsg; |
| 71 | currR = |
78 | currR = 0x3f; |
| 72 | currG = 0x15; |
79 | currG = 0x15; |
| 73 | currB = |
80 | currB = 0x63; |
| 74 | 81 | ||
| 75 | sei(); |
82 | sei(); |
| 76 | 83 | ||
| 77 | reformatRGB(currR,currG,currB); |
84 | reformatRGB(currR,currG,currB); |
| - | 85 | ||
| - | 86 | destR = 0xff; |
|
| - | 87 | currIntens = 100; |
|
| - | 88 | destIntens = 100; |
|
| 78 | 89 | ||
| 79 | /* main loop */ |
90 | /* main loop */ |
| 80 | while (1) { |
91 | while (1) { |
| 81 | /* service the CAN routines */ |
92 | /* service the CAN routines */ |
| 82 | Can_Service(); |
93 | Can_Service(); |
| 83 | 94 | ||
| 84 | /* Time to |
95 | /* Time to fade color? */ |
| 85 | if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) { |
96 | if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) { |
| - | 97 | currR = fade(currR, destR); |
|
| - | 98 | currG = fade(currG, destG); |
|
| 86 |
|
99 | currB = fade(currB, destB); |
| - | 100 | ||
| 87 | timeStamp = Timebase_CurrentTime(); |
101 | timeStamp = Timebase_CurrentTime(); |
| 88 | updateLED(); |
102 | updateLED(); |
| 89 | } |
103 | } |
| - | 104 | ||
| - | 105 | ||
| - | 106 | /* Time to fade intensity? */ |
|
| - | 107 | if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) { |
|
| - | 108 | currIntens = fade(currIntens, destIntens); |
|
| - | 109 | dimStamp = Timebase_CurrentTime(); |
|
| - | 110 | updateLED(); |
|
| - | 111 | } |
|
| - | 112 | ||
| 90 | 113 | ||
| 91 | 114 | ||
| 92 | /* check if any messages have been received */ |
115 | /* check if any messages have been received */ |
| 93 | /* A lot of remote control crap going on here now for testing */ |
116 | /* A lot of remote control crap going on here now for testing */ |
| 94 | while (Can_Receive(&rxMsg) == CAN_OK) { |
117 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| 95 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){ |
118 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){ |
| 96 | program = 1; |
119 | program = 1; |
| 97 | fadeSpeed = 0; |
120 | fadeSpeed = 0; |
| 98 | } |
121 | } |
| 99 | 122 | ||
| 100 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){ |
123 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){ |
| 101 | program = 2; |
124 | program = 2; |
| 102 | fadeSpeed = 0; |
125 | fadeSpeed = 0; |
| 103 | } |
126 | } |
| 104 | } |
127 | } |
| 105 | 128 | ||
| 106 | 129 | ||
| 107 | } |
130 | } |
| 108 | 131 | ||
| 109 | return 0; |
132 | return 0; |
| 110 | } |
133 | } |
| 111 | - | ||
| 112 | 134 | ||
| 113 | 135 | ||
| 114 | 136 | ||
| 115 | 137 | ||
| 116 | void updateLED(){ |
- | |
| 117 | 138 | ||
| 118 |
|
139 | void updateLED(){ |
| 119 | 140 | ||
| - | 141 | OCR1A = currR * currIntens / 255; |
|
| - | 142 | //TODO: Här ska givetvis de andra två färgerna också uppdateras. |
|
| - | 143 | ||
| - | 144 | ||
| - | 145 | //Bara för debugcrap |
|
| 120 | Can_Message_t txMsg; |
146 | Can_Message_t txMsg; |
| 121 | txMsg.Id = 0x55; |
147 | txMsg.Id = 0x55; |
| 122 | txMsg.RemoteFlag = 0; |
148 | txMsg.RemoteFlag = 0; |
| 123 | txMsg.ExtendedFlag = 1; |
149 | txMsg.ExtendedFlag = 1; |
| 124 | txMsg.DataLength = 4; |
150 | txMsg.DataLength = 4; |
| 125 | 151 | ||
| 126 | txMsg.Data.bytes[0] = currR * |
152 | txMsg.Data.bytes[0] = currR * currIntens / 255; |
| 127 | txMsg.Data.bytes[1] = currG * |
153 | txMsg.Data.bytes[1] = currG * currIntens / 255; |
| 128 | txMsg.Data.bytes[2] = currB * |
154 | txMsg.Data.bytes[2] = currB * currIntens / 255; |
| 129 | txMsg.Data.bytes[3] = |
155 | txMsg.Data.bytes[3] = currIntens; |
| 130 | 156 | ||
| 131 | Can_Send(&txMsg); |
157 | Can_Send(&txMsg); |
| 132 | 158 | ||
| 133 | 159 | ||
| 134 | 160 | ||
| 135 | //Red |
161 | //Red |
| 136 | //Green |
162 | //Green |
| 137 | //Blue |
163 | //Blue |
| 138 | } |
164 | } |
| - | 165 | ||
| 139 | 166 | ||
| 140 | 167 | ||
| 141 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B){ |
168 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B){ |
| 142 | uint8_t maxvalue; |
169 | uint8_t maxvalue; |
| 143 | uint16_t tempcontainer; |
170 | uint16_t tempcontainer; |
| 144 | 171 | ||
| 145 | if ((R>G)&(R>B)){ |
172 | if ((R>G)&(R>B)){ |
| 146 | maxvalue = R; |
173 | maxvalue = R; |
| 147 | } else if ((G>R)&(G>B)){ |
174 | } else if ((G>R)&(G>B)){ |
| 148 | maxvalue = G; |
175 | maxvalue = G; |
| 149 | } else { |
176 | } else { |
| 150 | maxvalue = B; |
177 | maxvalue = B; |
| 151 | } |
178 | } |
| 152 | 179 | ||
| 153 | tempcontainer = R * 255; |
180 | tempcontainer = R * 255; |
| 154 | currR = tempcontainer/maxvalue; |
181 | currR = tempcontainer/maxvalue; |
| 155 | tempcontainer = G * 255; |
182 | tempcontainer = G * 255; |
| 156 | currG = tempcontainer/maxvalue; |
183 | currG = tempcontainer/maxvalue; |
| 157 | tempcontainer = B * 255; |
184 | tempcontainer = B * 255; |
| 158 | currB = tempcontainer/maxvalue; |
185 | currB = tempcontainer/maxvalue; |
| 159 | 186 | ||
| 160 |
|
187 | currIntens = maxvalue; |
| - | 188 | } |
|
| 161 | 189 | ||
| - | 190 | uint8_t fade(uint8_t current, uint8_t destination){ |
|
| - | 191 | if (current > destination){ |
|
| - | 192 | return(current - 1); |
|
| - | 193 | } else if (current < destination){ |
|
| - | 194 | return(current + 1); |
|
| - | 195 | } |
|
| - | 196 | return(current); |
|
| 162 | } |
197 | } |
| 163 | 198 | ||