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
 
289 noddan 30
uint8_t currR;  //Current Red 0->255
31
uint8_t currG;  //Current Green 0->255
32
uint8_t currB;  //Current Blue 0->255
285 noddan 33
 
287 noddan 34
uint8_t destR;  //Destination Red
35
uint8_t destG;  //Destination Green
36
uint8_t destB;  //Destination Blue
285 noddan 37
 
289 noddan 38
uint8_t currIntens; //Current intensity. 0->255
39
uint8_t destIntens; //Destination intensity. 0->255
286 noddan 40
 
289 noddan 41
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange.
42
uint8_t dimSpeed;       //Dimmer speed. Sets the speed of intensitychange.
285 noddan 43
 
289 noddan 44
uint8_t program;        //This variable keeps track of what program is currently running. 
45
uint8_t programstate;   //This variable keeps track of where in a program we currently are. Will probably be removed later on.
46
 
285 noddan 47
/*----------------------------------------------------------------------------
48
 * Functions
49
 * -------------------------------------------------------------------------*/
50
void updateLED();
287 noddan 51
void reformatRGB(uint8_t R, uint8_t G, uint8_t B);
289 noddan 52
uint8_t fade(uint8_t current, uint8_t destination);
285 noddan 53
 
54
 
55
/*-----------------------------------------------------------------------------
56
 * Main Program
57
 *---------------------------------------------------------------------------*/
58
int main(void) {
59
    Timebase_Init();
60
    Serial_Init();
286 noddan 61
    Can_Init();
285 noddan 62
 
63
//Setting up OC1A, 16 bit counter used as 8 bit.
64
    /* Use OC1A */
65
    TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
66
    TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS11); /* Timer uses main system clock with 1/8 prescale */
67
    ICR1 = 256; //8 bit precision to match the other two counters.
68
    OCR1A = 128;  //50% as standard
69
    DDRB |= (1<<PB1); /* Enable pin as output */
70
 
71
//Initialize variables
287 noddan 72
    fadeSpeed = 250;
289 noddan 73
    dimSpeed = 255;
286 noddan 74
    timeStamp = 0;
289 noddan 75
    dimStamp = 0;
285 noddan 76
 
77
    Can_Message_t rxMsg;
289 noddan 78
    currR = 0x3f;
287 noddan 79
    currG = 0x15;
289 noddan 80
    currB = 0x63;
285 noddan 81
 
82
    sei(); 
83
 
287 noddan 84
    reformatRGB(currR,currG,currB);
85
 
289 noddan 86
    destR = 0xff;
87
    currIntens = 100;
88
    destIntens = 100;
89
 
285 noddan 90
    /* main loop */
91
    while (1) {
92
        /* service the CAN routines */
93
        Can_Service();
286 noddan 94
 
289 noddan 95
        /* Time to fade color? */
285 noddan 96
        if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {
289 noddan 97
            currR = fade(currR, destR);
98
            currG = fade(currG, destG);
99
            currB = fade(currB, destB);
100
 
285 noddan 101
            timeStamp = Timebase_CurrentTime();
102
            updateLED();
103
        }
104
 
105
 
289 noddan 106
        /* Time to fade intensity? */
107
        if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) {
108
            currIntens = fade(currIntens, destIntens);
109
            dimStamp = Timebase_CurrentTime();
110
            updateLED();
111
        }
112
 
113
 
114
 
285 noddan 115
        /* check if any messages have been received */
116
        /* A lot of remote control crap going on here now for testing */
117
        while (Can_Receive(&rxMsg) == CAN_OK) {
118
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){
119
                program = 1;
120
                fadeSpeed = 0;
121
            }
122
 
123
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){
124
                program = 2;
125
                fadeSpeed = 0;
126
            }
286 noddan 127
        }
285 noddan 128
 
129
 
286 noddan 130
    }
131
 
132
    return 0;
133
}
285 noddan 134
 
135
 
136
 
137
 
138
 
286 noddan 139
void updateLED(){
285 noddan 140
 
289 noddan 141
    OCR1A = currR * currIntens / 255;
142
    //TODO: Här ska givetvis de andra två färgerna också uppdateras.
285 noddan 143
 
289 noddan 144
 
145
    //Bara för debugcrap
287 noddan 146
    Can_Message_t txMsg;
147
    txMsg.Id = 0x55;
148
    txMsg.RemoteFlag = 0;
149
    txMsg.ExtendedFlag = 1;
150
    txMsg.DataLength = 4;
151
 
289 noddan 152
    txMsg.Data.bytes[0] = currR * currIntens / 255;
153
    txMsg.Data.bytes[1] = currG * currIntens / 255;
154
    txMsg.Data.bytes[2] = currB * currIntens / 255;
155
    txMsg.Data.bytes[3] = currIntens;
285 noddan 156
 
287 noddan 157
    Can_Send(&txMsg);
285 noddan 158
 
159
 
160
 
161
//Red
162
//Green
163
//Blue
164
}
165
 
286 noddan 166
 
289 noddan 167
 
287 noddan 168
void reformatRGB(uint8_t R, uint8_t G, uint8_t B){
169
    uint8_t maxvalue;
170
    uint16_t tempcontainer;
171
 
172
    if ((R>G)&(R>B)){
173
        maxvalue = R;
174
    } else if ((G>R)&(G>B)){
175
        maxvalue = G;
176
    } else {
177
        maxvalue = B;
178
    }
179
 
180
    tempcontainer = R * 255;
181
    currR = tempcontainer/maxvalue;
182
    tempcontainer = G * 255;
183
    currG = tempcontainer/maxvalue;
184
    tempcontainer = B * 255;
185
    currB = tempcontainer/maxvalue;
186
 
289 noddan 187
    currIntens = maxvalue;
188
}
287 noddan 189
 
289 noddan 190
uint8_t fade(uint8_t current, uint8_t destination){
191
    if (current > destination){
192
        return(current - 1);
193
    } else if (current < destination){
194
        return(current + 1);
195
    }
196
    return(current);
287 noddan 197
}