Subversion Repositories HomeAutomation

Rev

Rev 298 | 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.  * TODO: A lot, including. Using my new precious struct Color for currColor, destColor and some basic programfunctionality
  9.  */
  10.  
  11. /*-----------------------------------------------------------------------------
  12.  * Includes
  13.  *---------------------------------------------------------------------------*/
  14. /* system files */
  15. #include <avr/io.h>
  16. #include <avr/interrupt.h>
  17. #include <avr/wdt.h>
  18. #include <stdio.h>
  19. /* lib files */
  20. #include <can.h>
  21. #include <serial.h>
  22. #include <timebase.h>
  23. #include <math.h>
  24.  
  25.  
  26. /*----------------------------------------------------------------------------
  27.  * Ehm, struct?
  28.  * -------------------------------------------------------------------------*/
  29. struct Color
  30. {
  31.     uint8_t R;
  32.     uint8_t G;
  33.     uint8_t B;
  34.     uint8_t Intens;
  35. };
  36.  
  37. /*-----------------------------------------------------------------------------
  38.  * Variables
  39.  *---------------------------------------------------------------------------*/
  40. uint32_t timeStamp; //TimeStamp to keep track of time
  41. uint32_t dimStamp;  //Timestamp to keep track of dimmer changes
  42.  
  43. uint32_t tempStamp; //Timestamp to keep track of temp things during development
  44. uint8_t temp;
  45.  
  46. struct Color currColor;  //Current color
  47. struct Color destColor;  //Destination color
  48.  
  49. uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange.
  50. uint8_t dimSpeed;       //Dimmer speed. Sets the speed of intensitychange.
  51.  
  52. uint8_t program;        //This variable keeps track of what program is currently running.
  53. uint8_t programstate;   //This variable keeps track of where in a program we currently are. Will probably be removed later on.
  54.  
  55. /*----------------------------------------------------------------------------
  56.  * Functions
  57.  * -------------------------------------------------------------------------*/
  58. void updateLED();
  59. void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright);
  60. void changeColor(int8_t amount, uint8_t* color);
  61. uint8_t fade(uint8_t current, uint8_t destination);
  62.  
  63. /*-----------------------------------------------------------------------------
  64.  * Main Program
  65.  *---------------------------------------------------------------------------*/
  66. int main(void) {
  67.     Timebase_Init();
  68.     Serial_Init();
  69.     Can_Init();
  70.  
  71. //Setting up OC1A, 16 bit counter used as 8 bit.
  72.     TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */
  73.     TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS12); /* Timer uses main system clock with ? prescale */
  74.     ICR1 = 256; //8 bit precision to match the other two counters.
  75.     OCR1A = 0;  //0% as standard
  76.     DDRB |= (1<<PB1); /* Enable pin as output */
  77.  
  78. //Setting up OC0A
  79.     TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */
  80.     TCCR0B |= (1<<CS02); /* Prescaler updating now */
  81.     OCR0A = 0;  //0% standard
  82.     DDRD |= (1<<PD6);
  83.  
  84. //Setting up OC0B
  85.     TCCR0A |= (1<<COM0B1);
  86.     OCR0B = 0;
  87.     DDRD |= (1<<PD5);
  88.  
  89.  
  90. //Initialize variables
  91.     fadeSpeed = 10;
  92.     dimSpeed = 255;
  93.     timeStamp = 0;
  94.     dimStamp = 0;
  95.     Can_Message_t rxMsg;   
  96.  
  97.     reformatRGB(0x00,0xff,0x00,1);
  98.     destColor.Intens = 100;
  99.     currColor = destColor;
  100.  
  101. //Just temporary for now, they keep track of some kind of demo-program.
  102.     program = 1;
  103.     temp = 1;
  104.  
  105.  
  106.     sei();
  107.  
  108.     /* main loop */
  109.     while (1) {
  110.         /* service the CAN routines */
  111.         Can_Service();
  112.  
  113.         /* Time to fade color? */
  114.         if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) {
  115.             currColor.R = fade(currColor.R, destColor.R);          
  116.             currColor.G = fade(currColor.G, destColor.G);
  117.             currColor.B = fade(currColor.B, destColor.B);
  118.  
  119.             timeStamp = Timebase_CurrentTime();
  120.             updateLED();
  121.         }
  122.  
  123.  
  124.         /* Time to fade intensity? */
  125.         if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) {
  126.             currColor.Intens = fade(currColor.Intens, destColor.Intens);
  127.             dimStamp = Timebase_CurrentTime();
  128.             updateLED();
  129.         }
  130.  
  131.  
  132.         /* Just a nice fade-demo */
  133.         if ((Timebase_PassedTimeMillis(tempStamp) >= 2500)&(program == 1)) {
  134.             tempStamp = Timebase_CurrentTime();
  135.            
  136.             switch(temp){
  137.             case 1:
  138.                 reformatRGB(0xff,0x00,0x00,0);
  139.                 break;
  140.             case 2:
  141.                 reformatRGB(0xff,0xff,0x00,0);
  142.                 break;
  143.             case 3:
  144.                 reformatRGB(0x00,0xff,0x00,0);
  145.                 break;
  146.             case 4:
  147.                 reformatRGB(0x00,0xff,0xff,0);
  148.                 break;
  149.             case 5:
  150.                 reformatRGB(0x00,0x00,0xff,0);
  151.                 break;
  152.             case 6:
  153.                 reformatRGB(0xff,0x00,0xff,0);
  154.                 temp = 255;
  155.                 break;
  156.             }
  157.             temp++;
  158.                
  159.         }
  160.  
  161.  
  162.         /* check if any messages have been received */
  163.         /* A lot of remote control crap going on here now for testing */
  164.         while (Can_Receive(&rxMsg) == CAN_OK) {
  165.             if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){
  166.                 program = 1;
  167.                 fadeSpeed = 0;
  168.             }
  169.  
  170.             if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){
  171.                 program = 2;
  172.                 fadeSpeed = 0;
  173.             }
  174.         }
  175.  
  176.  
  177.     }
  178.    
  179.     return 0;
  180. }
  181.  
  182.  
  183.  
  184.  
  185. /************************************************************************************************
  186.  *     updateLED updates the PWM counter values and thus changes the color of the RGBLED.       *
  187.  *     It doesn't reculculate anything, except the values to write to the PWM registers.        *
  188.  *     TODO: Get rid of the tempcontainer!                                  *
  189.  ************************************************************************************************/
  190. void updateLED(){
  191.     //Bara för debugcrap
  192.     Can_Message_t txMsg;
  193.     txMsg.Id = 0x55;
  194.     txMsg.RemoteFlag = 0;
  195.     txMsg.ExtendedFlag = 1;
  196.     txMsg.DataLength = 4;
  197.  
  198.  
  199.     uint8_t tempcontainer;
  200.  
  201.     tempcontainer = currColor.R * currColor.Intens / 255;   //I really want to skip this, don't know how yet though.
  202.     OCR1A = tempcontainer; 
  203.       OCR0A = currColor.G * currColor.Intens / 255;
  204.     OCR0B = currColor.B * currColor.Intens / 255;      
  205.        
  206.     txMsg.Data.bytes[0] = currColor.R * currColor.Intens / 255;
  207.     txMsg.Data.bytes[1] = currColor.G * currColor.Intens / 255;
  208.     txMsg.Data.bytes[2] = currColor.B * currColor.Intens / 255;
  209.     txMsg.Data.bytes[3] = currColor.Intens;
  210.     Can_Send(&txMsg);
  211. }
  212.  
  213.  
  214.  
  215. /************************************************************************************************
  216.  *     reformatRGB sets a new target RGB triplet to fade to. It reformats it so that            *
  217.  *     the highest color always gets 0xff. Brightness is recalculated.                          *
  218.  *     if setBright is set to 0, the brightness will not be updated.                    *
  219.  *     TODO: Smarter 16-bit arithmetics would be nice. Also a pointer to struct to be updated.  *
  220.  ************************************************************************************************/
  221. void reformatRGB(uint8_t R, uint8_t G, uint8_t B, uint8_t setBright){
  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.     destColor.R = tempcontainer/maxvalue;
  235.     tempcontainer = G * 255;
  236.     destColor.G = tempcontainer/maxvalue;
  237.     tempcontainer = B * 255;
  238.     destColor.B = tempcontainer/maxvalue;
  239.  
  240.     if (setBright){
  241.         destColor.Intens = maxvalue;
  242.     }
  243. }
  244.  
  245.  
  246. /************************************************************************************************
  247.  *     fade tar in två värden, current och destination och för current ett steg                 *
  248.  *     närmare destination.                                                               *
  249.  *     Ev. TODO: skicka in två pekare och jämna ut dom istället.                        *
  250.  ************************************************************************************************/
  251. uint8_t fade(uint8_t current, uint8_t destination){
  252.     if (current > destination){
  253.         return(current - 1);
  254.     } else if (current < destination){
  255.         return(current + 1);
  256.     }
  257.     return(current);
  258. }
  259.  
  260.  
  261. /************************************************************************************************
  262.  *     changeColor ändrar en färg mängden amount. -127>127.                                     *
  263.  *     color kan vara dest eller curr R,G,B.                                                    *
  264.  *     Ev. TODO: om t.ex. röd är på max och man vill öka röd ska de andra färgerna minskas. *
  265.  ************************************************************************************************/
  266. void changeColor(int8_t amount, uint8_t* color){
  267.     if ( ((amount > 0)&(*color < 255)) || ((amount < 0)&(*color > 0)) ) //Se till att vi inte slår över *color
  268.     *color = *color + amount;
  269. }
  270.  
  271.