root / branches / autonomous_recharging / code / projects / libbayboard / orb.c @ 849
History | View | Annotate | Download (3.44 KB)
1 | 641 | emullini | /**
|
---|---|---|---|
2 | * Copyright (c) 2008 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 orb.c
|
||
28 | * @brief Orbs
|
||
29 | *
|
||
30 | * Implemenation for the orbs (tri-colored LEDs)
|
||
31 | *
|
||
32 | * @author Colony Project, CMU Robotics Club
|
||
33 | **/
|
||
34 | |||
35 | #include <avr/io.h> |
||
36 | #include "orb.h" |
||
37 | 729 | abuchan | #include "serial.h" |
38 | 641 | emullini | |
39 | /**
|
||
40 | * @defgroup orbs Orbs
|
||
41 | * @brief Functions for controlling the color of the orbs.
|
||
42 | *
|
||
43 | * Functions for controlling the color and lighting of the orbs.
|
||
44 | *
|
||
45 | * @{
|
||
46 | **/
|
||
47 | |||
48 | /**
|
||
49 | * Initializes the PWM for Orb control. This must be called before
|
||
50 | * the orbs are used for them to function.
|
||
51 | **/
|
||
52 | 729 | abuchan | void orb_init (void) |
53 | 641 | emullini | { |
54 | /*data direction registers, blue, green (bit 6) and red (bit 4)*/
|
||
55 | 669 | emullini | DDRB |= _BV(DDB4); |
56 | 641 | emullini | |
57 | 669 | emullini | DDRD |= _BV(DDD4)|_BV(DDD6); |
58 | 641 | emullini | |
59 | |||
60 | /*blue is here. it goes on timer 0*/
|
||
61 | /*timer counter control registers for timer 0*/
|
||
62 | 849 | emullini | TCCR0A |= _BV(COM0B1)|_BV(COM0B0)|_BV(WGM01)|_BV(WGM00); |
63 | 641 | emullini | |
64 | 849 | emullini | TCCR0B |= _BV(CS00); |
65 | 641 | emullini | OCR0B = 0;
|
66 | |||
67 | /*green is here. it goes on timer 2*/
|
||
68 | 849 | emullini | TCCR2A |= _BV(COM2B1)|_BV(COM2B0)|_BV(WGM21)|_BV(WGM20); |
69 | TCCR2B |= _BV(CS20); |
||
70 | 641 | emullini | OCR2B = 0;
|
71 | |||
72 | |||
73 | 669 | emullini | /*red is here. it goes on timer 1 which counts to the value in ICR1 instead of 8 bits*/
|
74 | 849 | emullini | TCCR1A |= _BV(COM1B1)|_BV(COM1B0)|_BV(WGM11); |
75 | TCCR1B |= _BV(WGM13)|_BV(WGM12)|_BV(CS10); |
||
76 | 641 | emullini | ICR1 = 0x9C40;
|
77 | OCR1B = 0;
|
||
78 | 658 | emullini | |
79 | 729 | abuchan | set_orb(0, 0, 0); |
80 | 641 | emullini | } |
81 | |||
82 | /**
|
||
83 | * Set both orbs to the color specified. orb_init must
|
||
84 | * be called before this function may be used.
|
||
85 | *
|
||
86 | * @param red the red component of the color
|
||
87 | * @param green the green component of the color
|
||
88 | * @param blue the blue component of the color
|
||
89 | *
|
||
90 | * @see orb_init
|
||
91 | **/
|
||
92 | |||
93 | 729 | abuchan | void set_orb(int red, int green, int blue){ |
94 | 641 | emullini | /*blue is here. it goes on timer 0*/
|
95 | 669 | emullini | if(blue==0){ |
96 | DDRB &= ~(_BV(DDB4)); |
||
97 | } |
||
98 | else{
|
||
99 | DDRB |= _BV(DDB4); |
||
100 | blue = blue%256;
|
||
101 | OCR0B = blue; |
||
102 | } |
||
103 | |||
104 | 641 | emullini | /*green is here. it goes on timer 2*/
|
105 | 669 | emullini | if(green==0){ |
106 | DDRD &= ~(_BV(DDD6)); |
||
107 | } |
||
108 | else{
|
||
109 | DDRD |= _BV(DDD6); |
||
110 | green = green%256;
|
||
111 | OCR2B = green; |
||
112 | } |
||
113 | 641 | emullini | |
114 | 657 | emullini | /*red is here. it goes on timer 1*/
|
115 | 641 | emullini | red = red%256;
|
116 | 729 | abuchan | red = red*157;
|
117 | 669 | emullini | OCR1B = red; |
118 | 641 | emullini | } |
119 | |||
120 | 653 | emullini | /**
|
121 | * Set both orbs to the color specified. orb_init must
|
||
122 | * be called before this function may be used.
|
||
123 | *
|
||
124 | 669 | emullini | * @param color use a pound-defined color
|
125 | 653 | emullini | *
|
126 | * @see orb_init
|
||
127 | **/
|
||
128 | |||
129 | 729 | abuchan | void set_color(long color){ |
130 | 656 | emullini | int red = (color>>16)&BYTEMASK; |
131 | 669 | emullini | int green = (color>>8)&BYTEMASK; |
132 | int blue = color&BYTEMASK;
|
||
133 | 729 | abuchan | set_orb(red, green, blue); |
134 | 653 | emullini | } |
135 | |||
136 | 641 | emullini | /** @} **/ //end group |