Rev 305 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 305 | Rev 306 | ||
|---|---|---|---|
| 1 | /** |
1 | /** |
| 2 | * CAN RGB LED. This application is used to control a RGB LED. |
2 | * CAN RGB LED. This application is used to control a RGB LED. |
| 3 | * Still under heavy development |
3 | * Still under heavy development |
| 4 | * |
4 | * |
| 5 | * @date 2007-02- |
5 | * @date 2007-02-10 |
| 6 | * @author Martin Nordén |
6 | * @author Martin Nordén |
| 7 | * |
7 | * |
| - | 8 | * Set RGB: <byte0 RGBLED_CMD_SETRGB> <byte 1 Set/Fade> <byte2 R> <byte3 G> <byte4 B> |
|
| - | 9 | * Change RGB: <byte 0 RGBLED_CMD_CHANGERGB> <byte 1 int R> <byte 2 int G> <byte 3 int B> (Note that they are all signed) |
|
| - | 10 | * Set Intens: <byte0 RGBLED_CMD_SETINTENS> <byte1 Set/Fade> <byte2 Intens> (<byte3 dimSpeed>) |
|
| - | 11 | * |
|
| 8 | * TODO: |
12 | * TODO: Better CAN Integration, Failsafe mode, Tempsensors etc. |
| 9 | */ |
13 | */ |
| - | 14 | ||
| 10 | 15 | ||
| 11 | /*----------------------------------------------------------------------------- |
16 | /*----------------------------------------------------------------------------- |
| 12 | * Includes |
17 | * Includes |
| 13 | *---------------------------------------------------------------------------*/ |
18 | *---------------------------------------------------------------------------*/ |
| 14 | /* system files */ |
19 | /* system files */ |
| 15 | #include <avr/io.h> |
20 | #include <avr/io.h> |
| 16 | #include <avr/interrupt.h> |
21 | #include <avr/interrupt.h> |
| 17 | #include <avr/wdt.h> |
22 | #include <avr/wdt.h> |
| 18 | #include <stdio.h> |
23 | #include <stdio.h> |
| - | 24 | #include <avr/pgmspace.h> //To allow read/write from flash |
|
| 19 | /* lib files */ |
25 | /* lib files */ |
| 20 | #include <can.h> |
26 | #include <can.h> |
| 21 | #include <serial.h> |
27 | #include <serial.h> |
| 22 | #include <timebase.h> |
28 | #include <timebase.h> |
| 23 | #include <math.h> //Not sure this is needed anymore.. |
29 | #include <math.h> //Not sure this is needed anymore.. |
| 24 | #include <avr/pgmspace.h> //To allow read/write from flash |
- | |
| 25 | 30 | ||
| - | 31 | /*---------------------------------------------------------------------------- |
|
| - | 32 | * Defines |
|
| - | 33 | * -------------------------------------------------------------------------*/ |
|
| - | 34 | ||
| - | 35 | //Not implemented yet. Will be implementet when I build a canUSB :) |
|
| - | 36 | ||
| - | 37 | #define RGBLED_CMD_GET 0x1400 |
|
| - | 38 | #define RGBLED_CMD_SEND 0x1401 |
|
| - | 39 | ||
| - | 40 | #define RGBLED_CMD_STATUS 0x00 /* Get status */ |
|
| - | 41 | #define RGBLED_CMD_SETRGB 0x01 /* Set RGB Triplet */ |
|
| - | 42 | #define RGBLED_CMD_CHANGERGB 0x02 /* Change RGB +/- */ |
|
| - | 43 | #define RGBLED_CMD_SETINTENS 0x10 /* Set intensity */ |
|
| 26 | 44 | ||
| 27 | /*---------------------------------------------------------------------------- |
45 | /*---------------------------------------------------------------------------- |
| 28 | * Ehm, struct? |
46 | * Ehm, struct? |
| 29 | * -------------------------------------------------------------------------*/ |
47 | * -------------------------------------------------------------------------*/ |
| 30 | typedef struct |
48 | typedef struct |
| 31 | { |
49 | { |
| 32 | uint8_t R; |
50 | uint8_t R; |
| 33 | uint8_t G; |
51 | uint8_t G; |
| 34 | uint8_t B; |
52 | uint8_t B; |
| 35 | uint8_t Intens; |
53 | uint8_t Intens; |
| 36 | } Color; |
54 | } Color; |
| 37 | 55 | ||
| 38 | /*----------------------------------------------------------------------------- |
56 | /*----------------------------------------------------------------------------- |
| 39 | * Programs! |
57 | * Programs! There will be a better way to add programs here.. some day. |
| 40 | *---------------------------------------------------------------------------*/ |
58 | *---------------------------------------------------------------------------*/ |
| 41 | 59 | ||
| 42 | //Special combinations that means something special |
60 | //Special combinations that means something special |
| 43 | //The combinations are useless for lighting anyway |
61 | //The combinations are useless for lighting anyway |
| 44 | //So we might as well use them! |
62 | //So we might as well use them! |
| 45 | //0x00, 0x00, 0x00, 0x00 means end of program, keep looping it |
63 | //0x00, 0x00, 0x00, 0x00 means end of program, keep looping it |
| 46 | //0x00, 0x00, 0x00, 0x01 means end of program, halt |
64 | //0x00, 0x00, 0x00, 0x01 means end of program, halt |
| 47 | 65 | ||
| 48 | Color pgm1[] PROGMEM = { |
66 | Color pgm1[] PROGMEM = { |
| - | 67 | {0xff,0x00,0x00,0x64}, |
|
| - | 68 | {0xff,0xff,0x00,0x10}, |
|
| - | 69 | {0x00,0xff,0x00,0xa0}, |
|
| - | 70 | {0x00,0xff,0xff,0x7f}, |
|
| - | 71 | {0x00,0x00,0xff,0x7f}, |
|
| - | 72 | {0xff,0x00,0xff,0x40}, |
|
| - | 73 | {0x00,0x00,0x00,0x00} |
|
| - | 74 | }; |
|
| - | 75 | ||
| - | 76 | Color pgm2[] PROGMEM = { |
|
| 49 | {0xff,0x00,0x00,0x00}, |
77 | {0xff,0x00,0x00,0x00}, |
| 50 | {0xff,0xff,0x00,0x00}, |
78 | {0xff,0xff,0x00,0x00}, |
| 51 | { |
79 | {0xff,0xff,0xff,0x00}, |
| 52 | { |
80 | {0xa0,0x00,0xa0,0x00}, |
| 53 | {0x00,0x00,0xff,0x00}, |
81 | {0x00,0x00,0xff,0x00}, |
| 54 | {0xff,0x00,0xff,0x00}, |
- | |
| 55 | {0x00,0x00,0x00,0x00} |
82 | {0x00,0x00,0x00,0x00} |
| 56 | }; |
83 | }; |
| 57 | 84 | ||
| 58 | Color |
85 | Color pgm3[] PROGMEM = { |
| 59 | {0x20,0x20,0x30,0x50}, |
- | |
| 60 | { |
86 | {0xff,0xff,0xff,0x00}, |
| 61 | { |
87 | {0x00,0x00,0x00,0x01} |
| 62 | }; |
88 | }; |
| 63 | 89 | ||
| 64 | Color* programs[] PROGMEM = { |
90 | Color* programs[] PROGMEM = { |
| 65 | (Color*)pgm1, |
91 | (Color*)pgm1, |
| 66 | (Color*)pgm2 |
92 | (Color*)pgm2, |
| - | 93 | (Color*)pgm3 |
|
| 67 | }; |
94 | }; |
| 68 | 95 | ||
| 69 | 96 | ||
| 70 | 97 | ||
| 71 | /*----------------------------------------------------------------------------- |
98 | /*----------------------------------------------------------------------------- |
| 72 | * Variables |
99 | * Variables |
| 73 | *---------------------------------------------------------------------------*/ |
100 | *---------------------------------------------------------------------------*/ |
| 74 | uint32_t timeStamp; //TimeStamp to keep track of time |
101 | uint32_t timeStamp; //TimeStamp to keep track of time |
| 75 | uint32_t dimStamp; //Timestamp to keep track of dimmer changes |
102 | uint32_t dimStamp; //Timestamp to keep track of dimmer changes |
| 76 | 103 | ||
| 77 | Color currColor; //Current color |
104 | Color currColor; //Current color |
| 78 | Color destColor; //Destination color |
105 | Color destColor; //Destination color |
| 79 | 106 | ||
| 80 | uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange. |
107 | uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange. |
| 81 | uint8_t dimSpeed; //Dimmer speed. Sets the speed of intensitychange. |
108 | uint8_t dimSpeed; //Dimmer speed. Sets the speed of intensitychange. |
| 82 | 109 | ||
| 83 | uint8_t currProgram; //This variable keeps track of what program is currently running. |
110 | uint8_t currProgram; //This variable keeps track of what program is currently running. |
| - | 111 | uint8_t programMode; //This keeps track of what mode we are currently using. 0: the program obeyes the current brightness, 1: the program is allowed to set it's own brightness |
|
| 84 | 112 | ||
| 85 | /*---------------------------------------------------------------------------- |
113 | /*---------------------------------------------------------------------------- |
| 86 | * Functions |
114 | * Functions |
| 87 | * -------------------------------------------------------------------------*/ |
115 | * -------------------------------------------------------------------------*/ |
| 88 | void updateLED(); |
116 | void updateLED(); |
| 89 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright); |
117 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright); |
| 90 | void changeColor(int8_t amount, uint8_t* color); |
118 | void changeColor(int8_t amount, uint8_t* color); |
| 91 | void fade(uint8_t* current, uint8_t* destination); |
119 | void fade(uint8_t* current, uint8_t* destination); |
| 92 | 120 | ||
| 93 | /*----------------------------------------------------------------------------- |
121 | /*----------------------------------------------------------------------------- |
| 94 | * Main Program |
122 | * Main Program |
| 95 | *---------------------------------------------------------------------------*/ |
123 | *---------------------------------------------------------------------------*/ |
| 96 | int main(void) { |
124 | int main(void) { |
| 97 | - | ||
| 98 | Timebase_Init(); |
125 | Timebase_Init(); |
| 99 | Serial_Init(); |
126 | Serial_Init(); |
| 100 | Can_Init(); |
127 | Can_Init(); |
| 101 | 128 | ||
| 102 | //Setting up OC1A, 16 bit counter used as 8 bit. |
129 | //Setting up OC1A, 16 bit counter used as 8 bit. |
| 103 | TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */ |
130 | TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */ |
| 104 | TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS12); /* Timer uses main system clock with ? prescale */ |
131 | TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS12); /* Timer uses main system clock with ? prescale */ |
| 105 | ICR1 = 256; //8 bit precision to match the other two counters. |
132 | ICR1 = 256; //8 bit precision to match the other two counters. |
| 106 | OCR1A = 0; //0% as standard |
133 | OCR1A = 0; //0% as standard |
| 107 | DDRB |= (1<<PB1); /* Enable pin as output */ |
134 | DDRB |= (1<<PB1); /* Enable pin as output */ |
| 108 | - | ||
| 109 | //Setting up OC0A |
135 | //Setting up OC0A |
| 110 | TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */ |
136 | TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */ |
| 111 | TCCR0B |= (1<<CS02); /* Prescaler updating now */ |
137 | TCCR0B |= (1<<CS02); /* Prescaler updating now */ |
| 112 | OCR0A = 0; //0% standard |
138 | OCR0A = 0; //0% standard |
| 113 | DDRD |= (1<<PD6); |
139 | DDRD |= (1<<PD6); |
| 114 | - | ||
| 115 | //Setting up OC0B |
140 | //Setting up OC0B |
| 116 | TCCR0A |= (1<<COM0B1); |
141 | TCCR0A |= (1<<COM0B1); |
| 117 | OCR0B = 0; |
142 | OCR0B = 0; |
| 118 | DDRD |= (1<<PD5); |
143 | DDRD |= (1<<PD5); |
| 119 | - | ||
| 120 | 144 | ||
| 121 | //Initialize variables |
145 | //Initialize variables |
| 122 | fadeSpeed = 10; |
146 | fadeSpeed = 10; |
| 123 | dimSpeed = |
147 | dimSpeed = 10; |
| 124 | timeStamp = 0; |
148 | timeStamp = 0; |
| 125 | dimStamp = 0; |
149 | dimStamp = 0; |
| 126 | Can_Message_t rxMsg; |
150 | Can_Message_t rxMsg; |
| 127 | 151 | ||
| 128 | reformatRGB(0x00,0xff,0x00,1); |
152 | reformatRGB(0x00,0xff,0x00,1); |
| 129 | destColor.Intens = |
153 | destColor.Intens = 250; |
| 130 | currColor = destColor; |
154 | currColor = destColor; |
| 131 | 155 | ||
| 132 | //Just temporary for now, they keep track of some kind of demo-program. |
156 | //Just temporary for now, they keep track of some kind of demo-program. |
| 133 | currProgram = 1; |
157 | currProgram = 1; |
| - | 158 | programMode = 0; |
|
| 134 | uint16_t temp_adress = pgm_read_word(&programs[currProgram-1]); //Get adress for program 0 |
159 | uint16_t temp_adress = pgm_read_word(&programs[currProgram-1]); //Get adress for program 0 |
| 135 | - | ||
| 136 | 160 | ||
| 137 | sei(); |
161 | sei(); |
| 138 | 162 | ||
| 139 | /* main loop */ |
163 | /* main loop */ |
| 140 | while (1) { |
164 | while (1) { |
| 141 | /* service the CAN routines */ |
165 | /* service the CAN routines */ |
| 142 | Can_Service(); |
166 | Can_Service(); |
| 143 | 167 | ||
| 144 | /* Time to fade color? */ |
168 | /* Time to fade color? */ |
| 145 | if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) { |
169 | if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) { |
| 146 | fade(&currColor.R, &destColor.R); |
170 | fade(&currColor.R, &destColor.R); |
| 147 | fade(&currColor.G, &destColor.G); |
171 | fade(&currColor.G, &destColor.G); |
| 148 | fade(&currColor.B, &destColor.B); |
172 | fade(&currColor.B, &destColor.B); |
| 149 | 173 | ||
| 150 | timeStamp = Timebase_CurrentTime(); |
174 | timeStamp = Timebase_CurrentTime(); |
| 151 | updateLED(); |
175 | updateLED(); |
| 152 | } |
176 | } |
| 153 | 177 | ||
| 154 | 178 | ||
| 155 | /* Time to fade intensity? */ |
179 | /* Time to fade intensity? */ |
| 156 | if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) { |
180 | if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) { |
| 157 | fade(&currColor.Intens, &destColor.Intens); |
181 | fade(&currColor.Intens, &destColor.Intens); |
| 158 | dimStamp = Timebase_CurrentTime(); |
182 | dimStamp = Timebase_CurrentTime(); |
| 159 | updateLED(); |
183 | updateLED(); |
| 160 | } |
184 | } |
| 161 | 185 | ||
| 162 | 186 | ||
| 163 | /* Program management */ |
187 | /* Program management */ |
| 164 | if (currProgram != 0){ //Check if a program is currently running |
188 | if (currProgram != 0){ //Check if a program is currently running |
| 165 | if ((currColor.R == destColor.R)&(currColor.G == destColor.G)&(currColor.B == destColor.B)&(currColor.Intens == destColor.Intens)){//Is the current sequence finished? |
189 | if ((currColor.R == destColor.R)&(currColor.G == destColor.G)&(currColor.B == destColor.B)&(currColor.Intens == destColor.Intens)){//Is the current sequence finished? |
| 166 | //Now we have to check if the program is finished and if we should loop |
190 | //Now we have to check if the program is finished and if we should loop |
| 167 | if ((pgm_read_byte(temp_adress)==0)&(pgm_read_byte(temp_adress+1)==0)&(pgm_read_byte(temp_adress+2)==0)){ |
191 | if ((pgm_read_byte(temp_adress)==0)&(pgm_read_byte(temp_adress+1)==0)&(pgm_read_byte(temp_adress+2)==0)){ |
| 168 | //The program has ended! Shall we loop it? |
192 | //The program has ended! Shall we loop it? |
| 169 | if (pgm_read_byte(temp_adress+3)==0){ |
193 | if (pgm_read_byte(temp_adress+3)==0){ |
| 170 | //Yes, let's loop it |
194 | //Yes, let's loop it |
| 171 | temp_adress = pgm_read_word(&programs[currProgram-1]); |
195 | temp_adress = pgm_read_word(&programs[currProgram-1]); |
| 172 | } else { |
196 | } else { |
| 173 | //No, halt! |
197 | //No, halt! |
| 174 | currProgram = 0; |
198 | currProgram = 0; |
| 175 | } |
199 | } |
| 176 | } else { |
200 | } else { |
| 177 | //The program has not ended, let's get the next RGB-triplet! |
201 | //The program has not ended, let's get the next RGB-triplet! |
| - | 202 | destColor.R = pgm_read_byte(temp_adress); |
|
| 178 |
|
203 | destColor.G = pgm_read_byte(temp_adress + 1); |
| 179 |
|
204 | destColor.B = pgm_read_byte(temp_adress + 2); |
| - | 205 | if (programMode == 1){ |
|
| - | 206 | destColor.Intens = pgm_read_byte(temp_adress + 3); |
|
| 180 | } |
207 | } |
| - | 208 | ||
| - | 209 | temp_adress = temp_adress + 4; |
|
| - | 210 | } |
|
| 181 | } |
211 | } |
| 182 | } |
212 | } |
| 183 | 213 | ||
| 184 | /* check if any messages have been received */ |
214 | /* check if any messages have been received */ |
| 185 | /* A lot of remote control crap going on here now for testing */ |
215 | /* A lot of remote control crap going on here now for testing */ |
| 186 | while (Can_Receive(&rxMsg) == CAN_OK) { |
216 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| 187 | if ((rxMsg.Data.bytes[0] == |
217 | if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x01)){ |
| 188 |
|
218 | currProgram = 1; |
| - | 219 | temp_adress = pgm_read_word(&programs[currProgram-1]); |
|
| - | 220 | } |
|
| - | 221 | ||
| - | 222 | if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x02)){ |
|
| 189 |
|
223 | currProgram = 2; |
| - | 224 | temp_adress = pgm_read_word(&programs[currProgram-1]); |
|
| 190 | } |
225 | } |
| 191 | 226 | ||
| 192 | if ((rxMsg.Data.bytes[0] == |
227 | if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x03)){ |
| 193 |
|
228 | currProgram = 3; |
| - | 229 | temp_adress = pgm_read_word(&programs[currProgram-1]); |
|
| - | 230 | } |
|
| - | 231 | ||
| - | 232 | if ((rxMsg.Data.bytes[0] == 0xf)&(rxMsg.Data.bytes[3] == 0x00)){ |
|
| 194 |
|
233 | currProgram = 0; |
| 195 | } |
234 | } |
| - | 235 | ||
| - | 236 | ||
| - | 237 | ||
| 196 | } |
238 | } |
| 197 | 239 | ||
| 198 | } |
240 | } |
| 199 | 241 | ||
| 200 | return 0; |
242 | return 0; |
| 201 | } |
243 | } |
| 202 | 244 | ||
| 203 | 245 | ||
| 204 | 246 | ||
| 205 | 247 | ||
| 206 | /************************************************************************************************ |
248 | /************************************************************************************************ |
| 207 | * updateLED updates the PWM counter values and thus changes the color of the RGBLED. * |
249 | * updateLED updates the PWM counter values and thus changes the color of the RGBLED. * |
| 208 | * It doesn't reculculate anything, except the values to write to the PWM registers. * |
250 | * It doesn't reculculate anything, except the values to write to the PWM registers. * |
| 209 | * TODO: Get rid of the tempcontainer! * |
251 | * TODO: Get rid of the tempcontainer! * |
| 210 | ************************************************************************************************/ |
252 | ************************************************************************************************/ |
| 211 | void updateLED(){ |
253 | void updateLED(){ |
| 212 | uint8_t tempcontainer; |
254 | uint8_t tempcontainer; |
| 213 | 255 | ||
| 214 | tempcontainer = currColor.R * currColor.Intens / 255; //I really want to skip this, don't know how yet though. |
256 | tempcontainer = currColor.R * currColor.Intens / 255; //I really want to skip this, don't know how yet though. |
| 215 | OCR1A = tempcontainer; |
257 | OCR1A = tempcontainer; |
| 216 | OCR0A = currColor.G * currColor.Intens / 255; |
258 | OCR0A = currColor.G * currColor.Intens / 255; |
| 217 | OCR0B = currColor.B * currColor.Intens / 255; |
259 | OCR0B = currColor.B * currColor.Intens / 255; |
| - | 260 | ||
| 218 | 261 | /* |
|
| 219 | //Bara för debugcrap |
262 | //Bara för debugcrap |
| 220 | Can_Message_t txMsg; |
263 | Can_Message_t txMsg; |
| 221 | txMsg.Id = 0x55; |
264 | txMsg.Id = 0x55; |
| 222 | txMsg.RemoteFlag = 0; |
265 | txMsg.RemoteFlag = 0; |
| 223 | txMsg.ExtendedFlag = 1; |
266 | txMsg.ExtendedFlag = 1; |
| 224 | txMsg.DataLength = 8; |
267 | txMsg.DataLength = 8; |
| 225 | txMsg.Data.bytes[0] = currColor.R; |
268 | txMsg.Data.bytes[0] = currColor.R; |
| 226 | txMsg.Data.bytes[1] = currColor.G; |
269 | txMsg.Data.bytes[1] = currColor.G; |
| 227 | txMsg.Data.bytes[2] = currColor.B; |
270 | txMsg.Data.bytes[2] = currColor.B; |
| 228 | txMsg.Data.bytes[3] = currColor.Intens; |
271 | txMsg.Data.bytes[3] = currColor.Intens; |
| 229 | 272 | ||
| 230 | Can_Send(&txMsg); |
273 | Can_Send(&txMsg); |
| - | 274 | */ |
|
| 231 | } |
275 | } |
| 232 | 276 | ||
| 233 | 277 | ||
| 234 | 278 | ||
| 235 | /************************************************************************************************ |
279 | /************************************************************************************************ |
| 236 | * reformatRGB sets a new target RGB triplet to fade to. It reformats it so that * |
280 | * reformatRGB sets a new target RGB triplet to fade to. It reformats it so that * |
| 237 | * the highest color always gets 0xff. Brightness is recalculated. * |
281 | * the highest color always gets 0xff. Brightness is recalculated. * |
| 238 | * if setBright is set to 0, the brightness will not be updated. * |
282 | * if setBright is set to 0, the brightness will not be updated. * |
| 239 | * TODO: Smarter 16-bit arithmetics would be nice. Also a pointer to struct to be updated. * |
283 | * TODO: Smarter 16-bit arithmetics would be nice. Also a pointer to struct to be updated. * |
| 240 | ************************************************************************************************/ |
284 | ************************************************************************************************/ |
| 241 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright){ |
285 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright){ |
| 242 | uint8_t maxvalue; |
286 | uint8_t maxvalue; |
| 243 | uint16_t tempcontainer; |
287 | uint16_t tempcontainer; |
| 244 | 288 | ||
| 245 | if ((R>=G)&(R>=B)){ |
289 | if ((R>=G)&(R>=B)){ |
| 246 | maxvalue = R; |
290 | maxvalue = R; |
| 247 | } else if ((G>=R)&(G>=B)){ |
291 | } else if ((G>=R)&(G>=B)){ |
| 248 | maxvalue = G; |
292 | maxvalue = G; |
| 249 | } else { |
293 | } else { |
| 250 | maxvalue = B; |
294 | maxvalue = B; |
| 251 | } |
295 | } |
| 252 | 296 | ||
| 253 | tempcontainer = R * 255; |
297 | tempcontainer = R * 255; |
| 254 | destColor.R = tempcontainer/maxvalue; |
298 | destColor.R = tempcontainer/maxvalue; |
| 255 | tempcontainer = G * 255; |
299 | tempcontainer = G * 255; |
| 256 | destColor.G = tempcontainer/maxvalue; |
300 | destColor.G = tempcontainer/maxvalue; |
| 257 | tempcontainer = B * 255; |
301 | tempcontainer = B * 255; |
| 258 | destColor.B = tempcontainer/maxvalue; |
302 | destColor.B = tempcontainer/maxvalue; |
| 259 | 303 | ||
| 260 | if (setBright){ |
304 | if (setBright){ |
| 261 | destColor.Intens = maxvalue; |
305 | destColor.Intens = maxvalue; |
| 262 | } |
306 | } |
| 263 | } |
307 | } |
| 264 | 308 | ||
| 265 | 309 | ||
| 266 | /************************************************************************************************ |
310 | /************************************************************************************************ |
| 267 | * fade |
311 | * fade takes two pointers, current and destination and fades current * |
| 268 | * |
312 | * one tick closer to destination. * |
| 269 | ************************************************************************************************/ |
313 | ************************************************************************************************/ |
| 270 | void fade(uint8_t* current, uint8_t* destination){ |
314 | void fade(uint8_t* current, uint8_t* destination){ |
| 271 | if (*current > *destination){ |
315 | if (*current > *destination){ |
| 272 | *current = *current - 1; |
316 | *current = *current - 1; |
| 273 | } else if (*current < *destination){ |
317 | } else if (*current < *destination){ |
| 274 | *current = *current + 1; |
318 | *current = *current + 1; |
| 275 | } |
319 | } |
| 276 | } |
320 | } |
| 277 | 321 | ||
| 278 | 322 | ||
| 279 | /************************************************************************************************ |
323 | /************************************************************************************************ |
| 280 | * changeColor |
324 | * changeColor changes a color accord to amount. -127>127. * |
| 281 | * color |
325 | * color is either dest och curr R,G,B. * |
| 282 | * |
326 | * Maybe TODO: If red is already 255 and we want to increase it, decrease the other ones. * |
| 283 | ************************************************************************************************/ |
327 | ************************************************************************************************/ |
| 284 | void changeColor(int8_t amount, uint8_t* color){ |
328 | void changeColor(int8_t amount, uint8_t* color){ |
| 285 | if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color |
329 | if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color |
| 286 | *color = *color + amount; |
330 | *color = *color + amount; |
| 287 | } |
331 | } |
| 288 | 332 | ||
| 289 | 333 | ||