Subversion Repositories HomeAutomation

Rev

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

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