Subversion Repositories HomeAutomation

Rev

Rev 343 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 343 Rev 411
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
 
-
 
24
#include <drivers/timer/timebase.h>
-
 
25
 
-
 
26
#include <config.h> // All configuration parameters
-
 
27
#include <bios.h>   // BIOS interface declarations, including CAN structure and ID defines.
23
 
28
 
24
/* lib files */
29
/* lib files */
25
#include <bios.h>
30
#include <bios.h>
26
#include <noddan_funcdefs.h>
31
#include <funcdefs/funcdefs.h>
27
 
32
 
28
/*----------------------------------------------------------------------------
33
/*----------------------------------------------------------------------------
29
 * Defines
34
 * Defines
30
 * -------------------------------------------------------------------------*/
35
 * -------------------------------------------------------------------------*/
31
 
36
 
32
//Working on implementing all of these...
37
//Working on implementing all of these...
33
 
38
 
34
#define RGBLED_CMD_STATUS           0x00    /* Get status */
39
#define RGBLED_CMD_STATUS           0x00    /* Get status */
35
#define RGBLED_CMD_SETRGB           0x01    /* Set RGB Triplet */
40
#define RGBLED_CMD_SETRGB           0x01    /* Set RGB Triplet */
36
#define RGBLED_CMD_CHANGERGB        0x02    /* Change RGB +/- */
41
#define RGBLED_CMD_CHANGERGB        0x02    /* Change RGB +/- */
37
#define RGBLED_CMD_SETPROGRAM       0x03  /* Set program */
42
#define RGBLED_CMD_SETPROGRAM       0x03  /* Set program */
38
#define RGBLED_CMD_SETFADESPEED     0x04  /* Set fadespeed */
43
#define RGBLED_CMD_SETFADESPEED     0x04  /* Set fadespeed */
39
#define RGBLED_CMD_SETDIMSPEED      0x05  /* Set dimspeed */
44
#define RGBLED_CMD_SETDIMSPEED      0x05  /* Set dimspeed */
40
#define RGBLED_CMD_SETINTENS        0x10  /* Set intensity */
45
#define RGBLED_CMD_SETINTENS        0x10  /* Set intensity */
41
 
46
 
42
#define APP_TYPE    0xf001
47
#define APP_TYPE    0xf001
43
#define APP_VERSION 0x0001
48
#define APP_VERSION 0x0001
44
 
49
 
45
#define GROUP_ID    0x01L // FIXME
50
#define GROUP_ID    0x01L // FIXME
46
#define TRUE 1
51
#define TRUE 1
47
#define FALSE !TRUE
52
#define FALSE !TRUE
48
 
53
 
49
 
54
 
50
/*----------------------------------------------------------------------------
55
/*----------------------------------------------------------------------------
51
 * Ehm, struct?
56
 * Ehm, struct?
52
 * -------------------------------------------------------------------------*/
57
 * -------------------------------------------------------------------------*/
53
typedef struct
58
typedef struct
54
{
59
{
55
    uint8_t R;
60
    uint8_t R;
56
    uint8_t G;
61
    uint8_t G;
57
    uint8_t B;
62
    uint8_t B;
58
    uint8_t Intens;
63
    uint8_t Intens;
59
} Color;
64
} Color;
60
 
65
 
61
/*-----------------------------------------------------------------------------
66
/*-----------------------------------------------------------------------------
62
 * Programs! There will be a better way to add programs here.. some day.
67
 * Programs! There will be a better way to add programs here.. some day.
63
 *---------------------------------------------------------------------------*/
68
 *---------------------------------------------------------------------------*/
64
 
69
 
65
//Special combinations that means something special
70
//Special combinations that means something special
66
//The combinations are useless for lighting anyway
71
//The combinations are useless for lighting anyway
67
//So we might as well use them!
72
//So we might as well use them!
68
//0x00, 0x00, 0x00, 0x00 means end of program, keep looping it
73
//0x00, 0x00, 0x00, 0x00 means end of program, keep looping it
69
//0x00, 0x00, 0x00, 0x01 means end of program, halt 
74
//0x00, 0x00, 0x00, 0x01 means end of program, halt 
70
 
75
 
71
Color pgm1[] PROGMEM = {
76
Color pgm1[] PROGMEM = {
72
{0xff,0x00,0x00,0x64},
77
{0xff,0x00,0x00,0x64},
73
{0xff,0xff,0x00,0x10},
78
{0xff,0xff,0x00,0x10},
74
{0x00,0xff,0x00,0xa0},
79
{0x00,0xff,0x00,0xa0},
75
{0x00,0xff,0xff,0x7f},
80
{0x00,0xff,0xff,0x7f},
76
{0x00,0x00,0xff,0x7f},
81
{0x00,0x00,0xff,0x7f},
77
{0xff,0x00,0xff,0x40},
82
{0xff,0x00,0xff,0x40},
78
{0x00,0x00,0x00,0x00}
83
{0x00,0x00,0x00,0x00}
79
};
84
};
80
 
85
 
81
Color pgm2[] PROGMEM = {
86
Color pgm2[] PROGMEM = {
82
{0xff,0x00,0x00,0x00},
87
{0xff,0x00,0x00,0x00},
83
{0xff,0xff,0x00,0x00},
88
{0xff,0xff,0x00,0x00},
84
{0xff,0xff,0xff,0x00},
89
{0xff,0xff,0xff,0x00},
85
{0xa0,0x00,0xa0,0x00},
90
{0xa0,0x00,0xa0,0x00},
86
{0x00,0x00,0xff,0x00},
91
{0x00,0x00,0xff,0x00},
87
{0x00,0x00,0x00,0x00}
92
{0x00,0x00,0x00,0x00}
88
};
93
};
89
 
94
 
90
Color pgm3[] PROGMEM = {
95
Color pgm3[] PROGMEM = {
91
{0xff,0xff,0xff,0x00},
96
{0xff,0xff,0xff,0x00},
92
{0x00,0x00,0x00,0x01}
97
{0x00,0x00,0x00,0x01}
93
};
98
};
94
 
99
 
95
Color* programs[] PROGMEM = {
100
Color* programs[] PROGMEM = {
96
(Color*)pgm1,
101
(Color*)pgm1,
97
(Color*)pgm2,
102
(Color*)pgm2,
98
(Color*)pgm3
103
(Color*)pgm3
99
};
104
};
100
 
105
 
101
 
106
 
102
 
107
 
103
/*-----------------------------------------------------------------------------
108
/*-----------------------------------------------------------------------------
104
 * Global variables
109
 * Global variables
105
 *---------------------------------------------------------------------------*/
110
 *---------------------------------------------------------------------------*/
106
uint32_t timeStamp; //TimeStamp to keep track of time
111
uint32_t timeStamp; //TimeStamp to keep track of time
107
uint32_t dimStamp;  //Timestamp to keep track of dimmer changes
112
uint32_t dimStamp;  //Timestamp to keep track of dimmer changes
108
 
113
 
109
Color currColor;    //Current color
114
Color currColor;    //Current color
110
Color destColor;    //Destination color
115
Color destColor;    //Destination color
111
 
116
 
112
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange.
117
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange.
113
uint8_t dimSpeed;       //Dimmer speed. Sets the speed of intensitychange.
118
uint8_t dimSpeed;       //Dimmer speed. Sets the speed of intensitychange.
114
 
119
 
115
uint8_t currProgram;    //This variable keeps track of what program is currently running. 
120
uint8_t currProgram;    //This variable keeps track of what program is currently running. 
116
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
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
117
 
122
 
118
uint8_t msg_received = FALSE;
123
uint8_t msg_received = FALSE;
119
 
124
 
120
Can_Message_t txMsg;
125
Can_Message_t txMsg;
121
Can_Message_t rxMsg;
126
Can_Message_t rxMsg;
122
 
127
 
123
/*----------------------------------------------------------------------------
128
/*----------------------------------------------------------------------------
124
 * Functions
129
 * Functions
125
 * -------------------------------------------------------------------------*/
130
 * -------------------------------------------------------------------------*/
126
void updateLED();
131
void updateLED();
127
void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright);
132
void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright);
128
void changeColor(int8_t amount, uint8_t* color);
133
void changeColor(int8_t amount, uint8_t* color);
129
void fade(uint8_t* current, uint8_t* destination);
134
void fade(uint8_t* current, uint8_t* destination);
130
 
135
 
131
 
136
 
132
/* Takes care about incoming messages and parses them if this node is adressed */
137
/* Takes care about incoming messages and parses them if this node is adressed */
133
void can_receive(Can_Message_t *msg){
138
void can_receive(Can_Message_t *msg){
134
    uint32_t act;
139
    uint32_t act;
135
    uint8_t m, act_type, group, node;
140
    uint8_t m, act_type, group, node;
136
    if( ((msg->Id & CLASS_MASK) >> CLASS_MASK_BITS) == CLASS_ACT ){
141
    if( ((msg->Id & CLASS_MASK) >> CLASS_MASK_BITS) == CLASS_ACT ){
137
        act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
142
        act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
138
        act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS;
143
        act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS;
139
        group = (act & ACT_GROUP_MASK) >> ACT_GROUP_BITS;
144
        group = (act & ACT_GROUP_MASK) >> ACT_GROUP_BITS;
140
        node = (act & ACT_NODE_MASK) >> ACT_NODE_BITS;
145
        node = (act & ACT_NODE_MASK) >> ACT_NODE_BITS;
141
 
146
 
142
        /* Check if it is command to control this servo */
147
        /* Check if it is command to control this servo */
143
        if(act_type == ACT_TYPE_RGB){
148
        if(act_type == ACT_TYPE_RGBLED){
144
            if(group == GROUP_ID || node == NODE_ID){
149
            if(group == GROUP_ID || node == NODE_ID){
145
                for(m=0;m<msg->DataLength;m++){
150
                for(m=0;m<msg->DataLength;m++){
146
                    rxMsg.Data.bytes[m] = msg->Data.bytes[m];
151
                    rxMsg.Data.bytes[m] = msg->Data.bytes[m];
147
                }
152
                }
148
                msg_received = TRUE;
153
                msg_received = TRUE;
149
            }
154
            }
150
        }
155
        }
151
    }
156
    }
152
}
157
}
153
 
158
 
154
 
159
 
155
/*-----------------------------------------------------------------------------
160
/*-----------------------------------------------------------------------------
156
 * Main Program
161
 * Main Program
157
 *---------------------------------------------------------------------------*/
162
 *---------------------------------------------------------------------------*/
158
int main(void) {
163
int main(void) {
159
 
164
 
160
//Setting up OC1A, 16 bit counter used as 8 bit.
165
//Setting up OC1A, 16 bit counter used as 8 bit.
161
    TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
166
    TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
162
    TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS12); /* Timer uses main system clock with ? prescale */
167
    TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS12); /* Timer uses main system clock with ? prescale */
163
    ICR1 = 256; //8 bit precision to match the other two counters.
168
    ICR1 = 256; //8 bit precision to match the other two counters.
164
    OCR1A = 0;  //0% as standard
169
    OCR1A = 0;  //0% as standard
165
    DDRB |= (1<<PB1); /* Enable pin as output */
170
    DDRB |= (1<<PB1); /* Enable pin as output */
166
//Setting up OC0A
171
//Setting up OC0A
167
    TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */
172
    TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */
168
    TCCR0B |= (1<<CS02); /* Prescaler updating now */
173
    TCCR0B |= (1<<CS02); /* Prescaler updating now */
169
    OCR0A = 0;  //0% standard
174
    OCR0A = 0;  //0% standard
170
    DDRD |= (1<<PD6);
175
    DDRD |= (1<<PD6);
171
//Setting up OC0B
176
//Setting up OC0B
172
    TCCR0A |= (1<<COM0B1);
177
    TCCR0A |= (1<<COM0B1);
173
    OCR0B = 0;
178
    OCR0B = 0;
174
    DDRD |= (1<<PD5);
179
    DDRD |= (1<<PD5);
175
 
180
 
176
//Initialize variables
181
//Initialize variables
177
    fadeSpeed = 10;
182
    fadeSpeed = 10;
178
    dimSpeed = 10;
183
    dimSpeed = 10;
179
    timeStamp = 0;
184
    timeStamp = 0;
180
    dimStamp = 0;
185
    dimStamp = 0;
181
 
186
 
182
    reformatRGB(0x00,0x00,0x00,1);
187
    reformatRGB(0x00,0x00,0x00,1);
183
    destColor.Intens = 100;
188
    destColor.Intens = 50;
184
    currColor = destColor;
189
    currColor = destColor;
185
 
190
 
186
//Just temporary for now, they keep track of some kind of demo-program.
191
//Just temporary for now, they keep track of some kind of demo-program.
187
    currProgram = 1;
192
    currProgram = 1;
188
    programMode = 0;
193
    programMode = 0;
189
    uint16_t temp_adress = pgm_read_word(&programs[currProgram-1]); //Get adress for program 0
194
    uint16_t temp_adress = pgm_read_word(&programs[currProgram-1]); //Get adress for program 0
190
 
195
 
191
    sei();
196
    sei();
192
 
197
 
193
    bios->can_callback = &can_receive;
198
    BIOS_CanCallback = &can_receive;
194
 
199
 
195
//Send a message that we are alive!
200
//Send a message that we are alive!
196
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
201
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
197
    txMsg.DataLength = 4;
202
    txMsg.DataLength = 4;
198
    txMsg.RemoteFlag = 0;
203
    txMsg.RemoteFlag = 0;
199
    txMsg.ExtendedFlag = 1;
204
    txMsg.ExtendedFlag = 1;
200
    txMsg.Data.words[0] = APP_TYPE;
205
    txMsg.Data.words[0] = APP_TYPE;
201
    txMsg.Data.words[1] = APP_VERSION;
206
    txMsg.Data.words[1] = APP_VERSION;
202
    bios->can_send(&txMsg);
207
    BIOS_CanSend(&txMsg);
-
 
208
 
-
 
209
//Lets get timebase up and running
-
 
210
    Timebase_Init();
203
 
211
 
204
 
212
 
205
    /* main loop */
213
    /* main loop */
206
    while (1) {
214
    while (1) {
207
       
215
       
208
        /* Time to fade color? */
216
        /* Time to fade color? */
209
        if ((bios->timebase_get() - timeStamp) >= fadeSpeed) {
217
        if ((Timebase_CurrentTime() - timeStamp) >= fadeSpeed) {
210
            fade(&currColor.R, &destColor.R);          
218
            fade(&currColor.R, &destColor.R);          
211
            fade(&currColor.G, &destColor.G);
219
            fade(&currColor.G, &destColor.G);
212
            fade(&currColor.B, &destColor.B);
220
            fade(&currColor.B, &destColor.B);
213
 
221
 
214
            timeStamp = bios->timebase_get();
222
            timeStamp = Timebase_CurrentTime();
215
            updateLED();
223
            updateLED();
216
        }
224
        }
217
 
225
 
218
 
226
 
219
        /* Time to fade intensity? */
227
        /* Time to fade intensity? */
220
        if (bios->timebase_get() - dimStamp >= dimSpeed) {
228
        if (Timebase_CurrentTime() - dimStamp >= dimSpeed) {
221
            fade(&currColor.Intens, &destColor.Intens);
229
            fade(&currColor.Intens, &destColor.Intens);
222
            dimStamp = bios->timebase_get();
230
            dimStamp = Timebase_CurrentTime();
223
            updateLED();
231
            updateLED();
224
        }
232
        }
225
 
233
 
226
 
234
 
227
        /* Program management */
235
        /* Program management */
228
        if (currProgram != 0) { //Check if a program is currently running
236
        if (currProgram != 0) { //Check if a program is currently running
229
            if ((currColor.R == destColor.R)&(currColor.G == destColor.G)&(currColor.B == destColor.B)&(currColor.Intens == destColor.Intens)){//Is the current sequence finished?
237
            if ((currColor.R == destColor.R)&(currColor.G == destColor.G)&(currColor.B == destColor.B)&(currColor.Intens == destColor.Intens)){//Is the current sequence finished?
230
                //Now we have to check if the program is finished and if we should loop
238
                //Now we have to check if the program is finished and if we should loop
231
                if ((pgm_read_byte(temp_adress)==0)&(pgm_read_byte(temp_adress+1)==0)&(pgm_read_byte(temp_adress+2)==0)){
239
                if ((pgm_read_byte(temp_adress)==0)&(pgm_read_byte(temp_adress+1)==0)&(pgm_read_byte(temp_adress+2)==0)){
232
                    //The program has ended! Shall we loop it?
240
                    //The program has ended! Shall we loop it?
233
                    if (pgm_read_byte(temp_adress+3)==0){
241
                    if (pgm_read_byte(temp_adress+3)==0){
234
                        //Yes, let's loop it
242
                        //Yes, let's loop it
235
                        temp_adress = pgm_read_word(&programs[currProgram-1]);
243
                        temp_adress = pgm_read_word(&programs[currProgram-1]);
236
                    } else {
244
                    } else {
237
                        //No, halt!
245
                        //No, halt!
238
                        currProgram = 0;
246
                        currProgram = 0;
239
                    }
247
                    }
240
                } else {
248
                } else {
241
                    //The program has not ended, let's get the next RGB-triplet!
249
                    //The program has not ended, let's get the next RGB-triplet!
242
                    destColor.R = pgm_read_byte(temp_adress);
250
                    destColor.R = pgm_read_byte(temp_adress);
243
                    destColor.G = pgm_read_byte(temp_adress + 1);
251
                    destColor.G = pgm_read_byte(temp_adress + 1);
244
                    destColor.B = pgm_read_byte(temp_adress + 2);
252
                    destColor.B = pgm_read_byte(temp_adress + 2);
245
                    if (programMode == 1){
253
                    if (programMode == 1){
246
                        destColor.Intens = pgm_read_byte(temp_adress + 3);
254
                        destColor.Intens = pgm_read_byte(temp_adress + 3);
247
                    }
255
                    }
248
 
256
 
249
                    temp_adress = temp_adress + 4;             
257
                    temp_adress = temp_adress + 4;             
250
                }
258
                }
251
 
259
 
252
            }
260
            }
253
 
261
 
254
        }
262
        }
255
 
263
 
256
        /* check if any messages have been received */
264
        /* check if any messages have been received */
257
        if(msg_received){
265
        if(msg_received){
258
            /* This node that controls a RGB LED is adressed */
266
            /* This node that controls a RGB LED is adressed */
259
            if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETPROGRAM ){
267
            if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETPROGRAM ){
260
                /* Set a program! */
268
                /* Set a program! */
261
                currProgram = rxMsg.Data.bytes[1];
269
                currProgram = rxMsg.Data.bytes[1];
262
                temp_adress = pgm_read_word(&programs[currProgram-1]);             
270
                temp_adress = pgm_read_word(&programs[currProgram-1]);             
263
            }else if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETINTENS ){
271
            }else if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETINTENS ){
264
                /* Set intensity! */
272
                /* Set intensity! */
265
                if (rxMsg.Data.bytes[1] == TRUE){
273
                if (rxMsg.Data.bytes[1] == TRUE){
266
                    currColor.Intens = rxMsg.Data.bytes[2];                
274
                    currColor.Intens = rxMsg.Data.bytes[2];                
267
                }
275
                }
268
                destColor.Intens = rxMsg.Data.bytes[2];
276
                destColor.Intens = rxMsg.Data.bytes[2];
269
                if (rxMsg.Data.bytes[3] != 0){
277
                if (rxMsg.Data.bytes[3] != 0){
270
                    dimSpeed = rxMsg.Data.bytes[3];
278
                    dimSpeed = rxMsg.Data.bytes[3];
271
                }
279
                }
272
                currProgram = rxMsg.Data.bytes[1];
280
                currProgram = rxMsg.Data.bytes[1];
273
                temp_adress = pgm_read_word(&programs[currProgram-1]);
281
                temp_adress = pgm_read_word(&programs[currProgram-1]);
274
            } else if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETRGB) {
282
            } else if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETRGB) {
275
                /* Set RGB-triplet! */
283
                /* Set RGB-triplet! */
276
                reformatRGB(rxMsg.Data.bytes[2],rxMsg.Data.bytes[3],rxMsg.Data.bytes[4],0);
284
                reformatRGB(rxMsg.Data.bytes[2],rxMsg.Data.bytes[3],rxMsg.Data.bytes[4],0);
277
                if (rxMsg.Data.bytes[5] != 0){
285
                if (rxMsg.Data.bytes[5] != 0){
278
                    destColor.Intens = rxMsg.Data.bytes[5];
286
                    destColor.Intens = rxMsg.Data.bytes[5];
279
                }
287
                }
280
                if (rxMsg.Data.bytes[1] == TRUE){
288
                if (rxMsg.Data.bytes[1] == TRUE){
281
                    currColor = destColor;
289
                    currColor = destColor;
282
                }
290
                }
283
 
291
 
284
                if (rxMsg.Data.bytes[7] != 0){
292
                if (rxMsg.Data.bytes[7] != 0){
285
                    fadeSpeed = rxMsg.Data.bytes[6] * 255 + rxMsg.Data.bytes[7];
293
                    fadeSpeed = rxMsg.Data.bytes[6] * 255 + rxMsg.Data.bytes[7];
286
                }
294
                }
287
                currProgram = 0;
295
                currProgram = 0;
288
               
296
               
289
            }
297
            }
290
            msg_received = FALSE; //Let's clear the flag yes.
298
            msg_received = FALSE; //Let's clear the flag yes.
291
        }
299
        }
292
 
300
 
293
    }
301
    }
294
   
302
   
295
    return 0;
303
    return 0;
296
}
304
}
297
 
305
 
298
 
306
 
299
 
307
 
300
 
308
 
301
/************************************************************************************************
309
/************************************************************************************************
302
 *     updateLED updates the PWM counter values and thus changes the color of the RGBLED.       *
310
 *     updateLED updates the PWM counter values and thus changes the color of the RGBLED.       *
303
 *     It doesn't reculculate anything, except the values to write to the PWM registers.        *
311
 *     It doesn't reculculate anything, except the values to write to the PWM registers.        *
304
 *     TODO: Get rid of the tempcontainer!                                  *
312
 *     TODO: Get rid of the tempcontainer!                                  *
305
 ************************************************************************************************/
313
 ************************************************************************************************/
306
void updateLED(){
314
void updateLED(){
307
    uint8_t tempcontainer;
315
    uint8_t tempcontainer;
308
 
316
 
309
    tempcontainer = currColor.R * currColor.Intens / 255;   //I really want to skip this, don't know how yet though.
317
    tempcontainer = currColor.R * currColor.Intens / 255;   //I really want to skip this, don't know how yet though.
310
    OCR1A = tempcontainer; 
318
    OCR1A = tempcontainer; 
311
      OCR0A = currColor.G * currColor.Intens / 255;
319
      OCR0A = currColor.G * currColor.Intens / 255;
312
    OCR0B = currColor.B * currColor.Intens / 255;      
320
    OCR0B = currColor.B * currColor.Intens / 255;      
313
 
321
 
314
/*     
322
       
315
    //Bara för debugcrap
323
    //Bara för debugcrap
316
    Can_Message_t txMsg;
324
    Can_Message_t txMsg;
317
    txMsg.Id = 0x55;
325
    txMsg.Id = 0x55;
318
    txMsg.RemoteFlag = 0;
326
    txMsg.RemoteFlag = 0;
319
    txMsg.ExtendedFlag = 1;
327
    txMsg.ExtendedFlag = 1;
320
    txMsg.DataLength = 4;
328
    txMsg.DataLength = 4;
321
    txMsg.Data.bytes[0] = currColor.R;
329
    txMsg.Data.bytes[0] = currColor.R;
322
    txMsg.Data.bytes[1] = currColor.G;
330
    txMsg.Data.bytes[1] = currColor.G;
323
    txMsg.Data.bytes[2] = currColor.B;
331
    txMsg.Data.bytes[2] = currColor.B;
324
    txMsg.Data.bytes[3] = currColor.Intens;
332
    txMsg.Data.bytes[3] = currColor.Intens;
325
 
333
 
326
    bios->can_send(&txMsg);
334
    BIOS_CanSend(&txMsg);
327
*/
335
 
328
}
336
}
329
 
337
 
330
 
338
 
331
 
339
 
332
/************************************************************************************************
340
/************************************************************************************************
333
 *     reformatRGB sets a new target RGB triplet to fade to. It reformats it so that            *
341
 *     reformatRGB sets a new target RGB triplet to fade to. It reformats it so that            *
334
 *     the highest color always gets 0xff. Brightness is recalculated.                          *
342
 *     the highest color always gets 0xff. Brightness is recalculated.                          *
335
 *     if setBright is set to 0, the brightness will not be updated.                    *
343
 *     if setBright is set to 0, the brightness will not be updated.                    *
336
 *     TODO: Smarter 16-bit arithmetics would be nice.                          *
344
 *     TODO: Smarter 16-bit arithmetics would be nice.                          *
337
 ************************************************************************************************/
345
 ************************************************************************************************/
338
void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright){
346
void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright){
339
    uint8_t maxvalue;
347
    uint8_t maxvalue;
340
    uint16_t tempcontainer;
348
    uint16_t tempcontainer;
341
 
349
 
342
    if ((R>=G)&(R>=B)){
350
    if ((R>=G)&(R>=B)){
343
        maxvalue = R;
351
        maxvalue = R;
344
    } else if ((G>=R)&(G>=B)){
352
    } else if ((G>=R)&(G>=B)){
345
        maxvalue = G;
353
        maxvalue = G;
346
    } else {
354
    } else {
347
        maxvalue = B;
355
        maxvalue = B;
348
    }
356
    }
349
 
357
 
350
    tempcontainer = R * 255;
358
    tempcontainer = R * 255;
351
    destColor.R = tempcontainer/maxvalue;
359
    destColor.R = tempcontainer/maxvalue;
352
    tempcontainer = G * 255;
360
    tempcontainer = G * 255;
353
    destColor.G = tempcontainer/maxvalue;
361
    destColor.G = tempcontainer/maxvalue;
354
    tempcontainer = B * 255;
362
    tempcontainer = B * 255;
355
    destColor.B = tempcontainer/maxvalue;
363
    destColor.B = tempcontainer/maxvalue;
356
 
364
 
357
    if (setBright){
365
    if (setBright){
358
        destColor.Intens = maxvalue;
366
        destColor.Intens = maxvalue;
359
    }
367
    }
360
}
368
}
361
 
369
 
362
 
370
 
363
/************************************************************************************************
371
/************************************************************************************************
364
 *     fade takes two pointers, current and destination and fades current               *
372
 *     fade takes two pointers, current and destination and fades current               *
365
 *     one tick closer to destination.                                                        *
373
 *     one tick closer to destination.                                                        *
366
 ************************************************************************************************/
374
 ************************************************************************************************/
367
void fade(uint8_t* current, uint8_t* destination){
375
void fade(uint8_t* current, uint8_t* destination){
368
    if (*current > *destination){
376
    if (*current > *destination){
369
        *current = *current - 1;
377
        *current = *current - 1;
370
    } else if (*current < *destination){
378
    } else if (*current < *destination){
371
        *current = *current + 1;
379
        *current = *current + 1;
372
    }
380
    }
373
}
381
}
374
 
382
 
375
 
383
 
376
/************************************************************************************************
384
/************************************************************************************************
377
 *     changeColor changes a color accord to amount. -127>127.                                  *
385
 *     changeColor changes a color accord to amount. -127>127.                                  *
378
 *     color is either dest och curr R,G,B.                                                     *
386
 *     color is either dest och curr R,G,B.                                                     *
379
 *     Maybe TODO: If red is already 255 and we want to increase it, decrease the other ones.   *
387
 *     Maybe TODO: If red is already 255 and we want to increase it, decrease the other ones.   *
380
 ************************************************************************************************/
388
 ************************************************************************************************/
381
void changeColor(int8_t amount, uint8_t* color){
389
void changeColor(int8_t amount, uint8_t* color){
382
    if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color
390
    if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color
383
    *color = *color + amount;
391
    *color = *color + amount;
384
}
392
}
385
 
393
 
386
 
394