Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. #include "sns_PIR.h"
  3.  
  4. #ifdef sns_PIR_USEEEPROM
  5. #include "sns_PIR_eeprom.h"
  6. struct eeprom_sns_PIR EEMEM eeprom_sns_PIR =
  7. {
  8.     {
  9.         ///TODO: Define initialization values on the EEPROM variables here, this will generate a *.eep file that can be used to store this values to the node, can in future be done with a EEPROM module and the make-scrips. Write the values in the exact same order as the struct is defined in the *.h file.
  10.         0xAB,   // x
  11.         0x1234  // y
  12.     },
  13.     0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  14. };
  15. #endif
  16.  
  17. void sns_PIR_Init(void)
  18. {
  19. #ifdef sns_PIR_USEEEPROM
  20.     if (EEDATA_OK)
  21.     {
  22.       ///TODO: Use stored data to set initial values for the module
  23.       blablaX = eeprom_read_byte(EEDATA.x);
  24.       blablaY = eeprom_read_word(EEDATA.y);
  25.     } else
  26.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  27.       eeprom_write_byte_crc(EEDATA.x, 0xAB, WITHOUT_CRC);
  28.       eeprom_write_word_crc(EEDATA.y, 0x1234, WITHOUT_CRC);
  29.       EEDATA_UPDATE_CRC;
  30.     }
  31. #endif  
  32.     ///TODO: Initialize hardware etc here
  33.  
  34.     // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  35.     // Pcint_SetCallbackPin(sns_PIR_PCINT, EXP_C , &sns_PIR_pcint_callback);
  36.    
  37.     ADC_Init();
  38.     Timer_SetTimeout(sns_PIR_BRIGHT_TIMER, sns_PIR_SEND_PERIOD*1000, TimerTypeFreeRunning, 0);
  39. }
  40.  
  41. void sns_PIR_Process(void)
  42. {
  43.     if (Timer_Expired(sns_PIR_BRIGHT_TIMER)) {
  44.         uint16_t brightness = ADC_Get(sns_PIR_BRIGHTNESS_AD);
  45.         //busVoltage = (busVoltage & 0x03ff) * ADC_FACTOR;
  46.        
  47.         StdCan_Msg_t txMsg;
  48.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  49.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  50.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_PIR;
  51.         txMsg.Header.ModuleId = sns_PIR_ID;
  52.         txMsg.Header.Command = CAN_MODULE_CMD_PIR_MOTION;
  53.         txMsg.Length = 3;
  54.         txMsg.Data[0] = 0;
  55.         txMsg.Data[1] = (brightness>>2)&0xff;
  56.         txMsg.Data[2] = 0;
  57.         //txMsg.Data[1] = (busVoltage>>(ADC_SCALE-6+8))&0xff;
  58.         //txMsg.Data[2] = (busVoltage>>(ADC_SCALE-6))&0xff;
  59.  
  60.         StdCan_Put(&txMsg);
  61.     }
  62. }
  63.  
  64. void sns_PIR_HandleMessage(StdCan_Msg_t *rxMsg)
  65. {
  66.     /*if (  StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_xyz && ///TODO: Change this to the actual class type
  67.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  68.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_xyz && ///TODO: Change this to the actual module type
  69.         rxMsg->Header.ModuleId == sns_PIR_ID)
  70.     {
  71.         switch (rxMsg->Header.Command)
  72.         {
  73.         case CAN_CMD_MODULE_DUMMY:
  74.         ///TODO: Do something dummy
  75.         break;
  76.         }
  77.     }*/
  78. }
  79.  
  80. void sns_PIR_List(uint8_t ModuleSequenceNumber)
  81. {
  82.     StdCan_Msg_t txMsg;
  83.    
  84.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  85.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  86.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_PIR;
  87.     txMsg.Header.ModuleId = sns_PIR_ID;
  88.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  89.     txMsg.Length = 6;
  90.  
  91.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  92.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  93.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  94.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  95.    
  96.     txMsg.Data[4] = NUMBER_OF_MODULES;
  97.     txMsg.Data[5] = ModuleSequenceNumber;
  98.    
  99.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  100. }
  101.