Subversion Repositories HomeAutomation

Rev

Rev 413 | Rev 563 | Go to most recent revision | Show entire file | Regard 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
#include <funcdefs/funcdefs.h>
33
//#include <funcdefs/funcdefs.h>
-
 
34
 
-
 
35
#include <settings.h>
32
 
36
 
33
/*----------------------------------------------------------------------------
37
/*----------------------------------------------------------------------------
34
 * Defines
38
 * Defines
35
 * -------------------------------------------------------------------------*/
39
 * -------------------------------------------------------------------------*/
36
 
40
 
37
//Working on implementing all of these...
41
//Working on implementing all of these...
38
 
42
 
39
#define RGBLED_CMD_STATUS           0x00    /* Get status */
43
#define RGBLED_CMD_STATUS           0x00    /* Get status */
40
#define RGBLED_CMD_SETRGB           0x01    /* Set RGB Triplet */
44
#define RGBLED_CMD_SETRGB           0x01    /* Set RGB Triplet */
41
#define RGBLED_CMD_CHANGERGB        0x02    /* Change RGB +/- */
45
#define RGBLED_CMD_CHANGERGB        0x02    /* Change RGB +/- */
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    0xf001
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
53
 
57
 
54
 
58
 
55
/*----------------------------------------------------------------------------
59
/*----------------------------------------------------------------------------
56
 * Ehm, struct?
60
 * Ehm, struct?
57
 * -------------------------------------------------------------------------*/
61
 * -------------------------------------------------------------------------*/
58
typedef struct
62
typedef struct
59
{
63
{
60
    uint8_t R;
64
    uint8_t R;
61
    uint8_t G;
65
    uint8_t G;
62
    uint8_t B;
66
    uint8_t B;
Line 118... Line 122...
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
uint8_t msg_received = FALSE;
127
Can_Message_t txMsg;    //Transmit message storage
124
 
128
 
125
Can_Message_t txMsg;
129
volatile Can_Message_t rxMsg; // Message storage
126
Can_Message_t rxMsg;
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 about incoming messages and parses them if this node is adressed */
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
    uint8_t m, act_type, group, node;
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
                for(m=0;m<msg->DataLength;m++){
144
    if (!rxMsgFull) {
151
                    rxMsg.Data.bytes[m] = msg->Data.bytes[m];
145
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
152
                }
-
 
153
                msg_received = TRUE;
146
        rxMsgFull = 1;
154
            }
-
 
155
        }
-
 
156
    }
147
    }
157
}
148
}
158
 
149
 
159
 
150
 
160
/*-----------------------------------------------------------------------------
151
/*-----------------------------------------------------------------------------
Line 227... Line 218...
227
        /* Time to fade intensity? */
218
        /* Time to fade intensity? */
228
        if (Timebase_CurrentTime() - dimStamp >= dimSpeed) {
219
        if (Timebase_CurrentTime() - dimStamp >= dimSpeed) {
229
            fade(&currColor.Intens, &destColor.Intens);
220
            fade(&currColor.Intens, &destColor.Intens);
230
            dimStamp = Timebase_CurrentTime();
221
            dimStamp = Timebase_CurrentTime();
231
            updateLED();
222
            updateLED();
232
        }
223
        }
233
 
224
 
234
 
225
 
235
        /* Program management */
226
        /* Program management */
236
        if (currProgram != 0) { //Check if a program is currently running
227
        if (currProgram != 0) { //Check if a program is currently running
237
            if ((currColor.R == destColor.R)&(currColor.G == destColor.G)&(currColor.B == destColor.B)&(currColor.Intens == destColor.Intens)){//Is the current sequence finished?
228
            if ((currColor.R == destColor.R)&(currColor.G == destColor.G)&(currColor.B == destColor.B)&(currColor.Intens == destColor.Intens)){//Is the current sequence finished?
Line 239... Line 230...
239
                if ((pgm_read_byte(temp_adress)==0)&(pgm_read_byte(temp_adress+1)==0)&(pgm_read_byte(temp_adress+2)==0)){
230
                if ((pgm_read_byte(temp_adress)==0)&(pgm_read_byte(temp_adress+1)==0)&(pgm_read_byte(temp_adress+2)==0)){
240
                    //The program has ended! Shall we loop it?
231
                    //The program has ended! Shall we loop it?
241
                    if (pgm_read_byte(temp_adress+3)==0){
232
                    if (pgm_read_byte(temp_adress+3)==0){
242
                        //Yes, let's loop it
233
                        //Yes, let's loop it
243
                        temp_adress = pgm_read_word(&programs[currProgram-1]);
234
                        temp_adress = pgm_read_word(&programs[currProgram-1]);
244
                    } else {
235
                    } else {
245
                        //No, halt!
236
                        //No, halt!
246
                        currProgram = 0;
237
                        currProgram = 0;
247
                    }
238
                    }
248
                } else {
239
                } else {
249
                    //The program has not ended, let's get the next RGB-triplet!
240
                    //The program has not ended, let's get the next RGB-triplet!
Line 253... Line 244...
253
                    if (programMode == 1){
244
                    if (programMode == 1){
254
                        destColor.Intens = pgm_read_byte(temp_adress + 3);
245
                        destColor.Intens = pgm_read_byte(temp_adress + 3);
255
                    }
246
                    }
256
 
247
 
257
                    temp_adress = temp_adress + 4;             
248
                    temp_adress = temp_adress + 4;             
258
                }
249
                }
259
 
250
 
260
            }
251
            }
261
 
252
 
262
        }
253
        }
-
 
254
 
263
 
255
 
264
        /* check if any messages have been received */
256
        /* Check if any messages has been recieved */
265
        if(msg_received){
257
        if (rxMsgFull) {
-
 
258
            uint16_t acttype;
-
 
259
            uint8_t rgbledid;
-
 
260
       
-
 
261
            if ( ((rxMsg.Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS) == CAN_ACT){
-
 
262
                // Actuator package
-
 
263
                acttype =(uint16_t)((rxMsg.Id & CAN_MASK_ACT_TYPE) >> CAN_SHIFT_ACT_TYPE);
-
 
264
                rgbledid = (uint8_t)((rxMsg.Id & CAN_MASK_ACT_ID) >> CAN_SHIFT_ACT_ID);
-
 
265
               
-
 
266
                if ((acttype == ACT_TYPE_RGBLED) && (rgbledid == THISRGBLEDID)) {
266
            /* This node that controls a RGB LED is adressed */
267
                    // This is a message for this rgbled
267
            if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETPROGRAM ){
268
                    if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETPROGRAM ){
268
                /* Set a program! */
269
                        /* Set a program! */
269
                currProgram = rxMsg.Data.bytes[1];
270
                        currProgram = rxMsg.Data.bytes[1];
270
                temp_adress = pgm_read_word(&programs[currProgram-1]);             
271
                        temp_adress = pgm_read_word(&programs[currProgram-1]);             
271
            }else if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETINTENS ){
272
                    } else if ( rxMsg.Data.bytes[0] == RGBLED_CMD_SETINTENS ){
272
                /* Set intensity! */
273
                        /* Set intensity! */
273
                if (rxMsg.Data.bytes[1] == TRUE){
274
                        if (rxMsg.Data.bytes[1] == TRUE){
274
                    currColor.Intens = rxMsg.Data.bytes[2];                
275
                            currColor.Intens = rxMsg.Data.bytes[2];                
275
                }
276
                        }
Line 285... Line 286...
285
                if (rxMsg.Data.bytes[5] != 0){
286
                        if (rxMsg.Data.bytes[5] != 0){
286
                    destColor.Intens = rxMsg.Data.bytes[5];
287
                            destColor.Intens = rxMsg.Data.bytes[5];
287
                }
288
                        }
288
                if (rxMsg.Data.bytes[1] == TRUE){
289
                        if (rxMsg.Data.bytes[1] == TRUE){
289
                    currColor = destColor;
290
                            currColor = destColor;
290
                }
291
                        }
291
 
292
 
292
                if (rxMsg.Data.bytes[7] != 0){
293
                        if (rxMsg.Data.bytes[7] != 0){
293
                    fadeSpeed = rxMsg.Data.bytes[6] * 255 + rxMsg.Data.bytes[7];
294
                            fadeSpeed = rxMsg.Data.bytes[6] * 255 + rxMsg.Data.bytes[7];
294
                }
295
                        }
295
                currProgram = 0;
296
                        currProgram = 0;               
296
               
-
 
297
            }
297
                    }
-
 
298
                   
-
 
299
                }
-
 
300
            }
298
            msg_received = FALSE; //Let's clear the flag yes.
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 318... Line 323...
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 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