Subversion Repositories HomeAutomation

Rev

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