root / branches / library_refactor / projects / libdragonfly / lights.c @ 1104
History | View | Annotate | Download (19.6 KB)
1 |
/**
|
---|---|
2 |
* Copyright (c) 2007 Colony Project
|
3 |
*
|
4 |
* Permission is hereby granted, free of charge, to any person
|
5 |
* obtaining a copy of this software and associated documentation
|
6 |
* files (the "Software"), to deal in the Software without
|
7 |
* restriction, including without limitation the rights to use,
|
8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
* copies of the Software, and to permit persons to whom the
|
10 |
* Software is furnished to do so, subject to the following
|
11 |
* conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be
|
14 |
* included in all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
23 |
* OTHER DEALINGS IN THE SOFTWARE.
|
24 |
**/
|
25 |
|
26 |
/**
|
27 |
* @file ligths.c
|
28 |
* @brief Orbs
|
29 |
*
|
30 |
* Implemenation for the orbs (tri-colored LEDs)
|
31 |
*
|
32 |
* @author Colony Project, CMU Robotics Club
|
33 |
* @bug Unfinished
|
34 |
**/
|
35 |
|
36 |
/*
|
37 |
lights.c
|
38 |
Controls orb1 and orb2. Can be extended for a software PWM that may be used
|
39 |
for servos in the future.
|
40 |
|
41 |
author: CMU Robotics Club, Colony Project
|
42 |
|
43 |
Change Log:
|
44 |
3/31/2009 - Martin
|
45 |
Rewritten from scratch (mostly), fixes some code duplication, long ISRs,
|
46 |
bugs, unnecessary synchronized code, memory waste
|
47 |
*/
|
48 |
|
49 |
/*
|
50 |
Operation:
|
51 |
On timer overflow:
|
52 |
- switch on LEDs (where value>0, according to a pre-determined mask)
|
53 |
- load the first output compare value
|
54 |
At compare match:
|
55 |
- switch off LEDs (according to mask)
|
56 |
- load the next output compare value
|
57 |
|
58 |
*/
|
59 |
|
60 |
/*
|
61 |
TODO:
|
62 |
- do the inversion of flags when computing then, not in the ISR
|
63 |
- do the timing right
|
64 |
- handle 0 values (use the pwm_init_mask)
|
65 |
- Determine sorting time, possibly improve sorting algorithm
|
66 |
*/
|
67 |
|
68 |
/*
|
69 |
* Random notes:
|
70 |
* - Current motor frequency is 32 us/30 KHz
|
71 |
* - AVR suckage: there is not timer mode with immediate OCR update and
|
72 |
* overflow interrupt at TOP (CTC value)
|
73 |
* - AVR suckage: Set on compare match/Clear on overflow not available with
|
74 |
* non-PWM modes (especially not with immediate OCR update)
|
75 |
* - Frequency is 120 Hz (8 ms) next lower (prescaler) is 30 Hz which flickers
|
76 |
* Not that we could still use the slower prescaler and manually reload
|
77 |
* after 127. This would still cost resolution, but 128 steps should be
|
78 |
* enough.
|
79 |
* - Overflow interrupt 2.5 us (0.03%), compare interrupts are 6*10us (when
|
80 |
* using all different values) (0.75%) or 1*26 us (when using all same
|
81 |
* values)
|
82 |
* - Where to put the time base?
|
83 |
* - buzzer => doesn't work because of varying frequency
|
84 |
* - motors => possible? would trigger often (?)
|
85 |
* - lights => must put lights on 16 bit timer (no OCR left)
|
86 |
*/
|
87 |
|
88 |
#include "lights.h" |
89 |
#include <avr/interrupt.h> |
90 |
#include "dragonfly_lib.h" |
91 |
|
92 |
|
93 |
// *********
|
94 |
// ** I/O **
|
95 |
// *********
|
96 |
|
97 |
#define NUM_ORBS 2 // Number or orbs |
98 |
#define NUM_COLORS 3 // Number of colors per orb |
99 |
|
100 |
// Orb port
|
101 |
#define ORBPORT PORTC
|
102 |
#define ORBDDR DDRC
|
103 |
|
104 |
// Orb pins
|
105 |
#define ORB1_RED 0 |
106 |
#define ORB1_GREEN 1 |
107 |
#define ORB1_BLUE 2 |
108 |
#define ORB2_RED 4 |
109 |
#define ORB2_GREEN 5 |
110 |
#define ORB2_BLUE 6 |
111 |
|
112 |
|
113 |
// ***********
|
114 |
// ** Masks **
|
115 |
// ***********
|
116 |
|
117 |
// Some useful bit masks. All of them are are calculated from the I/O
|
118 |
// definitions above. The calculations should be done at compile time (even if
|
119 |
// they are not, they are only executed once at startup).
|
120 |
|
121 |
// Masks for the individual LEDs
|
122 |
#define orb1_red_mask _BV (ORB1_RED )
|
123 |
#define orb1_green_mask _BV (ORB1_GREEN)
|
124 |
#define orb1_blue_mask _BV (ORB1_BLUE )
|
125 |
#define orb2_red_mask _BV (ORB2_RED )
|
126 |
#define orb2_green_mask _BV (ORB2_GREEN)
|
127 |
#define orb2_blue_mask _BV (ORB2_BLUE )
|
128 |
|
129 |
// Mask for all LEDs
|
130 |
const uint8_t all_orbs_mask=
|
131 |
orb1_red_mask | orb1_green_mask | orb1_blue_mask | |
132 |
orb2_red_mask | orb2_green_mask | orb2_blue_mask; |
133 |
|
134 |
// Mask for the individual LEDs, organized as an array for programmatic access.
|
135 |
// The layout of this array is orb_mask[orb_num, color_num]
|
136 |
const uint8_t orb_mask[NUM_ORBS][NUM_COLORS]=
|
137 |
{ |
138 |
{ orb1_red_mask, orb1_green_mask, orb1_blue_mask }, |
139 |
{ orb2_red_mask, orb2_green_mask, orb2_blue_mask } |
140 |
}; |
141 |
|
142 |
// ***********
|
143 |
// ** Types **
|
144 |
// ***********
|
145 |
|
146 |
struct pwm_channel_t
|
147 |
{ |
148 |
uint8_t time; |
149 |
uint8_t mask; |
150 |
}; |
151 |
|
152 |
|
153 |
// ***************
|
154 |
// ** Variables **
|
155 |
// ***************
|
156 |
|
157 |
#define num_pwm_channels NUM_ORBS*NUM_COLORS
|
158 |
struct pwm_channel_t pwm_channels[num_pwm_channels];
|
159 |
uint8_t orb_values[NUM_ORBS][NUM_COLORS]; |
160 |
uint8_t pwm_init_mask; |
161 |
// TODO use everywhere, add methods to switch on/off, add init function
|
162 |
bool enable_orb_pwm=true; |
163 |
|
164 |
|
165 |
// ****************
|
166 |
// ** Timer ISRs **
|
167 |
// ****************
|
168 |
|
169 |
volatile uint8_t current_pwm_channel=0; |
170 |
|
171 |
SIGNAL (SIG_OVERFLOW0) |
172 |
{ |
173 |
PORTF=2;
|
174 |
// Turn all appropriate PWM channels on
|
175 |
ORBPORT&=~pwm_init_mask; |
176 |
|
177 |
// Start at the first channel
|
178 |
current_pwm_channel=0;
|
179 |
|
180 |
// Load the first OCR
|
181 |
OCR0=pwm_channels[current_pwm_channel].time; |
182 |
PORTF=0;
|
183 |
} |
184 |
|
185 |
SIGNAL(SIG_OUTPUT_COMPARE0) |
186 |
{ |
187 |
PORTF=4;
|
188 |
// TODO:
|
189 |
// - delayed interrupt
|
190 |
// - synchronization OK?
|
191 |
|
192 |
// If the interrupt is executed w/o delay, TCNT0 == time+1 (and TIME=OCR0)
|
193 |
|
194 |
// TODO improve (check overflow; maybe use return after last; fix
|
195 |
// 0/1/254/255 values)
|
196 |
while (TCNT0==pwm_channels[current_pwm_channel].time+1) |
197 |
{ |
198 |
// Turn the current channel off
|
199 |
ORBPORT|=pwm_channels[current_pwm_channel].mask; |
200 |
|
201 |
// Increment the channel
|
202 |
current_pwm_channel++; |
203 |
|
204 |
// If there is a next channel, load its OCR value
|
205 |
if (current_pwm_channel<=(num_pwm_channels-1)) |
206 |
if (pwm_channels[current_pwm_channel].time<255) |
207 |
OCR0=pwm_channels[current_pwm_channel].time; |
208 |
} |
209 |
PORTF=0;
|
210 |
} |
211 |
|
212 |
|
213 |
|
214 |
// ************************************
|
215 |
// ** Internal orb setting functions **
|
216 |
// ************************************
|
217 |
|
218 |
// TODO: make a public version of this one, but keep a private one which does
|
219 |
// not sort them so you can update both sides and update only once.
|
220 |
|
221 |
static void apply_orbs (void) |
222 |
{ |
223 |
if (enable_orb_pwm)
|
224 |
{ |
225 |
// PWM mode
|
226 |
|
227 |
// Sort the orb values.
|
228 |
|
229 |
// 1. Write the orb values and corresponding masks to the pwm channels
|
230 |
// array unsorted
|
231 |
for (uint8_t orb=0; orb<2; ++orb) |
232 |
{ |
233 |
for (uint8_t color=0; color<3; ++color) |
234 |
{ |
235 |
// TODO this should be faster w/o multiplication
|
236 |
uint8_t index=NUM_COLORS*orb+color; |
237 |
pwm_channels[index].time=orb_values[orb][color]; |
238 |
pwm_channels[index].mask=orb_mask[orb][color]; |
239 |
} |
240 |
} |
241 |
|
242 |
// 2. Sort the values. Use bubble sort for now.
|
243 |
for (uint8_t count=num_pwm_channels-1; count>0; --count) |
244 |
{ |
245 |
for (uint8_t i=num_pwm_channels-1; i>0; --i) |
246 |
{ |
247 |
if (pwm_channels[i].time<pwm_channels[i-1].time) |
248 |
{ |
249 |
uint8_t temp; |
250 |
|
251 |
// Swap the times
|
252 |
temp=pwm_channels[i].time; |
253 |
pwm_channels[i].time=pwm_channels[i-1].time;
|
254 |
pwm_channels[i-1].time=temp;
|
255 |
|
256 |
// Swap the masks
|
257 |
temp=pwm_channels[i].mask; |
258 |
pwm_channels[i].mask=pwm_channels[i-1].mask;
|
259 |
pwm_channels[i-1].mask=temp;
|
260 |
} |
261 |
} |
262 |
} |
263 |
|
264 |
pwm_init_mask=all_orbs_mask; // FIXME use real values
|
265 |
|
266 |
// Sort the values from orb_values[NUM_ORBS][NUM_COLORS].
|
267 |
|
268 |
} |
269 |
else
|
270 |
{ |
271 |
// Binary mode.
|
272 |
// Don't do anything, the orbs pins are set in orb_n_set.
|
273 |
// It would be more consistent to set them here (because you could
|
274 |
// update them independently and then apply the changes at once), but it
|
275 |
// is faster this way, and being fast is the whole point of using the
|
276 |
// binary orb mode anyway.
|
277 |
} |
278 |
} |
279 |
|
280 |
static void orb_n_set (uint8_t num, uint8_t red, uint8_t green, uint8_t blue) |
281 |
{ |
282 |
if (enable_orb_pwm)
|
283 |
{ |
284 |
// PWM mode
|
285 |
orb_values[num][0]=red;
|
286 |
orb_values[num][1]=green;
|
287 |
orb_values[num][2]=blue;
|
288 |
} |
289 |
else
|
290 |
{ |
291 |
// Binary mode
|
292 |
// The outputs are inverted.
|
293 |
if (!red) ORBPORT|=orb_mask[num][0]; else ORBPORT&=~orb_mask[num][0]; |
294 |
if (!green) ORBPORT|=orb_mask[num][1]; else ORBPORT&=~orb_mask[num][1]; |
295 |
if (!blue) ORBPORT|=orb_mask[num][2]; else ORBPORT&=~orb_mask[num][2]; |
296 |
} |
297 |
} |
298 |
|
299 |
// ************************************
|
300 |
// ** Frontend orb setting functions **
|
301 |
// ************************************
|
302 |
|
303 |
// All of these functions use orb_n_set to set the actual values, and then call
|
304 |
// apply_orbs() to apply the changes. orb_n_set should be used (although it
|
305 |
// would be faster to set the array directly) because the binary/pwm mode has
|
306 |
// to be handled.
|
307 |
|
308 |
/**
|
309 |
* Set orb1 to the color specified. orb_init must be called before this function
|
310 |
* may be used.
|
311 |
*
|
312 |
* @param red the red component of the color
|
313 |
* @param green the green component of the color
|
314 |
* @param blue the blue component of the color
|
315 |
*
|
316 |
* @see orb_init
|
317 |
**/
|
318 |
void orb1_set (uint8_t red, uint8_t green, uint8_t blue)
|
319 |
{ |
320 |
orb_n_set (0, red, green, blue);
|
321 |
apply_orbs (); |
322 |
} |
323 |
|
324 |
/**
|
325 |
* Set orb2 to the color specified. orb_init must be called before this function
|
326 |
* may be used.
|
327 |
*
|
328 |
* @param red_led the red component of the color
|
329 |
* @param green_led the green component of the color
|
330 |
* @param blue_led the blue component of the color
|
331 |
*
|
332 |
* @see orb_init
|
333 |
**/
|
334 |
void orb2_set (uint8_t red, uint8_t green, uint8_t blue)
|
335 |
{ |
336 |
orb_n_set (1, red, green, blue);
|
337 |
apply_orbs (); |
338 |
} |
339 |
|
340 |
/**
|
341 |
* Set both orbs to the color specified. orb_init must be called before this
|
342 |
* function may be used.
|
343 |
*
|
344 |
* @param red_led the red component of the color
|
345 |
* @param green_led the green component of the color
|
346 |
* @param blue_led the blue component of the color
|
347 |
*
|
348 |
* @see orb_init, orb1_set, orb2_set
|
349 |
**/
|
350 |
void orb_set (uint8_t red, uint8_t green, uint8_t blue)
|
351 |
{ |
352 |
orb_n_set (0, red, green, blue);
|
353 |
orb_n_set (1, red, green, blue);
|
354 |
apply_orbs (); |
355 |
} |
356 |
|
357 |
void orbs_set (
|
358 |
uint8_t red1, uint8_t green1, uint8_t blue1, |
359 |
uint8_t red2, uint8_t green2, uint8_t blue2) |
360 |
{ |
361 |
orb_n_set (0, red1, green1, blue1);
|
362 |
orb_n_set (1, red2, green2, blue2);
|
363 |
apply_orbs (); |
364 |
} |
365 |
|
366 |
|
367 |
|
368 |
|
369 |
//////////////////////////////////////////////////////////////////////////////////////////////////////////
|
370 |
|
371 |
//#define ORB_RESET 1025
|
372 |
//#define ORBMASK 0x77
|
373 |
//
|
374 |
///***** Port and Pin Definitions ****/
|
375 |
//
|
376 |
//
|
377 |
//// an orb node
|
378 |
//struct ORB_NODE {
|
379 |
// uint8_t num;
|
380 |
// uint16_t angle;
|
381 |
//};
|
382 |
//
|
383 |
//the change in an orb
|
384 |
//struct ORB_CHANGE {
|
385 |
// uint16_t port_val;
|
386 |
// uint16_t split_time_period;
|
387 |
//};
|
388 |
//
|
389 |
//// the status of an orb
|
390 |
//struct ORB_STATUS_STRUCT {
|
391 |
// struct ORB_NODE orbs[ORB_COUNT];
|
392 |
// uint16_t orb_angles[ORB_COUNT];
|
393 |
// struct ORB_CHANGE changes[ORB_COUNT+1];
|
394 |
// uint8_t change_count;
|
395 |
// uint8_t new_angles;
|
396 |
// uint8_t current_orb;
|
397 |
//
|
398 |
//} ORB_STATUS;
|
399 |
//
|
400 |
//void orb_sort(void);
|
401 |
//void orb_setup_pulse(void);
|
402 |
//
|
403 |
//SIGNAL (SIG_OUTPUT_COMPARE3C){
|
404 |
//
|
405 |
// //pull the correct ones down
|
406 |
// ORBPORT &= (~ORBMASK)|(ORB_STATUS.changes[ORB_STATUS.current_orb].port_val);
|
407 |
//
|
408 |
// ++ORB_STATUS.current_orb; //now look at next orb transition
|
409 |
//
|
410 |
// if (ORB_STATUS.current_orb < ORB_STATUS.change_count) { //if it isnt the end...
|
411 |
//
|
412 |
// //setup timer for next pull down
|
413 |
// OCR3C = TCNT3+ORB_STATUS.changes[ORB_STATUS.current_orb].split_time_period;
|
414 |
//
|
415 |
// }
|
416 |
// else { //we are done with these pulses
|
417 |
// orb_setup_pulse();
|
418 |
// }
|
419 |
//
|
420 |
//}
|
421 |
//
|
422 |
//
|
423 |
////sets a channel to a value
|
424 |
//void orb_set_angle(uint16_t orb, uint16_t angle) {
|
425 |
// uint8_t mysreg;
|
426 |
//
|
427 |
// orb=orb&0x07; //only have 8
|
428 |
// angle=angle&0xff; //only accept 0-255
|
429 |
// angle=255-angle; //inverse intensity
|
430 |
// angle=angle<<2; //scale up so that we dont run it too often
|
431 |
// angle+=3; //0 values dont really work
|
432 |
// if (ORB_STATUS.orb_angles[orb] != angle) { //if the angle has changed
|
433 |
// mysreg=SREG;
|
434 |
// cli(); //disable interrupts
|
435 |
// ORB_STATUS.orb_angles[orb] = angle; //update angle
|
436 |
// ORB_STATUS.new_angles = 1;
|
437 |
// SREG=mysreg; //put interrupt status back
|
438 |
// }
|
439 |
//}
|
440 |
//
|
441 |
//
|
442 |
//void orb_sort(void) {
|
443 |
// uint16_t done = 0, i;
|
444 |
//
|
445 |
// while (! done) {
|
446 |
// done = 1;
|
447 |
//
|
448 |
// for (i = 0; i < ORB_COUNT - 1; ++i) { //loop through all
|
449 |
//
|
450 |
// //if they are out of order, swap them
|
451 |
// if (ORB_STATUS.orbs[i].angle > ORB_STATUS.orbs[i+1].angle) {
|
452 |
// ORB_STATUS.orbs[i].angle ^= ORB_STATUS.orbs[i+1].angle;
|
453 |
// ORB_STATUS.orbs[i+1].angle ^= ORB_STATUS.orbs[i].angle;
|
454 |
// ORB_STATUS.orbs[i].angle ^= ORB_STATUS.orbs[i+1].angle;
|
455 |
//
|
456 |
// ORB_STATUS.orbs[i].num ^= ORB_STATUS.orbs[i+1].num;
|
457 |
// ORB_STATUS.orbs[i+1].num ^= ORB_STATUS.orbs[i].num;
|
458 |
// ORB_STATUS.orbs[i].num ^= ORB_STATUS.orbs[i+1].num;
|
459 |
//
|
460 |
// done = 0;
|
461 |
// }
|
462 |
// }
|
463 |
// }
|
464 |
//}
|
465 |
//
|
466 |
////calculate the split times
|
467 |
//void orb_setup_pulse(void) {
|
468 |
// uint16_t i;
|
469 |
// uint16_t my_port;
|
470 |
// uint16_t sum = 0;
|
471 |
// uint16_t split_time;
|
472 |
//
|
473 |
// my_port = 0xff; //all on
|
474 |
//
|
475 |
// if (ORB_STATUS.new_angles) {
|
476 |
//
|
477 |
// ORB_STATUS.change_count = 0;
|
478 |
// for (i = 0; i < ORB_COUNT; ++i) { //get the new values
|
479 |
// ORB_STATUS.orbs[i].angle = ORB_STATUS.orb_angles[ORB_STATUS.orbs[i].num];
|
480 |
// }
|
481 |
//
|
482 |
// orb_sort(); //sort them
|
483 |
// ORB_STATUS.new_angles = 0;
|
484 |
//
|
485 |
// for (i = 0; i < ORB_COUNT; ++i) { //calculate split times
|
486 |
// split_time = ORB_STATUS.orbs[i].angle - sum;
|
487 |
// my_port &= ~_BV(ORB_STATUS.orbs[i].num);
|
488 |
//
|
489 |
// for (; i < ORB_COUNT - 1 && ORB_STATUS.orbs[i].angle == ORB_STATUS.orbs[i+1].angle; ++i) {
|
490 |
// my_port &= ~_BV(ORB_STATUS.orbs[i+1].num); //look for doups
|
491 |
// }
|
492 |
//
|
493 |
// ORB_STATUS.changes[ORB_STATUS.change_count].port_val = my_port; //which pins are low
|
494 |
// ORB_STATUS.changes[ORB_STATUS.change_count].split_time_period = split_time;
|
495 |
//
|
496 |
// ++ORB_STATUS.change_count;
|
497 |
//
|
498 |
// sum += split_time;
|
499 |
// }
|
500 |
//
|
501 |
// ORB_STATUS.changes[ORB_STATUS.change_count].port_val = my_port;
|
502 |
// ORB_STATUS.changes[ORB_STATUS.change_count].split_time_period = ORB_RESET - sum; //get a constant period
|
503 |
//
|
504 |
// ++ORB_STATUS.change_count;
|
505 |
//
|
506 |
// }
|
507 |
//
|
508 |
//
|
509 |
//
|
510 |
// ORB_STATUS.current_orb = 0;
|
511 |
//
|
512 |
// ORBPORT |= ORBMASK; //start with all high
|
513 |
// OCR3C = TCNT3 + ORB_STATUS.changes[0].split_time_period; //wait for first split
|
514 |
//
|
515 |
//}
|
516 |
//
|
517 |
///**
|
518 |
// * @defgroup orbs Orbs
|
519 |
// * @brief Functions for controlling the color of the orbs.
|
520 |
// *
|
521 |
// * Functions for controlling the color and lighting of the orbs.
|
522 |
// *
|
523 |
// * @{
|
524 |
// **/
|
525 |
//
|
526 |
|
527 |
/**
|
528 |
* Set both orbs to the specified color. This function
|
529 |
* is intended to be used with the predefined
|
530 |
* colors. orb_init must be called before this
|
531 |
* function may be used.
|
532 |
*
|
533 |
* @param col the color to set the orbs to
|
534 |
*
|
535 |
* @see orb_init
|
536 |
**/
|
537 |
void orb_set_color(uint8_t col)
|
538 |
{ |
539 |
// uint16_t red, green, blue;
|
540 |
//
|
541 |
// red = ((col & 0xE0) >> 5) * 36;
|
542 |
// green = ((col & 0x1C) >> 2) * 36;
|
543 |
// blue = (col & 0x03) * 85;
|
544 |
//
|
545 |
// orb_set(red, green, blue);
|
546 |
} |
547 |
|
548 |
/**
|
549 |
* Set orb1 to the specified color. This function
|
550 |
* is intended to be used with the predefined
|
551 |
* colors. orb_init must be called before this
|
552 |
* function may be used.
|
553 |
*
|
554 |
* @param col the color to set the orbs to
|
555 |
*
|
556 |
* @see orb_init
|
557 |
**/
|
558 |
void orb1_set_color(uint8_t col)
|
559 |
{ |
560 |
// uint16_t red, green, blue;
|
561 |
//
|
562 |
// red = ((col & 0xE0) >> 5) * 36;
|
563 |
// green = ((col & 0x1C) >> 2) * 36;
|
564 |
// blue = (col & 0x03) * 85;
|
565 |
//
|
566 |
// orb1_set(red, green, blue);
|
567 |
} |
568 |
|
569 |
/**
|
570 |
* Set orb2 to the specified color. This function
|
571 |
* is intended to be used with the predefined
|
572 |
* colors. orb_init must be called before this
|
573 |
* function may be used.
|
574 |
*
|
575 |
* @param col the color to set the orbs to
|
576 |
*
|
577 |
* @see orb_init
|
578 |
**/
|
579 |
void orb2_set_color(uint8_t col)
|
580 |
{ |
581 |
// uint16_t red, green, blue;
|
582 |
//
|
583 |
// red = ((col & 0xE0) >> 5) * 36;
|
584 |
// green = ((col & 0x1C) >> 2) * 36;
|
585 |
// blue = (col & 0x03) * 85;
|
586 |
//
|
587 |
// orb2_set(red, green, blue);
|
588 |
} |
589 |
|
590 |
//DOES THIS WORK?
|
591 |
// Disables the timer1 interrupt, disabling the Orb's color fading capabilities
|
592 |
// You can still turn the red, green, and blue leds on and off with set_orb_dio
|
593 |
/* If we use the PWM for anything else besides the ORB, this implementation needs to be done better */
|
594 |
/**
|
595 |
* Disables the orb color fading capabilities
|
596 |
* by disabling the timer1 interrupt.
|
597 |
*
|
598 |
* @see orb_init
|
599 |
**/
|
600 |
void orb_disable()
|
601 |
{ |
602 |
// TCCR3B &= 0; //Turn off everything
|
603 |
// ORB_PORT |= _BV(ORB1_RED);
|
604 |
// ORB_PORT |= _BV(ORB1_GREEN);
|
605 |
// ORB_PORT |= _BV(ORB1_BLUE);
|
606 |
// ORB_PORT |= _BV(ORB2_RED);
|
607 |
// ORB_PORT |= _BV(ORB2_GREEN);
|
608 |
// ORB_PORT |= _BV(ORB2_BLUE);
|
609 |
} |
610 |
|
611 |
//DOES THIS WORK?
|
612 |
// Enables the timer1 interrupt, enabling the Orb's color fading capabilities
|
613 |
/**
|
614 |
* Enables the orb's color fading capabilities.
|
615 |
*
|
616 |
* @see orb_init
|
617 |
**/
|
618 |
void orb_enable()
|
619 |
{ |
620 |
//// TCCR0 |= _BV(COM01) | _BV(COM00) | _BV(WGM00) | _BV(CS01); //Toggle OC Pin on match, FAST PWM Mode, clock/8
|
621 |
// TCCR3B =_BV(CS31);
|
622 |
} |
623 |
|
624 |
/** @} **/ //end group |
625 |
|
626 |
|
627 |
// ********************
|
628 |
// ** Initialization **
|
629 |
// ********************
|
630 |
|
631 |
/**
|
632 |
* Initializes the PWM for Orb control. This must be called before
|
633 |
* the orbs are used for them to function.
|
634 |
**/
|
635 |
void orb_init ()
|
636 |
{ |
637 |
// Use 8 bit TC0. Timer mode:
|
638 |
// We cannot use CTC mode because it can only clear on OCR0 (in contrast
|
639 |
// to the 16 bit timers which can also use the ICR for that) and OCR0 is
|
640 |
// already used for generating output compare interrupts. We also need
|
641 |
// immediate (non double buffered) update of OCR0, so the only mode left
|
642 |
// is "Normal".
|
643 |
// Note that for a timer counting from 0 to 255, there are 256 states and
|
644 |
// thus 257 output possibilities (0/256...256/256)! Possible ways to deal
|
645 |
// with that:
|
646 |
// 1. use a 16 bit variable for the PWM value (memory waste, overhead)
|
647 |
// 2. use an additional flag for the 257th value (inconvenient)
|
648 |
// 3. use 1/256...256/256 (skip 0, never complete off)
|
649 |
// 4. use 0/256...256/256 (skip 256, never complete on)
|
650 |
// 5. skip a value somewhere in the middle
|
651 |
// For this implementation, variant 4 was chosen.
|
652 |
// Using and 8 bit timer has the added advantage that all the comparisons
|
653 |
// are faster.
|
654 |
|
655 |
// Enable the output ports and turn off the LEDs
|
656 |
ORBDDR |= all_orbs_mask; |
657 |
ORBPORT |= all_orbs_mask; |
658 |
|
659 |
// Set all orbs to "off"
|
660 |
orb_set (0, 0, 0); |
661 |
|
662 |
// *** Set up the timer
|
663 |
|
664 |
// Normal mode, Compare match output off, Prescaler
|
665 |
TCCR0=_BV(CS02) | _BV(CS01) | _BV(CS00); // 1024, 30 Hz
|
666 |
TCCR0=_BV(CS02) | _BV(CS01); // 1024, 30 Hz
|
667 |
|
668 |
// Enable compare match and overflow interrupts
|
669 |
TIMSK=_BV(OCIE0) | _BV(TOIE0); |
670 |
|
671 |
// Debug
|
672 |
DDRF=6;
|
673 |
|
674 |
|
675 |
// The output compare flag (and interrupt) is set at the next timer clock
|
676 |
// cycle after compare match. So time=pwm_value-1 (for pwm_value==0: don't
|
677 |
// switch on at all)
|
678 |
// ORB1: red
|
679 |
// ORB2: green
|
680 |
|
681 |
// Left: greenish red, Right: greenish blue
|
682 |
// For testing, set some pretty colors
|
683 |
orbs_set (250, 127, 3, 3, 127, 250); |
684 |
//orbs_set (252, 129, 5, 3, 127, 250);
|
685 |
//orbs_set (127, 127, 127, 127, 127, 127);
|
686 |
|
687 |
// // Init mask: all masks where value>0
|
688 |
// pwm_init_mask=
|
689 |
// orb1_red_mask |
|
690 |
// orb1_green_mask |
|
691 |
// orb1_blue_mask |
|
692 |
// orb2_green_mask |
|
693 |
// orb2_blue_mask
|
694 |
// ;
|
695 |
//
|
696 |
// // Channels: value-1
|
697 |
// pwm_channels[0].mask=orb2_red_mask ; pwm_channels[0].time= 3-1;//0
|
698 |
// pwm_channels[1].mask=orb1_blue_mask ; pwm_channels[1].time= 3-1;//1
|
699 |
// pwm_channels[2].mask=orb2_green_mask ; pwm_channels[2].time=127-1;//126
|
700 |
// pwm_channels[3].mask=orb1_green_mask ; pwm_channels[3].time=127-1;//127
|
701 |
// pwm_channels[4].mask=orb2_blue_mask ; pwm_channels[4].time=250-1;//254
|
702 |
// pwm_channels[5].mask=orb1_red_mask ; pwm_channels[5].time=250-1;//255
|
703 |
} |
704 |
|
705 |
|