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