Rev 286 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 286 | Rev 287 | ||
|---|---|---|---|
| 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 | * |
3 | * Still under heavy development |
| 4 | * Still untested with a real RGB-LED, HSV Colorspace might be better suited. We'll see... |
- | |
| 5 | - | ||
| 6 | * TODO: Use pointers in the HSL_2_RGB functions etc. It's currently a waste of memory. |
- | |
| 7 | * |
4 | * |
| 8 | * @date 2007-02-04 |
5 | * @date 2007-02-04 |
| 9 | * @author Martin Nordén |
6 | * @author Martin Nordén |
| 10 | * |
7 | * |
| 11 | */ |
8 | */ |
| 12 | 9 | ||
| 13 | /*----------------------------------------------------------------------------- |
10 | /*----------------------------------------------------------------------------- |
| 14 | * Includes |
11 | * Includes |
| 15 | *---------------------------------------------------------------------------*/ |
12 | *---------------------------------------------------------------------------*/ |
| 16 | /* system files */ |
13 | /* system files */ |
| 17 | #include <avr/io.h> |
14 | #include <avr/io.h> |
| 18 | #include <avr/interrupt.h> |
15 | #include <avr/interrupt.h> |
| 19 | #include <avr/wdt.h> |
16 | #include <avr/wdt.h> |
| 20 | #include <stdio.h> |
17 | #include <stdio.h> |
| 21 | /* lib files */ |
18 | /* lib files */ |
| 22 | #include <can.h> |
19 | #include <can.h> |
| 23 | #include <serial.h> |
20 | #include <serial.h> |
| 24 | #include <timebase.h> |
21 | #include <timebase.h> |
| 25 | #include <math.h> |
22 | #include <math.h> |
| 26 | 23 | ||
| 27 | /*----------------------------------------------------------------------------- |
24 | /*----------------------------------------------------------------------------- |
| 28 | * Variables |
25 | * Variables |
| 29 | *---------------------------------------------------------------------------*/ |
26 | *---------------------------------------------------------------------------*/ |
| 30 | uint32_t timeStamp; |
27 | uint32_t timeStamp; |
| 31 | 28 | ||
| 32 | uint8_t |
29 | uint8_t currR; //Current Red |
| 33 | uint8_t |
30 | uint8_t currG; //Current Green |
| 34 | uint8_t |
31 | uint8_t currB; //Current Blue |
| 35 | 32 | ||
| 36 |
|
33 | uint8_t destR; //Destination Red |
| 37 |
|
34 | uint8_t destG; //Destination Green |
| 38 |
|
35 | uint8_t destB; //Destination Blue |
| 39 | 36 | ||
| 40 | double destH; |
- | |
| 41 | double destS; |
- | |
| 42 |
|
37 | uint8_t intensity; //Current intensity. |
| 43 | 38 | ||
| 44 | uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading |
39 | uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading |
| 45 | uint8_t program; //This variable keeps track of what program is currently running. 0 means no program. |
40 | uint8_t program; //This variable keeps track of what program is currently running. 0 means no program. |
| 46 | uint8_t programstate; //This variable keeps track of where in a program we currently are. |
41 | uint8_t programstate; //This variable keeps track of where in a program we currently are. |
| 47 | 42 | ||
| 48 | /*---------------------------------------------------------------------------- |
43 | /*---------------------------------------------------------------------------- |
| 49 | * Functions |
44 | * Functions |
| 50 | * -------------------------------------------------------------------------*/ |
45 | * -------------------------------------------------------------------------*/ |
| 51 | void updateLED(); |
46 | void updateLED(); |
| 52 |
|
47 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B); |
| 53 | 48 | ||
| 54 | 49 | ||
| 55 | /*----------------------------------------------------------------------------- |
50 | /*----------------------------------------------------------------------------- |
| 56 | * Main Program |
51 | * Main Program |
| 57 | *---------------------------------------------------------------------------*/ |
52 | *---------------------------------------------------------------------------*/ |
| 58 | int main(void) { |
53 | int main(void) { |
| 59 | Timebase_Init(); |
54 | Timebase_Init(); |
| 60 | Serial_Init(); |
55 | Serial_Init(); |
| 61 | Can_Init(); |
56 | Can_Init(); |
| 62 | 57 | ||
| 63 | //Setting up OC1A, 16 bit counter used as 8 bit. |
58 | //Setting up OC1A, 16 bit counter used as 8 bit. |
| 64 | /* Use OC1A */ |
59 | /* Use OC1A */ |
| 65 | TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */ |
60 | TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */ |
| 66 | TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS11); /* Timer uses main system clock with 1/8 prescale */ |
61 | TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS11); /* Timer uses main system clock with 1/8 prescale */ |
| 67 | ICR1 = 256; //8 bit precision to match the other two counters. |
62 | ICR1 = 256; //8 bit precision to match the other two counters. |
| 68 | OCR1A = 128; //50% as standard |
63 | OCR1A = 128; //50% as standard |
| 69 | DDRB |= (1<<PB1); /* Enable pin as output */ |
64 | DDRB |= (1<<PB1); /* Enable pin as output */ |
| 70 | 65 | ||
| 71 | //Initialize variables |
66 | //Initialize variables |
| 72 | fadeSpeed = |
67 | fadeSpeed = 250; |
| 73 | timeStamp = 0; |
68 | timeStamp = 0; |
| 74 | 69 | ||
| 75 | Can_Message_t rxMsg; |
70 | Can_Message_t rxMsg; |
| 76 | - | ||
| 77 | //Init HSL |
- | |
| 78 |
|
71 | currR = 0x53; |
| 79 |
|
72 | currG = 0x15; |
| 80 |
|
73 | currB = 0xa0; |
| 81 | 74 | ||
| 82 | sei(); |
75 | sei(); |
| - | 76 | ||
| - | 77 | reformatRGB(currR,currG,currB); |
|
| 83 | 78 | ||
| 84 | /* main loop */ |
79 | /* main loop */ |
| 85 | while (1) { |
80 | while (1) { |
| 86 | /* service the CAN routines */ |
81 | /* service the CAN routines */ |
| 87 | Can_Service(); |
82 | Can_Service(); |
| 88 | 83 | ||
| 89 | /* Time to see if we want to update our values */ |
84 | /* Time to see if we want to update our values */ |
| 90 | if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) { |
85 | if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) { |
| - | 86 | intensity = intensity -1; |
|
| 91 | timeStamp = Timebase_CurrentTime(); |
87 | timeStamp = Timebase_CurrentTime(); |
| 92 | currH = currH + 0.01; |
- | |
| 93 | if (currH > 1) currH = 0; |
- | |
| 94 | updateLED(); |
88 | updateLED(); |
| 95 | } |
89 | } |
| 96 | 90 | ||
| 97 | 91 | ||
| 98 | /* check if any messages have been received */ |
92 | /* check if any messages have been received */ |
| 99 | /* A lot of remote control crap going on here now for testing */ |
93 | /* A lot of remote control crap going on here now for testing */ |
| 100 | while (Can_Receive(&rxMsg) == CAN_OK) { |
94 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| 101 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){ |
95 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){ |
| 102 | program = 1; |
96 | program = 1; |
| 103 | fadeSpeed = 0; |
97 | fadeSpeed = 0; |
| 104 | } |
98 | } |
| 105 | 99 | ||
| 106 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){ |
100 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){ |
| 107 | program = 2; |
101 | program = 2; |
| 108 | fadeSpeed = 0; |
102 | fadeSpeed = 0; |
| 109 | } |
103 | } |
| 110 | } |
104 | } |
| 111 | 105 | ||
| 112 | 106 | ||
| 113 | } |
107 | } |
| 114 | 108 | ||
| 115 | return 0; |
109 | return 0; |
| 116 | } |
110 | } |
| 117 | 111 | ||
| 118 | 112 | ||
| 119 | 113 | ||
| 120 | 114 | ||
| 121 | 115 | ||
| 122 | void updateLED(){ |
116 | void updateLED(){ |
| 123 | - | ||
| 124 | 117 | ||
| 125 |
|
118 | OCR1A = currR * intensity / 255; |
| 126 | { |
119 | |
| 127 | R = currL * 255; //RGB results = 0 ÷ 255 |
- | |
| 128 |
|
120 | Can_Message_t txMsg; |
| 129 |
|
121 | txMsg.Id = 0x55; |
| 130 | } |
- | |
| 131 | else |
- | |
| 132 | { |
- | |
| 133 |
|
122 | txMsg.RemoteFlag = 0; |
| 134 |
|
123 | txMsg.ExtendedFlag = 1; |
| 135 |
|
124 | txMsg.DataLength = 4; |
| 136 | else var_2 = ( currL + currS ) - ( currS * currL ); |
- | |
| 137 | 125 | ||
| 138 |
|
126 | txMsg.Data.bytes[0] = currR * intensity / 255; |
| 139 | - | ||
| 140 |
|
127 | txMsg.Data.bytes[1] = currG * intensity / 255; |
| 141 |
|
128 | txMsg.Data.bytes[2] = currB * intensity / 255; |
| 142 |
|
129 | txMsg.Data.bytes[3] = intensity; |
| 143 | } |
- | |
| 144 | 130 | ||
| - | 131 | Can_Send(&txMsg); |
|
| 145 | 132 | ||
| 146 | 133 | ||
| 147 | OCR1A = R; |
- | |
| 148 | 134 | ||
| 149 | //Red |
135 | //Red |
| 150 | //Green |
136 | //Green |
| 151 | //Blue |
137 | //Blue |
| 152 | } |
138 | } |
| 153 | 139 | ||
| 154 | 140 | ||
| 155 | uint8_t |
141 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B){ |
| - | 142 | uint8_t maxvalue; |
|
| - | 143 | uint16_t tempcontainer; |
|
| 156 | { |
144 | |
| - | 145 | if ((R>G)&(R>B)){ |
|
| - | 146 | maxvalue = R; |
|
| 157 |
|
147 | } else if ((G>R)&(G>B)){ |
| - | 148 | maxvalue = G; |
|
| - | 149 | } else { |
|
| - | 150 | maxvalue = B; |
|
| - | 151 | } |
|
| - | 152 | ||
| 158 |
|
153 | tempcontainer = R * 255; |
| 159 |
|
154 | currR = tempcontainer/maxvalue; |
| - | 155 | tempcontainer = G * 255; |
|
| 160 |
|
156 | currG = tempcontainer/maxvalue; |
| - | 157 | tempcontainer = B * 255; |
|
| 161 |
|
158 | currB = tempcontainer/maxvalue; |
| - | 159 | ||
| 162 |
|
160 | intensity = maxvalue; |
| - | 161 | ||
| 163 | } |
162 | } |
| - | 163 | ||