Rev 413 | Rev 563 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 413 | Rev 546 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 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-10 |
5 | * @date 2007-02-10 |
| 6 | * @author Martin Nordén |
6 | * @author Martin Nordén |
| 7 | * |
7 | * |
| 8 | * TODO: Better CAN Integration, Failsafe mode, Tempsensors etc. |
8 | * TODO: Better CAN Integration, Failsafe mode, Tempsensors etc. |
| 9 | * Also, check current/destination intensity better when in a program? |
9 | * Also, check current/destination intensity better when in a program? |
| 10 | * |
10 | * |
| 11 | * A little comment here :) |
11 | * A little comment here :) |
| Line 18... | Line 18... | ||
| 18 | /* system files */ |
18 | /* system files */ |
| 19 | #include <avr/interrupt.h> |
19 | #include <avr/interrupt.h> |
| 20 | #include <inttypes.h> |
20 | #include <inttypes.h> |
| 21 | #include <stdio.h> |
21 | #include <stdio.h> |
| 22 | #include <avr/pgmspace.h> //To allow read/write from flash |
22 | #include <avr/pgmspace.h> //To allow read/write from flash |
| - | 23 | ||
| 23 | 24 | ||
| 24 | #include <drivers/timer/timebase.h> |
25 | #include <drivers/timer/timebase.h> |
| 25 | 26 | ||
| 26 | #include <config.h> // All configuration parameters |
27 | #include <config.h> // All configuration parameters |
| 27 | #include <bios.h> // BIOS interface declarations, including CAN structure and ID defines. |
28 | #include <bios.h> // BIOS interface declarations, including CAN structure and ID defines. |
| 28 | 29 | ||
| - | 30 | #include <string.h> //for memcpy |
|
| 29 | /* lib files */ |
31 | /* lib files */ |
| 30 | #include <bios.h> |
32 | #include <bios.h> |
| 31 |
|
33 | //#include <funcdefs/funcdefs.h> |
| - | 34 | ||
| - | 35 | #include <settings.h> |
|
| 32 | 36 | ||
| 33 | /*---------------------------------------------------------------------------- |
37 | /*---------------------------------------------------------------------------- |
| 34 | * Defines |
38 | * Defines |
| 35 | * -------------------------------------------------------------------------*/ |
39 | * -------------------------------------------------------------------------*/ |
| 36 | 40 | ||
| Line 42... | Line 46... | ||
| 42 | #define RGBLED_CMD_SETPROGRAM 0x03 /* Set program */ |
46 | #define RGBLED_CMD_SETPROGRAM 0x03 /* Set program */ |
| 43 | #define RGBLED_CMD_SETFADESPEED 0x04 /* Set fadespeed */ |
47 | #define RGBLED_CMD_SETFADESPEED 0x04 /* Set fadespeed */ |
| 44 | #define RGBLED_CMD_SETDIMSPEED 0x05 /* Set dimspeed */ |
48 | #define RGBLED_CMD_SETDIMSPEED 0x05 /* Set dimspeed */ |
| 45 | #define RGBLED_CMD_SETINTENS 0x10 /* Set intensity */ |
49 | #define RGBLED_CMD_SETINTENS 0x10 /* Set intensity */ |
| 46 | 50 | ||
| 47 | #define APP_TYPE |
51 | #define APP_TYPE CAN_APPTYPES_RGBLED |
| 48 | #define APP_VERSION 0x0001 |
52 | #define APP_VERSION 0x0001 |
| 49 | 53 | ||
| 50 | #define GROUP_ID 0x01L // FIXME |
54 | #define GROUP_ID 0x01L // FIXME |
| 51 | #define TRUE 1 |
55 | #define TRUE 1 |
| 52 | #define FALSE !TRUE |
56 | #define FALSE !TRUE |
| Line 100... | Line 104... | ||
| 100 | Color* programs[] PROGMEM = { |
104 | Color* programs[] PROGMEM = { |
| 101 | (Color*)pgm1, |
105 | (Color*)pgm1, |
| 102 | (Color*)pgm2, |
106 | (Color*)pgm2, |
| 103 | (Color*)pgm3 |
107 | (Color*)pgm3 |
| 104 | }; |
108 | }; |
| 105 | 109 | ||
| 106 | 110 | ||
| 107 | 111 | ||
| 108 | /*----------------------------------------------------------------------------- |
112 | /*----------------------------------------------------------------------------- |
| 109 | * Global variables |
113 | * Global variables |
| 110 | *---------------------------------------------------------------------------*/ |
114 | *---------------------------------------------------------------------------*/ |
| 111 | uint32_t timeStamp; //TimeStamp to keep track of time |
115 | uint32_t timeStamp; //TimeStamp to keep track of time |
| 112 | uint32_t dimStamp; //Timestamp to keep track of dimmer changes |
116 | uint32_t dimStamp; //Timestamp to keep track of dimmer changes |
| 113 | 117 | ||
| 114 | Color currColor; //Current color |
118 | Color currColor; //Current color |
| 115 | Color destColor; //Destination color |
119 | Color destColor; //Destination color |
| 116 | 120 | ||
| 117 | uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange. |
121 | uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange. |
| 118 | uint8_t dimSpeed; //Dimmer speed. Sets the speed of intensitychange. |
122 | uint8_t dimSpeed; //Dimmer speed. Sets the speed of intensitychange. |
| 119 | 123 | ||
| 120 | uint8_t currProgram; //This variable keeps track of what program is currently running. |
124 | uint8_t currProgram; //This variable keeps track of what program is currently running. |
| 121 | uint8_t programMode; //This keeps track of what mode we are currently using. 0: the program obeyes the current brightness, 1: the program is allowed to set it's own brightness |
125 | uint8_t programMode; //This keeps track of what mode we are currently using. 0: the program obeyes the current brightness, 1: the program is allowed to set it's own brightness |
| 122 | 126 | ||
| 123 |
|
127 | Can_Message_t txMsg; //Transmit message storage |
| 124 | 128 | ||
| 125 | Can_Message_t |
129 | volatile Can_Message_t rxMsg; // Message storage |
| 126 |
|
130 | volatile uint8_t rxMsgFull; // Synchronization flag |
| 127 | 131 | ||
| 128 | /*---------------------------------------------------------------------------- |
132 | /*---------------------------------------------------------------------------- |
| 129 | * Functions |
133 | * Functions |
| 130 | * -------------------------------------------------------------------------*/ |
134 | * -------------------------------------------------------------------------*/ |
| 131 | void updateLED(); |
135 | void updateLED(void); |
| 132 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright); |
136 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright); |
| 133 | void changeColor(int8_t amount, uint8_t* color); |
137 | void changeColor(int8_t amount, uint8_t* color); |
| 134 | void fade(uint8_t* current, uint8_t* destination); |
138 | void fade(uint8_t* current, uint8_t* destination); |
| 135 | 139 | ||
| 136 | 140 | ||
| 137 | /* Takes care |
141 | /* Takes care of incoming messages and parses them if this node is adressed */ |
| 138 | void can_receive(Can_Message_t *msg) |
142 | void can_receive(Can_Message_t *msg) |
| 139 | uint32_t act; |
- | |
| 140 |
|
143 | { // CAN callback function |
| 141 | if( ((msg->Id & CLASS_MASK) >> CLASS_MASK_BITS) == CLASS_ACT ){ |
- | |
| 142 | act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS; |
- | |
| 143 | act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS; |
- | |
| 144 | group = (act & ACT_GROUP_MASK) >> ACT_GROUP_BITS; |
- | |
| 145 | node = (act & ACT_NODE_MASK) >> ACT_NODE_BITS; |
- | |
| 146 | - | ||
| 147 | /* Check if it is command to control this servo */ |
- | |
| 148 | if(act_type == ACT_TYPE_RGBLED){ |
- | |
| 149 | if(group == GROUP_ID || node == NODE_ID){ |
- | |
| 150 |
|
144 | if (!rxMsgFull) { |
| 151 |
|
145 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
| 152 | } |
- | |
| 153 |
|
146 | rxMsgFull = 1; |
| 154 | } |
- | |
| 155 | } |
- | |
| 156 | } |
147 | } |
| 157 | } |
148 | } |
| 158 | 149 | ||
| 159 | 150 | ||
| 160 | /*----------------------------------------------------------------------------- |
151 | /*----------------------------------------------------------------------------- |
| 161 | * Main Program |
152 | * Main Program |
| 162 | *---------------------------------------------------------------------------*/ |
153 | *---------------------------------------------------------------------------*/ |
| 163 | int main(void) { |
154 | int main(void) { |
| 164 | 155 | ||
| Line 259... | Line 250... | ||
| 259 | 250 | ||
| 260 | } |
251 | } |
| 261 | 252 | ||
| 262 | } |
253 | } |
| 263 | 254 | ||
| - | 255 | ||
| 264 | /* |
256 | /* Check if any messages has been recieved */ |
| 265 | if( |
257 | if (rxMsgFull) { |
| 266 | /* This node that controls a RGB LED is adressed */ |
- | |
| 267 | if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETPROGRAM ){ |
- | |
| 268 |
|
258 | uint16_t acttype; |
| 269 | currProgram = rxMsg.Data.bytes[1]; |
- | |
| 270 | temp_adress = pgm_read_word(&programs[currProgram-1]); |
- | |
| 271 | }else if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETINTENS ){ |
- | |
| 272 |
|
259 | uint8_t rgbledid; |
| 273 | if (rxMsg.Data.bytes[1] == TRUE){ |
- | |
| 274 | currColor.Intens = rxMsg.Data.bytes[2]; |
- | |
| 275 | } |
- | |
| 276 | destColor.Intens = rxMsg.Data.bytes[2]; |
- | |
| 277 | if (rxMsg.Data.bytes[3] != 0){ |
- | |
| 278 | dimSpeed = rxMsg.Data.bytes[3]; |
- | |
| 279 |
|
260 | |
| 280 | currProgram = rxMsg.Data.bytes[1]; |
- | |
| 281 | temp_adress = pgm_read_word(&programs[currProgram-1]); |
- | |
| 282 |
|
261 | if ( ((rxMsg.Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS) == CAN_ACT){ |
| 283 |
|
262 | // Actuator package |
| 284 |
|
263 | acttype =(uint16_t)((rxMsg.Id & CAN_MASK_ACT_TYPE) >> CAN_SHIFT_ACT_TYPE); |
| 285 | if (rxMsg.Data.bytes[5] != 0){ |
- | |
| 286 | destColor.Intens = rxMsg.Data.bytes[5]; |
- | |
| 287 | } |
- | |
| 288 | if (rxMsg.Data.bytes[1] == TRUE){ |
- | |
| 289 | currColor = destColor; |
- | |
| 290 | } |
- | |
| 291 | - | ||
| 292 | if (rxMsg.Data.bytes[7] != 0){ |
- | |
| 293 |
|
264 | rgbledid = (uint8_t)((rxMsg.Id & CAN_MASK_ACT_ID) >> CAN_SHIFT_ACT_ID); |
| 294 | } |
- | |
| 295 | currProgram = 0; |
- | |
| 296 | 265 | ||
| - | 266 | if ((acttype == ACT_TYPE_RGBLED) && (rgbledid == THISRGBLEDID)) { |
|
| - | 267 | // This is a message for this rgbled |
|
| - | 268 | if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETPROGRAM ){ |
|
| - | 269 | /* Set a program! */ |
|
| - | 270 | currProgram = rxMsg.Data.bytes[1]; |
|
| - | 271 | temp_adress = pgm_read_word(&programs[currProgram-1]); |
|
| - | 272 | } else if ( rxMsg.Data.bytes[0] == RGBLED_CMD_SETINTENS ){ |
|
| - | 273 | /* Set intensity! */ |
|
| - | 274 | if (rxMsg.Data.bytes[1] == TRUE){ |
|
| - | 275 | currColor.Intens = rxMsg.Data.bytes[2]; |
|
| - | 276 | } |
|
| - | 277 | destColor.Intens = rxMsg.Data.bytes[2]; |
|
| - | 278 | if (rxMsg.Data.bytes[3] != 0){ |
|
| - | 279 | dimSpeed = rxMsg.Data.bytes[3]; |
|
| - | 280 | } |
|
| - | 281 | currProgram = rxMsg.Data.bytes[1]; |
|
| - | 282 | temp_adress = pgm_read_word(&programs[currProgram-1]); |
|
| - | 283 | } else if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETRGB) { |
|
| - | 284 | /* Set RGB-triplet! */ |
|
| - | 285 | reformatRGB(rxMsg.Data.bytes[2],rxMsg.Data.bytes[3],rxMsg.Data.bytes[4],0); |
|
| - | 286 | if (rxMsg.Data.bytes[5] != 0){ |
|
| - | 287 | destColor.Intens = rxMsg.Data.bytes[5]; |
|
| - | 288 | } |
|
| - | 289 | if (rxMsg.Data.bytes[1] == TRUE){ |
|
| - | 290 | currColor = destColor; |
|
| - | 291 | } |
|
| - | 292 | ||
| - | 293 | if (rxMsg.Data.bytes[7] != 0){ |
|
| - | 294 | fadeSpeed = rxMsg.Data.bytes[6] * 255 + rxMsg.Data.bytes[7]; |
|
| - | 295 | } |
|
| - | 296 | currProgram = 0; |
|
| - | 297 | } |
|
| - | 298 | ||
| - | 299 | } |
|
| 297 | } |
300 | } |
| 298 |
|
301 | rxMsgFull = 0; // |
| - | 302 | ||
| 299 | } |
303 | } |
| - | 304 | /* Done checking incoming packages */ |
|
| 300 | 305 | ||
| 301 | } |
306 | } |
| 302 | 307 | ||
| 303 | return 0; |
308 | return 0; |
| 304 | } |
309 | } |
| Line 307... | Line 312... | ||
| 307 | 312 | ||
| 308 | 313 | ||
| 309 | /************************************************************************************************ |
314 | /************************************************************************************************ |
| 310 | * updateLED updates the PWM counter values and thus changes the color of the RGBLED. * |
315 | * updateLED updates the PWM counter values and thus changes the color of the RGBLED. * |
| 311 | * It doesn't reculculate anything, except the values to write to the PWM registers. * |
316 | * It doesn't reculculate anything, except the values to write to the PWM registers. * |
| 312 | * TODO: Get rid of the tempcontainer! * |
317 | * TODO: Get rid of the tempcontainer! * |
| 313 | ************************************************************************************************/ |
318 | ************************************************************************************************/ |
| 314 | void updateLED(){ |
319 | void updateLED(){ |
| 315 | uint8_t tempcontainer; |
320 | uint8_t tempcontainer; |
| 316 | 321 | ||
| 317 | tempcontainer = currColor.R * currColor.Intens / 255; //I really want to skip this, don't know how yet though. |
322 | tempcontainer = currColor.R * currColor.Intens / 255; //I really want to skip this, don't know how yet though. |
| 318 | OCR1A = tempcontainer; |
323 | OCR1A = tempcontainer; |
| 319 | OCR0A = currColor.G * currColor.Intens / 255; |
324 | OCR0A = currColor.G * currColor.Intens / 255; |
| 320 | OCR0B = currColor.B * currColor.Intens / 255; |
325 | OCR0B = currColor.B * currColor.Intens / 255; |
| 321 | 326 | ||
| 322 | 327 | ||
| 323 | //Bara för debugcrap |
328 | //Bara för debugcrap |
| 324 | Can_Message_t txMsg; |
329 | Can_Message_t txMsg; |
| 325 | txMsg.Id = 0x55; |
330 | txMsg.Id = 0x55; |
| 326 | txMsg.RemoteFlag = 0; |
331 | txMsg.RemoteFlag = 0; |
| 327 | txMsg.ExtendedFlag = 1; |
332 | txMsg.ExtendedFlag = 1; |
| 328 | txMsg.DataLength = 4; |
333 | txMsg.DataLength = 4; |
| Line 338... | Line 343... | ||
| 338 | 343 | ||
| 339 | 344 | ||
| 340 | /************************************************************************************************ |
345 | /************************************************************************************************ |
| 341 | * reformatRGB sets a new target RGB triplet to fade to. It reformats it so that * |
346 | * reformatRGB sets a new target RGB triplet to fade to. It reformats it so that * |
| 342 | * the highest color always gets 0xff. Brightness is recalculated. * |
347 | * the highest color always gets 0xff. Brightness is recalculated. * |
| 343 | * if setBright is set to 0, the brightness will not be updated. * |
348 | * if setBright is set to 0, the brightness will not be updated. * |
| 344 | * TODO: Smarter 16-bit arithmetics would be nice. * |
349 | * TODO: Smarter 16-bit arithmetics would be nice. * |
| 345 | ************************************************************************************************/ |
350 | ************************************************************************************************/ |
| 346 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright){ |
351 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright){ |
| 347 | uint8_t maxvalue; |
352 | uint8_t maxvalue; |
| 348 | uint16_t tempcontainer; |
353 | uint16_t tempcontainer; |
| 349 | 354 | ||
| Line 367... | Line 372... | ||
| 367 | } |
372 | } |
| 368 | } |
373 | } |
| 369 | 374 | ||
| 370 | 375 | ||
| 371 | /************************************************************************************************ |
376 | /************************************************************************************************ |
| 372 | * fade takes two pointers, current and destination and fades current * |
377 | * fade takes two pointers, current and destination and fades current * |
| 373 | * one tick closer to destination. * |
378 | * one tick closer to destination. * |
| 374 | ************************************************************************************************/ |
379 | ************************************************************************************************/ |
| 375 | void fade(uint8_t* current, uint8_t* destination){ |
380 | void fade(uint8_t* current, uint8_t* destination){ |
| 376 | if (*current > *destination){ |
381 | if (*current > *destination){ |
| 377 | *current = *current - 1; |
382 | *current = *current - 1; |
| 378 | } else if (*current < *destination){ |
383 | } else if (*current < *destination){ |
| Line 385... | Line 390... | ||
| 385 | * changeColor changes a color accord to amount. -127>127. * |
390 | * changeColor changes a color accord to amount. -127>127. * |
| 386 | * color is either dest och curr R,G,B. * |
391 | * color is either dest och curr R,G,B. * |
| 387 | * Maybe TODO: If red is already 255 and we want to increase it, decrease the other ones. * |
392 | * Maybe TODO: If red is already 255 and we want to increase it, decrease the other ones. * |
| 388 | ************************************************************************************************/ |
393 | ************************************************************************************************/ |
| 389 | void changeColor(int8_t amount, uint8_t* color){ |
394 | void changeColor(int8_t amount, uint8_t* color){ |
| 390 | if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color |
395 | if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color |
| 391 | *color = *color + amount; |
396 | *color = *color + amount; |
| 392 | } |
397 | } |
| 393 | 398 | ||