Subversion Repositories HomeAutomation

Rev

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

Rev 287 Rev 289
Line 22... Line 22...
22
#include <math.h>
22
#include <math.h>
23
 
23
 
24
/*-----------------------------------------------------------------------------
24
/*-----------------------------------------------------------------------------
25
 * Variables
25
 * Variables
26
 *---------------------------------------------------------------------------*/
26
 *---------------------------------------------------------------------------*/
27
uint32_t timeStamp;
27
uint32_t timeStamp; //TimeStamp to keep track of time
-
 
28
uint32_t dimStamp;  //Timestamp to keep track of dimmer changes
28
 
29
 
29
uint8_t currR;  //Current Red
30
uint8_t currR;  //Current Red 0->255
30
uint8_t currG;  //Current Green
31
uint8_t currG;  //Current Green 0->255
31
uint8_t currB;  //Current Blue
32
uint8_t currB;  //Current Blue 0->255
32
 
33
 
33
uint8_t destR;  //Destination Red
34
uint8_t destR;  //Destination Red
34
uint8_t destG;  //Destination Green
35
uint8_t destG;  //Destination Green
35
uint8_t destB;  //Destination Blue
36
uint8_t destB;  //Destination Blue
36
 
37
 
37
uint8_t intensity;  //Current intensity.
38
uint8_t currIntens; //Current intensity. 0->255
-
 
39
uint8_t destIntens; //Destination intensity. 0->255
38
 
40
 
39
uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading
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.
-
 
43
 
40
uint8_t program;        //This variable keeps track of what program is currently running. 0 means no program.
44
uint8_t program;        //This variable keeps track of what program is currently running. 
41
uint8_t programstate;   //This variable keeps track of where in a program we currently are.
45
uint8_t programstate;   //This variable keeps track of where in a program we currently are. Will probably be removed later on.
42
 
46
 
43
/*----------------------------------------------------------------------------
47
/*----------------------------------------------------------------------------
44
 * Functions
48
 * Functions
45
 * -------------------------------------------------------------------------*/
49
 * -------------------------------------------------------------------------*/
46
void updateLED();
50
void updateLED();
47
void reformatRGB(uint8_t R, uint8_t G, uint8_t B);
51
void reformatRGB(uint8_t R, uint8_t G, uint8_t B);
-
 
52
uint8_t fade(uint8_t current, uint8_t destination);
48
 
53
 
49
 
54
 
50
/*-----------------------------------------------------------------------------
55
/*-----------------------------------------------------------------------------
51
 * Main Program
56
 * Main Program
52
 *---------------------------------------------------------------------------*/
57
 *---------------------------------------------------------------------------*/
Line 63... Line 68...
63
    OCR1A = 128;  //50% as standard
68
    OCR1A = 128;  //50% as standard
64
    DDRB |= (1<<PB1); /* Enable pin as output */
69
    DDRB |= (1<<PB1); /* Enable pin as output */
65
 
70
 
66
//Initialize variables
71
//Initialize variables
67
    fadeSpeed = 250;
72
    fadeSpeed = 250;
-
 
73
    dimSpeed = 255;
68
    timeStamp = 0;
74
    timeStamp = 0;
-
 
75
    dimStamp = 0;
69
 
76
 
70
    Can_Message_t rxMsg;
77
    Can_Message_t rxMsg;
71
    currR = 0x53;
78
    currR = 0x3f;
72
    currG = 0x15;
79
    currG = 0x15;
73
    currB = 0xa0;
80
    currB = 0x63;
74
 
81
 
75
    sei(); 
82
    sei(); 
76
 
83
 
77
    reformatRGB(currR,currG,currB);
84
    reformatRGB(currR,currG,currB);
-
 
85
 
-
 
86
    destR = 0xff;
-
 
87
    currIntens = 100;
-
 
88
    destIntens = 100;
78
 
89
 
79
    /* main loop */
90
    /* main loop */
80
    while (1) {
91
    while (1) {
81
        /* service the CAN routines */
92
        /* service the CAN routines */
82
        Can_Service();
93
        Can_Service();
83
 
94
 
84
        /* Time to see if we want to update our values */
95
        /* Time to fade color? */
85
        if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {
96
        if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {
-
 
97
            currR = fade(currR, destR);
-
 
98
            currG = fade(currG, destG);
86
            intensity = intensity -1;
99
            currB = fade(currB, destB);
-
 
100
 
87
            timeStamp = Timebase_CurrentTime();
101
            timeStamp = Timebase_CurrentTime();
88
            updateLED();
102
            updateLED();
89
        }
103
        }
-
 
104
 
-
 
105
 
-
 
106
        /* Time to fade intensity? */
-
 
107
        if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) {
-
 
108
            currIntens = fade(currIntens, destIntens);
-
 
109
            dimStamp = Timebase_CurrentTime();
-
 
110
            updateLED();
-
 
111
        }
-
 
112
 
90
 
113
 
91
 
114
 
92
        /* check if any messages have been received */
115
        /* check if any messages have been received */
93
        /* A lot of remote control crap going on here now for testing */
116
        /* A lot of remote control crap going on here now for testing */
94
        while (Can_Receive(&rxMsg) == CAN_OK) {
117
        while (Can_Receive(&rxMsg) == CAN_OK) {
Line 98... Line 121...
98
            }
121
            }
99
 
122
 
100
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){
123
            if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){
101
                program = 2;
124
                program = 2;
102
                fadeSpeed = 0;
125
                fadeSpeed = 0;
103
            }
126
            }
104
        }
127
        }
105
 
128
 
106
 
129
 
107
    }
130
    }
108
   
131
   
109
    return 0;
132
    return 0;
110
}
133
}
111
 
-
 
112
 
134
 
113
 
135
 
114
 
136
 
115
 
137
 
116
void updateLED(){
-
 
117
 
138
 
118
OCR1A = currR * intensity / 255;
139
void updateLED(){
119
 
140
 
-
 
141
    OCR1A = currR * currIntens / 255;
-
 
142
    //TODO: Här ska givetvis de andra två färgerna också uppdateras.
-
 
143
 
-
 
144
   
-
 
145
    //Bara för debugcrap
120
    Can_Message_t txMsg;
146
    Can_Message_t txMsg;
121
    txMsg.Id = 0x55;
147
    txMsg.Id = 0x55;
122
    txMsg.RemoteFlag = 0;
148
    txMsg.RemoteFlag = 0;
123
    txMsg.ExtendedFlag = 1;
149
    txMsg.ExtendedFlag = 1;
124
    txMsg.DataLength = 4;
150
    txMsg.DataLength = 4;
125
   
151
   
126
    txMsg.Data.bytes[0] = currR * intensity / 255;
152
    txMsg.Data.bytes[0] = currR * currIntens / 255;
127
    txMsg.Data.bytes[1] = currG * intensity / 255;
153
    txMsg.Data.bytes[1] = currG * currIntens / 255;
128
    txMsg.Data.bytes[2] = currB * intensity / 255;
154
    txMsg.Data.bytes[2] = currB * currIntens / 255;
129
    txMsg.Data.bytes[3] = intensity;
155
    txMsg.Data.bytes[3] = currIntens;
130
 
156
 
131
    Can_Send(&txMsg);
157
    Can_Send(&txMsg);
132
 
158
 
133
 
159
 
134
 
160
 
135
//Red
161
//Red
136
//Green
162
//Green
137
//Blue
163
//Blue
138
}
164
}
-
 
165
 
139
 
166
 
140
 
167
 
141
void reformatRGB(uint8_t R, uint8_t G, uint8_t B){
168
void reformatRGB(uint8_t R, uint8_t G, uint8_t B){
142
    uint8_t maxvalue;
169
    uint8_t maxvalue;
143
    uint16_t tempcontainer;
170
    uint16_t tempcontainer;
Line 155... Line 182...
155
    tempcontainer = G * 255;
182
    tempcontainer = G * 255;
156
    currG = tempcontainer/maxvalue;
183
    currG = tempcontainer/maxvalue;
157
    tempcontainer = B * 255;
184
    tempcontainer = B * 255;
158
    currB = tempcontainer/maxvalue;
185
    currB = tempcontainer/maxvalue;
159
 
186
 
160
    intensity = maxvalue;
187
    currIntens = maxvalue;
-
 
188
}
161
 
189
 
-
 
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);
162
}
197
}