Subversion Repositories HomeAutomation

Rev

Rev 542 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * IR transmitter driver.
  3.  *
  4.  * @date    2007-06-24
  5.  *
  6.  * @author  Anders Runeson
  7.  *  
  8.  */
  9.  
  10. /*-----------------------------------------------------------------------------
  11.  * Includes
  12.  *---------------------------------------------------------------------------*/
  13. #include <config.h>
  14. #include <drivers/ir/transmitter/irtransmitter.h>
  15. #include <avr/io.h>
  16.  
  17. /*-----------------------------------------------------------------------------
  18.  * Globals
  19.  *---------------------------------------------------------------------------*/
  20. static uint8_t stateTransmit = STATE_IDLE;
  21. static uint8_t repeateProto = IR_PROTO_UNKNOWN;
  22. static uint8_t repeateAddress;
  23. static uint8_t repeateCommand;
  24. static uint8_t repeateTime=100;
  25. static uint8_t repeateReps=1;
  26. static uint8_t repeateAdditional=0;         //can be used for any protocol as general purpose variable
  27.  
  28. /*-----------------------------------------------------------------------------
  29.  * Prerequisites
  30.  *---------------------------------------------------------------------------*/
  31.  
  32.  
  33. /*-----------------------------------------------------------------------------
  34.  * Public Functions
  35.  *---------------------------------------------------------------------------*/
  36.  
  37.  
  38. void setIrPort(void) {
  39.     TCCR0A |= (1<<COM0A0);
  40. }
  41.  
  42. void clearIrPort(void) {
  43.     TCCR0A &= ~(1<<COM0A0);
  44. }
  45.  
  46. /**
  47.  * Transmit RC5 data, http://www.sbprojects.com/knowledge/ir/rc5.htm
  48.  *
  49.  * @param address
  50.  *      Pointer to store the address of the received data
  51.  * @param command
  52.  *      Pointer to store the command of the received data
  53.  * @return
  54.  *      IR_OK if data received successfully, one of several errormessages if not
  55.  */
  56. uint8_t transmitRC5(uint8_t address, uint8_t command) {
  57.     OCR0A = (((F_CPU/2000)/IR_RC5_F_MOD) -1);       // set up modulation
  58.    
  59.     clearIrPort();                                  // set port low to be on the safe side
  60.     repeateTime = IR_RC5_REPETITION;                // set up repitition time
  61.     return IR_OK;
  62. }
  63.  
  64. /**
  65.  * Transmit SIRC data, http://www.sbprojects.com/knowledge/ir/sirc.htm
  66.  *
  67.  * @param address
  68.  *      Pointer to store the address of the received data
  69.  * @param command
  70.  *      Pointer to store the command of the received data
  71.  * @return
  72.  *      IR_OK if data received successfully, one of several errormessages if not
  73.  */
  74. uint8_t transmitSIRC(uint8_t address, uint8_t command) {
  75.     OCR0A = (((F_CPU/2000)/IR_SIRC_F_MOD) -1);      // set up modulation
  76.    
  77.     clearIrPort();                                  // set port low to be on the safe side
  78.     repeateTime = IR_SIRC_REPETITION;               // set up repitition time
  79.     return IR_OK;
  80. }
  81.  
  82. /**
  83.  * Transmit Sharp data, http://www.sbprojects.com/knowledge/ir/sharp.htm
  84.  *
  85.  * @param address
  86.  *      Pointer to store the address of the received data
  87.  * @param command
  88.  *      Pointer to store the command of the received data
  89.  * @return
  90.  *      IR_OK if data received successfully, one of several errormessages if not
  91.  */
  92. uint8_t transmitSharp(uint8_t address, uint8_t command) {
  93.     OCR0A = (((F_CPU/2000)/IR_SHARP_F_MOD) -1);     // set up modulation
  94.    
  95.     clearIrPort();                                  // set port low to be on the safe side
  96.     repeateTime = IR_SHARP_REPETITION;              // set up repitition time
  97.     return IR_OK;
  98. }
  99.  
  100.  
  101. /**
  102.  * Transmit Samsung data
  103.  *
  104.  * @param address
  105.  *      Pointer to store the address of the received data
  106.  * @param command
  107.  *      Pointer to store the command of the received data
  108.  * @return
  109.  *      IR_OK if data received successfully, one of several errormessages if not
  110.  */
  111. uint8_t transmitSamsung(uint8_t address, uint8_t command) {
  112.     OCR0A = (((F_CPU/2000)/IR_SAMS_F_MOD) -1);      // set up modulation
  113.    
  114.     /* Send startbit */
  115.     setTimerVal(65536-IR_SAMS_ST_BIT);
  116.     setIrPort();
  117.     while(!isTimerOvfl()) {}
  118.     clearIrPort();
  119.     /* Send startpause */
  120.     setTimerVal(65536-IR_SAMS_ST_PAUSE);
  121.     while(!isTimerOvfl()) {}
  122.    
  123.     /* first send address */
  124.     uint8_t byteToSend = address;
  125.     for (uint8_t i = 0; i < 65; i++) {
  126.         if (i==16) {
  127.             /* then send address again */
  128.             //byteToSend = address;
  129.         } else if (i==32) {
  130.             /* then send command */
  131.             byteToSend = command;
  132.         } else if (i==48) {
  133.             /* then send inverted command */
  134.             byteToSend = ~command;
  135.         }
  136.         if ((i&1) == 1) {       /* if odd, ir-pause */
  137.             if ((byteToSend>>((i>>1)&0x7))&1) {
  138.                 setTimerVal(65536-IR_SAMS_LOW_ONE);
  139.             } else {
  140.                 setTimerVal(65536-IR_SAMS_LOW_ZERO);
  141.             }
  142.            
  143.             while(!isTimerOvfl()) {}
  144.         } else {                /* if even, ir-bit */
  145.             setIrPort();
  146.             setTimerVal(65536-IR_SAMS_HIGH);
  147.             while(!isTimerOvfl()) {}
  148.             clearIrPort();
  149.         }
  150.     }
  151.    
  152.     clearIrPort();                                  // set port low to be on the safe side
  153.     repeateTime = IR_SAMS_REPETITION;               // set up repitition time
  154.     return IR_OK;
  155. }
  156.  
  157.  
  158. //TODO: skriv doxygen-header som för de andra funktionerna
  159. /**
  160.  *
  161.  *
  162.  *
  163.  *
  164.  */
  165. void IrTransmit_Init(void) {
  166.     IRDDR |= (1<<IRBIT);
  167.    
  168.     #if defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)
  169.     TCCR0A = (0<<COM0A1)|(0<<COM0A0)|(1<<WGM01)|(0<<WGM00);
  170.     TCCR0B = (0<<WGM02)|(1<<CS00)|(0<<CS01)|(0<<CS02);
  171.     #endif
  172.    
  173.     initTimer();
  174. }
  175.  
  176. uint8_t doTransmit(uint8_t proto, uint8_t address, uint8_t command) {
  177.     if (proto == IR_PROTO_SHARP) {
  178.         return transmitSharp(address, command);
  179.     } else if (proto == IR_PROTO_SAMS) {
  180.         return transmitSamsung(address, command);
  181.     } /*else if (proto == IR_PROTO_NEC) {
  182.         return transmitRC5(address, command);
  183.     } else if (proto == IR_PROTO_RC5) {
  184.         return transmitRC5(address, command);
  185.     } */
  186.    
  187.    
  188.     return IR_NO_PROTOCOL;
  189.    
  190. }
  191.  
  192.  
  193. /**
  194.  * Start transmit ir
  195.  *
  196.  * @param proto
  197.  *      The protocol of the data to be transmitted
  198.  * @param address
  199.  *      The address of the data to be transmitted
  200.  * @param command
  201.  *      The command of the data to be transmitted
  202.  * @return
  203.  *      IR_OK if data received successfully, one of several errormessages if not
  204.  */
  205. uint8_t IrTransmit_Start(uint8_t proto, uint8_t address, uint8_t command) {
  206.     repeateProto = proto;
  207.     repeateAddress = address;
  208.     repeateCommand = command;
  209.     repeateTime = 100;          //set a default repeateTime, if transmit protocol misses that
  210.     repeateAdditional = 0;
  211.    
  212.     if (proto == IR_PROTO_SHARP) {
  213.         repeateReps = IR_SHARP_REPS;
  214.     } else if (proto == IR_PROTO_SAMS) {
  215.         repeateReps = IR_SAMS_REPS;
  216.     }
  217.    
  218.     uint8_t retval = doTransmit(proto, address, command);
  219.     if (retval != IR_OK) {
  220.         return retval;
  221.     }
  222.     stateTransmit = STATE_TRANS;
  223.    
  224.     return IR_OK;
  225. }
  226.  
  227. /**
  228.  * Call this function as often as possible during a transmission
  229.  * should be replaced with an interrupted timer
  230.  *
  231.  */
  232. void IrTransmit_Poll(void) {
  233.     if (stateTransmit == STATE_TRANS) {
  234.         if (isTimerOvfl()) {
  235.             repeateTime--;
  236.             if (repeateTime == 0) {
  237.                 if (repeateReps > 0) {
  238.                     repeateReps--;
  239.                 }
  240.                 doTransmit(repeateProto, repeateAddress, repeateCommand);
  241.             } else {
  242.                 setTimerVal(TIM_1MS_RELOAD_VAL);
  243.             }
  244.         }
  245.     }
  246. }
  247.  
  248. /**
  249.  * Dont stop polling until the stopfunction returns ok
  250.  *
  251.  *
  252.  */
  253. uint8_t IrTransmit_Stop(void) {
  254.    
  255.     if (repeateReps > 0) {
  256.         return IR_NOT_FINISHED;
  257.     }
  258.     stateTransmit = STATE_IDLE;
  259.    
  260.     return IR_OK;
  261. }
  262.  
  263.