Subversion Repositories HomeAutomation

Rev

Rev 1491 | Rev 1737 | 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[IR_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[IR_SUPPORTED_NUM_CHANNELS];
  26. #endif
  27.  
  28. uint16_t    buf[3][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 < IR_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 < IR_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 < IR_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].repeatCount = 0;
  121.         irTxChannel[i].txlen = 0;
  122.         irTxChannel[i].proto.data=0;
  123.         irTxChannel[i].proto.repeats=0;
  124.         irTxChannel[i].proto.framecnt=0;
  125.         irTxChannel[i].proto.protocol=0;
  126. #endif
  127.     }
  128.    
  129. #if IR_RX_ENABLE==1
  130.     irRxChannel[0].timerNum=sns_irTransceive_RX0_REPEATE_TIMER;
  131.     irRxChannel[1].timerNum=sns_irTransceive_RX1_REPEATE_TIMER;
  132.     irRxChannel[2].timerNum=sns_irTransceive_RX2_REPEATE_TIMER;
  133. #endif
  134.  
  135. #if IR_TX_ENABLE==1
  136.     irTxChannel[0].timerNum=sns_irTransceive_TX0_REPEATE_TIMER;
  137.     irTxChannel[1].timerNum=sns_irTransceive_TX1_REPEATE_TIMER;
  138.     irTxChannel[2].timerNum=sns_irTransceive_TX2_REPEATE_TIMER;
  139. #endif
  140.    
  141.     IrTransceiver_Init();
  142.     gpio_set_out(sns_irTransceive_VCC_EN0_PIN);
  143.     gpio_set_out(sns_irTransceive_VCC_EN1_PIN);
  144.     gpio_set_out(sns_irTransceive_VCC_EN2_PIN);
  145.     gpio_set_pin(sns_irTransceive_VCC_EN0_PIN);
  146.     gpio_set_pin(sns_irTransceive_VCC_EN1_PIN);
  147.     gpio_set_pin(sns_irTransceive_VCC_EN2_PIN);
  148.  
  149. #if IR_RX_ENABLE==1
  150.     irRxChannel[0].rxbuf = buf[0];
  151.     IrTransceiver_InitRxChannel(0, irRxChannel[0].rxbuf, sns_irTransceive_RX_done_callback, sns_irTransceive_RX0_PCINT, sns_irTransceive_RX0_PIN);
  152. #endif
  153.  
  154. #if IR_TX_ENABLE==1
  155.     gpio_set_out(sns_irTransceive_MOD_PIN);
  156.     gpio_set_out(sns_irTransceive_TX0_PIN);
  157.     gpio_set_out(sns_irTransceive_TX1_PIN);
  158.     gpio_set_out(sns_irTransceive_TX2_PIN);
  159. #if IR_TX_ACTIVE_LOW==1
  160.     gpio_set_pin(sns_irTransceive_TX0_PIN);
  161.     gpio_set_pin(sns_irTransceive_TX1_PIN);
  162.     gpio_set_pin(sns_irTransceive_TX2_PIN);
  163. #endif
  164.  
  165.     irTxChannel[0].txbuf = buf[0];
  166.     IrTransceiver_InitTxChannel(0, sns_irTransceive_TX_done_callback, sns_irTransceive_TX0_PIN);
  167.  
  168.     irTxChannel[1].txbuf = buf[1];
  169.     IrTransceiver_InitTxChannel(1, sns_irTransceive_TX_done_callback, sns_irTransceive_TX1_PIN);
  170.  
  171.     irTxChannel[2].txbuf = buf[2];
  172.     IrTransceiver_InitTxChannel(2, sns_irTransceive_TX_done_callback, sns_irTransceive_TX2_PIN);
  173. #endif
  174.  
  175. }
  176.  
  177. void sns_irTransceive_Process(void)
  178. {
  179.     for (uint8_t channel=0; channel < IR_SUPPORTED_NUM_CHANNELS; channel++)
  180.     {
  181. //gpio_toggle_pin(EXP_A);
  182.  
  183. #if IR_RX_ENABLE==1
  184.         switch (irRxChannel[channel].state)
  185.         {
  186.         case sns_irTransceive_STATE_IDLE:
  187.             irRxChannel[channel].state = sns_irTransceive_STATE_START_RECEIVE;
  188.             break;
  189.  
  190.         case sns_irTransceive_STATE_START_RECEIVE:
  191.             IrTransceiver_ResetRx(channel);
  192.             cli();
  193.             irRxChannel[channel].newData = FALSE;
  194.             sei();
  195.             irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
  196.             break;
  197.        
  198.         case sns_irTransceive_STATE_RECEIVING:
  199.             if (irRxChannel[channel].newData == TRUE) {
  200.                 //TODO: move this line to the RX callback
  201.                 IrTransceiver_DisableRx(channel);
  202.                 cli();
  203.                 irRxChannel[channel].newData = FALSE;
  204.                 sei();
  205.  
  206.                 /* Let protocol driver parse and then send on CAN */
  207.                 uint8_t res2 = parseProtocol(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, &irRxChannel[channel].proto);
  208.                 if (res2 == IR_OK && irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  209.                     irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
  210.                     irTxMsg.Data[0] |= channel<<4;
  211.                     irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  212.                     irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  213.                     irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  214.                     irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  215.                     irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  216.  
  217.                     StdCan_Put(&irTxMsg);
  218.                 }
  219.                 else if (irRxChannel[channel].proto.protocol == IR_PROTO_UNKNOWN)
  220.                 {
  221. #if (sns_irTransceive_SEND_DEBUG==1)
  222.                     send_debug(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
  223.                     irRxChannel[channel].proto.timeout=300;
  224. #endif
  225.                 }
  226.                
  227.                 /* Enable the receiver again */
  228.                 IrTransceiver_EnableRx(channel);
  229.                
  230.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  231.             }
  232.             break;
  233.  
  234.         case sns_irTransceive_STATE_START_PAUSE:
  235.             /* set a timer so we can send release button event when no new IR is arriving */
  236.             Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  237.             irRxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  238.             break;
  239.  
  240.         case sns_irTransceive_STATE_PAUSING:
  241.             /* reset timer if new IR arrived */
  242.             if (irRxChannel[channel].newData == TRUE || IrTransceiver_GetStoreEnableRx(channel) == TRUE) {
  243.                 cli();
  244.                 irRxChannel[channel].newData = FALSE;
  245.                 sei();
  246.                 Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  247.             }
  248.        
  249.             if (Timer_Expired(irRxChannel[channel].timerNum)) {
  250.                 irRxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  251.             }
  252.             break;
  253.  
  254.         case sns_irTransceive_STATE_START_IDLE:
  255.             if (irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
  256.                 /* Send button release command on CAN */
  257.                 irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
  258.                 irTxMsg.Data[0] |= channel<<4;
  259.                 irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
  260.                 irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
  261.                 irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
  262.                 irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
  263.                 irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
  264.  
  265.                 StdCan_Put(&irTxMsg);
  266.             }
  267.             irRxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  268.             break;
  269.         }
  270. #endif
  271.  
  272. #if IR_TX_ENABLE==1
  273.         switch (irTxChannel[channel].state)
  274.         {
  275.         case sns_irTransceive_STATE_IDLE:
  276.             /* transmission is started when a command is received on can */
  277.             break;
  278.  
  279.         case sns_irTransceive_STATE_START_TRANSMIT:
  280. //      printf("1\n");
  281.             if (expandProtocol(irTxChannel[channel].txbuf, &irTxChannel[channel].txlen, &irTxChannel[channel].proto) == IR_OK)
  282.             {
  283.                 IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, irTxChannel[channel].txlen);
  284.                 irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
  285. //      printf("2\n");
  286.             }
  287.             else
  288.             {
  289.                 irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  290.             }
  291.             break;
  292.        
  293.         case sns_irTransceive_STATE_TRANSMITTING:
  294.             if (irTxChannel[channel].sendComplete == TRUE)
  295.             {
  296.                 cli();
  297.                 irTxChannel[channel].sendComplete = FALSE;
  298.                 sei();
  299.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
  300. //      printf("3\n");
  301.             }
  302.             break;
  303.  
  304.         case sns_irTransceive_STATE_START_PAUSE:
  305.             if (irTxChannel[channel].repeatCount < irTxChannel[channel].proto.repeats)
  306.             {
  307.                 irTxChannel[channel].repeatCount++;
  308.             }
  309.            
  310.             Timer_SetTimeout(irTxChannel[channel].timerNum, irTxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
  311.  
  312.             if (irTxChannel[channel].proto.framecnt != 255)
  313.             {
  314.                 irTxChannel[channel].proto.framecnt++;
  315.             }
  316.            
  317.             irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
  318.             break;
  319.  
  320.         case sns_irTransceive_STATE_PAUSING:
  321.             if (Timer_Expired(irTxChannel[channel].timerNum))
  322.             {
  323.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  324. //      printf("4\n");
  325.             }
  326.            
  327.             /* transmission is stopped when such command is recevied on can */
  328.             if (irTxChannel[channel].stopSend == TRUE && irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats)
  329.             {
  330.                 //TODO maybe send message on can for status? to confirm stopped sending, ready for new command
  331.                 irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
  332. //      printf("5\n");
  333.             }
  334.             break;
  335.  
  336.         case sns_irTransceive_STATE_START_IDLE:
  337.             irTxChannel[channel].stopSend = FALSE;
  338.             irTxChannel[channel].repeatCount = 0;
  339.             irTxChannel[channel].proto.framecnt = 0;
  340.            
  341.             irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
  342. //      printf("6\n");
  343.             break;
  344.         }
  345. #endif
  346.  
  347.     }
  348. }
  349.  
  350. void sns_irTransceive_HandleMessage(StdCan_Msg_t *rxMsg)
  351. {
  352.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  353.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  354.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_IRTRANSCEIVE &&
  355.         rxMsg->Header.ModuleId == sns_irTransceive_ID)
  356.     {
  357.         uint8_t channel;
  358.         switch (rxMsg->Header.Command)
  359.         {
  360.         case CAN_MODULE_CMD_PHYSICAL_IR:
  361.             channel = rxMsg->Data[0]>>4;
  362.             if ((0xf&rxMsg->Data[0]) == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED && channel < IR_SUPPORTED_NUM_CHANNELS)
  363.             {
  364.                 if (irTxChannel[channel].state == sns_irTransceive_STATE_IDLE)
  365.                 {
  366.                     irTxChannel[channel].proto.protocol = rxMsg->Data[1];
  367.  
  368.                     irTxChannel[channel].proto.data = rxMsg->Data[2];
  369.                     irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  370.                     irTxChannel[channel].proto.data |= rxMsg->Data[3];
  371.                     irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  372.                     irTxChannel[channel].proto.data |= rxMsg->Data[4];
  373.                     irTxChannel[channel].proto.data = irTxChannel[channel].proto.data<<8;
  374.                     irTxChannel[channel].proto.data |= rxMsg->Data[5];
  375.  
  376.                     irTxChannel[channel].state = sns_irTransceive_STATE_START_TRANSMIT;
  377.                 }
  378.             }
  379.             else if ((0xf&rxMsg->Data[0]) == CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED && channel < IR_SUPPORTED_NUM_CHANNELS)
  380.             {
  381.                 if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE)
  382.                 {
  383.                     irTxChannel[channel].stopSend = TRUE;
  384.                 }
  385.             }
  386.             break;
  387.         }
  388.     }
  389. }
  390.  
  391. void sns_irTransceive_List(uint8_t ModuleSequenceNumber)
  392. {
  393.     StdCan_Msg_t txMsg;
  394.    
  395.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  396.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  397.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
  398.  
  399.     txMsg.Header.ModuleId = sns_irTransceive_ID;
  400.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  401.     txMsg.Length = 6;
  402.  
  403.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  404.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  405.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  406.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  407.    
  408.     txMsg.Data[4] = NUMBER_OF_MODULES;
  409.     txMsg.Data[5] = ModuleSequenceNumber;
  410.    
  411.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  412. }
  413.