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