Subversion Repositories HomeAutomation

Rev

Rev 1047 | Rev 2040 | Go to most recent revision | 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, &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>>24)&0xff;
  89.                 irTxMsg.Data[3] = (proto.data>>16)&0xff;
  90.                 irTxMsg.Data[4] = (proto.data>>8)&0xff;
  91.                 irTxMsg.Data[5] = proto.data&0xff;
  92.                 irTxMsg.Length = 6;
  93.                 StdCan_Put(&irTxMsg);
  94.             } else if (proto.protocol == IR_PROTO_UNKNOWN) {
  95. #if (sns_irReceive_SEND_DEBUG==1)
  96.                 send_debug(rxbuffer, len);
  97.                 proto.timeout=300;
  98. #endif
  99.             }
  100.            
  101.             state = sns_irReceive_STATE_START_PAUSE;
  102.         }
  103.         break;
  104.  
  105.     case sns_irReceive_STATE_START_PAUSE:
  106.         IrTransceiver_Receive_Start(rxbuffer);
  107.         Timer_SetTimeout(sns_irReceive_REPEATE_TIMER, proto.timeout, TimerTypeOneShot, 0);
  108.         state = sns_irReceive_STATE_PAUSING;
  109.         break;
  110.  
  111.     case sns_irReceive_STATE_PAUSING:
  112.         //resetta timebase-var om ir finns
  113.         res = IrTransceiver_Receive_Poll(&len);
  114.         if (res == IR_NOT_FINISHED || res == IR_OK) {
  115.             Timer_SetTimeout(sns_irReceive_REPEATE_TIMER, proto.timeout, TimerTypeOneShot, 0);
  116.         }
  117.         if (res == IR_OK) {
  118.             /* start a new reception */
  119.             IrTransceiver_Receive_Start(rxbuffer);
  120.         }
  121.        
  122.         if (Timer_Expired(sns_irReceive_REPEATE_TIMER)) {
  123.             state = sns_irReceive_STATE_START_IDLE;
  124.         }
  125.         break;
  126.  
  127.     case sns_irReceive_STATE_START_IDLE:
  128.         if (proto.protocol != IR_PROTO_UNKNOWN) {
  129.             //skicka på can
  130.             irTxMsg.Data[0] = CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_RELEASED;
  131.             StdCan_Put(&irTxMsg);
  132.         }
  133.         state = sns_irReceive_STATE_IDLE;
  134.         break;
  135.  
  136.     }
  137.  
  138. }
  139.  
  140. void sns_irReceive_HandleMessage(StdCan_Msg_t *rxMsg)
  141. {
  142.     /*if (  StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  143.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  144.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_IRRECEIVE &&
  145.         rxMsg->Header.ModuleId == sns_irReceive_ID)
  146.     {
  147.         switch (rxMsg->Header.Command)
  148.         {
  149.         case CAN_CMD_MODULE_DUMMY:
  150.         ///TODO: Do something dummy
  151.         break;
  152.         }
  153.     }*/
  154. }
  155.  
  156. void sns_irReceive_List(uint8_t ModuleSequenceNumber)
  157. {
  158.     StdCan_Msg_t txMsg;
  159.    
  160.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  161.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  162.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRRECEIVE;
  163.  
  164.     txMsg.Header.ModuleId = sns_irReceive_ID;
  165.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  166.     txMsg.Length = 6;
  167.  
  168.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  169.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  170.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  171.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  172.    
  173.     txMsg.Data[4] = NUMBER_OF_MODULES;
  174.     txMsg.Data[5] = ModuleSequenceNumber;
  175.    
  176.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  177. }
  178.