Rev 301 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 301 | Rev 302 | ||
|---|---|---|---|
| 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 | * TODO: A lot, |
8 | * TODO: A lot, currently trying to get some kind of ready-made programs to work. |
| 9 | */ |
9 | */ |
| 10 | 10 | ||
| 11 | /*----------------------------------------------------------------------------- |
11 | /*----------------------------------------------------------------------------- |
| 12 | * Includes |
12 | * Includes |
| 13 | *---------------------------------------------------------------------------*/ |
13 | *---------------------------------------------------------------------------*/ |
| 14 | /* system files */ |
14 | /* system files */ |
| 15 | #include <avr/io.h> |
15 | #include <avr/io.h> |
| 16 | #include <avr/interrupt.h> |
16 | #include <avr/interrupt.h> |
| 17 | #include <avr/wdt.h> |
17 | #include <avr/wdt.h> |
| 18 | #include <stdio.h> |
18 | #include <stdio.h> |
| 19 | /* lib files */ |
19 | /* lib files */ |
| 20 | #include <can.h> |
20 | #include <can.h> |
| 21 | #include <serial.h> |
21 | #include <serial.h> |
| 22 | #include <timebase.h> |
22 | #include <timebase.h> |
| 23 | #include <math.h> |
23 | #include <math.h> //Not sure this is needed anymore.. |
| - | 24 | #include <avr/pgmspace.h> //To allow read/write from flash |
|
| 24 | 25 | ||
| 25 | 26 | ||
| 26 | /*---------------------------------------------------------------------------- |
27 | /*---------------------------------------------------------------------------- |
| 27 | * Ehm, struct? |
28 | * Ehm, struct? |
| 28 | * -------------------------------------------------------------------------*/ |
29 | * -------------------------------------------------------------------------*/ |
| 29 | struct |
30 | typedef struct |
| 30 | { |
31 | { |
| 31 | uint8_t R; |
32 | uint8_t R; |
| 32 | uint8_t G; |
33 | uint8_t G; |
| 33 | uint8_t B; |
34 | uint8_t B; |
| 34 | uint8_t Intens; |
35 | uint8_t Intens; |
| - | 36 | } Color; |
|
| - | 37 | ||
| - | 38 | /*----------------------------------------------------------------------------- |
|
| - | 39 | * Programs! This will most likely be improooved, but I have to start somewhere. |
|
| - | 40 | *---------------------------------------------------------------------------*/ |
|
| - | 41 | ||
| - | 42 | /* |
|
| - | 43 | struct Color pgm1[] = { |
|
| - | 44 | {0x10,0x20,0x30,0x40}, |
|
| - | 45 | {0x10,0x20,0x30,0x40}, |
|
| - | 46 | {0x10,0x20,0x30,0x40} |
|
| - | 47 | }; |
|
| - | 48 | ||
| - | 49 | struct Color pgm2[] = { |
|
| - | 50 | {0x20,0x20,0x30,0x50}, |
|
| - | 51 | {0x20,0x20,0x30,0x50}, |
|
| - | 52 | {0x20,0x20,0x30,0x50} |
|
| - | 53 | }; |
|
| - | 54 | ||
| - | 55 | struct Color* programs[] PROGMEM = { |
|
| - | 56 | (struct Color*)pgm1, |
|
| - | 57 | (struct Color*)pgm2 |
|
| 35 | }; |
58 | }; |
| - | 59 | ||
| - | 60 | ||
| - | 61 | uint16_t temp_address = pgm_read_word(&programs[1]); |
|
| - | 62 | */ |
|
| 36 | 63 | ||
| 37 | /*----------------------------------------------------------------------------- |
64 | /*----------------------------------------------------------------------------- |
| 38 | * Variables |
65 | * Variables |
| 39 | *---------------------------------------------------------------------------*/ |
66 | *---------------------------------------------------------------------------*/ |
| 40 | uint32_t timeStamp; //TimeStamp to keep track of time |
67 | uint32_t timeStamp; //TimeStamp to keep track of time |
| 41 | uint32_t dimStamp; //Timestamp to keep track of dimmer changes |
68 | uint32_t dimStamp; //Timestamp to keep track of dimmer changes |
| 42 | 69 | ||
| 43 | uint32_t tempStamp; //Timestamp to keep track of temp things during development |
70 | uint32_t tempStamp; //Timestamp to keep track of temp things during development |
| 44 | uint8_t temp; |
71 | uint8_t temp; |
| 45 | 72 | ||
| 46 |
|
73 | Color currColor; //Current color |
| 47 |
|
74 | Color destColor; //Destination color |
| 48 | 75 | ||
| 49 | uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange. |
76 | uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange. |
| 50 | uint8_t dimSpeed; //Dimmer speed. Sets the speed of intensitychange. |
77 | uint8_t dimSpeed; //Dimmer speed. Sets the speed of intensitychange. |
| 51 | 78 | ||
| 52 | uint8_t program; //This variable keeps track of what program is currently running. |
79 | uint8_t program; //This variable keeps track of what program is currently running. |
| 53 | uint8_t programstate; //This variable keeps track of where in a program we currently are. Will probably be removed later on. |
80 | uint8_t programstate; //This variable keeps track of where in a program we currently are. Will probably be removed later on. |
| 54 | 81 | ||
| 55 | /*---------------------------------------------------------------------------- |
82 | /*---------------------------------------------------------------------------- |
| 56 | * Functions |
83 | * Functions |
| 57 | * -------------------------------------------------------------------------*/ |
84 | * -------------------------------------------------------------------------*/ |
| 58 | void updateLED(); |
85 | void updateLED(); |
| 59 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright); |
86 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright); |
| 60 | void changeColor(int8_t amount, uint8_t* color); |
87 | void changeColor(int8_t amount, uint8_t* color); |
| 61 |
|
88 | void fade(uint8_t* current, uint8_t* destination); |
| 62 | 89 | ||
| 63 | /*----------------------------------------------------------------------------- |
90 | /*----------------------------------------------------------------------------- |
| 64 | * Main Program |
91 | * Main Program |
| 65 | *---------------------------------------------------------------------------*/ |
92 | *---------------------------------------------------------------------------*/ |
| 66 | int main(void) { |
93 | int main(void) { |
| 67 | Timebase_Init(); |
94 | Timebase_Init(); |
| 68 | Serial_Init(); |
95 | Serial_Init(); |
| 69 | Can_Init(); |
96 | Can_Init(); |
| 70 | 97 | ||
| 71 | //Setting up OC1A, 16 bit counter used as 8 bit. |
98 | //Setting up OC1A, 16 bit counter used as 8 bit. |
| 72 | TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */ |
99 | TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */ |
| 73 | TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS12); /* Timer uses main system clock with ? prescale */ |
100 | TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS12); /* Timer uses main system clock with ? prescale */ |
| 74 | ICR1 = 256; //8 bit precision to match the other two counters. |
101 | ICR1 = 256; //8 bit precision to match the other two counters. |
| 75 | OCR1A = 0; //0% as standard |
102 | OCR1A = 0; //0% as standard |
| 76 | DDRB |= (1<<PB1); /* Enable pin as output */ |
103 | DDRB |= (1<<PB1); /* Enable pin as output */ |
| 77 | 104 | ||
| 78 | //Setting up OC0A |
105 | //Setting up OC0A |
| 79 | TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */ |
106 | TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */ |
| 80 | TCCR0B |= (1<<CS02); /* Prescaler updating now */ |
107 | TCCR0B |= (1<<CS02); /* Prescaler updating now */ |
| 81 | OCR0A = 0; //0% standard |
108 | OCR0A = 0; //0% standard |
| 82 | DDRD |= (1<<PD6); |
109 | DDRD |= (1<<PD6); |
| 83 | 110 | ||
| 84 | //Setting up OC0B |
111 | //Setting up OC0B |
| 85 | TCCR0A |= (1<<COM0B1); |
112 | TCCR0A |= (1<<COM0B1); |
| 86 | OCR0B = 0; |
113 | OCR0B = 0; |
| 87 | DDRD |= (1<<PD5); |
114 | DDRD |= (1<<PD5); |
| 88 | 115 | ||
| 89 | 116 | ||
| 90 | //Initialize variables |
117 | //Initialize variables |
| 91 | fadeSpeed = 10; |
118 | fadeSpeed = 10; |
| 92 | dimSpeed = 255; |
119 | dimSpeed = 255; |
| 93 | timeStamp = 0; |
120 | timeStamp = 0; |
| 94 | dimStamp = 0; |
121 | dimStamp = 0; |
| 95 | Can_Message_t rxMsg; |
122 | Can_Message_t rxMsg; |
| 96 | 123 | ||
| 97 | reformatRGB(0x00,0xff,0x00,1); |
124 | reformatRGB(0x00,0xff,0x00,1); |
| 98 | destColor.Intens = 100; |
125 | destColor.Intens = 100; |
| 99 | currColor = destColor; |
126 | currColor = destColor; |
| 100 | 127 | ||
| 101 | //Just temporary for now, they keep track of some kind of demo-program. |
128 | //Just temporary for now, they keep track of some kind of demo-program. |
| 102 | program = 1; |
129 | program = 1; |
| 103 | temp = 1; |
130 | temp = 1; |
| 104 | 131 | ||
| 105 | 132 | ||
| 106 | sei(); |
133 | sei(); |
| 107 | 134 | ||
| 108 | /* main loop */ |
135 | /* main loop */ |
| 109 | while (1) { |
136 | while (1) { |
| 110 | /* service the CAN routines */ |
137 | /* service the CAN routines */ |
| 111 | Can_Service(); |
138 | Can_Service(); |
| 112 | 139 | ||
| 113 | /* Time to fade color? */ |
140 | /* Time to fade color? */ |
| 114 | if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) { |
141 | if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) { |
| 115 |
|
142 | fade(&currColor.R, &destColor.R); |
| 116 |
|
143 | fade(&currColor.G, &destColor.G); |
| 117 |
|
144 | fade(&currColor.B, &destColor.B); |
| 118 | 145 | ||
| 119 | timeStamp = Timebase_CurrentTime(); |
146 | timeStamp = Timebase_CurrentTime(); |
| 120 | updateLED(); |
147 | updateLED(); |
| 121 | } |
148 | } |
| 122 | 149 | ||
| 123 | 150 | ||
| 124 | /* Time to fade intensity? */ |
151 | /* Time to fade intensity? */ |
| 125 | if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) { |
152 | if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) { |
| 126 |
|
153 | fade(&currColor.Intens, &destColor.Intens); |
| 127 | dimStamp = Timebase_CurrentTime(); |
154 | dimStamp = Timebase_CurrentTime(); |
| 128 | updateLED(); |
155 | updateLED(); |
| 129 | } |
156 | } |
| - | 157 | ||
| 130 | 158 | ||
| - | 159 | /* Program management */ |
|
| - | 160 | if (program != 0){ //Check if a program is currently running |
|
| - | 161 | //if (currColor == destColor){ //Is the current sequence finished? |
|
| - | 162 | //Set next state. |
|
| - | 163 | //} |
|
| - | 164 | } |
|
| 131 | 165 | ||
| 132 | /* Just a nice fade-demo */ |
166 | /* Just a nice fade-demo */ |
| 133 | if ((Timebase_PassedTimeMillis(tempStamp) >= 2500)&(program == 1)) { |
167 | if ((Timebase_PassedTimeMillis(tempStamp) >= 2500)&(program == 1)) { |
| 134 | tempStamp = Timebase_CurrentTime(); |
168 | tempStamp = Timebase_CurrentTime(); |
| 135 | 169 | ||
| 136 | switch(temp){ |
170 | switch(temp){ |
| 137 | case 1: |
171 | case 1: |
| 138 | reformatRGB(0xff,0x00,0x00,0); |
172 | reformatRGB(0xff,0x00,0x00,0); |
| 139 | break; |
173 | break; |
| 140 | case 2: |
174 | case 2: |
| 141 | reformatRGB(0xff,0xff,0x00,0); |
175 | reformatRGB(0xff,0xff,0x00,0); |
| 142 | break; |
176 | break; |
| 143 | case 3: |
177 | case 3: |
| 144 | reformatRGB(0x00,0xff,0x00,0); |
178 | reformatRGB(0x00,0xff,0x00,0); |
| 145 | break; |
179 | break; |
| 146 | case 4: |
180 | case 4: |
| 147 | reformatRGB(0x00,0xff,0xff,0); |
181 | reformatRGB(0x00,0xff,0xff,0); |
| 148 | break; |
182 | break; |
| 149 | case 5: |
183 | case 5: |
| 150 | reformatRGB(0x00,0x00,0xff,0); |
184 | reformatRGB(0x00,0x00,0xff,0); |
| 151 | break; |
185 | break; |
| 152 | case 6: |
186 | case 6: |
| 153 | reformatRGB(0xff,0x00,0xff,0); |
187 | reformatRGB(0xff,0x00,0xff,0); |
| 154 | temp = 255; |
188 | temp = 255; |
| 155 | break; |
189 | break; |
| 156 | } |
190 | } |
| 157 | temp++; |
191 | temp++; |
| 158 | 192 | ||
| 159 | } |
193 | } |
| 160 | 194 | ||
| 161 | 195 | ||
| 162 | /* check if any messages have been received */ |
196 | /* check if any messages have been received */ |
| 163 | /* A lot of remote control crap going on here now for testing */ |
197 | /* A lot of remote control crap going on here now for testing */ |
| 164 | while (Can_Receive(&rxMsg) == CAN_OK) { |
198 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| 165 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){ |
199 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){ |
| 166 | program = 1; |
200 | program = 1; |
| 167 | fadeSpeed = 0; |
201 | fadeSpeed = 0; |
| 168 | } |
202 | } |
| 169 | 203 | ||
| 170 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){ |
204 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){ |
| 171 | program = 2; |
205 | program = 2; |
| 172 | fadeSpeed = 0; |
206 | fadeSpeed = 0; |
| 173 | } |
207 | } |
| 174 | } |
208 | } |
| 175 | 209 | ||
| 176 | 210 | ||
| 177 | } |
211 | } |
| 178 | 212 | ||
| 179 | return 0; |
213 | return 0; |
| 180 | } |
214 | } |
| 181 | 215 | ||
| 182 | 216 | ||
| 183 | 217 | ||
| 184 | 218 | ||
| 185 | /************************************************************************************************ |
219 | /************************************************************************************************ |
| 186 | * updateLED updates the PWM counter values and thus changes the color of the RGBLED. * |
220 | * updateLED updates the PWM counter values and thus changes the color of the RGBLED. * |
| 187 | * It doesn't reculculate anything, except the values to write to the PWM registers. * |
221 | * It doesn't reculculate anything, except the values to write to the PWM registers. * |
| 188 | * TODO: Get rid of the tempcontainer! * |
222 | * TODO: Get rid of the tempcontainer! * |
| 189 | ************************************************************************************************/ |
223 | ************************************************************************************************/ |
| 190 | void updateLED(){ |
224 | void updateLED(){ |
| - | 225 | uint8_t tempcontainer; |
|
| - | 226 | ||
| - | 227 | tempcontainer = currColor.R * currColor.Intens / 255; //I really want to skip this, don't know how yet though. |
|
| - | 228 | OCR1A = tempcontainer; |
|
| - | 229 | OCR0A = currColor.G * currColor.Intens / 255; |
|
| - | 230 | OCR0B = currColor.B * currColor.Intens / 255; |
|
| - | 231 | ||
| 191 | //Bara för debugcrap |
232 | //Bara för debugcrap |
| 192 | Can_Message_t txMsg; |
233 | Can_Message_t txMsg; |
| 193 | txMsg.Id = 0x55; |
234 | txMsg.Id = 0x55; |
| 194 | txMsg.RemoteFlag = 0; |
235 | txMsg.RemoteFlag = 0; |
| 195 | txMsg.ExtendedFlag = 1; |
236 | txMsg.ExtendedFlag = 1; |
| 196 | txMsg.DataLength = 4; |
237 | txMsg.DataLength = 4; |
| 197 | - | ||
| 198 | - | ||
| 199 | uint8_t tempcontainer; |
- | |
| 200 | - | ||
| 201 | tempcontainer = currColor.R * currColor.Intens / 255; //I really want to skip this, don't know how yet though. |
- | |
| 202 | OCR1A = tempcontainer; |
- | |
| 203 | OCR0A = currColor.G * currColor.Intens / 255; |
- | |
| 204 | OCR0B = currColor.B * currColor.Intens / 255; |
- | |
| 205 | - | ||
| 206 | txMsg.Data.bytes[0] = currColor.R * currColor.Intens / 255; |
238 | txMsg.Data.bytes[0] = currColor.R * currColor.Intens / 255; |
| 207 | txMsg.Data.bytes[1] = currColor.G * currColor.Intens / 255; |
239 | txMsg.Data.bytes[1] = currColor.G * currColor.Intens / 255; |
| 208 | txMsg.Data.bytes[2] = currColor.B * currColor.Intens / 255; |
240 | txMsg.Data.bytes[2] = currColor.B * currColor.Intens / 255; |
| 209 | txMsg.Data.bytes[3] = currColor.Intens; |
241 | txMsg.Data.bytes[3] = currColor.Intens; |
| 210 | Can_Send(&txMsg); |
242 | Can_Send(&txMsg); |
| 211 | } |
243 | } |
| 212 | 244 | ||
| 213 | 245 | ||
| 214 | 246 | ||
| 215 | /************************************************************************************************ |
247 | /************************************************************************************************ |
| 216 | * reformatRGB sets a new target RGB triplet to fade to. It reformats it so that * |
248 | * reformatRGB sets a new target RGB triplet to fade to. It reformats it so that * |
| 217 | * the highest color always gets 0xff. Brightness is recalculated. * |
249 | * the highest color always gets 0xff. Brightness is recalculated. * |
| 218 | * if setBright is set to 0, the brightness will not be updated. * |
250 | * if setBright is set to 0, the brightness will not be updated. * |
| 219 | * TODO: Smarter 16-bit arithmetics would be nice. Also a pointer to struct to be updated. * |
251 | * TODO: Smarter 16-bit arithmetics would be nice. Also a pointer to struct to be updated. * |
| 220 | ************************************************************************************************/ |
252 | ************************************************************************************************/ |
| 221 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright){ |
253 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright){ |
| 222 | uint8_t maxvalue; |
254 | uint8_t maxvalue; |
| 223 | uint16_t tempcontainer; |
255 | uint16_t tempcontainer; |
| 224 | 256 | ||
| 225 | if ((R>=G)&(R>=B)){ |
257 | if ((R>=G)&(R>=B)){ |
| 226 | maxvalue = R; |
258 | maxvalue = R; |
| 227 | } else if ((G>=R)&(G>=B)){ |
259 | } else if ((G>=R)&(G>=B)){ |
| 228 | maxvalue = G; |
260 | maxvalue = G; |
| 229 | } else { |
261 | } else { |
| 230 | maxvalue = B; |
262 | maxvalue = B; |
| 231 | } |
263 | } |
| 232 | 264 | ||
| 233 | tempcontainer = R * 255; |
265 | tempcontainer = R * 255; |
| 234 | destColor.R = tempcontainer/maxvalue; |
266 | destColor.R = tempcontainer/maxvalue; |
| 235 | tempcontainer = G * 255; |
267 | tempcontainer = G * 255; |
| 236 | destColor.G = tempcontainer/maxvalue; |
268 | destColor.G = tempcontainer/maxvalue; |
| 237 | tempcontainer = B * 255; |
269 | tempcontainer = B * 255; |
| 238 | destColor.B = tempcontainer/maxvalue; |
270 | destColor.B = tempcontainer/maxvalue; |
| 239 | 271 | ||
| 240 | if (setBright){ |
272 | if (setBright){ |
| 241 | destColor.Intens = maxvalue; |
273 | destColor.Intens = maxvalue; |
| 242 | } |
274 | } |
| 243 | } |
275 | } |
| 244 | 276 | ||
| 245 | 277 | ||
| 246 | /************************************************************************************************ |
278 | /************************************************************************************************ |
| 247 | * fade tar in två värden, current och destination och för current ett steg * |
279 | * fade tar in två värden, current och destination och för current ett steg * |
| 248 | * närmare destination. * |
280 | * närmare destination. * |
| 249 | * Ev. TODO: skicka in två pekare och jämna ut dom istället. * |
- | |
| 250 | ************************************************************************************************/ |
281 | ************************************************************************************************/ |
| 251 |
|
282 | void fade(uint8_t* current, uint8_t* destination){ |
| 252 | if (current |
283 | if (*current > *destination){ |
| 253 |
|
284 | *current = *current - 1; |
| 254 | } else if (current |
285 | } else if (*current < *destination){ |
| 255 |
|
286 | *current = *current + 1; |
| 256 | } |
287 | } |
| 257 | return(current); |
- | |
| 258 | } |
288 | } |
| 259 | 289 | ||
| 260 | 290 | ||
| 261 | /************************************************************************************************ |
291 | /************************************************************************************************ |
| 262 | * changeColor ändrar en färg mängden amount. -127>127. * |
292 | * changeColor ändrar en färg mängden amount. -127>127. * |
| 263 | * color kan vara dest eller curr R,G,B. * |
293 | * color kan vara dest eller curr R,G,B. * |
| 264 | * Ev. TODO: om t.ex. röd är på max och man vill öka röd ska de andra färgerna minskas. * |
294 | * Ev. TODO: om t.ex. röd är på max och man vill öka röd ska de andra färgerna minskas. * |
| 265 | ************************************************************************************************/ |
295 | ************************************************************************************************/ |
| 266 | void changeColor(int8_t amount, uint8_t* color){ |
296 | void changeColor(int8_t amount, uint8_t* color){ |
| 267 | if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color |
297 | if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color |
| 268 | *color = *color + amount; |
298 | *color = *color + amount; |
| 269 | } |
299 | } |
| 270 | 300 | ||
| 271 | 301 | ||