Subversion Repositories HomeAutomation

Rev

Rev 1479 | Rev 1484 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. #include "sns_irTransceive.h"
  3.  
  4. #if IR_RX_ENABLE==1
  5. struct {
  6.     uint8_t             state;
  7.     uint8_t             newData;
  8.     uint16_t            *rxbuf;
  9.     uint8_t             rxlen;
  10.     uint8_t             timerNum;
  11.     Ir_Protocol_Data_t  proto;
  12. } irRxChannel[sns_irTransceive_SUPPORTED_NUM_CHANNELS];
  13. #endif
  14.  
  15. uint16_t    buf0[MAX_NR_TIMES];
  16. uint16_t    buf1[MAX_NR_TIMES];
  17. uint16_t    buf2[MAX_NR_TIMES];
  18.  
  19.  
  20. StdCan_Msg_t        irTxMsg;
  21.  
  22. #if (sns_irTransceive_SEND_DEBUG==1)
  23. void send_debug(uint16_t *buffer, uint8_t len) {
  24.     StdCan_Msg_t dbgIrTxMsg;
  25.     /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol */
  26.  
  27.     StdCan_Set_class(dbgIrTxMsg.Header, CAN_MODULE_CLASS_SNS);
  28.     StdCan_Set_direction(dbgIrTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  29.     dbgIrTxMsg.Length = 8;
  30.     dbgIrTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  31.     dbgIrTxMsg.Header.ModuleId = sns_irTransceive_ID;
  32.     dbgIrTxMsg.Header.Command = CAN_MODULE_CMD_IRTRANSCEIVE_IRRAW;
  33.     for (uint8_t i = 0; i < len>>2; i++) {
  34.         uint8_t index = i<<2;
  35.  
  36.         dbgIrTxMsg.Data[0] = (buffer[index]>>8)&0xff;
  37.         dbgIrTxMsg.Data[1] = (buffer[index]>>0)&0xff;
  38.         dbgIrTxMsg.Data[2] = (buffer[index+1]>>8)&0xff;
  39.         dbgIrTxMsg.Data[3] = (buffer[index+1]>>0)&0xff;
  40.         dbgIrTxMsg.Data[4] = (buffer[index+2]>>8)&0xff;
  41.         dbgIrTxMsg.Data[5] = (buffer[index+2]>>0)&0xff;
  42.         dbgIrTxMsg.Data[6] = (buffer[index+3]>>8)&0xff;
  43.         dbgIrTxMsg.Data[7] = (buffer[index+3]>>0)&0xff;
  44.                
  45.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  46.         while (StdCan_Put(&dbgIrTxMsg) != StdCan_Ret_OK) {}
  47.         _delay_ms(1);
  48.     }
  49.    
  50.     uint8_t lastpacketcnt = len&0x03;
  51.     if (lastpacketcnt > 0) {
  52.         dbgIrTxMsg.Length = lastpacketcnt<<1;
  53.         for (uint8_t i = 0; i < lastpacketcnt; i++) {
  54.             dbgIrTxMsg.Data[i<<1] = (buffer[(len&0xfc)|i]>>8)&0xff;
  55.             dbgIrTxMsg.Data[(i<<1)+1] = (buffer[(len&0xfc)|i]>>0)&0xff;
  56.         }
  57.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  58.         while (StdCan_Put(&dbgIrTxMsg) != StdCan_Ret_OK) {}
  59.         _delay_ms(1);
  60.     }
  61.  
  62. }
  63. #endif
  64.  
  65. #if IR_RX_ENABLE==1
  66. void sns_irTransceive_RX_done_callback(uint8_t channel, uint16_t *buffer, uint8_t len)
  67. {
  68.     if (channel < sns_irTransceive_SUPPORTED_NUM_CHANNELS)
  69.     {
  70.         irRxChannel[channel].newData = TRUE;
  71.         irRxChannel[channel].rxlen = len;
  72.     }
  73. }
  74. #endif
  75.  
  76. void sns_irTransceive_TX_done_callback(uint8_t channel)
  77. {
  78.  
  79. }
  80.  
  81. void sns_irTransceive_Init(void)
  82. {
  83. gpio_set_out(EXP_A);
  84. gpio_set_pin(EXP_A);
  85.     StdCan_Set_class(irTxMsg.Header, CAN_MODULE_CLASS_SNS);
  86.     StdCan_Set_direction(irTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  87.     irTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  88.     irTxMsg.Header.ModuleId = sns_irTransceive_ID;
  89.     irTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
  90.     irTxMsg.Length = 6;
  91.  
  92.     for (uint8_t i = 0; i < sns_irTransceive_SUPPORTED_NUM_CHANNELS; i++)
  93.     {
  94. #if IR_RX_ENABLE==1
  95.         irRxChannel[i].state = sns_irTransceive_STATE_RECEIVING;
  96.         irRxChannel[i].newData = FALSE;
  97.         irRxChannel[i].rxlen = 0;
  98.         irRxChannel[i].proto.timeout=0;
  99.         irRxChannel[i].proto.data=0;
  100.         irRxChannel[i].proto.repeats=0;
  101.         irRxChannel[i].proto.protocol=0;
  102. #endif
  103.     }
  104.    
  105. #if IR_RX_ENABLE==1
  106.     irRxChannel[0].timerNum=sns_irTransceive_RX0_REPEATE_TIMER;
  107.     irRxChannel[1].timerNum=sns_irTransceive_RX1_REPEATE_TIMER;
  108.     irRxChannel[2].timerNum=sns_irTransceive_RX2_REPEATE_TIMER;
  109. #endif
  110.    
  111.     IrTransceiver_Init();
  112.  
  113. #if IR_RX_ENABLE==1
  114.     irRxChannel[0].rxbuf = buf0;
  115.     IrTransceiver_InitRxChannel(0, irRxChannel[0].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
  116. #endif
  117.  
  118. #if IR_TX_ENABLE==1
  119.     IrTransceiver_InitTxChannel(0, sns_irTransceive_TX_done_callback, sns_irTransceive_TX0_PIN);
  120. #endif
  121.  
  122. }
  123.  
  124. void sns_irTransceive_Process(void)
  125. {
  126.     uint8_t res;
  127.    
  128.     for (uint8_t channel=0; channel < sns_irTransceive_SUPPORTED_NUM_CHANNELS; channel++)
  129.     {
  130. //gpio_toggle_pin(EXP_A);
  131.  
  132. #if IR_RX_ENABLE==1
  133.         switch (irRxChannel[channel].state)
  134.         {
  135.         case sns_irTransceive_STATE_IDLE:
  136.             irRxChannel[channel].state = sns_irTransceive_STATE_START_RECEIVE;
  137.             break;
  138.  
  139.         case sns_irTransceive_STATE_START_RECEIVE:
  140.             IrTransceiver_ResetRx(channel);
  141.             cli();
  142.             irRxChannel[channel].newData = FALSE;
  143.             sei();
  144.             irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
  145.             break;
  146.        
  147.         case sns_irTransceive_STATE_RECEIVING:
  148.             if (irRxChannel[channel].newData == TRUE) {
  149.                 //TODO: move this line to the RX callback
  150.                 IrTransceiver_DisableRx(channel);
  151.                 cli();
  152.                 irRxChannel[channel].newData = FALSE;
  153.                 sei();
  154.  
  155.                 /* Let protocol driver parse and then send on CAN */
  156.                 uint8_t res2 = parseProtocol(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, &irRxChannel[channel].proto);
  157.                 if (res2 == IR_OK && irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  158.                     irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
  159.                     irTxMsg.Data[0] |= channel<<4;
  160.                     irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  161.                     irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  162.                     irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  163.                     irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  164.                     irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  165.  
  166.                     StdCan_Put(&irTxMsg);
  167.                 }
  168.                 else if (irRxChannel[channel].proto.protocol == IR_PROTO_UNKNOWN)
  169.                 {
  170. #if (sns_irTransceive_SEND_DEBUG==1)
  171.                     send_debug(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
  172.                     irRxChannel[channel].proto.timeout=300;
  173. #endif
  174.                 }
  175.                
  176.                 /* Enable the receiver again */
  177.                 IrTransceiver_EnableRx(channel);
  178.                
  179.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  180.             }
  181.             break;
  182.  
  183.         case sns_irTransceive_STATE_START_PAUSE:
  184.             /* set a timer so we can send release button event when no new IR is arriving */
  185.             Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  186.             irRxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  187.             break;
  188.  
  189.         case sns_irTransceive_STATE_PAUSING:
  190.             /* reset timer if new IR arrived */
  191.             if (irRxChannel[channel].newData == TRUE || IrTransceiver_GetStoreEnableRx(channel) == TRUE) {
  192.                 cli();
  193.                 irRxChannel[channel].newData = FALSE;
  194.                 sei();
  195.                 Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  196.             }
  197.        
  198.             if (Timer_Expired(irRxChannel[channel].timerNum)) {
  199.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  200.             }
  201.             break;
  202.  
  203.         case sns_irTransceive_STATE_START_IDLE:
  204.             if (irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  205.                 /* Send button release command on CAN */
  206.                 irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
  207.                 irTxMsg.Data[0] |= channel<<4;
  208.                 irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  209.                 irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  210.                 irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  211.                 irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  212.                 irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  213.  
  214.                 StdCan_Put(&irTxMsg);
  215.             }
  216.             irRxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  217.             break;
  218.         }
  219. #endif
  220.  
  221.  
  222.     }
  223. }
  224.  
  225. void sns_irTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
  226. {
  227.     /*if (  StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  228.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  229.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_IRTRANSCEIVE &&
  230.         rxMsg->Header.ModuleId == sns_irTransceive_ID)
  231.     {
  232.         switch (rxMsg->Header.Command)
  233.         {
  234.         case CAN_CMD_MODULE_DUMMY:
  235.         ///TODO: Do something dummy
  236.         break;
  237.         }
  238.     }*/
  239. }
  240.  
  241. void sns_irTransceive_List(uint8_t ModuleSequenceNumber)
  242. {
  243.     StdCan_Msg_t txMsg;
  244.    
  245.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  246.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  247.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  248.  
  249.     txMsg.Header.ModuleId = sns_irTransceive_ID;
  250.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  251.     txMsg.Length = 6;
  252.  
  253.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  254.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  255.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  256.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  257.    
  258.     txMsg.Data[4] = NUMBER_OF_MODULES;
  259.     txMsg.Data[5] = ModuleSequenceNumber;
  260.    
  261.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  262. }
  263.