Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

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