Subversion Repositories HomeAutomation

Rev

Rev 305 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 305 Rev 306
Line 4... Line 4...
4
 *
4
 *
5
 * @date    2007-02-04
5
 * @date    2007-02-10
6
 * @author  Martin Nordén
6
 * @author  Martin Nordén
7
 *  
7
 *
-
 
8
 * Set RGB: <byte0 RGBLED_CMD_SETRGB> <byte 1 Set/Fade> <byte2 R> <byte3 G> <byte4 B>
-
 
9
 * Change RGB: <byte 0 RGBLED_CMD_CHANGERGB> <byte 1 int R> <byte 2 int G> <byte 3 int B>  (Note that they are all signed)
-
 
10
 * Set Intens: <byte0 RGBLED_CMD_SETINTENS> <byte1 Set/Fade> <byte2 Intens> (<byte3 dimSpeed>)
-
 
11
 *
8
 * TODO: A lot, currently trying to get some kind of ready-made programs to work.
12
 * TODO: Better CAN Integration, Failsafe mode, Tempsensors etc.
9
 */
13
 */
-
 
14
 
10
 
15
 
11
/*-----------------------------------------------------------------------------
16
/*-----------------------------------------------------------------------------
12
 * Includes
17
 * Includes
13
 *---------------------------------------------------------------------------*/
18
 *---------------------------------------------------------------------------*/
14
/* system files */
19
/* system files */
15
#include <avr/io.h>
20
#include <avr/io.h>
16
#include <avr/interrupt.h>
21
#include <avr/interrupt.h>
17
#include <avr/wdt.h>
22
#include <avr/wdt.h>
18
#include <stdio.h>
23
#include <stdio.h>
-
 
24
#include <avr/pgmspace.h> //To allow read/write from flash
19
/* lib files */
25
/* lib files */
20
#include <can.h>
26
#include <can.h>
21
#include <serial.h>
27
#include <serial.h>
22
#include <timebase.h>
28
#include <timebase.h>
23
#include <math.h> //Not sure this is needed anymore..
29
#include <math.h> //Not sure this is needed anymore..
24
#include <avr/pgmspace.h> //To allow read/write from flash
-
 
25
 
30
 
-
 
31
/*----------------------------------------------------------------------------
-
 
32
 * Defines
-
 
33
 * -------------------------------------------------------------------------*/
-
 
34
 
-
 
35
//Not implemented yet. Will be implementet when I build a canUSB :)
-
 
36
 
-
 
37
#define RGBLED_CMD_GET      0x1400
-
 
38
#define RGBLED_CMD_SEND     0x1401
-
 
39
 
-
 
40
#define RGBLED_CMD_STATUS       0x00    /* Get status */
-
 
41
#define RGBLED_CMD_SETRGB       0x01    /* Set RGB Triplet */
-
 
42
#define RGBLED_CMD_CHANGERGB  0x02  /* Change RGB +/- */
-
 
43
#define RGBLED_CMD_SETINTENS  0x10  /* Set intensity */
26
 
44
 
27
/*----------------------------------------------------------------------------
45
/*----------------------------------------------------------------------------
28
 * Ehm, struct?
46
 * Ehm, struct?
29
 * -------------------------------------------------------------------------*/
47
 * -------------------------------------------------------------------------*/
30
typedef struct
48
typedef struct
Line 34... Line 52...
34
    uint8_t B;
52
    uint8_t B;
35
    uint8_t Intens;
53
    uint8_t Intens;
36
} Color;
54
} Color;
37
 
55
 
38
/*-----------------------------------------------------------------------------
56
/*-----------------------------------------------------------------------------
39
 * Programs! This will most likely be improooved, but I have to start somewhere.
57
 * Programs! There will be a better way to add programs here.. some day.
40
 *---------------------------------------------------------------------------*/
58
 *---------------------------------------------------------------------------*/
41
 
59
 
42
//Special combinations that means something special
60
//Special combinations that means something special
43
//The combinations are useless for lighting anyway
61
//The combinations are useless for lighting anyway
44
//So we might as well use them!
62
//So we might as well use them!
45
//0x00, 0x00, 0x00, 0x00 means end of program, keep looping it
63
//0x00, 0x00, 0x00, 0x00 means end of program, keep looping it
46
//0x00, 0x00, 0x00, 0x01 means end of program, halt 
64
//0x00, 0x00, 0x00, 0x01 means end of program, halt 
47
 
65
 
48
Color pgm1[] PROGMEM = {
66
Color pgm1[] PROGMEM = {
-
 
67
{0xff,0x00,0x00,0x64},
-
 
68
{0xff,0xff,0x00,0x10},
-
 
69
{0x00,0xff,0x00,0xa0},
-
 
70
{0x00,0xff,0xff,0x7f},
-
 
71
{0x00,0x00,0xff,0x7f},
-
 
72
{0xff,0x00,0xff,0x40},
-
 
73
{0x00,0x00,0x00,0x00}
-
 
74
};
-
 
75
 
-
 
76
Color pgm2[] PROGMEM = {
49
{0xff,0x00,0x00,0x00},
77
{0xff,0x00,0x00,0x00},
50
{0xff,0xff,0x00,0x00},
78
{0xff,0xff,0x00,0x00},
51
{0x00,0xff,0x00,0x00},
79
{0xff,0xff,0xff,0x00},
52
{0x00,0xff,0xff,0x00},
80
{0xa0,0x00,0xa0,0x00},
53
{0x00,0x00,0xff,0x00},
81
{0x00,0x00,0xff,0x00},
54
{0xff,0x00,0xff,0x00},
-
 
55
{0x00,0x00,0x00,0x00}
82
{0x00,0x00,0x00,0x00}
56
};
83
};
57
 
84
 
58
Color pgm2[] PROGMEM = {
85
Color pgm3[] PROGMEM = {
59
{0x20,0x20,0x30,0x50},
-
 
60
{0x20,0x20,0x30,0x50},
86
{0xff,0xff,0xff,0x00},
61
{0x20,0x20,0x30,0x50}
87
{0x00,0x00,0x00,0x01}
62
};
88
};
63
 
89
 
64
Color* programs[] PROGMEM = {
90
Color* programs[] PROGMEM = {
65
(Color*)pgm1,
91
(Color*)pgm1,
66
(Color*)pgm2
92
(Color*)pgm2,
-
 
93
(Color*)pgm3
67
};
94
};
68
 
95
 
69
 
96
 
70
 
97
 
71
/*-----------------------------------------------------------------------------
98
/*-----------------------------------------------------------------------------
72
 * Variables
99
 * Variables
73
 *---------------------------------------------------------------------------*/
100
 *---------------------------------------------------------------------------*/
74
uint32_t timeStamp; //TimeStamp to keep track of time
101
uint32_t timeStamp; //TimeStamp to keep track of time
75
uint32_t dimStamp;  //Timestamp to keep track of dimmer changes
102
uint32_t dimStamp;  //Timestamp to keep track of dimmer changes
Line 79... Line 106...
79
 
106
 
80
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange.
107
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange.
81
uint8_t dimSpeed;       //Dimmer speed. Sets the speed of intensitychange.
108
uint8_t dimSpeed;       //Dimmer speed. Sets the speed of intensitychange.
82
 
109
 
83
uint8_t currProgram;        //This variable keeps track of what program is currently running. 
110
uint8_t currProgram;    //This variable keeps track of what program is currently running. 
-
 
111
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
84
 
112
 
85
/*----------------------------------------------------------------------------
113
/*----------------------------------------------------------------------------
86
 * Functions
114
 * Functions
87
 * -------------------------------------------------------------------------*/
115
 * -------------------------------------------------------------------------*/
88
void updateLED();
116
void updateLED();
Line 92... Line 120...
92
 
120
 
93
/*-----------------------------------------------------------------------------
121
/*-----------------------------------------------------------------------------
94
 * Main Program
122
 * Main Program
95
 *---------------------------------------------------------------------------*/
123
 *---------------------------------------------------------------------------*/
96
int main(void) {
124
int main(void) {
97
 
-
 
98
    Timebase_Init();
125
    Timebase_Init();
99
    Serial_Init();
126
    Serial_Init();
100
    Can_Init();
127
    Can_Init();
101
 
128
 
102
//Setting up OC1A, 16 bit counter used as 8 bit.
129
//Setting up OC1A, 16 bit counter used as 8 bit.
103
    TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
130
    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 */
131
    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.
132
    ICR1 = 256; //8 bit precision to match the other two counters.
106
    OCR1A = 0;  //0% as standard
133
    OCR1A = 0;  //0% as standard
107
    DDRB |= (1<<PB1); /* Enable pin as output */
134
    DDRB |= (1<<PB1); /* Enable pin as output */
108
 
-
 
109
//Setting up OC0A
135
//Setting up OC0A
110
    TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */
136
    TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */
111
    TCCR0B |= (1<<CS02); /* Prescaler updating now */
137
    TCCR0B |= (1<<CS02); /* Prescaler updating now */
112
    OCR0A = 0;  //0% standard
138
    OCR0A = 0;  //0% standard
113
    DDRD |= (1<<PD6);
139
    DDRD |= (1<<PD6);
114
 
-
 
115
//Setting up OC0B
140
//Setting up OC0B
116
    TCCR0A |= (1<<COM0B1);
141
    TCCR0A |= (1<<COM0B1);
117
    OCR0B = 0;
142
    OCR0B = 0;
118
    DDRD |= (1<<PD5);
143
    DDRD |= (1<<PD5);
119
 
-
 
120
 
144
 
121
//Initialize variables
145
//Initialize variables
122
    fadeSpeed = 10;
146
    fadeSpeed = 10;
123
    dimSpeed = 255;
147
    dimSpeed = 10;
124
    timeStamp = 0;
148
    timeStamp = 0;
125
    dimStamp = 0;
149
    dimStamp = 0;
126
    Can_Message_t rxMsg;   
150
    Can_Message_t rxMsg;   
127
 
151
 
128
    reformatRGB(0x00,0xff,0x00,1);
152
    reformatRGB(0x00,0xff,0x00,1);
129
    destColor.Intens = 100;
153
    destColor.Intens = 250;
130
    currColor = destColor;
154
    currColor = destColor;
131
 
155
 
132
//Just temporary for now, they keep track of some kind of demo-program.
156
//Just temporary for now, they keep track of some kind of demo-program.
133
    currProgram = 1;
157
    currProgram = 1;
-
 
158
    programMode = 0;
134
    uint16_t temp_adress = pgm_read_word(&programs[currProgram-1]); //Get adress for program 0
159
    uint16_t temp_adress = pgm_read_word(&programs[currProgram-1]); //Get adress for program 0
135
 
-
 
136
 
160
 
137
    sei();
161
    sei();
138
 
162
 
139
    /* main loop */
163
    /* main loop */
140
    while (1) {
164
    while (1) {
Line 166... Line 190...
166
                //Now we have to check if the program is finished and if we should loop
190
                //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)){
191
                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?
192
                    //The program has ended! Shall we loop it?
169
                    if (pgm_read_byte(temp_adress+3)==0){
193
                    if (pgm_read_byte(temp_adress+3)==0){
170
                        //Yes, let's loop it
194
                        //Yes, let's loop it
171
                        temp_adress = pgm_read_word(&programs[currProgram-1]);
195
                        temp_adress = pgm_read_word(&programs[currProgram-1]);
172
                    } else {
196
                    } else {
173
                        //No, halt!
197
                        //No, halt!
174
                        currProgram = 0;
198
                        currProgram = 0;
175
                    }
199
                    }
176
                } else {
200
                } else {
177
                    //The program has not ended, let's get the next RGB-triplet!
201
                    //The program has not ended, let's get the next RGB-triplet!
-
 
202
                    destColor.R = pgm_read_byte(temp_adress);
178
                    reformatRGB(pgm_read_byte(temp_adress),pgm_read_byte(temp_adress + 1),pgm_read_byte(temp_adress + 2),0);
203
                    destColor.G = pgm_read_byte(temp_adress + 1);
179
                    temp_adress = temp_adress + 4;                 
204
                    destColor.B = pgm_read_byte(temp_adress + 2);
-
 
205
                    if (programMode == 1){
-
 
206
                        destColor.Intens = pgm_read_byte(temp_adress + 3);
180
                }
207
                    }
-
 
208
 
-
 
209
                    temp_adress = temp_adress + 4;             
-
 
210
                }
181
            }
211
            }
182
        }
212
        }
183
 
213
 
184
        /* check if any messages have been received */
214
        /* check if any messages have been received */
185
        /* A lot of remote control crap going on here now for testing */
215
        /* A lot of remote control crap going on here now for testing */
186
        while (Can_Receive(&rxMsg) == CAN_OK) {
216
        while (Can_Receive(&rxMsg) == CAN_OK) {
187
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){
217
            if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x01)){
188
                //program = 1;
218
                currProgram = 1;
-
 
219
                temp_adress = pgm_read_word(&programs[currProgram-1]);
-
 
220
            }
-
 
221
 
-
 
222
            if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x02)){
189
                //fadeSpeed = 0;
223
                currProgram = 2;
-
 
224
                temp_adress = pgm_read_word(&programs[currProgram-1]);
190
            }
225
            }
191
 
226
 
192
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){
227
            if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x03)){
193
                //program = 2;
228
                currProgram = 3;
-
 
229
                temp_adress = pgm_read_word(&programs[currProgram-1]);
-
 
230
            }
-
 
231
 
-
 
232
            if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x00)){
194
                //fadeSpeed = 0;
233
                currProgram = 0;
195
            }
234
            }
-
 
235
 
-
 
236
 
-
 
237
 
196
        }
238
        }
197
 
239
 
198
    }
240
    }
199
   
241
   
200
    return 0;
242
    return 0;
201
}
243
}
202
 
244
 
203
 
245
 
204
 
246
 
205
 
247
 
206
/************************************************************************************************
248
/************************************************************************************************
207
 *     updateLED updates the PWM counter values and thus changes the color of the RGBLED.       *
249
 *     updateLED updates the PWM counter values and thus changes the color of the RGBLED.       *
208
 *     It doesn't reculculate anything, except the values to write to the PWM registers.        *
250
 *     It doesn't reculculate anything, except the values to write to the PWM registers.        *
209
 *     TODO: Get rid of the tempcontainer!                                  *
251
 *     TODO: Get rid of the tempcontainer!                                  *
210
 ************************************************************************************************/
252
 ************************************************************************************************/
Line 213... Line 255...
213
 
255
 
214
    tempcontainer = currColor.R * currColor.Intens / 255;   //I really want to skip this, don't know how yet though.
256
    tempcontainer = currColor.R * currColor.Intens / 255;   //I really want to skip this, don't know how yet though.
215
    OCR1A = tempcontainer; 
257
    OCR1A = tempcontainer; 
216
      OCR0A = currColor.G * currColor.Intens / 255;
258
      OCR0A = currColor.G * currColor.Intens / 255;
217
    OCR0B = currColor.B * currColor.Intens / 255;      
259
    OCR0B = currColor.B * currColor.Intens / 255;      
-
 
260
 
218
       
261
/*     
219
    //Bara för debugcrap
262
    //Bara för debugcrap
220
    Can_Message_t txMsg;
263
    Can_Message_t txMsg;
221
    txMsg.Id = 0x55;
264
    txMsg.Id = 0x55;
222
    txMsg.RemoteFlag = 0;
265
    txMsg.RemoteFlag = 0;
223
    txMsg.ExtendedFlag = 1;
266
    txMsg.ExtendedFlag = 1;
Line 226... Line 269...
226
    txMsg.Data.bytes[1] = currColor.G;
269
    txMsg.Data.bytes[1] = currColor.G;
227
    txMsg.Data.bytes[2] = currColor.B;
270
    txMsg.Data.bytes[2] = currColor.B;
228
    txMsg.Data.bytes[3] = currColor.Intens;
271
    txMsg.Data.bytes[3] = currColor.Intens;
229
 
272
 
230
    Can_Send(&txMsg);
273
    Can_Send(&txMsg);
-
 
274
*/
231
}
275
}
232
 
276
 
233
 
277
 
234
 
278
 
235
/************************************************************************************************
279
/************************************************************************************************
Line 262... Line 306...
262
    }
306
    }
263
}
307
}
264
 
308
 
265
 
309
 
266
/************************************************************************************************
310
/************************************************************************************************
267
 *     fade tar in två värden, current och destination och för current ett steg                 *
311
 *     fade takes two pointers, current and destination and fades current               *
268
 *     närmare destination.                                                               *
312
 *     one tick closer to destination.                                                        *
269
 ************************************************************************************************/
313
 ************************************************************************************************/
270
void fade(uint8_t* current, uint8_t* destination){
314
void fade(uint8_t* current, uint8_t* destination){
271
    if (*current > *destination){
315
    if (*current > *destination){
272
        *current = *current - 1;
316
        *current = *current - 1;
273
    } else if (*current < *destination){
317
    } else if (*current < *destination){
Line 275... Line 319...
275
    }
319
    }
276
}
320
}
277
 
321
 
278
 
322
 
279
/************************************************************************************************
323
/************************************************************************************************
280
 *     changeColor ändrar en färg mängden amount. -127>127.                                     *
324
 *     changeColor changes a color accord to amount. -127>127.                                  *
281
 *     color kan vara dest eller curr R,G,B.                                                    *
325
 *     color is either dest och curr R,G,B.                                                     *
282
 *     Ev. TODO: om t.ex. röd är på max och man vill öka röd ska de andra färgerna minskas. *
326
 *     Maybe TODO: If red is already 255 and we want to increase it, decrease the other ones.   *
283
 ************************************************************************************************/
327
 ************************************************************************************************/
284
void changeColor(int8_t amount, uint8_t* color){
328
void changeColor(int8_t amount, uint8_t* color){
285
    if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color
329
    if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color
286
    *color = *color + amount;
330
    *color = *color + amount;
287
}
331
}