Subversion Repositories HomeAutomation

Rev

Rev 413 | Rev 563 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 413 Rev 546
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 :)
12
 */
12
 */
13
 
13
 
14
 
14
 
15
/*-----------------------------------------------------------------------------
15
/*-----------------------------------------------------------------------------
16
 * Includes
16
 * Includes
17
 *---------------------------------------------------------------------------*/
17
 *---------------------------------------------------------------------------*/
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;
63
    uint8_t Intens;
67
    uint8_t Intens;
64
} Color;
68
} Color;
65
 
69
 
66
/*-----------------------------------------------------------------------------
70
/*-----------------------------------------------------------------------------
67
 * Programs! There will be a better way to add programs here.. some day.
71
 * Programs! There will be a better way to add programs here.. some day.
68
 *---------------------------------------------------------------------------*/
72
 *---------------------------------------------------------------------------*/
69
 
73
 
70
//Special combinations that means something special
74
//Special combinations that means something special
71
//The combinations are useless for lighting anyway
75
//The combinations are useless for lighting anyway
72
//So we might as well use them!
76
//So we might as well use them!
73
//0x00, 0x00, 0x00, 0x00 means end of program, keep looping it
77
//0x00, 0x00, 0x00, 0x00 means end of program, keep looping it
74
//0x00, 0x00, 0x00, 0x01 means end of program, halt 
78
//0x00, 0x00, 0x00, 0x01 means end of program, halt 
75
 
79
 
76
Color pgm1[] PROGMEM = {
80
Color pgm1[] PROGMEM = {
77
{0xff,0x00,0x00,0x64},
81
{0xff,0x00,0x00,0x64},
78
{0xff,0xff,0x00,0x10},
82
{0xff,0xff,0x00,0x10},
79
{0x00,0xff,0x00,0xa0},
83
{0x00,0xff,0x00,0xa0},
80
{0x00,0xff,0xff,0x7f},
84
{0x00,0xff,0xff,0x7f},
81
{0x00,0x00,0xff,0x7f},
85
{0x00,0x00,0xff,0x7f},
82
{0xff,0x00,0xff,0x40},
86
{0xff,0x00,0xff,0x40},
83
{0x00,0x00,0x00,0x00}
87
{0x00,0x00,0x00,0x00}
84
};
88
};
85
 
89
 
86
Color pgm2[] PROGMEM = {
90
Color pgm2[] PROGMEM = {
87
{0xff,0x00,0x00,0x00},
91
{0xff,0x00,0x00,0x00},
88
{0xff,0xff,0x00,0x00},
92
{0xff,0xff,0x00,0x00},
89
{0xff,0xff,0xff,0x00},
93
{0xff,0xff,0xff,0x00},
90
{0xa0,0x00,0xa0,0x00},
94
{0xa0,0x00,0xa0,0x00},
91
{0x00,0x00,0xff,0x00},
95
{0x00,0x00,0xff,0x00},
92
{0x00,0x00,0x00,0x00}
96
{0x00,0x00,0x00,0x00}
93
};
97
};
94
 
98
 
95
Color pgm3[] PROGMEM = {
99
Color pgm3[] PROGMEM = {
96
{0xff,0xff,0xff,0x00},
100
{0xff,0xff,0xff,0x00},
97
{0x00,0x00,0x00,0x01}
101
{0x00,0x00,0x00,0x01}
98
};
102
};
99
 
103
 
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
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
/*-----------------------------------------------------------------------------
161
 * Main Program
152
 * Main Program
162
 *---------------------------------------------------------------------------*/
153
 *---------------------------------------------------------------------------*/
163
int main(void) {
154
int main(void) {
164
 
155
 
165
//Setting up OC1A, 16 bit counter used as 8 bit.
156
//Setting up OC1A, 16 bit counter used as 8 bit.
166
    TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
157
    TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
167
    TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS12); /* Timer uses main system clock with ? prescale */
158
    TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS12); /* Timer uses main system clock with ? prescale */
168
    ICR1 = 256; //8 bit precision to match the other two counters.
159
    ICR1 = 256; //8 bit precision to match the other two counters.
169
    OCR1A = 0;  //0% as standard
160
    OCR1A = 0;  //0% as standard
170
    DDRB |= (1<<PB1); /* Enable pin as output */
161
    DDRB |= (1<<PB1); /* Enable pin as output */
171
//Setting up OC0A
162
//Setting up OC0A
172
    TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */
163
    TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */
173
    TCCR0B |= (1<<CS02); /* Prescaler updating now */
164
    TCCR0B |= (1<<CS02); /* Prescaler updating now */
174
    OCR0A = 0;  //0% standard
165
    OCR0A = 0;  //0% standard
175
    DDRD |= (1<<PD6);
166
    DDRD |= (1<<PD6);
176
//Setting up OC0B
167
//Setting up OC0B
177
    TCCR0A |= (1<<COM0B1);
168
    TCCR0A |= (1<<COM0B1);
178
    OCR0B = 0;
169
    OCR0B = 0;
179
    DDRD |= (1<<PD5);
170
    DDRD |= (1<<PD5);
180
 
171
 
181
//Initialize variables
172
//Initialize variables
182
    fadeSpeed = 10;
173
    fadeSpeed = 10;
183
    dimSpeed = 10;
174
    dimSpeed = 10;
184
    timeStamp = 0;
175
    timeStamp = 0;
185
    dimStamp = 0;
176
    dimStamp = 0;
186
 
177
 
187
    reformatRGB(0x00,0x00,0x00,1);
178
    reformatRGB(0x00,0x00,0x00,1);
188
    destColor.Intens = 50;
179
    destColor.Intens = 50;
189
    currColor = destColor;
180
    currColor = destColor;
190
 
181
 
191
//Just temporary for now, they keep track of some kind of demo-program.
182
//Just temporary for now, they keep track of some kind of demo-program.
192
    currProgram = 1;
183
    currProgram = 1;
193
    programMode = 0;
184
    programMode = 0;
194
    uint16_t temp_adress = pgm_read_word(&programs[currProgram-1]); //Get adress for program 0
185
    uint16_t temp_adress = pgm_read_word(&programs[currProgram-1]); //Get adress for program 0
195
 
186
 
196
    sei();
187
    sei();
197
 
188
 
198
    BIOS_CanCallback = &can_receive;
189
    BIOS_CanCallback = &can_receive;
199
 
190
 
200
//Send a message that we are alive!
191
//Send a message that we are alive!
201
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
192
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
202
    txMsg.DataLength = 4;
193
    txMsg.DataLength = 4;
203
    txMsg.RemoteFlag = 0;
194
    txMsg.RemoteFlag = 0;
204
    txMsg.ExtendedFlag = 1;
195
    txMsg.ExtendedFlag = 1;
205
    txMsg.Data.words[0] = APP_TYPE;
196
    txMsg.Data.words[0] = APP_TYPE;
206
    txMsg.Data.words[1] = APP_VERSION;
197
    txMsg.Data.words[1] = APP_VERSION;
207
    BIOS_CanSend(&txMsg);
198
    BIOS_CanSend(&txMsg);
208
 
199
 
209
//Lets get timebase up and running
200
//Lets get timebase up and running
210
    Timebase_Init();
201
    Timebase_Init();
211
 
202
 
212
 
203
 
213
    /* main loop */
204
    /* main loop */
214
    while (1) {
205
    while (1) {
215
       
206
       
216
        /* Time to fade color? */
207
        /* Time to fade color? */
217
        if ((Timebase_CurrentTime() - timeStamp) >= fadeSpeed) {
208
        if ((Timebase_CurrentTime() - timeStamp) >= fadeSpeed) {
218
            fade(&currColor.R, &destColor.R);          
209
            fade(&currColor.R, &destColor.R);          
219
            fade(&currColor.G, &destColor.G);
210
            fade(&currColor.G, &destColor.G);
220
            fade(&currColor.B, &destColor.B);
211
            fade(&currColor.B, &destColor.B);
221
 
212
 
222
            timeStamp = Timebase_CurrentTime();
213
            timeStamp = Timebase_CurrentTime();
223
            updateLED();
214
            updateLED();
224
        }
215
        }
225
 
216
 
226
 
217
 
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?
238
                //Now we have to check if the program is finished and if we should loop
229
                //Now we have to check if the program is finished and if we should loop
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!
250
                    destColor.R = pgm_read_byte(temp_adress);
241
                    destColor.R = pgm_read_byte(temp_adress);
251
                    destColor.G = pgm_read_byte(temp_adress + 1);
242
                    destColor.G = pgm_read_byte(temp_adress + 1);
252
                    destColor.B = pgm_read_byte(temp_adress + 2);
243
                    destColor.B = pgm_read_byte(temp_adress + 2);
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
                        }
276
                destColor.Intens = rxMsg.Data.bytes[2];
277
                        destColor.Intens = rxMsg.Data.bytes[2];
277
                if (rxMsg.Data.bytes[3] != 0){
278
                        if (rxMsg.Data.bytes[3] != 0){
278
                    dimSpeed = rxMsg.Data.bytes[3];
279
                            dimSpeed = rxMsg.Data.bytes[3];
279
                }
280
                        }
280
                currProgram = rxMsg.Data.bytes[1];
281
                        currProgram = rxMsg.Data.bytes[1];
281
                temp_adress = pgm_read_word(&programs[currProgram-1]);
282
                        temp_adress = pgm_read_word(&programs[currProgram-1]);
282
            } else if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETRGB) {
283
                    } else if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETRGB) {
283
                /* Set RGB-triplet! */
284
                        /* Set RGB-triplet! */
284
                reformatRGB(rxMsg.Data.bytes[2],rxMsg.Data.bytes[3],rxMsg.Data.bytes[4],0);
285
                        reformatRGB(rxMsg.Data.bytes[2],rxMsg.Data.bytes[3],rxMsg.Data.bytes[4],0);
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
}
305
 
310
 
306
 
311
 
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;
329
    txMsg.Data.bytes[0] = currColor.R;
334
    txMsg.Data.bytes[0] = currColor.R;
330
    txMsg.Data.bytes[1] = currColor.G;
335
    txMsg.Data.bytes[1] = currColor.G;
331
    txMsg.Data.bytes[2] = currColor.B;
336
    txMsg.Data.bytes[2] = currColor.B;
332
    txMsg.Data.bytes[3] = currColor.Intens;
337
    txMsg.Data.bytes[3] = currColor.Intens;
333
 
338
 
334
    BIOS_CanSend(&txMsg);
339
    BIOS_CanSend(&txMsg);
335
 
340
 
336
}
341
}
337
 
342
 
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
 
350
    if ((R>=G)&(R>=B)){
355
    if ((R>=G)&(R>=B)){
351
        maxvalue = R;
356
        maxvalue = R;
352
    } else if ((G>=R)&(G>=B)){
357
    } else if ((G>=R)&(G>=B)){
353
        maxvalue = G;
358
        maxvalue = G;
354
    } else {
359
    } else {
355
        maxvalue = B;
360
        maxvalue = B;
356
    }
361
    }
357
 
362
 
358
    tempcontainer = R * 255;
363
    tempcontainer = R * 255;
359
    destColor.R = tempcontainer/maxvalue;
364
    destColor.R = tempcontainer/maxvalue;
360
    tempcontainer = G * 255;
365
    tempcontainer = G * 255;
361
    destColor.G = tempcontainer/maxvalue;
366
    destColor.G = tempcontainer/maxvalue;
362
    tempcontainer = B * 255;
367
    tempcontainer = B * 255;
363
    destColor.B = tempcontainer/maxvalue;
368
    destColor.B = tempcontainer/maxvalue;
364
 
369
 
365
    if (setBright){
370
    if (setBright){
366
        destColor.Intens = maxvalue;
371
        destColor.Intens = maxvalue;
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){
379
        *current = *current + 1;
384
        *current = *current + 1;
380
    }
385
    }
381
}
386
}
382
 
387
 
383
 
388
 
384
/************************************************************************************************
389
/************************************************************************************************
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
 
394
 
399