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
 *  
8
 */
9
 
10
/*-----------------------------------------------------------------------------
11
 * Includes
12
 *---------------------------------------------------------------------------*/
13
/* system files */
14
#include <avr/io.h>
15
#include <avr/interrupt.h>
16
#include <avr/wdt.h>
17
#include <stdio.h>
18
/* lib files */
19
#include <can.h>
20
#include <serial.h>
21
#include <timebase.h>
22
#include <math.h>
23
 
24
/*-----------------------------------------------------------------------------
25
 * Variables
26
 *---------------------------------------------------------------------------*/
289 noddan 27
uint32_t timeStamp; //TimeStamp to keep track of time
28
uint32_t dimStamp;  //Timestamp to keep track of dimmer changes
285 noddan 29
 
298 noddan 30
uint32_t tempStamp; //Timestamp to keep track of temp things during development
31
uint8_t temp;
32
 
289 noddan 33
uint8_t currR;  //Current Red 0->255
34
uint8_t currG;  //Current Green 0->255
35
uint8_t currB;  //Current Blue 0->255
285 noddan 36
 
287 noddan 37
uint8_t destR;  //Destination Red
38
uint8_t destG;  //Destination Green
39
uint8_t destB;  //Destination Blue
285 noddan 40
 
289 noddan 41
uint8_t currIntens; //Current intensity. 0->255
42
uint8_t destIntens; //Destination intensity. 0->255
286 noddan 43
 
289 noddan 44
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange.
45
uint8_t dimSpeed;       //Dimmer speed. Sets the speed of intensitychange.
285 noddan 46
 
289 noddan 47
uint8_t program;        //This variable keeps track of what program is currently running. 
48
uint8_t programstate;   //This variable keeps track of where in a program we currently are. Will probably be removed later on.
49
 
285 noddan 50
/*----------------------------------------------------------------------------
51
 * Functions
52
 * -------------------------------------------------------------------------*/
53
void updateLED();
287 noddan 54
void reformatRGB(uint8_t R, uint8_t G, uint8_t B);
298 noddan 55
void setDestRGB(uint8_t R, uint8_t G, uint8_t B);
56
 
57
void changeColor(int8_t amount, uint8_t* color);
58
 
289 noddan 59
uint8_t fade(uint8_t current, uint8_t destination);
285 noddan 60
 
61
 
62
/*-----------------------------------------------------------------------------
63
 * Main Program
64
 *---------------------------------------------------------------------------*/
65
int main(void) {
66
    Timebase_Init();
67
    Serial_Init();
286 noddan 68
    Can_Init();
285 noddan 69
 
70
//Setting up OC1A, 16 bit counter used as 8 bit.
71
    TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
72
    TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS11); /* Timer uses main system clock with 1/8 prescale */
73
    ICR1 = 256; //8 bit precision to match the other two counters.
298 noddan 74
    OCR1A = 0;  //0% as standard
285 noddan 75
    DDRB |= (1<<PB1); /* Enable pin as output */
76
 
298 noddan 77
//Setting up OC0A
291 noddan 78
    TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */
298 noddan 79
    TCCR0B |= (1<<CS01)|(1<<CS00); /* Prescaler ???? Check datasheet */
80
    OCR0A = 0;  //0% standard
291 noddan 81
    DDRD |= (1<<PD6);
82
 
298 noddan 83
//Setting up OC0B
291 noddan 84
    TCCR0A |= (1<<COM0B1);
298 noddan 85
    OCR0B = 0;
291 noddan 86
    DDRD |= (1<<PD5);
87
 
298 noddan 88
 
285 noddan 89
//Initialize variables
298 noddan 90
    fadeSpeed = 10;
289 noddan 91
    dimSpeed = 255;
286 noddan 92
    timeStamp = 0;
289 noddan 93
    dimStamp = 0;
285 noddan 94
    Can_Message_t rxMsg;
95
 
298 noddan 96
    temp = 0;
97
 
285 noddan 98
    sei(); 
99
 
298 noddan 100
    reformatRGB(0x00,0xff,0x00);
101
    currR = destR;
102
    currG = destG;
103
    currB = destB;
104
    currIntens = 100;
105
    destIntens = 100;
287 noddan 106
 
298 noddan 107
    program = 0;
289 noddan 108
 
298 noddan 109
 
285 noddan 110
    /* main loop */
111
    while (1) {
112
        /* service the CAN routines */
113
        Can_Service();
286 noddan 114
 
289 noddan 115
        /* Time to fade color? */
298 noddan 116
        if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {           
117
 
118
            changeColor(1, &destB);
119
            changeColor(-1, &destG);
120
 
289 noddan 121
            currR = fade(currR, destR);
122
            currG = fade(currG, destG);
123
            currB = fade(currB, destB);
124
 
285 noddan 125
            timeStamp = Timebase_CurrentTime();
126
            updateLED();
127
        }
128
 
129
 
289 noddan 130
        /* Time to fade intensity? */
131
        if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) {
132
            currIntens = fade(currIntens, destIntens);
133
            dimStamp = Timebase_CurrentTime();
134
            updateLED();
135
        }
136
 
137
 
298 noddan 138
        /* Just a nice fade-demo */
139
        if ((Timebase_PassedTimeMillis(tempStamp) >= 2500)&(program == 1)) {
140
            tempStamp = Timebase_CurrentTime();
141
 
142
            switch(temp){
143
            case 1:
144
                setDestRGB(0xff,0x00,0x00);
145
                break;
146
            case 2:
147
                setDestRGB(0xff,0xff,0x00);
148
                break;
149
            case 3:
150
                setDestRGB(0x00,0xff,0x00);
151
                break;
152
            case 4:
153
                setDestRGB(0x00,0xff,0xff);
154
                break;
155
            case 5:
156
                setDestRGB(0x00,0x00,0xff);
157
                break;
158
            case 6:
159
                setDestRGB(0xff,0x00,0xff);
160
                temp = 255;
161
                break;
162
            }
163
            temp++;
164
 
165
        }
289 noddan 166
 
298 noddan 167
 
285 noddan 168
        /* check if any messages have been received */
169
        /* A lot of remote control crap going on here now for testing */
170
        while (Can_Receive(&rxMsg) == CAN_OK) {
171
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){
172
                program = 1;
173
                fadeSpeed = 0;
174
            }
175
 
176
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){
177
                program = 2;
178
                fadeSpeed = 0;
179
            }
286 noddan 180
        }
285 noddan 181
 
182
 
286 noddan 183
    }
184
 
185
    return 0;
186
}
285 noddan 187
 
188
 
189
 
190
 
191
 
286 noddan 192
void updateLED(){
298 noddan 193
    uint16_t tempcontainer;
285 noddan 194
 
298 noddan 195
    tempcontainer = currR * currIntens / 255;  
196
    OCR1A = tempcontainer;
197
    tempcontainer = currG * currIntens / 255;  
198
      OCR0A = tempcontainer;
199
    tempcontainer = currB * currIntens / 255;      
200
    OCR0B = tempcontainer;
285 noddan 201
 
289 noddan 202
 
203
    //Bara för debugcrap
287 noddan 204
    Can_Message_t txMsg;
205
    txMsg.Id = 0x55;
206
    txMsg.RemoteFlag = 0;
207
    txMsg.ExtendedFlag = 1;
208
    txMsg.DataLength = 4;
209
 
298 noddan 210
    txMsg.Data.bytes[0] = currR;// * currIntens / 255;
211
    txMsg.Data.bytes[1] = currG;// * currIntens / 255;
212
    txMsg.Data.bytes[2] = currB;// * currIntens / 255;
289 noddan 213
    txMsg.Data.bytes[3] = currIntens;
287 noddan 214
    Can_Send(&txMsg);
298 noddan 215
}
285 noddan 216
 
217
 
298 noddan 218
/* setDestRGB sets a new target RGB triplet to fade to. Note that the brightness is kept at approximatly the same level
219
 * even though the color changes */
285 noddan 220
 
298 noddan 221
void setDestRGB(uint8_t R, uint8_t G, uint8_t B){
222
    uint8_t maxvalue;
223
    uint16_t tempcontainer;
224
 
225
    if ((R>=G)&(R>=B)){
226
        maxvalue = R;
227
    } else if ((G>=R)&(G>=B)){
228
        maxvalue = G;
229
    } else {
230
        maxvalue = B;
231
    }
232
 
233
    tempcontainer = R * 255;
234
    destR = tempcontainer/maxvalue;
235
    tempcontainer = G * 255;
236
    destG = tempcontainer/maxvalue;
237
    tempcontainer = B * 255;
238
    destB = tempcontainer/maxvalue;
239
 
285 noddan 240
}
241
 
298 noddan 242
/* reformatRGB sets a new target RGB triplet to fade to. Note that the brightness is recalculated. */
286 noddan 243
 
287 noddan 244
void reformatRGB(uint8_t R, uint8_t G, uint8_t B){
245
    uint8_t maxvalue;
246
    uint16_t tempcontainer;
247
 
298 noddan 248
    if ((R>=G)&(R>=B)){
287 noddan 249
        maxvalue = R;
298 noddan 250
    } else if ((G>=R)&(G>=B)){
287 noddan 251
        maxvalue = G;
252
    } else {
253
        maxvalue = B;
254
    }
255
 
256
    tempcontainer = R * 255;
298 noddan 257
    destR = tempcontainer/maxvalue;
287 noddan 258
    tempcontainer = G * 255;
298 noddan 259
    destG = tempcontainer/maxvalue;
287 noddan 260
    tempcontainer = B * 255;
298 noddan 261
    destB = tempcontainer/maxvalue;
287 noddan 262
 
298 noddan 263
    destIntens = maxvalue;
289 noddan 264
}
287 noddan 265
 
289 noddan 266
uint8_t fade(uint8_t current, uint8_t destination){
267
    if (current > destination){
268
        return(current - 1);
269
    } else if (current < destination){
270
        return(current + 1);
271
    }
272
    return(current);
287 noddan 273
}
298 noddan 274
 
275
/*
276
 * changeColor ändrar en färg mängden amount. -127>127.
277
 * color kan vara dest eller curr R,G,B.
278
 * Ev. TODO: om t.ex. röd är på max och man vill öka röd ska de andra färgerna minskas. */
279
void changeColor(int8_t amount, uint8_t* color){
280
    if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color
281
    *color = *color + amount;
282
}