Subversion Repositories HomeAutomation

Rev

Rev 1047 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. #include "sns_irReceive.h"
  3.  
  4. uint8_t state = sns_irReceive_STATE_IDLE;
  5. Ir_Protocol_Data_t proto;
  6. uint8_t len=0;
  7. uint16_t rxbuffer[MAX_NR_TIMES];
  8. StdCan_Msg_t irTxMsg;
  9.  
  10.  
  11. #if (sns_irReceive_SEND_DEBUG==1)
  12. void send_debug(uint16_t *buffer, uint8_t len) {
  13.  
  14.     /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol */
  15.  
  16.     StdCan_Set_class(irTxMsg.Header, CAN_MODULE_CLASS_SNS);
  17.     StdCan_Set_direction(irTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  18.     irTxMsg.Length = 8;
  19.     irTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRRECEIVE;
  20.     irTxMsg.Header.ModuleId = sns_irReceive_ID;
  21.     irTxMsg.Header.Command = CAN_MODULE_CMD_IRRECEIVE_IRRAW;
  22.     for (uint8_t i = 0; i < len>>2; i++) {
  23.         uint8_t index = i<<2;
  24.  
  25.         irTxMsg.Data[0] = (buffer[index]>>8)&0xff;
  26.         irTxMsg.Data[1] = (buffer[index]>>0)&0xff;
  27.         irTxMsg.Data[2] = (buffer[index+1]>>8)&0xff;
  28.         irTxMsg.Data[3] = (buffer[index+1]>>0)&0xff;
  29.         irTxMsg.Data[4] = (buffer[index+2]>>8)&0xff;
  30.         irTxMsg.Data[5] = (buffer[index+2]>>0)&0xff;
  31.         irTxMsg.Data[6] = (buffer[index+3]>>8)&0xff;
  32.         irTxMsg.Data[7] = (buffer[index+3]>>0)&0xff;
  33.                
  34.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  35.         while (StdCan_Put(&irTxMsg) != StdCan_Ret_OK) {}
  36.     }
  37.    
  38.     uint8_t lastpacketcnt = len&0x03;
  39.     if (lastpacketcnt > 0) {
  40.         irTxMsg.Length = lastpacketcnt<<1;
  41.         for (uint8_t i = 0; i < lastpacketcnt; i++) {
  42.             irTxMsg.Data[i<<1] = (buffer[(len&0xfc)|i]>>8)&0xff;
  43.             irTxMsg.Data[(i<<1)+1] = (buffer[(len&0xfc)|i]>>0)&0xff;
  44.         }
  45.         /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
  46.         while (StdCan_Put(&irTxMsg) != StdCan_Ret_OK) {}
  47.     }
  48.  
  49. }
  50. #endif
  51.  
  52. void sns_irReceive_Init(void)
  53. {
  54.     IrTransceiver_Init();
  55.     proto.timeout=0; proto.data=0; proto.repeats=0; proto.protocol=0;
  56.  
  57. }
  58.  
  59. void sns_irReceive_Process(void)
  60. {
  61.     uint8_t res;
  62.     switch (state)
  63.     {
  64.     case sns_irReceive_STATE_IDLE:
  65.         IrTransceiver_Receive_Start(rxbuffer);
  66.         state = sns_irReceive_STATE_START_RECEIVE;
  67.         break;
  68.  
  69.     case sns_irReceive_STATE_START_RECEIVE:
  70.         state = sns_irReceive_STATE_RECEIVING;
  71.         break;
  72.  
  73.     case sns_irReceive_STATE_RECEIVING:
  74.         res = IrTransceiver_Receive_Poll(&len);
  75.         if ((res == IR_OK) && (len > 0)) {
  76.             //låt protocols parsa och skicka sen på can
  77.             uint8_t res2 = parseProtocol(rxbuffer, len, &proto);
  78.             if (res2 == IR_OK && proto.protocol != IR_PROTO_UNKNOWN) {
  79.                 StdCan_Set_class(irTxMsg.Header, CAN_MODULE_CLASS_SNS);
  80.                 StdCan_Set_direction(irTxMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  81.                 irTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRRECEIVE;
  82.                 irTxMsg.Header.ModuleId = sns_irReceive_ID;
  83.                 irTxMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_IR;
  84.                 irTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
  85.                 irTxMsg.Data[1] = proto.protocol;
  86.                 irTxMsg.Data[2] = (proto.data>>24)&0xff;
  87.                 irTxMsg.Data[3] = (proto.data>>16)&0xff;
  88.                 irTxMsg.Data[4] = (proto.data>>8)&0xff;
  89.                 irTxMsg.Data[5] = proto.data&0xff;
  90.                 irTxMsg.Length = 6;
  91.                 StdCan_Put(&irTxMsg);
  92.             } else if (proto.protocol == IR_PROTO_UNKNOWN) {
  93. #if (sns_irReceive_SEND_DEBUG==1)
  94.                 send_debug(rxbuffer, len);
  95.                 proto.timeout=300;
  96. #endif
  97.             }
  98.            
  99.             state = sns_irReceive_STATE_START_PAUSE;
  100.         }
  101.         break;
  102.  
  103.     case sns_irReceive_STATE_START_PAUSE:
  104.         IrTransceiver_Receive_Start(rxbuffer);
  105.         Timer_SetTimeout(sns_irReceive_REPEATE_TIMER, proto.timeout, TimerTypeOneShot, 0);
  106.         state = sns_irReceive_STATE_PAUSING;
  107.         break;
  108.  
  109.     case sns_irReceive_STATE_PAUSING:
  110.         //resetta timebase-var om ir finns
  111.         res = IrTransceiver_Receive_Poll(&len);
  112.         if (res == IR_NOT_FINISHED || res == IR_OK) {
  113.             Timer_SetTimeout(sns_irReceive_REPEATE_TIMER, proto.timeout, TimerTypeOneShot, 0);
  114.         }
  115.         if (res == IR_OK) {
  116.             /* start a new reception */
  117.             IrTransceiver_Receive_Start(rxbuffer);
  118.         }
  119.        
  120.         if (Timer_Expired(sns_irReceive_REPEATE_TIMER)) {
  121.             state = sns_irReceive_STATE_START_IDLE;
  122.         }
  123.         break;
  124.  
  125.     case sns_irReceive_STATE_START_IDLE:
  126.         if (proto.protocol != IR_PROTO_UNKNOWN) {
  127.             //skicka på can
  128.             irTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
  129.             StdCan_Put(&irTxMsg);
  130.         }
  131.         state = sns_irReceive_STATE_IDLE;
  132.         break;
  133.  
  134.     }
  135.  
  136. }
  137.  
  138. void sns_irReceive_HandleMessage(StdCan_Msg_t *rxMsg)
  139. {
  140.     /*if (  StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  141.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  142.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_IRRECEIVE &&
  143.         rxMsg->Header.ModuleId == sns_irReceive_ID)
  144.     {
  145.         switch (rxMsg->Header.Command)
  146.         {
  147.         case CAN_CMD_MODULE_DUMMY:
  148.         ///TODO: Do something dummy
  149.         break;
  150.         }
  151.     }*/
  152. }
  153.  
  154. void sns_irReceive_List(uint8_t ModuleSequenceNumber)
  155. {
  156.     StdCan_Msg_t txMsg;
  157.    
  158.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  159.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  160.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRRECEIVE;
  161.  
  162.     txMsg.Header.ModuleId = sns_irReceive_ID;
  163.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  164.     txMsg.Length = 6;
  165.  
  166.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  167.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  168.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  169.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  170.    
  171.     txMsg.Data[4] = NUMBER_OF_MODULES;
  172.     txMsg.Data[5] = ModuleSequenceNumber;
  173.    
  174.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  175. }
  176.