Subversion Repositories HomeAutomation

Rev

Rev 289 | 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; //TimeStamp to keep track of time
  28. uint32_t dimStamp;  //Timestamp to keep track of dimmer changes
  29.  
  30. uint8_t currR;  //Current Red 0->255
  31. uint8_t currG;  //Current Green 0->255
  32. uint8_t currB;  //Current Blue 0->255
  33.  
  34. uint8_t destR;  //Destination Red
  35. uint8_t destG;  //Destination Green
  36. uint8_t destB;  //Destination Blue
  37.  
  38. uint8_t currIntens; //Current intensity. 0->255
  39. uint8_t destIntens; //Destination intensity. 0->255
  40.  
  41. uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange.
  42. uint8_t dimSpeed;       //Dimmer speed. Sets the speed of intensitychange.
  43.  
  44. uint8_t program;        //This variable keeps track of what program is currently running.
  45. uint8_t programstate;   //This variable keeps track of where in a program we currently are. Will probably be removed later on.
  46.  
  47. /*----------------------------------------------------------------------------
  48.  * Functions
  49.  * -------------------------------------------------------------------------*/
  50. void updateLED();
  51. void reformatRGB(uint8_t R, uint8_t G, uint8_t B);
  52. uint8_t fade(uint8_t current, uint8_t destination);
  53.  
  54.  
  55. /*-----------------------------------------------------------------------------
  56.  * Main Program
  57.  *---------------------------------------------------------------------------*/
  58. int main(void) {
  59.     Timebase_Init();
  60.     Serial_Init();
  61.     Can_Init();
  62.  
  63. //Setting up OC1A, 16 bit counter used as 8 bit.
  64.     /* Use OC1A */
  65.     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 */
  67.     ICR1 = 256; //8 bit precision to match the other two counters.
  68.     OCR1A = 1;  //50% as standard
  69.     DDRB |= (1<<PB1); /* Enable pin as output */
  70.  
  71. /* Use OC0A */
  72.     TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */
  73.     TCCR0B |= (1<<CS02);//(1<<CS02)|(1<<CS00); /* Prescaler 1/1024 */
  74.     OCR0A = 40;
  75.     DDRD |= (1<<PD6);
  76.  
  77. /* Use OC0B */
  78.     TCCR0A |= (1<<COM0B1);
  79.     OCR0B = 40;
  80.     DDRD |= (1<<PD5);
  81.  
  82. //Initialize variables
  83.     fadeSpeed = 250;
  84.     dimSpeed = 255;
  85.     timeStamp = 0;
  86.     dimStamp = 0;
  87.  
  88.     Can_Message_t rxMsg;
  89.     currR = 0x64;
  90.     currG = 0x15;
  91.     currB = 0x63;
  92.  
  93.     sei(); 
  94.  
  95.     reformatRGB(currR,currG,currB);
  96.  
  97.     currIntens = 20;
  98.     destIntens = 200;
  99.  
  100.     /* main loop */
  101.     while (1) {
  102.         /* service the CAN routines */
  103.         Can_Service();
  104.  
  105.         /* Time to fade color? */
  106.         if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {
  107.             currR = fade(currR, destR);
  108.             currG = fade(currG, destG);
  109.             currB = fade(currB, destB);
  110.  
  111.             timeStamp = Timebase_CurrentTime();
  112.             updateLED();
  113.         }
  114.  
  115.  
  116.         /* Time to fade intensity? */
  117.         if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) {
  118.             currIntens = fade(currIntens, destIntens);
  119.             dimStamp = Timebase_CurrentTime();
  120.             updateLED();
  121.         }
  122.  
  123.  
  124.  
  125.         /* check if any messages have been received */
  126.         /* A lot of remote control crap going on here now for testing */
  127.         while (Can_Receive(&rxMsg) == CAN_OK) {
  128.             if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){
  129.                 program = 1;
  130.                 fadeSpeed = 0;
  131.             }
  132.  
  133.             if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){
  134.                 program = 2;
  135.                 fadeSpeed = 0;
  136.             }
  137.         }
  138.  
  139.  
  140.     }
  141.    
  142.     return 0;
  143. }
  144.  
  145.  
  146.  
  147.  
  148.  
  149. void updateLED(){
  150.  
  151. //  OCR1A = currR * currIntens / 255;
  152.     //TODO: Här ska givetvis de andra två färgerna också uppdateras.
  153.  
  154.    
  155.     //Bara för debugcrap
  156.     Can_Message_t txMsg;
  157.     txMsg.Id = 0x55;
  158.     txMsg.RemoteFlag = 0;
  159.     txMsg.ExtendedFlag = 1;
  160.     txMsg.DataLength = 4;
  161.    
  162.     txMsg.Data.bytes[0] = currR * currIntens / 255;
  163.     txMsg.Data.bytes[1] = currG * currIntens / 255;
  164.     txMsg.Data.bytes[2] = currB * currIntens / 255;
  165.     txMsg.Data.bytes[3] = currIntens;
  166.  
  167.     Can_Send(&txMsg);
  168.  
  169.  
  170.  
  171. //Red
  172. //Green
  173. //Blue
  174. }
  175.  
  176.  
  177. void reformatRGB(uint8_t R, uint8_t G, uint8_t B){
  178.     uint8_t maxvalue;
  179.     uint16_t tempcontainer;
  180.  
  181.     if ((R>G)&(R>B)){
  182.         maxvalue = R;
  183.     } else if ((G>R)&(G>B)){
  184.         maxvalue = G;
  185.     } else {
  186.         maxvalue = B;
  187.     }
  188.  
  189.     tempcontainer = R * 255;
  190.     currR = tempcontainer/maxvalue;
  191.     tempcontainer = G * 255;
  192.     currG = tempcontainer/maxvalue;
  193.     tempcontainer = B * 255;
  194.     currB = tempcontainer/maxvalue;
  195.  
  196.     currIntens = maxvalue;
  197. }
  198.  
  199. uint8_t fade(uint8_t current, uint8_t destination){
  200.     if (current > destination){
  201.         return(current - 1);
  202.     } else if (current < destination){
  203.         return(current + 1);
  204.     }
  205.     return(current);
  206. }
  207.