Subversion Repositories HomeAutomation

Rev

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