Subversion Repositories HomeAutomation

Rev

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

Rev 301 Rev 302
Line 3... Line 3...
3
 * Still under heavy development
3
 * Still under heavy development
4
 *
4
 *
5
 * @date    2007-02-04
5
 * @date    2007-02-04
6
 * @author  Martin Nordén
6
 * @author  Martin Nordén
7
 *  
7
 *  
8
 * TODO: A lot, including. Using my new precious struct Color for currColor, destColor and some basic programfunctionality
8
 * TODO: A lot, currently trying to get some kind of ready-made programs to work.
9
 */
9
 */
10
 
10
 
11
/*-----------------------------------------------------------------------------
11
/*-----------------------------------------------------------------------------
12
 * Includes
12
 * Includes
13
 *---------------------------------------------------------------------------*/
13
 *---------------------------------------------------------------------------*/
Line 18... Line 18...
18
#include <stdio.h>
18
#include <stdio.h>
19
/* lib files */
19
/* lib files */
20
#include <can.h>
20
#include <can.h>
21
#include <serial.h>
21
#include <serial.h>
22
#include <timebase.h>
22
#include <timebase.h>
23
#include <math.h>
23
#include <math.h> //Not sure this is needed anymore..
-
 
24
#include <avr/pgmspace.h> //To allow read/write from flash
24
 
25
 
25
 
26
 
26
/*----------------------------------------------------------------------------
27
/*----------------------------------------------------------------------------
27
 * Ehm, struct?
28
 * Ehm, struct?
28
 * -------------------------------------------------------------------------*/
29
 * -------------------------------------------------------------------------*/
29
struct Color
30
typedef struct
30
{
31
{
31
    uint8_t R;
32
    uint8_t R;
32
    uint8_t G;
33
    uint8_t G;
33
    uint8_t B;
34
    uint8_t B;
34
    uint8_t Intens;
35
    uint8_t Intens;
-
 
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}
-
 
47
};
-
 
48
 
-
 
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
35
};
58
};
-
 
59
 
-
 
60
 
-
 
61
uint16_t temp_address = pgm_read_word(&programs[1]);
-
 
62
*/
36
 
63
 
37
/*-----------------------------------------------------------------------------
64
/*-----------------------------------------------------------------------------
38
 * Variables
65
 * Variables
39
 *---------------------------------------------------------------------------*/
66
 *---------------------------------------------------------------------------*/
40
uint32_t timeStamp; //TimeStamp to keep track of time
67
uint32_t timeStamp; //TimeStamp to keep track of time
41
uint32_t dimStamp;  //Timestamp to keep track of dimmer changes
68
uint32_t dimStamp;  //Timestamp to keep track of dimmer changes
42
 
69
 
43
uint32_t tempStamp; //Timestamp to keep track of temp things during development
70
uint32_t tempStamp; //Timestamp to keep track of temp things during development
44
uint8_t temp;
71
uint8_t temp;
45
 
72
 
46
struct Color currColor;  //Current color
73
Color currColor;  //Current color
47
struct Color destColor;  //Destination color
74
Color destColor;  //Destination color
48
 
75
 
49
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange.
76
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange.
50
uint8_t dimSpeed;       //Dimmer speed. Sets the speed of intensitychange.
77
uint8_t dimSpeed;       //Dimmer speed. Sets the speed of intensitychange.
51
 
78
 
52
uint8_t program;        //This variable keeps track of what program is currently running. 
79
uint8_t program;        //This variable keeps track of what program is currently running. 
Line 56... Line 83...
56
 * Functions
83
 * Functions
57
 * -------------------------------------------------------------------------*/
84
 * -------------------------------------------------------------------------*/
58
void updateLED();
85
void updateLED();
59
void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright);
86
void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright);
60
void changeColor(int8_t amount, uint8_t* color);
87
void changeColor(int8_t amount, uint8_t* color);
61
uint8_t fade(uint8_t current, uint8_t destination);
88
void fade(uint8_t* current, uint8_t* destination);
62
 
89
 
63
/*-----------------------------------------------------------------------------
90
/*-----------------------------------------------------------------------------
64
 * Main Program
91
 * Main Program
65
 *---------------------------------------------------------------------------*/
92
 *---------------------------------------------------------------------------*/
66
int main(void) {
93
int main(void) {
Line 110... Line 137...
110
        /* service the CAN routines */
137
        /* service the CAN routines */
111
        Can_Service();
138
        Can_Service();
112
 
139
 
113
        /* Time to fade color? */
140
        /* Time to fade color? */
114
        if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {
141
        if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {
115
            currColor.R = fade(currColor.R, destColor.R);          
142
            fade(&currColor.R, &destColor.R);          
116
            currColor.G = fade(currColor.G, destColor.G);
143
            fade(&currColor.G, &destColor.G);
117
            currColor.B = fade(currColor.B, destColor.B);
144
            fade(&currColor.B, &destColor.B);
118
 
145
 
119
            timeStamp = Timebase_CurrentTime();
146
            timeStamp = Timebase_CurrentTime();
120
            updateLED();
147
            updateLED();
121
        }
148
        }
122
 
149
 
123
 
150
 
124
        /* Time to fade intensity? */
151
        /* Time to fade intensity? */
125
        if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) {
152
        if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) {
126
            currColor.Intens = fade(currColor.Intens, destColor.Intens);
153
            fade(&currColor.Intens, &destColor.Intens);
127
            dimStamp = Timebase_CurrentTime();
154
            dimStamp = Timebase_CurrentTime();
128
            updateLED();
155
            updateLED();
129
        }
156
        }
-
 
157
 
130
 
158
 
-
 
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
        }
131
 
165
 
132
        /* Just a nice fade-demo */
166
        /* Just a nice fade-demo */
133
        if ((Timebase_PassedTimeMillis(tempStamp) >= 2500)&(program == 1)) {
167
        if ((Timebase_PassedTimeMillis(tempStamp) >= 2500)&(program == 1)) {
134
            tempStamp = Timebase_CurrentTime();
168
            tempStamp = Timebase_CurrentTime();
135
           
169
           
Line 186... Line 220...
186
 *     updateLED updates the PWM counter values and thus changes the color of the RGBLED.       *
220
 *     updateLED updates the PWM counter values and thus changes the color of the RGBLED.       *
187
 *     It doesn't reculculate anything, except the values to write to the PWM registers.        *
221
 *     It doesn't reculculate anything, except the values to write to the PWM registers.        *
188
 *     TODO: Get rid of the tempcontainer!                                  *
222
 *     TODO: Get rid of the tempcontainer!                                  *
189
 ************************************************************************************************/
223
 ************************************************************************************************/
190
void updateLED(){
224
void updateLED(){
-
 
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
       
191
    //Bara för debugcrap
232
    //Bara för debugcrap
192
    Can_Message_t txMsg;
233
    Can_Message_t txMsg;
193
    txMsg.Id = 0x55;
234
    txMsg.Id = 0x55;
194
    txMsg.RemoteFlag = 0;
235
    txMsg.RemoteFlag = 0;
195
    txMsg.ExtendedFlag = 1;
236
    txMsg.ExtendedFlag = 1;
196
    txMsg.DataLength = 4;
237
    txMsg.DataLength = 4;
197
 
-
 
198
 
-
 
199
    uint8_t tempcontainer;
-
 
200
 
-
 
201
    tempcontainer = currColor.R * currColor.Intens / 255;   //I really want to skip this, don't know how yet though.
-
 
202
    OCR1A = tempcontainer; 
-
 
203
      OCR0A = currColor.G * currColor.Intens / 255;
-
 
204
    OCR0B = currColor.B * currColor.Intens / 255;      
-
 
205
       
-
 
206
    txMsg.Data.bytes[0] = currColor.R * currColor.Intens / 255;
238
    txMsg.Data.bytes[0] = currColor.R * currColor.Intens / 255;
207
    txMsg.Data.bytes[1] = currColor.G * currColor.Intens / 255;
239
    txMsg.Data.bytes[1] = currColor.G * currColor.Intens / 255;
208
    txMsg.Data.bytes[2] = currColor.B * currColor.Intens / 255;
240
    txMsg.Data.bytes[2] = currColor.B * currColor.Intens / 255;
209
    txMsg.Data.bytes[3] = currColor.Intens;
241
    txMsg.Data.bytes[3] = currColor.Intens;
210
    Can_Send(&txMsg);
242
    Can_Send(&txMsg);
Line 244... Line 276...
244
 
276
 
245
 
277
 
246
/************************************************************************************************
278
/************************************************************************************************
247
 *     fade tar in två värden, current och destination och för current ett steg                 *
279
 *     fade tar in två värden, current och destination och för current ett steg                 *
248
 *     närmare destination.                                                               *
280
 *     närmare destination.                                                               *
249
 *     Ev. TODO: skicka in två pekare och jämna ut dom istället.                        *
-
 
250
 ************************************************************************************************/
281
 ************************************************************************************************/
251
uint8_t fade(uint8_t current, uint8_t destination){
282
void fade(uint8_t* current, uint8_t* destination){
252
    if (current > destination){
283
    if (*current > *destination){
253
        return(current - 1);
284
        *current = *current - 1;
254
    } else if (current < destination){
285
    } else if (*current < *destination){
255
        return(current + 1);
286
        *current = *current + 1;
256
    }
287
    }
257
    return(current);
-
 
258
}
288
}
259
 
289
 
260
 
290
 
261
/************************************************************************************************
291
/************************************************************************************************
262
 *     changeColor ändrar en färg mängden amount. -127>127.                                     *
292
 *     changeColor ändrar en färg mängden amount. -127>127.                                     *