Subversion Repositories HomeAutomation

Rev

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