Rev 291 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 291 | Rev 298 | ||
|---|---|---|---|
| 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; //TimeStamp to keep track of time |
27 | uint32_t timeStamp; //TimeStamp to keep track of time |
| 28 | uint32_t dimStamp; //Timestamp to keep track of dimmer changes |
28 | uint32_t dimStamp; //Timestamp to keep track of dimmer changes |
| - | 29 | ||
| - | 30 | uint32_t tempStamp; //Timestamp to keep track of temp things during development |
|
| - | 31 | uint8_t temp; |
|
| 29 | 32 | ||
| 30 | uint8_t currR; //Current Red 0->255 |
33 | uint8_t currR; //Current Red 0->255 |
| 31 | uint8_t currG; //Current Green 0->255 |
34 | uint8_t currG; //Current Green 0->255 |
| 32 | uint8_t currB; //Current Blue 0->255 |
35 | uint8_t currB; //Current Blue 0->255 |
| 33 | 36 | ||
| 34 | uint8_t destR; //Destination Red |
37 | uint8_t destR; //Destination Red |
| 35 | uint8_t destG; //Destination Green |
38 | uint8_t destG; //Destination Green |
| 36 | uint8_t destB; //Destination Blue |
39 | uint8_t destB; //Destination Blue |
| 37 | 40 | ||
| 38 | uint8_t currIntens; //Current intensity. 0->255 |
41 | uint8_t currIntens; //Current intensity. 0->255 |
| 39 | uint8_t destIntens; //Destination intensity. 0->255 |
42 | uint8_t destIntens; //Destination intensity. 0->255 |
| 40 | 43 | ||
| 41 | uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange. |
44 | 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. |
45 | uint8_t dimSpeed; //Dimmer speed. Sets the speed of intensitychange. |
| 43 | 46 | ||
| 44 | uint8_t program; //This variable keeps track of what program is currently running. |
47 | uint8_t program; //This variable keeps track of what program is currently running. |
| 45 | uint8_t programstate; //This variable keeps track of where in a program we currently are. Will probably be removed later on. |
48 | uint8_t programstate; //This variable keeps track of where in a program we currently are. Will probably be removed later on. |
| 46 | 49 | ||
| 47 | /*---------------------------------------------------------------------------- |
50 | /*---------------------------------------------------------------------------- |
| 48 | * Functions |
51 | * Functions |
| 49 | * -------------------------------------------------------------------------*/ |
52 | * -------------------------------------------------------------------------*/ |
| 50 | void updateLED(); |
53 | void updateLED(); |
| 51 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B); |
54 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B); |
| - | 55 | void setDestRGB(uint8_t R, uint8_t G, uint8_t B); |
|
| - | 56 | ||
| - | 57 | void changeColor(int8_t amount, uint8_t* color); |
|
| - | 58 | ||
| 52 | uint8_t fade(uint8_t current, uint8_t destination); |
59 | uint8_t fade(uint8_t current, uint8_t destination); |
| 53 | 60 | ||
| 54 | 61 | ||
| 55 | /*----------------------------------------------------------------------------- |
62 | /*----------------------------------------------------------------------------- |
| 56 | * Main Program |
63 | * Main Program |
| 57 | *---------------------------------------------------------------------------*/ |
64 | *---------------------------------------------------------------------------*/ |
| 58 | int main(void) { |
65 | int main(void) { |
| 59 | Timebase_Init(); |
66 | Timebase_Init(); |
| 60 | Serial_Init(); |
67 | Serial_Init(); |
| 61 | Can_Init(); |
68 | Can_Init(); |
| 62 | 69 | ||
| 63 | //Setting up OC1A, 16 bit counter used as 8 bit. |
70 | //Setting up OC1A, 16 bit counter used as 8 bit. |
| 64 | /* Use OC1A */ |
- | |
| 65 | TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */ |
71 | TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */ |
| 66 | TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS11); /* Timer uses main system clock with 1/8 prescale */ |
72 | TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS11); /* Timer uses main system clock with 1/8 prescale */ |
| 67 | ICR1 = 256; //8 bit precision to match the other two counters. |
73 | ICR1 = 256; //8 bit precision to match the other two counters. |
| 68 | OCR1A = |
74 | OCR1A = 0; //0% as standard |
| 69 | DDRB |= (1<<PB1); /* Enable pin as output */ |
75 | DDRB |= (1<<PB1); /* Enable pin as output */ |
| 70 | 76 | ||
| 71 |
|
77 | //Setting up OC0A |
| 72 | TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */ |
78 | TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */ |
| 73 | TCCR0B |= (1<< |
79 | TCCR0B |= (1<<CS01)|(1<<CS00); /* Prescaler ???? Check datasheet */ |
| 74 | OCR0A = |
80 | OCR0A = 0; //0% standard |
| 75 | DDRD |= (1<<PD6); |
81 | DDRD |= (1<<PD6); |
| 76 | 82 | ||
| 77 |
|
83 | //Setting up OC0B |
| 78 | TCCR0A |= (1<<COM0B1); |
84 | TCCR0A |= (1<<COM0B1); |
| 79 | OCR0B = |
85 | OCR0B = 0; |
| 80 | DDRD |= (1<<PD5); |
86 | DDRD |= (1<<PD5); |
| - | 87 | ||
| 81 | 88 | ||
| 82 | //Initialize variables |
89 | //Initialize variables |
| 83 | fadeSpeed = |
90 | fadeSpeed = 10; |
| 84 | dimSpeed = 255; |
91 | dimSpeed = 255; |
| 85 | timeStamp = 0; |
92 | timeStamp = 0; |
| 86 | dimStamp = 0; |
93 | dimStamp = 0; |
| 87 | - | ||
| 88 | Can_Message_t rxMsg; |
94 | Can_Message_t rxMsg; |
| 89 | currR = 0x64; |
95 | |
| 90 |
|
96 | temp = 0; |
| 91 | currB = 0x63; |
- | |
| 92 | 97 | ||
| 93 | sei(); |
98 | sei(); |
| 94 | 99 | ||
| 95 | reformatRGB( |
100 | reformatRGB(0x00,0xff,0x00); |
| - | 101 | currR = destR; |
|
| 96 | 102 | currG = destG; |
|
| - | 103 | currB = destB; |
|
| 97 | currIntens = |
104 | currIntens = 100; |
| 98 | destIntens = |
105 | destIntens = 100; |
| - | 106 | ||
| - | 107 | program = 0; |
|
| - | 108 | ||
| 99 | 109 | ||
| 100 | /* main loop */ |
110 | /* main loop */ |
| 101 | while (1) { |
111 | while (1) { |
| 102 | /* service the CAN routines */ |
112 | /* service the CAN routines */ |
| 103 | Can_Service(); |
113 | Can_Service(); |
| 104 | 114 | ||
| 105 | /* Time to fade color? */ |
115 | /* Time to fade color? */ |
| 106 | if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) { |
116 | if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) { |
| - | 117 | ||
| - | 118 | changeColor(1, &destB); |
|
| - | 119 | changeColor(-1, &destG); |
|
| - | 120 | ||
| 107 | currR = fade(currR, destR); |
121 | currR = fade(currR, destR); |
| 108 | currG = fade(currG, destG); |
122 | currG = fade(currG, destG); |
| 109 | currB = fade(currB, destB); |
123 | currB = fade(currB, destB); |
| 110 | 124 | ||
| 111 | timeStamp = Timebase_CurrentTime(); |
125 | timeStamp = Timebase_CurrentTime(); |
| 112 | updateLED(); |
126 | updateLED(); |
| 113 | } |
127 | } |
| 114 | 128 | ||
| 115 | 129 | ||
| 116 | /* Time to fade intensity? */ |
130 | /* Time to fade intensity? */ |
| 117 | if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) { |
131 | if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) { |
| 118 | currIntens = fade(currIntens, destIntens); |
132 | currIntens = fade(currIntens, destIntens); |
| 119 | dimStamp = Timebase_CurrentTime(); |
133 | dimStamp = Timebase_CurrentTime(); |
| 120 | updateLED(); |
134 | updateLED(); |
| 121 | } |
135 | } |
| 122 | 136 | ||
| - | 137 | ||
| - | 138 | /* Just a nice fade-demo */ |
|
| - | 139 | if ((Timebase_PassedTimeMillis(tempStamp) >= 2500)&(program == 1)) { |
|
| - | 140 | tempStamp = Timebase_CurrentTime(); |
|
| - | 141 | ||
| - | 142 | switch(temp){ |
|
| - | 143 | case 1: |
|
| - | 144 | setDestRGB(0xff,0x00,0x00); |
|
| - | 145 | break; |
|
| - | 146 | case 2: |
|
| - | 147 | setDestRGB(0xff,0xff,0x00); |
|
| - | 148 | break; |
|
| - | 149 | case 3: |
|
| - | 150 | setDestRGB(0x00,0xff,0x00); |
|
| - | 151 | break; |
|
| - | 152 | case 4: |
|
| - | 153 | setDestRGB(0x00,0xff,0xff); |
|
| - | 154 | break; |
|
| - | 155 | case 5: |
|
| - | 156 | setDestRGB(0x00,0x00,0xff); |
|
| - | 157 | break; |
|
| - | 158 | case 6: |
|
| - | 159 | setDestRGB(0xff,0x00,0xff); |
|
| - | 160 | temp = 255; |
|
| - | 161 | break; |
|
| - | 162 | } |
|
| - | 163 | temp++; |
|
| - | 164 | ||
| - | 165 | } |
|
| 123 | 166 | ||
| 124 | 167 | ||
| 125 | /* check if any messages have been received */ |
168 | /* check if any messages have been received */ |
| 126 | /* A lot of remote control crap going on here now for testing */ |
169 | /* A lot of remote control crap going on here now for testing */ |
| 127 | while (Can_Receive(&rxMsg) == CAN_OK) { |
170 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| 128 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){ |
171 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){ |
| 129 | program = 1; |
172 | program = 1; |
| 130 | fadeSpeed = 0; |
173 | fadeSpeed = 0; |
| 131 | } |
174 | } |
| 132 | 175 | ||
| 133 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){ |
176 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){ |
| 134 | program = 2; |
177 | program = 2; |
| 135 | fadeSpeed = 0; |
178 | fadeSpeed = 0; |
| 136 | } |
179 | } |
| 137 | } |
180 | } |
| 138 | 181 | ||
| 139 | 182 | ||
| 140 | } |
183 | } |
| 141 | 184 | ||
| 142 | return 0; |
185 | return 0; |
| 143 | } |
186 | } |
| 144 | 187 | ||
| 145 | 188 | ||
| 146 | 189 | ||
| 147 | 190 | ||
| 148 | 191 | ||
| 149 | void updateLED(){ |
192 | void updateLED(){ |
| - | 193 | uint16_t tempcontainer; |
|
| 150 | 194 | ||
| 151 |
|
195 | tempcontainer = currR * currIntens / 255; |
| - | 196 | OCR1A = tempcontainer; |
|
| - | 197 | tempcontainer = currG * currIntens / 255; |
|
| - | 198 | OCR0A = tempcontainer; |
|
| 152 |
|
199 | tempcontainer = currB * currIntens / 255; |
| - | 200 | OCR0B = tempcontainer; |
|
| 153 | 201 | ||
| 154 | 202 | ||
| 155 | //Bara för debugcrap |
203 | //Bara för debugcrap |
| 156 | Can_Message_t txMsg; |
204 | Can_Message_t txMsg; |
| 157 | txMsg.Id = 0x55; |
205 | txMsg.Id = 0x55; |
| 158 | txMsg.RemoteFlag = 0; |
206 | txMsg.RemoteFlag = 0; |
| 159 | txMsg.ExtendedFlag = 1; |
207 | txMsg.ExtendedFlag = 1; |
| 160 | txMsg.DataLength = 4; |
208 | txMsg.DataLength = 4; |
| 161 | 209 | ||
| 162 | txMsg.Data.bytes[0] = currR |
210 | txMsg.Data.bytes[0] = currR;// * currIntens / 255; |
| 163 | txMsg.Data.bytes[1] = currG |
211 | txMsg.Data.bytes[1] = currG;// * currIntens / 255; |
| 164 | txMsg.Data.bytes[2] = currB |
212 | txMsg.Data.bytes[2] = currB;// * currIntens / 255; |
| 165 | txMsg.Data.bytes[3] = currIntens; |
213 | txMsg.Data.bytes[3] = currIntens; |
| 166 | - | ||
| 167 | Can_Send(&txMsg); |
214 | Can_Send(&txMsg); |
| - | 215 | } |
|
| - | 216 | ||
| - | 217 | ||
| - | 218 | /* setDestRGB sets a new target RGB triplet to fade to. Note that the brightness is kept at approximatly the same level |
|
| - | 219 | * even though the color changes */ |
|
| - | 220 | ||
| - | 221 | void setDestRGB(uint8_t R, uint8_t G, uint8_t B){ |
|
| - | 222 | uint8_t maxvalue; |
|
| - | 223 | uint16_t tempcontainer; |
|
| 168 | 224 | ||
| - | 225 | if ((R>=G)&(R>=B)){ |
|
| - | 226 | maxvalue = R; |
|
| - | 227 | } else if ((G>=R)&(G>=B)){ |
|
| - | 228 | maxvalue = G; |
|
| - | 229 | } else { |
|
| - | 230 | maxvalue = B; |
|
| - | 231 | } |
|
| - | 232 | ||
| - | 233 | tempcontainer = R * 255; |
|
| - | 234 | destR = tempcontainer/maxvalue; |
|
| - | 235 | tempcontainer = G * 255; |
|
| - | 236 | destG = tempcontainer/maxvalue; |
|
| - | 237 | tempcontainer = B * 255; |
|
| - | 238 | destB = tempcontainer/maxvalue; |
|
| 169 | 239 | ||
| 170 | - | ||
| 171 | //Red |
- | |
| 172 | //Green |
- | |
| 173 | //Blue |
- | |
| 174 | } |
240 | } |
| 175 | 241 | ||
| - | 242 | /* reformatRGB sets a new target RGB triplet to fade to. Note that the brightness is recalculated. */ |
|
| 176 | 243 | ||
| 177 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B){ |
244 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B){ |
| 178 | uint8_t maxvalue; |
245 | uint8_t maxvalue; |
| 179 | uint16_t tempcontainer; |
246 | uint16_t tempcontainer; |
| 180 | 247 | ||
| 181 | if ((R>G)&(R>B)){ |
248 | if ((R>=G)&(R>=B)){ |
| 182 | maxvalue = R; |
249 | maxvalue = R; |
| 183 | } else if ((G>R)&(G>B)){ |
250 | } else if ((G>=R)&(G>=B)){ |
| 184 | maxvalue = G; |
251 | maxvalue = G; |
| 185 | } else { |
252 | } else { |
| 186 | maxvalue = B; |
253 | maxvalue = B; |
| 187 | } |
254 | } |
| 188 | 255 | ||
| 189 | tempcontainer = R * 255; |
256 | tempcontainer = R * 255; |
| 190 |
|
257 | destR = tempcontainer/maxvalue; |
| 191 | tempcontainer = G * 255; |
258 | tempcontainer = G * 255; |
| 192 |
|
259 | destG = tempcontainer/maxvalue; |
| 193 | tempcontainer = B * 255; |
260 | tempcontainer = B * 255; |
| 194 |
|
261 | destB = tempcontainer/maxvalue; |
| 195 | 262 | ||
| 196 |
|
263 | destIntens = maxvalue; |
| 197 | } |
264 | } |
| 198 | 265 | ||
| 199 | uint8_t fade(uint8_t current, uint8_t destination){ |
266 | uint8_t fade(uint8_t current, uint8_t destination){ |
| 200 | if (current > destination){ |
267 | if (current > destination){ |
| 201 | return(current - 1); |
268 | return(current - 1); |
| 202 | } else if (current < destination){ |
269 | } else if (current < destination){ |
| 203 | return(current + 1); |
270 | return(current + 1); |
| 204 | } |
271 | } |
| 205 | return(current); |
272 | return(current); |
| - | 273 | } |
|
| - | 274 | ||
| - | 275 | /* |
|
| - | 276 | * changeColor ändrar en färg mängden amount. -127>127. |
|
| - | 277 | * color kan vara dest eller curr R,G,B. |
|
| - | 278 | * Ev. TODO: om t.ex. röd är på max och man vill öka röd ska de andra färgerna minskas. */ |
|
| - | 279 | void changeColor(int8_t amount, uint8_t* color){ |
|
| - | 280 | if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color |
|
| - | 281 | *color = *color + amount; |
|
| 206 | } |
282 | } |
| 207 | 283 | ||