Subversion Repositories HomeAutomation

Rev

Rev 286 | Blame | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * CAN RGB LED. This application is used to control a RGB LED.
  3.  * Still under heavy development
  4.  *
  5.  * @date    2007-02-04
  6.  * @author  Martin Nordén
  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.  *---------------------------------------------------------------------------*/
  27. uint32_t timeStamp;
  28.  
  29. uint8_t currR;  //Current Red
  30. uint8_t currG;  //Current Green
  31. uint8_t currB;  //Current Blue
  32.  
  33. uint8_t destR;  //Destination Red
  34. uint8_t destG;  //Destination Green
  35. uint8_t destB;  //Destination Blue
  36.  
  37. uint8_t intensity;  //Current intensity.
  38.  
  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();
  47. void reformatRGB(uint8_t R, uint8_t G, uint8_t B);
  48.  
  49.  
  50. /*-----------------------------------------------------------------------------
  51.  * Main Program
  52.  *---------------------------------------------------------------------------*/
  53. int main(void) {
  54.     Timebase_Init();
  55.     Serial_Init();
  56.     Can_Init();
  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
  67.     fadeSpeed = 250;
  68.     timeStamp = 0;
  69.  
  70.     Can_Message_t rxMsg;
  71.     currR = 0x53;
  72.     currG = 0x15;
  73.     currB = 0xa0;
  74.  
  75.     sei(); 
  76.  
  77.     reformatRGB(currR,currG,currB);
  78.  
  79.     /* main loop */
  80.     while (1) {
  81.         /* service the CAN routines */
  82.         Can_Service();
  83.  
  84.         /* Time to see if we want to update our values */
  85.         if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {
  86.             intensity = intensity -1;
  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.             }
  104.         }
  105.  
  106.  
  107.     }
  108.    
  109.     return 0;
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116. void updateLED(){
  117.  
  118. OCR1A = currR * intensity / 255;
  119.  
  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;
  130.  
  131.     Can_Send(&txMsg);
  132.  
  133.  
  134.  
  135. //Red
  136. //Green
  137. //Blue
  138. }
  139.  
  140.  
  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. }
  163.