Subversion Repositories HomeAutomation

Rev

Rev 1440 | Rev 1481 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. #include "sns_PIR.h"
  2.  
  3. #define ABS(a) ((a) < 0 ? (-1*a) : (a))
  4.  
  5.  
  6. // a weighted moving avarage value is used to remove "DC offset"
  7. static uint32_t zeroOffset = 0;
  8.  
  9. // a second avarage value is used to detect changes in avarage "energy"
  10. #define NR_AVG_VALUES   15
  11. static int8_t avgValues[NR_AVG_VALUES];
  12. static uint8_t avgIndex = 0;
  13.  
  14.  
  15. void sns_PIR_Init(void)
  16. {
  17.     ltc2450_init();
  18.     Timer_SetTimeout(sns_PIR_BRIGHT_TIMER, 40, TimerTypeFreeRunning, 0);
  19. }
  20.  
  21.  
  22. void sns_PIR_Process(void)
  23. {
  24.     static uint8_t printCounter = 0;
  25.     if (Timer_Expired(sns_PIR_BRIGHT_TIMER)) {
  26.         uint16_t pirValue;
  27.         do {
  28.             // if ADC is busy, returned value is 0xFFFF
  29.             pirValue = ltc2450_read();
  30.         } while (pirValue >= 0xFFFF);
  31.        
  32.         // first time?
  33.         if (zeroOffset == 0) {
  34.             zeroOffset = pirValue;
  35.         }
  36.         else {
  37.             zeroOffset = (zeroOffset*4 + (uint32_t)pirValue)/5;
  38.         }
  39.        
  40.         int8_t test = (int8_t)((int32_t)pirValue - (int32_t)zeroOffset);
  41.         avgValues[avgIndex++] = test;
  42.         if (avgIndex >= NR_AVG_VALUES) {
  43.             avgIndex = 0;
  44.         }
  45.         uint16_t avgValue = 0;
  46.         for (uint8_t i=0; i<NR_AVG_VALUES; i++) {
  47.             avgValue += ABS(avgValues[i]);
  48.         }
  49.         uint8_t motionDetect = (avgValue >= 80);
  50.        
  51.                
  52.         if (printCounter++ >= 200/40) {
  53.             printCounter = 0;
  54.             //printf("AD:%u\n", pirValue);
  55.             printf("Motion: %u\n", motionDetect);
  56.            
  57.             /*StdCan_Msg_t txMsg;
  58.             StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  59.             StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  60.             txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_PIR;
  61.             txMsg.Header.ModuleId = sns_PIR_ID;
  62.             txMsg.Header.Command = CAN_MODULE_CMD_PIR_MOTION;
  63.             txMsg.Length = 3;
  64.             txMsg.Data[0] = 0;
  65.             txMsg.Data[1] = (uint8_t)((pirValue & 0xFF00) >> 2);
  66.             txMsg.Data[2] = (uint8_t)((pirValue & 0x00FF) >> 0);
  67.             StdCan_Put(&txMsg);*/
  68.         }
  69.     }
  70. }
  71.  
  72. void sns_PIR_HandleMessage(StdCan_Msg_t *rxMsg)
  73. {
  74.     /*if (  StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_xyz && ///TODO: Change this to the actual class type
  75.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  76.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_xyz && ///TODO: Change this to the actual module type
  77.         rxMsg->Header.ModuleId == sns_PIR_ID)
  78.     {
  79.         switch (rxMsg->Header.Command)
  80.         {
  81.         case CAN_CMD_MODULE_DUMMY:
  82.         ///TODO: Do something dummy
  83.         break;
  84.         }
  85.     }*/
  86. }
  87.  
  88. void sns_PIR_List(uint8_t ModuleSequenceNumber)
  89. {
  90.     StdCan_Msg_t txMsg;
  91.    
  92.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  93.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  94.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_PIR;
  95.     txMsg.Header.ModuleId = sns_PIR_ID;
  96.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  97.     txMsg.Length = 6;
  98.  
  99.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  100.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  101.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  102.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  103.    
  104.     txMsg.Data[4] = NUMBER_OF_MODULES;
  105.     txMsg.Data[5] = ModuleSequenceNumber;
  106.    
  107.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  108. }
  109.  
  110.