Subversion Repositories HomeAutomation

Rev

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

Rev 330 Rev 331
Line 3... Line 3...
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
 * Set RGB: <byte0 RGBLED_CMD_SETRGB> <byte 1 Set/Fade> <byte2 R> <byte3 G> <byte4 B>
8
 * TODO: Better CAN Integration, Failsafe mode, Tempsensors etc.
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>)
9
 * Also, check current/destination intensity better when in a program?
11
 *
10
 *
12
 * TODO: Better CAN Integration, Failsafe mode, Tempsensors etc.
-
 
13
 */
11
 */
14
 
12
 
15
 
13
 
16
/*-----------------------------------------------------------------------------
14
/*-----------------------------------------------------------------------------
17
 * Includes
15
 * Includes
18
 *---------------------------------------------------------------------------*/
16
 *---------------------------------------------------------------------------*/
19
/* system files */
17
/* system files */
20
#include <avr/interrupt.h>
18
#include <avr/interrupt.h>
21
#include <inttypes.h>
19
#include <inttypes.h>
22
#include <stdio.h>
20
#include <stdio.h>
23
#include <bios.h>
-
 
24
#include <avr/pgmspace.h> //To allow read/write from flash
21
#include <avr/pgmspace.h> //To allow read/write from flash
25
 
22
 
26
/* lib files */
23
/* lib files */
27
//#include <can.h>
24
#include <bios.h>
28
//#include <serial.h>
-
 
29
//#include <timebase.h>
25
#include <noddan_funcdefs.h>
30
//#include <math.h> //Not sure this is needed anymore..
-
 
31
 
26
 
32
/*----------------------------------------------------------------------------
27
/*----------------------------------------------------------------------------
33
 * Defines
28
 * Defines
34
 * -------------------------------------------------------------------------*/
29
 * -------------------------------------------------------------------------*/
35
 
30
 
36
//Not implemented yet. Will be implementet when I build a canUSB :)
31
//Working on implementing all of these...
37
 
-
 
38
#define RGBLED_CMD_GET      0x1400
-
 
39
#define RGBLED_CMD_SEND     0x1401
-
 
40
 
32
 
41
#define RGBLED_CMD_STATUS       0x00    /* Get status */
33
#define RGBLED_CMD_STATUS           0x00    /* Get status */
42
#define RGBLED_CMD_SETRGB       0x01    /* Set RGB Triplet */
34
#define RGBLED_CMD_SETRGB           0x01    /* Set RGB Triplet */
43
#define RGBLED_CMD_CHANGERGB  0x02  /* Change RGB +/- */
35
#define RGBLED_CMD_CHANGERGB        0x02    /* Change RGB +/- */
-
 
36
#define RGBLED_CMD_SETPROGRAM       0x03  /* Set program */
-
 
37
#define RGBLED_CMD_SETFADESPEED     0x04  /* Set fadespeed */
-
 
38
#define RGBLED_CMD_SETDIMSPEED      0x05  /* Set dimspeed */
44
#define RGBLED_CMD_SETINTENS  0x10  /* Set intensity */
39
#define RGBLED_CMD_SETINTENS        0x10  /* Set intensity */
-
 
40
 
-
 
41
#define APP_TYPE    0xf001
-
 
42
#define APP_VERSION 0x0001
-
 
43
 
-
 
44
#define GROUP_ID    0x01L // FIXME
-
 
45
#define TRUE 1
-
 
46
#define FALSE !TRUE
-
 
47
 
45
 
48
 
46
/*----------------------------------------------------------------------------
49
/*----------------------------------------------------------------------------
47
 * Ehm, struct?
50
 * Ehm, struct?
48
 * -------------------------------------------------------------------------*/
51
 * -------------------------------------------------------------------------*/
49
typedef struct
52
typedef struct
Line 84... Line 87...
84
};
87
};
85
 
88
 
86
Color pgm3[] PROGMEM = {
89
Color pgm3[] PROGMEM = {
87
{0xff,0xff,0xff,0x00},
90
{0xff,0xff,0xff,0x00},
88
{0x00,0x00,0x00,0x01}
91
{0x00,0x00,0x00,0x01}
89
};
92
};
90
 
93
 
91
Color* programs[] PROGMEM = {
94
Color* programs[] PROGMEM = {
92
(Color*)pgm1,
95
(Color*)pgm1,
93
(Color*)pgm2,
96
(Color*)pgm2,
94
(Color*)pgm3
97
(Color*)pgm3
95
};
98
};
96
 
99
 
97
 
100
 
98
 
101
 
99
/*-----------------------------------------------------------------------------
102
/*-----------------------------------------------------------------------------
100
 * Variables
103
 * Global variables
101
 *---------------------------------------------------------------------------*/
104
 *---------------------------------------------------------------------------*/
102
uint32_t timeStamp; //TimeStamp to keep track of time
105
uint32_t timeStamp; //TimeStamp to keep track of time
103
uint32_t dimStamp;  //Timestamp to keep track of dimmer changes
106
uint32_t dimStamp;  //Timestamp to keep track of dimmer changes
104
uint32_t time;      //Temptime for converting (TODO)
-
 
105
 
107
 
106
Color currColor;    //Current color
108
Color currColor;    //Current color
107
Color destColor;    //Destination color
109
Color destColor;    //Destination color
108
 
110
 
109
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange.
111
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange.
110
uint8_t dimSpeed;       //Dimmer speed. Sets the speed of intensitychange.
112
uint8_t dimSpeed;       //Dimmer speed. Sets the speed of intensitychange.
111
 
113
 
112
uint8_t currProgram;    //This variable keeps track of what program is currently running. 
114
uint8_t currProgram;    //This variable keeps track of what program is currently running. 
113
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
115
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
-
 
116
 
-
 
117
uint8_t msg_received = FALSE;
-
 
118
 
-
 
119
Can_Message_t txMsg;
-
 
120
Can_Message_t rxMsg;
114
 
121
 
115
/*----------------------------------------------------------------------------
122
/*----------------------------------------------------------------------------
116
 * Functions
123
 * Functions
117
 * -------------------------------------------------------------------------*/
124
 * -------------------------------------------------------------------------*/
118
void updateLED();
125
void updateLED();
119
void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright);
126
void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright);
120
void changeColor(int8_t amount, uint8_t* color);
127
void changeColor(int8_t amount, uint8_t* color);
121
void fade(uint8_t* current, uint8_t* destination);
128
void fade(uint8_t* current, uint8_t* destination);
122
 
129
 
123
 
130
 
-
 
131
/* Takes care about incoming messages and parses them if this node is adressed */
124
void can_receive(Can_Message_t *msg) {
132
void can_receive(Can_Message_t *msg){
-
 
133
    uint32_t act;
-
 
134
    uint8_t m, act_type, group, node;
-
 
135
    if( ((msg->Id & CLASS_MASK) >> CLASS_MASK_BITS) == CLASS_ACT ){
-
 
136
        act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
-
 
137
        act_type = (act & ACT_TYPE_MASK) >> ACT_TYPE_BITS;
-
 
138
        group = (act & ACT_GROUP_MASK) >> ACT_GROUP_BITS;
-
 
139
        node = (act & ACT_NODE_MASK) >> ACT_NODE_BITS;
-
 
140
 
125
    //What should we do when can recieve?
141
        /* Check if it is command to control this servo */
-
 
142
        if(act_type == ACT_TYPE_RGB){
-
 
143
            if(group == GROUP_ID || node == NODE_ID){
-
 
144
                for(m=0;m<msg->DataLength;m++){
-
 
145
                    rxMsg.Data.bytes[m] = msg->Data.bytes[m];
-
 
146
                }
-
 
147
                msg_received = TRUE;
-
 
148
            }
-
 
149
        }
-
 
150
    }
126
}
151
}
-
 
152
 
127
 
153
 
128
/*-----------------------------------------------------------------------------
154
/*-----------------------------------------------------------------------------
129
 * Main Program
155
 * Main Program
130
 *---------------------------------------------------------------------------*/
156
 *---------------------------------------------------------------------------*/
131
int main(void) {
157
int main(void) {
132
//  Timebase_Init();
-
 
133
//  Serial_Init();
-
 
134
//  Can_Init();
-
 
135
 
158
 
136
//Setting up OC1A, 16 bit counter used as 8 bit.
159
//Setting up OC1A, 16 bit counter used as 8 bit.
137
    TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
160
    TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
138
    TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS12); /* Timer uses main system clock with ? prescale */
161
    TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS12); /* Timer uses main system clock with ? prescale */
139
    ICR1 = 256; //8 bit precision to match the other two counters.
162
    ICR1 = 256; //8 bit precision to match the other two counters.
Line 152... Line 175...
152
//Initialize variables
175
//Initialize variables
153
    fadeSpeed = 10;
176
    fadeSpeed = 10;
154
    dimSpeed = 10;
177
    dimSpeed = 10;
155
    timeStamp = 0;
178
    timeStamp = 0;
156
    dimStamp = 0;
179
    dimStamp = 0;
157
//  Can_Message_t rxMsg;    
-
 
158
 
180
 
159
    reformatRGB(0x00,0xff,0x00,1);
181
    reformatRGB(0x00,0x00,0x00,1);
160
    destColor.Intens = 100;
182
    destColor.Intens = 100;
161
    currColor = destColor;
183
    currColor = destColor;
162
 
184
 
163
//Just temporary for now, they keep track of some kind of demo-program.
185
//Just temporary for now, they keep track of some kind of demo-program.
164
    currProgram = 1;
186
    currProgram = 1;
Line 167... Line 189...
167
 
189
 
168
    sei();
190
    sei();
169
 
191
 
170
    bios->can_callback = &can_receive;
192
    bios->can_callback = &can_receive;
171
 
193
 
172
//Lets send out that we're alive
194
//Send a message that we are alive!
173
    Can_Message_t txMsg;
-
 
174
    txMsg.Id = 0x42; //booted!
195
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
175
    txMsg.DataLength = 4;
196
    txMsg.DataLength = 4;
176
    txMsg.RemoteFlag = 0;
197
    txMsg.RemoteFlag = 0;
177
    txMsg.ExtendedFlag = 1;
198
    txMsg.ExtendedFlag = 1;
178
    txMsg.Data.words[0] = 0x13;
199
    txMsg.Data.words[0] = APP_TYPE;
179
    txMsg.Data.words[1] = 0x13;
200
    txMsg.Data.words[1] = APP_VERSION;
180
    bios->can_send(&txMsg);
201
    bios->can_send(&txMsg);
181
//Data sent.
-
 
182
 
202
 
183
//Just temporary
-
 
184
//  currColor.R = 0;
-
 
185
//  destColor.R = 100;
-
 
186
 
-
 
187
 
203
 
188
    /* main loop */
204
    /* main loop */
189
    while (1) {
205
    while (1) {
190
       
206
       
191
        /* Time to fade color? */
207
        /* Time to fade color? */
192
        time = bios->timebase_get();
-
 
193
        if ((time - timeStamp) >= fadeSpeed) {
208
        if ((bios->timebase_get() - timeStamp) >= fadeSpeed) {
194
            fade(&currColor.R, &destColor.R);          
209
            fade(&currColor.R, &destColor.R);          
195
            fade(&currColor.G, &destColor.G);
210
            fade(&currColor.G, &destColor.G);
196
            fade(&currColor.B, &destColor.B);
211
            fade(&currColor.B, &destColor.B);
197
 
212
 
198
            timeStamp = bios->timebase_get();
213
            timeStamp = bios->timebase_get();
199
            updateLED();
214
            updateLED();
200
        }
215
        }
201
 
216
 
202
 
217
 
203
        /* Time to fade intensity? */
218
        /* Time to fade intensity? */
204
//      if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) {
219
        if (bios->timebase_get() - dimStamp >= dimSpeed) {
205
//          fade(&currColor.Intens, &destColor.Intens);
220
            fade(&currColor.Intens, &destColor.Intens);
206
//          dimStamp = Timebase_CurrentTime();
221
            dimStamp = bios->timebase_get();
207
//          updateLED();
222
            updateLED();
208
//      }
223
        }
209
 
224
 
210
 
225
 
211
        /* Program management */
226
        /* Program management */
212
        if (currProgram != 0) { //Check if a program is currently running
227
        if (currProgram != 0) { //Check if a program is currently running
213
            if ((currColor.R == destColor.R)&(currColor.G == destColor.G)&(currColor.B == destColor.B)&(currColor.Intens == destColor.Intens)){//Is the current sequence finished?
228
            if ((currColor.R == destColor.R)&(currColor.G == destColor.G)&(currColor.B == destColor.B)&(currColor.Intens == destColor.Intens)){//Is the current sequence finished?
Line 217... Line 232...
217
                    if (pgm_read_byte(temp_adress+3)==0){
232
                    if (pgm_read_byte(temp_adress+3)==0){
218
                        //Yes, let's loop it
233
                        //Yes, let's loop it
219
                        temp_adress = pgm_read_word(&programs[currProgram-1]);
234
                        temp_adress = pgm_read_word(&programs[currProgram-1]);
220
                    } else {
235
                    } else {
221
                        //No, halt!
236
                        //No, halt!
222
                        currProgram = 0;
237
                        currProgram = 0;
223
                    }
238
                    }
224
                } else {
239
                } else {
225
                    //The program has not ended, let's get the next RGB-triplet!
240
                    //The program has not ended, let's get the next RGB-triplet!
226
                    destColor.R = pgm_read_byte(temp_adress);
241
                    destColor.R = pgm_read_byte(temp_adress);
227
                    destColor.G = pgm_read_byte(temp_adress + 1);
242
                    destColor.G = pgm_read_byte(temp_adress + 1);
228
                    destColor.B = pgm_read_byte(temp_adress + 2);
243
                    destColor.B = pgm_read_byte(temp_adress + 2);
229
                    if (programMode == 1){
244
                    if (programMode == 1){
230
                        destColor.Intens = pgm_read_byte(temp_adress + 3);
245
                        destColor.Intens = pgm_read_byte(temp_adress + 3);
231
                    }
246
                    }
232
 
247
 
233
                    temp_adress = temp_adress + 4;             
248
                    temp_adress = temp_adress + 4;             
-
 
249
                }
-
 
250
 
-
 
251
            }
-
 
252
 
-
 
253
        }
-
 
254
 
-
 
255
        /* check if any messages have been received */
-
 
256
        if(msg_received){
-
 
257
            /* This node that controls a RGB LED is adressed */
-
 
258
            if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETPROGRAM ){
-
 
259
                /* Set a program! */
-
 
260
                currProgram = rxMsg.Data.bytes[1];
-
 
261
                temp_adress = pgm_read_word(&programs[currProgram-1]);             
-
 
262
            }else if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETINTENS ){
-
 
263
                /* Set intensity! */
-
 
264
                if (rxMsg.Data.bytes[1] == TRUE){
-
 
265
                    currColor.Intens = rxMsg.Data.bytes[2];                
-
 
266
                }
-
 
267
                destColor.Intens = rxMsg.Data.bytes[2];
-
 
268
                if (rxMsg.Data.bytes[3] != 0){
-
 
269
                    dimSpeed = rxMsg.Data.bytes[3];
-
 
270
                }
-
 
271
                currProgram = rxMsg.Data.bytes[1];
-
 
272
                temp_adress = pgm_read_word(&programs[currProgram-1]);
-
 
273
            } else if( rxMsg.Data.bytes[0] == RGBLED_CMD_SETRGB) {
-
 
274
                /* Set RGB-triplet! */
-
 
275
                reformatRGB(rxMsg.Data.bytes[2],rxMsg.Data.bytes[3],rxMsg.Data.bytes[4],0);
-
 
276
                if (rxMsg.Data.bytes[5] != 0){
-
 
277
                    destColor.Intens = rxMsg.Data.bytes[5];
-
 
278
                }
-
 
279
                if (rxMsg.Data.bytes[1] == TRUE){
-
 
280
                    currColor = destColor;
-
 
281
                }
-
 
282
 
-
 
283
                if (rxMsg.Data.bytes[7] != 0){
-
 
284
                    fadeSpeed = rxMsg.Data.bytes[6] * 255 + rxMsg.Data.bytes[7];
234
                }
285
                }
-
 
286
                currProgram = 0;
235
 
287
               
236
            }
288
            }
237
 
-
 
-
 
289
            msg_received = FALSE; //Let's clear the flag yes.
238
        }
290
        }
239
 
291
 
240
    }
292
    }
241
   
293
   
242
    return 0;
294
    return 0;
Line 256... Line 308...
256
    tempcontainer = currColor.R * currColor.Intens / 255;   //I really want to skip this, don't know how yet though.
308
    tempcontainer = currColor.R * currColor.Intens / 255;   //I really want to skip this, don't know how yet though.
257
    OCR1A = tempcontainer; 
309
    OCR1A = tempcontainer; 
258
      OCR0A = currColor.G * currColor.Intens / 255;
310
      OCR0A = currColor.G * currColor.Intens / 255;
259
    OCR0B = currColor.B * currColor.Intens / 255;      
311
    OCR0B = currColor.B * currColor.Intens / 255;      
260
 
312
 
261
       
313
/*     
262
    //Bara för debugcrap
314
    //Bara för debugcrap
263
    Can_Message_t txMsg;
315
    Can_Message_t txMsg;
264
    txMsg.Id = 0x55;
316
    txMsg.Id = 0x55;
265
    txMsg.RemoteFlag = 0;
317
    txMsg.RemoteFlag = 0;
266
    txMsg.ExtendedFlag = 1;
318
    txMsg.ExtendedFlag = 1;
Line 269... Line 321...
269
    txMsg.Data.bytes[1] = currColor.G;
321
    txMsg.Data.bytes[1] = currColor.G;
270
    txMsg.Data.bytes[2] = currColor.B;
322
    txMsg.Data.bytes[2] = currColor.B;
271
    txMsg.Data.bytes[3] = currColor.Intens;
323
    txMsg.Data.bytes[3] = currColor.Intens;
272
 
324
 
273
    bios->can_send(&txMsg);
325
    bios->can_send(&txMsg);
274
 
326
*/
275
}
327
}
276
 
328
 
277
 
329
 
278
 
330
 
279
/************************************************************************************************
331
/************************************************************************************************
280
 *     reformatRGB sets a new target RGB triplet to fade to. It reformats it so that            *
332
 *     reformatRGB sets a new target RGB triplet to fade to. It reformats it so that            *
281
 *     the highest color always gets 0xff. Brightness is recalculated.                          *
333
 *     the highest color always gets 0xff. Brightness is recalculated.                          *
282
 *     if setBright is set to 0, the brightness will not be updated.                    *
334
 *     if setBright is set to 0, the brightness will not be updated.                    *
283
 *     TODO: Smarter 16-bit arithmetics would be nice. Also a pointer to struct to be updated.  *
335
 *     TODO: Smarter 16-bit arithmetics would be nice.                          *
284
 ************************************************************************************************/
336
 ************************************************************************************************/
285
void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright){
337
void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright){
286
    uint8_t maxvalue;
338
    uint8_t maxvalue;
287
    uint16_t tempcontainer;
339
    uint16_t tempcontainer;
288
 
340
 
Line 290... Line 342...
290
        maxvalue = R;
342
        maxvalue = R;
291
    } else if ((G>=R)&(G>=B)){
343
    } else if ((G>=R)&(G>=B)){
292
        maxvalue = G;
344
        maxvalue = G;
293
    } else {
345
    } else {
294
        maxvalue = B;
346
        maxvalue = B;
295
    }
347
    }
296
 
348
 
297
    tempcontainer = R * 255;
349
    tempcontainer = R * 255;
298
    destColor.R = tempcontainer/maxvalue;
350
    destColor.R = tempcontainer/maxvalue;
299
    tempcontainer = G * 255;
351
    tempcontainer = G * 255;
300
    destColor.G = tempcontainer/maxvalue;
352
    destColor.G = tempcontainer/maxvalue;
301
    tempcontainer = B * 255;
353
    tempcontainer = B * 255;
302
    destColor.B = tempcontainer/maxvalue;
354
    destColor.B = tempcontainer/maxvalue;
303
 
355
 
304
    if (setBright){
356
    if (setBright){
305
        destColor.Intens = maxvalue;
357
        destColor.Intens = maxvalue;
306
    }
358
    }
307
}
359
}
308
 
360
 
309
 
361
 
310
/************************************************************************************************
362
/************************************************************************************************
311
 *     fade takes two pointers, current and destination and fades current               *
363
 *     fade takes two pointers, current and destination and fades current               *
312
 *     one tick closer to destination.                                                        *
364
 *     one tick closer to destination.                                                        *
313
 ************************************************************************************************/
365
 ************************************************************************************************/
314
void fade(uint8_t* current, uint8_t* destination){
366
void fade(uint8_t* current, uint8_t* destination){
315
    if (*current > *destination){
367
    if (*current > *destination){
316
        *current = *current - 1;
368
        *current = *current - 1;
317
    } else if (*current < *destination){
369
    } else if (*current < *destination){
318
        *current = *current + 1;
370
        *current = *current + 1;
Line 327... Line 379...
327
 ************************************************************************************************/
379
 ************************************************************************************************/
328
void changeColor(int8_t amount, uint8_t* color){
380
void changeColor(int8_t amount, uint8_t* color){
329
    if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color
381
    if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color
330
    *color = *color + amount;
382
    *color = *color + amount;
331
}
383
}
-
 
384