Subversion Repositories HomeAutomation

Rev

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