Subversion Repositories HomeAutomation

Rev

Rev 291 | 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. uint32_t tempStamp; //Timestamp to keep track of temp things during development
  31. uint8_t temp;
  32.  
  33. uint8_t currR;  //Current Red 0->255
  34. uint8_t currG;  //Current Green 0->255
  35. uint8_t currB;  //Current Blue 0->255
  36.  
  37. uint8_t destR;  //Destination Red
  38. uint8_t destG;  //Destination Green
  39. uint8_t destB;  //Destination Blue
  40.  
  41. uint8_t currIntens; //Current intensity. 0->255
  42. uint8_t destIntens; //Destination intensity. 0->255
  43.  
  44. uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange.
  45. uint8_t dimSpeed;       //Dimmer speed. Sets the speed of intensitychange.
  46.  
  47. uint8_t program;        //This variable keeps track of what program is currently running.
  48. uint8_t programstate;   //This variable keeps track of where in a program we currently are. Will probably be removed later on.
  49.  
  50. /*----------------------------------------------------------------------------
  51.  * Functions
  52.  * -------------------------------------------------------------------------*/
  53. void updateLED();
  54. void reformatRGB(uint8_t R, uint8_t G, uint8_t B);
  55. void setDestRGB(uint8_t R, uint8_t G, uint8_t B);
  56.  
  57. void changeColor(int8_t amount, uint8_t* color);
  58.  
  59. uint8_t fade(uint8_t current, uint8_t destination);
  60.  
  61.  
  62. /*-----------------------------------------------------------------------------
  63.  * Main Program
  64.  *---------------------------------------------------------------------------*/
  65. int main(void) {
  66.     Timebase_Init();
  67.     Serial_Init();
  68.     Can_Init();
  69.  
  70. //Setting up OC1A, 16 bit counter used as 8 bit.
  71.     TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
  72.     TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS11); /* Timer uses main system clock with 1/8 prescale */
  73.     ICR1 = 256; //8 bit precision to match the other two counters.
  74.     OCR1A = 0;  //0% as standard
  75.     DDRB |= (1<<PB1); /* Enable pin as output */
  76.  
  77. //Setting up OC0A
  78.     TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */
  79.     TCCR0B |= (1<<CS01)|(1<<CS00); /* Prescaler ???? Check datasheet */
  80.     OCR0A = 0;  //0% standard
  81.     DDRD |= (1<<PD6);
  82.  
  83. //Setting up OC0B
  84.     TCCR0A |= (1<<COM0B1);
  85.     OCR0B = 0;
  86.     DDRD |= (1<<PD5);
  87.  
  88.  
  89. //Initialize variables
  90.     fadeSpeed = 10;
  91.     dimSpeed = 255;
  92.     timeStamp = 0;
  93.     dimStamp = 0;
  94.     Can_Message_t rxMsg;
  95.  
  96.     temp = 0;
  97.    
  98.     sei(); 
  99.  
  100.     reformatRGB(0x00,0xff,0x00);
  101.     currR = destR;
  102.     currG = destG;
  103.     currB = destB;
  104.     currIntens = 100;
  105.     destIntens = 100;
  106.  
  107.     program = 0;
  108.  
  109.  
  110.     /* main loop */
  111.     while (1) {
  112.         /* service the CAN routines */
  113.         Can_Service();
  114.  
  115.         /* Time to fade color? */
  116.         if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {           
  117.  
  118.             changeColor(1, &destB);
  119.             changeColor(-1, &destG);
  120.  
  121.             currR = fade(currR, destR);
  122.             currG = fade(currG, destG);
  123.             currB = fade(currB, destB);
  124.  
  125.             timeStamp = Timebase_CurrentTime();
  126.             updateLED();
  127.         }
  128.  
  129.  
  130.         /* Time to fade intensity? */
  131.         if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) {
  132.             currIntens = fade(currIntens, destIntens);
  133.             dimStamp = Timebase_CurrentTime();
  134.             updateLED();
  135.         }
  136.  
  137.  
  138.         /* Just a nice fade-demo */
  139.         if ((Timebase_PassedTimeMillis(tempStamp) >= 2500)&(program == 1)) {
  140.             tempStamp = Timebase_CurrentTime();
  141.            
  142.             switch(temp){
  143.             case 1:
  144.                 setDestRGB(0xff,0x00,0x00);
  145.                 break;
  146.             case 2:
  147.                 setDestRGB(0xff,0xff,0x00);
  148.                 break;
  149.             case 3:
  150.                 setDestRGB(0x00,0xff,0x00);
  151.                 break;
  152.             case 4:
  153.                 setDestRGB(0x00,0xff,0xff);
  154.                 break;
  155.             case 5:
  156.                 setDestRGB(0x00,0x00,0xff);
  157.                 break;
  158.             case 6:
  159.                 setDestRGB(0xff,0x00,0xff);
  160.                 temp = 255;
  161.                 break;
  162.             }
  163.             temp++;
  164.                
  165.         }
  166.  
  167.  
  168.         /* check if any messages have been received */
  169.         /* A lot of remote control crap going on here now for testing */
  170.         while (Can_Receive(&rxMsg) == CAN_OK) {
  171.             if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){
  172.                 program = 1;
  173.                 fadeSpeed = 0;
  174.             }
  175.  
  176.             if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){
  177.                 program = 2;
  178.                 fadeSpeed = 0;
  179.             }
  180.         }
  181.  
  182.  
  183.     }
  184.    
  185.     return 0;
  186. }
  187.  
  188.  
  189.  
  190.  
  191.  
  192. void updateLED(){
  193.     uint16_t tempcontainer;
  194.  
  195.     tempcontainer = currR * currIntens / 255;  
  196.     OCR1A = tempcontainer;
  197.     tempcontainer = currG * currIntens / 255;  
  198.       OCR0A = tempcontainer;
  199.     tempcontainer = currB * currIntens / 255;      
  200.     OCR0B = tempcontainer;
  201.  
  202.    
  203.     //Bara för debugcrap
  204.     Can_Message_t txMsg;
  205.     txMsg.Id = 0x55;
  206.     txMsg.RemoteFlag = 0;
  207.     txMsg.ExtendedFlag = 1;
  208.     txMsg.DataLength = 4;
  209.    
  210.     txMsg.Data.bytes[0] = currR;// * currIntens / 255;
  211.     txMsg.Data.bytes[1] = currG;// * currIntens / 255;
  212.     txMsg.Data.bytes[2] = currB;// * currIntens / 255;
  213.     txMsg.Data.bytes[3] = currIntens;
  214.     Can_Send(&txMsg);
  215. }
  216.  
  217.  
  218. /* setDestRGB sets a new target RGB triplet to fade to. Note that the brightness is kept at approximatly the same level
  219.  * even though the color changes */
  220.  
  221. void setDestRGB(uint8_t R, uint8_t G, uint8_t B){
  222.     uint8_t maxvalue;
  223.     uint16_t tempcontainer;
  224.  
  225.     if ((R>=G)&(R>=B)){
  226.         maxvalue = R;
  227.     } else if ((G>=R)&(G>=B)){
  228.         maxvalue = G;
  229.     } else {
  230.         maxvalue = B;
  231.     }
  232.  
  233.     tempcontainer = R * 255;
  234.     destR = tempcontainer/maxvalue;
  235.     tempcontainer = G * 255;
  236.     destG = tempcontainer/maxvalue;
  237.     tempcontainer = B * 255;
  238.     destB = tempcontainer/maxvalue;
  239.  
  240. }
  241.  
  242. /* reformatRGB sets a new target RGB triplet to fade to. Note that the brightness is recalculated. */
  243.  
  244. void reformatRGB(uint8_t R, uint8_t G, uint8_t B){
  245.     uint8_t maxvalue;
  246.     uint16_t tempcontainer;
  247.  
  248.     if ((R>=G)&(R>=B)){
  249.         maxvalue = R;
  250.     } else if ((G>=R)&(G>=B)){
  251.         maxvalue = G;
  252.     } else {
  253.         maxvalue = B;
  254.     }
  255.  
  256.     tempcontainer = R * 255;
  257.     destR = tempcontainer/maxvalue;
  258.     tempcontainer = G * 255;
  259.     destG = tempcontainer/maxvalue;
  260.     tempcontainer = B * 255;
  261.     destB = tempcontainer/maxvalue;
  262.  
  263.     destIntens = maxvalue;
  264. }
  265.  
  266. uint8_t fade(uint8_t current, uint8_t destination){
  267.     if (current > destination){
  268.         return(current - 1);
  269.     } else if (current < destination){
  270.         return(current + 1);
  271.     }
  272.     return(current);
  273. }
  274.  
  275. /*
  276.  * changeColor ändrar en färg mängden amount. -127>127.
  277.  * color kan vara dest eller curr R,G,B.
  278.  * Ev. TODO: om t.ex. röd är på max och man vill öka röd ska de andra färgerna minskas. */
  279. void changeColor(int8_t amount, uint8_t* color){
  280.     if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color
  281.     *color = *color + amount;
  282. }
  283.