Subversion Repositories HomeAutomation

Rev

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

Rev 302 Rev 304
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
//0x00, 0x00, 0x00, 0x00 means end of program. This combination is useless for lighting anyway.
42
/*
43
 
43
struct Color pgm1[] = {
44
Color pgm1[] PROGMEM = {
-
 
45
{0xff,0x00,0x00,0x00},
-
 
46
{0xff,0xff,0x00,0x00},
44
{0x10,0x20,0x30,0x40},
47
{0x00,0xff,0x00,0x00},
-
 
48
{0x00,0xff,0xff,0x00},
45
{0x10,0x20,0x30,0x40},
49
{0x00,0x00,0xff,0x00},
-
 
50
{0xff,0x00,0xff,0x00},
46
{0x10,0x20,0x30,0x40}
51
{0x00,0x00,0x00,0x00}
47
};
52
};
48
 
53
 
49
struct Color pgm2[] = {
54
Color pgm2[] PROGMEM = {
50
{0x20,0x20,0x30,0x50},
55
{0x20,0x20,0x30,0x50},
51
{0x20,0x20,0x30,0x50},
56
{0x20,0x20,0x30,0x50},
52
{0x20,0x20,0x30,0x50}
57
{0x20,0x20,0x30,0x50}
53
};
58
};
54
 
59
 
55
struct Color* programs[] PROGMEM = {
60
Color* programs[] PROGMEM = {
56
(struct Color*)pgm1,
61
(Color*)pgm1,
57
(struct Color*)pgm2
62
(Color*)pgm2
58
};
63
};
59
 
64
 
60
 
65
 
61
uint16_t temp_address = pgm_read_word(&programs[1]);
-
 
62
*/
-
 
63
 
66
 
64
/*-----------------------------------------------------------------------------
67
/*-----------------------------------------------------------------------------
65
 * Variables
68
 * Variables
66
 *---------------------------------------------------------------------------*/
69
 *---------------------------------------------------------------------------*/
67
uint32_t timeStamp; //TimeStamp to keep track of time
70
uint32_t timeStamp; //TimeStamp to keep track of time
68
uint32_t dimStamp;  //Timestamp to keep track of dimmer changes
71
uint32_t dimStamp;  //Timestamp to keep track of dimmer changes
69
 
72
 
70
uint32_t tempStamp; //Timestamp to keep track of temp things during development
73
uint32_t tempStamp; //Timestamp to keep track of temp things during development
71
uint8_t temp;
74
uint8_t temp;
72
 
75
 
73
Color currColor;  //Current color
76
Color currColor;  //Current color
74
Color destColor;  //Destination color
77
Color destColor;  //Destination color
75
 
78
 
76
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange.
79
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange.
77
uint8_t dimSpeed;       //Dimmer speed. Sets the speed of intensitychange.
80
uint8_t dimSpeed;       //Dimmer speed. Sets the speed of intensitychange.
78
 
81
 
79
uint8_t program;        //This variable keeps track of what program is currently running. 
82
uint8_t program;        //This variable keeps track of what program is currently running. 
80
uint8_t programstate;   //This variable keeps track of where in a program we currently are. Will probably be removed later on.
83
uint8_t programstate;   //This variable keeps track of where in a program we currently are. Will probably be removed later on.
81
 
84
 
82
/*----------------------------------------------------------------------------
85
/*----------------------------------------------------------------------------
83
 * Functions
86
 * Functions
84
 * -------------------------------------------------------------------------*/
87
 * -------------------------------------------------------------------------*/
85
void updateLED();
88
void updateLED();
86
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);
87
void changeColor(int8_t amount, uint8_t* color);
90
void changeColor(int8_t amount, uint8_t* color);
88
void fade(uint8_t* current, uint8_t* destination);
91
void fade(uint8_t* current, uint8_t* destination);
89
 
92
 
90
/*-----------------------------------------------------------------------------
93
/*-----------------------------------------------------------------------------
91
 * Main Program
94
 * Main Program
92
 *---------------------------------------------------------------------------*/
95
 *---------------------------------------------------------------------------*/
93
int main(void) {
96
int main(void) {
-
 
97
 
94
    Timebase_Init();
98
    Timebase_Init();
95
    Serial_Init();
99
    Serial_Init();
96
    Can_Init();
100
    Can_Init();
97
 
101
 
98
//Setting up OC1A, 16 bit counter used as 8 bit.
102
//Setting up OC1A, 16 bit counter used as 8 bit.
99
    TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
103
    TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
100
    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 */
101
    ICR1 = 256; //8 bit precision to match the other two counters.
105
    ICR1 = 256; //8 bit precision to match the other two counters.
102
    OCR1A = 0;  //0% as standard
106
    OCR1A = 0;  //0% as standard
103
    DDRB |= (1<<PB1); /* Enable pin as output */
107
    DDRB |= (1<<PB1); /* Enable pin as output */
104
 
108
 
105
//Setting up OC0A
109
//Setting up OC0A
106
    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 */
107
    TCCR0B |= (1<<CS02); /* Prescaler updating now */
111
    TCCR0B |= (1<<CS02); /* Prescaler updating now */
108
    OCR0A = 0;  //0% standard
112
    OCR0A = 0;  //0% standard
109
    DDRD |= (1<<PD6);
113
    DDRD |= (1<<PD6);
110
 
114
 
111
//Setting up OC0B
115
//Setting up OC0B
112
    TCCR0A |= (1<<COM0B1);
116
    TCCR0A |= (1<<COM0B1);
113
    OCR0B = 0;
117
    OCR0B = 0;
114
    DDRD |= (1<<PD5);
118
    DDRD |= (1<<PD5);
115
 
119
 
116
 
120
 
117
//Initialize variables
121
//Initialize variables
118
    fadeSpeed = 10;
122
    fadeSpeed = 10;
119
    dimSpeed = 255;
123
    dimSpeed = 255;
120
    timeStamp = 0;
124
    timeStamp = 0;
121
    dimStamp = 0;
125
    dimStamp = 0;
122
    Can_Message_t rxMsg;   
126
    Can_Message_t rxMsg;   
123
 
127
 
124
    reformatRGB(0x00,0xff,0x00,1);
128
    reformatRGB(0x00,0xff,0x00,1);
125
    destColor.Intens = 100;
129
    destColor.Intens = 100;
126
    currColor = destColor;
130
    currColor = destColor;
127
 
131
 
128
//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.
129
    program = 1;
133
    program = 1;
130
    temp = 1;
134
    temp = 1;
-
 
135
    uint16_t temp_adress = pgm_read_word(&programs[0]); //Get adress for program 0
131
 
136
 
132
 
137
 
133
    sei();
138
    sei();
134
 
139
 
135
    /* main loop */
140
    /* main loop */
136
    while (1) {
141
    while (1) {
137
        /* service the CAN routines */
142
        /* service the CAN routines */
138
        Can_Service();
143
        Can_Service();
139
 
144
 
140
        /* Time to fade color? */
145
        /* Time to fade color? */
141
        if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {
146
        if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {
142
            fade(&currColor.R, &destColor.R);          
147
            fade(&currColor.R, &destColor.R);          
143
            fade(&currColor.G, &destColor.G);
148
            fade(&currColor.G, &destColor.G);
144
            fade(&currColor.B, &destColor.B);
149
            fade(&currColor.B, &destColor.B);
145
 
150
 
146
            timeStamp = Timebase_CurrentTime();
151
            timeStamp = Timebase_CurrentTime();
147
            updateLED();
152
            updateLED();
148
        }
153
        }
149
 
154
 
150
 
155
 
151
        /* Time to fade intensity? */
156
        /* Time to fade intensity? */
152
        if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) {
157
        if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) {
153
            fade(&currColor.Intens, &destColor.Intens);
158
            fade(&currColor.Intens, &destColor.Intens);
154
            dimStamp = Timebase_CurrentTime();
159
            dimStamp = Timebase_CurrentTime();
155
            updateLED();
160
            updateLED();
156
        }
161
        }
157
 
162
 
158
 
163
 
159
        /* Program management */
164
        /* Program management */
160
        if (program != 0){ //Check if a program is currently running
165
        if (program != 0){ //Check if a program is currently running
161
            //if (currColor == destColor){ //Is the current sequence finished?
166
            //if (currColor == destColor){ //Is the current sequence finished?
162
                //Set next state.
167
                //Set next state.
163
            //}
168
            //}
164
        }
169
        }
165
 
170
 
166
        /* Just a nice fade-demo */
171
        /* Just a nice fade-demo */
167
        if ((Timebase_PassedTimeMillis(tempStamp) >= 2500)&(program == 1)) {
172
        if ((Timebase_PassedTimeMillis(tempStamp) >= 2500)&(program == 1)) {
168
            tempStamp = Timebase_CurrentTime();
173
            tempStamp = Timebase_CurrentTime();
169
           
-
 
170
            switch(temp){
-
 
171
            case 1:
-
 
172
                reformatRGB(0xff,0x00,0x00,0);
-
 
173
                break;
-
 
174
            case 2:
-
 
175
                reformatRGB(0xff,0xff,0x00,0);
174
            reformatRGB(pgm_read_byte(temp_adress),pgm_read_byte(temp_adress + 1),pgm_read_byte(temp_adress + 2),0);
176
                break;
-
 
177
            case 3:
-
 
178
                reformatRGB(0x00,0xff,0x00,0);
-
 
179
                break;
-
 
180
            case 4:
-
 
181
                reformatRGB(0x00,0xff,0xff,0);
-
 
182
                break;
-
 
183
            case 5:
-
 
184
                reformatRGB(0x00,0x00,0xff,0);
-
 
185
                break;
-
 
186
            case 6:
-
 
187
                reformatRGB(0xff,0x00,0xff,0);
175
            temp_adress = temp_adress + 4;
188
                temp = 255;
-
 
189
                break;
-
 
190
            }
-
 
191
            temp++;
-
 
192
               
-
 
193
        }
176
        }
194
 
177
 
195
 
178
 
196
        /* check if any messages have been received */
179
        /* check if any messages have been received */
197
        /* A lot of remote control crap going on here now for testing */
180
        /* A lot of remote control crap going on here now for testing */
198
        while (Can_Receive(&rxMsg) == CAN_OK) {
181
        while (Can_Receive(&rxMsg) == CAN_OK) {
199
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){
182
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){
200
                program = 1;
183
                program = 1;
201
                fadeSpeed = 0;
184
                fadeSpeed = 0;
202
            }
185
            }
203
 
186
 
204
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){
187
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){
205
                program = 2;
188
                program = 2;
206
                fadeSpeed = 0;
189
                fadeSpeed = 0;
207
            }
190
            }
208
        }
191
        }
209
 
-
 
210
 
192
 
211
    }
193
    }
212
   
194
   
213
    return 0;
195
    return 0;
214
}
196
}
215
 
197
 
216
 
198
 
217
 
199
 
218
 
200
 
219
/************************************************************************************************
201
/************************************************************************************************
220
 *     updateLED updates the PWM counter values and thus changes the color of the RGBLED.       *
202
 *     updateLED updates the PWM counter values and thus changes the color of the RGBLED.       *
221
 *     It doesn't reculculate anything, except the values to write to the PWM registers.        *
203
 *     It doesn't reculculate anything, except the values to write to the PWM registers.        *
222
 *     TODO: Get rid of the tempcontainer!                                  *
204
 *     TODO: Get rid of the tempcontainer!                                  *
223
 ************************************************************************************************/
205
 ************************************************************************************************/
224
void updateLED(){
206
void updateLED(){
225
    uint8_t tempcontainer;
207
    uint8_t tempcontainer;
226
 
208
 
227
    tempcontainer = currColor.R * currColor.Intens / 255;   //I really want to skip this, don't know how yet though.
209
    tempcontainer = currColor.R * currColor.Intens / 255;   //I really want to skip this, don't know how yet though.
228
    OCR1A = tempcontainer; 
210
    OCR1A = tempcontainer; 
229
      OCR0A = currColor.G * currColor.Intens / 255;
211
      OCR0A = currColor.G * currColor.Intens / 255;
230
    OCR0B = currColor.B * currColor.Intens / 255;      
212
    OCR0B = currColor.B * currColor.Intens / 255;      
231
       
213
       
232
    //Bara för debugcrap
214
    //Bara för debugcrap
233
    Can_Message_t txMsg;
215
    Can_Message_t txMsg;
234
    txMsg.Id = 0x55;
216
    txMsg.Id = 0x55;
235
    txMsg.RemoteFlag = 0;
217
    txMsg.RemoteFlag = 0;
236
    txMsg.ExtendedFlag = 1;
218
    txMsg.ExtendedFlag = 1;
237
    txMsg.DataLength = 4;
219
    txMsg.DataLength = 4;
238
    txMsg.Data.bytes[0] = currColor.R * currColor.Intens / 255;
220
    txMsg.Data.bytes[0] = currColor.R * currColor.Intens / 255;
239
    txMsg.Data.bytes[1] = currColor.G * currColor.Intens / 255;
221
    txMsg.Data.bytes[1] = currColor.G * currColor.Intens / 255;
240
    txMsg.Data.bytes[2] = currColor.B * currColor.Intens / 255;
222
    txMsg.Data.bytes[2] = currColor.B * currColor.Intens / 255;
241
    txMsg.Data.bytes[3] = currColor.Intens;
223
    txMsg.Data.bytes[3] = currColor.Intens;
242
    Can_Send(&txMsg);
224
    Can_Send(&txMsg);
243
}
225
}
244
 
226
 
245
 
227
 
246
 
228
 
247
/************************************************************************************************
229
/************************************************************************************************
248
 *     reformatRGB sets a new target RGB triplet to fade to. It reformats it so that            *
230
 *     reformatRGB sets a new target RGB triplet to fade to. It reformats it so that            *
249
 *     the highest color always gets 0xff. Brightness is recalculated.                          *
231
 *     the highest color always gets 0xff. Brightness is recalculated.                          *
250
 *     if setBright is set to 0, the brightness will not be updated.                    *
232
 *     if setBright is set to 0, the brightness will not be updated.                    *
251
 *     TODO: Smarter 16-bit arithmetics would be nice. Also a pointer to struct to be updated.  *
233
 *     TODO: Smarter 16-bit arithmetics would be nice. Also a pointer to struct to be updated.  *
252
 ************************************************************************************************/
234
 ************************************************************************************************/
253
void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright){
235
void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright){
254
    uint8_t maxvalue;
236
    uint8_t maxvalue;
255
    uint16_t tempcontainer;
237
    uint16_t tempcontainer;
256
 
238
 
257
    if ((R>=G)&(R>=B)){
239
    if ((R>=G)&(R>=B)){
258
        maxvalue = R;
240
        maxvalue = R;
259
    } else if ((G>=R)&(G>=B)){
241
    } else if ((G>=R)&(G>=B)){
260
        maxvalue = G;
242
        maxvalue = G;
261
    } else {
243
    } else {
262
        maxvalue = B;
244
        maxvalue = B;
263
    }
245
    }
264
 
246
 
265
    tempcontainer = R * 255;
247
    tempcontainer = R * 255;
266
    destColor.R = tempcontainer/maxvalue;
248
    destColor.R = tempcontainer/maxvalue;
267
    tempcontainer = G * 255;
249
    tempcontainer = G * 255;
268
    destColor.G = tempcontainer/maxvalue;
250
    destColor.G = tempcontainer/maxvalue;
269
    tempcontainer = B * 255;
251
    tempcontainer = B * 255;
270
    destColor.B = tempcontainer/maxvalue;
252
    destColor.B = tempcontainer/maxvalue;
271
 
253
 
272
    if (setBright){
254
    if (setBright){
273
        destColor.Intens = maxvalue;
255
        destColor.Intens = maxvalue;
274
    }
256
    }
275
}
257
}
276
 
258
 
277
 
259
 
278
/************************************************************************************************
260
/************************************************************************************************
279
 *     fade tar in två värden, current och destination och för current ett steg                 *
261
 *     fade tar in två värden, current och destination och för current ett steg                 *
280
 *     närmare destination.                                                               *
262
 *     närmare destination.                                                               *
281
 ************************************************************************************************/
263
 ************************************************************************************************/
282
void fade(uint8_t* current, uint8_t* destination){
264
void fade(uint8_t* current, uint8_t* destination){
283
    if (*current > *destination){
265
    if (*current > *destination){
284
        *current = *current - 1;
266
        *current = *current - 1;
285
    } else if (*current < *destination){
267
    } else if (*current < *destination){
286
        *current = *current + 1;
268
        *current = *current + 1;
287
    }
269
    }
288
}
270
}
289
 
271
 
290
 
272
 
291
/************************************************************************************************
273
/************************************************************************************************
292
 *     changeColor ändrar en färg mängden amount. -127>127.                                     *
274
 *     changeColor ändrar en färg mängden amount. -127>127.                                     *
293
 *     color kan vara dest eller curr R,G,B.                                                    *
275
 *     color kan vara dest eller curr R,G,B.                                                    *
294
 *     Ev. TODO: om t.ex. röd är på max och man vill öka röd ska de andra färgerna minskas. *
276
 *     Ev. TODO: om t.ex. röd är på max och man vill öka röd ska de andra färgerna minskas. *
295
 ************************************************************************************************/
277
 ************************************************************************************************/
296
void changeColor(int8_t amount, uint8_t* color){
278
void changeColor(int8_t amount, uint8_t* color){
297
    if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color
279
    if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color
298
    *color = *color + amount;
280
    *color = *color + amount;
299
}
281
}
300
 
282
 
301
 
283