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.
291 noddan 68
    OCR1A = 1;  //50% as standard
285 noddan 69
    DDRB |= (1<<PB1); /* Enable pin as output */
70
 
291 noddan 71
/* Use OC0A */
72
    TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */
73
    TCCR0B |= (1<<CS02);//(1<<CS02)|(1<<CS00); /* Prescaler 1/1024 */
74
    OCR0A = 40;
75
    DDRD |= (1<<PD6);
76
 
77
/* Use OC0B */
78
    TCCR0A |= (1<<COM0B1);
79
    OCR0B = 40;
80
    DDRD |= (1<<PD5);
81
 
285 noddan 82
//Initialize variables
287 noddan 83
    fadeSpeed = 250;
289 noddan 84
    dimSpeed = 255;
286 noddan 85
    timeStamp = 0;
289 noddan 86
    dimStamp = 0;
285 noddan 87
 
88
    Can_Message_t rxMsg;
291 noddan 89
    currR = 0x64;
287 noddan 90
    currG = 0x15;
289 noddan 91
    currB = 0x63;
285 noddan 92
 
93
    sei(); 
94
 
287 noddan 95
    reformatRGB(currR,currG,currB);
96
 
291 noddan 97
    currIntens = 20;
98
    destIntens = 200;
289 noddan 99
 
285 noddan 100
    /* main loop */
101
    while (1) {
102
        /* service the CAN routines */
103
        Can_Service();
286 noddan 104
 
289 noddan 105
        /* Time to fade color? */
285 noddan 106
        if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {
289 noddan 107
            currR = fade(currR, destR);
108
            currG = fade(currG, destG);
109
            currB = fade(currB, destB);
110
 
285 noddan 111
            timeStamp = Timebase_CurrentTime();
112
            updateLED();
113
        }
114
 
115
 
289 noddan 116
        /* Time to fade intensity? */
117
        if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) {
118
            currIntens = fade(currIntens, destIntens);
119
            dimStamp = Timebase_CurrentTime();
120
            updateLED();
121
        }
122
 
123
 
124
 
285 noddan 125
        /* check if any messages have been received */
126
        /* A lot of remote control crap going on here now for testing */
127
        while (Can_Receive(&rxMsg) == CAN_OK) {
128
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){
129
                program = 1;
130
                fadeSpeed = 0;
131
            }
132
 
133
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){
134
                program = 2;
135
                fadeSpeed = 0;
136
            }
286 noddan 137
        }
285 noddan 138
 
139
 
286 noddan 140
    }
141
 
142
    return 0;
143
}
285 noddan 144
 
145
 
146
 
147
 
148
 
286 noddan 149
void updateLED(){
285 noddan 150
 
291 noddan 151
//  OCR1A = currR * currIntens / 255;
289 noddan 152
    //TODO: Här ska givetvis de andra två färgerna också uppdateras.
285 noddan 153
 
289 noddan 154
 
155
    //Bara för debugcrap
287 noddan 156
    Can_Message_t txMsg;
157
    txMsg.Id = 0x55;
158
    txMsg.RemoteFlag = 0;
159
    txMsg.ExtendedFlag = 1;
160
    txMsg.DataLength = 4;
161
 
289 noddan 162
    txMsg.Data.bytes[0] = currR * currIntens / 255;
163
    txMsg.Data.bytes[1] = currG * currIntens / 255;
164
    txMsg.Data.bytes[2] = currB * currIntens / 255;
165
    txMsg.Data.bytes[3] = currIntens;
285 noddan 166
 
287 noddan 167
    Can_Send(&txMsg);
285 noddan 168
 
169
 
170
 
171
//Red
172
//Green
173
//Blue
174
}
175
 
286 noddan 176
 
287 noddan 177
void reformatRGB(uint8_t R, uint8_t G, uint8_t B){
178
    uint8_t maxvalue;
179
    uint16_t tempcontainer;
180
 
181
    if ((R>G)&(R>B)){
182
        maxvalue = R;
183
    } else if ((G>R)&(G>B)){
184
        maxvalue = G;
185
    } else {
186
        maxvalue = B;
187
    }
188
 
189
    tempcontainer = R * 255;
190
    currR = tempcontainer/maxvalue;
191
    tempcontainer = G * 255;
192
    currG = tempcontainer/maxvalue;
193
    tempcontainer = B * 255;
194
    currB = tempcontainer/maxvalue;
195
 
289 noddan 196
    currIntens = maxvalue;
197
}
287 noddan 198
 
289 noddan 199
uint8_t fade(uint8_t current, uint8_t destination){
200
    if (current > destination){
201
        return(current - 1);
202
    } else if (current < destination){
203
        return(current + 1);
204
    }
205
    return(current);
287 noddan 206
}