Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. #include "sns_DHT11.h"
  3. uint8_t sendNewTemp = 0;
  4. uint8_t temperature = 0;
  5. uint8_t humidity = 0;
  6.  
  7. #ifdef sns_DHT11_USEEEPROM
  8. #include "sns_DHT11_eeprom.h"
  9. struct eeprom_sns_DHT11 EEMEM eeprom_sns_DHT11 =
  10. {
  11.     {
  12.         ///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.
  13.         0xAB,       // x
  14.         0x1234      // y
  15.         0x12345678  // z
  16.     },
  17.     0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  18. };
  19. #endif
  20.  
  21. void ConvertTemperature2_callback(uint8_t timer)
  22. {
  23.     uint8_t word[5];
  24.     if (dht11_readData(&word)) {
  25.         sendNewTemp = 1;
  26.         temperature = word[2];
  27.         humidity = word[0];
  28.     }
  29. }
  30.  
  31. void ConvertTemperature_callback(uint8_t timer)
  32. {
  33.     dht11_start();
  34.     Timer_SetTimeout(sns_DHT11_18MS_TIMER, 18, TimerTypeOneShot, &ConvertTemperature2_callback);
  35. }
  36.  
  37. void sns_DHT11_Init(void)
  38. {
  39. #ifdef sns_DHT11_USEEEPROM
  40.     if (EEDATA_OK)
  41.     {
  42.       ///TODO: Use stored data to set initial values for the module
  43.       blablaX = eeprom_read_byte(EEDATA.x);
  44.       blablaY = eeprom_read_word(EEDATA16.y);
  45.       blablaZ = eeprom_read_dword(EEDATA32.y);
  46.     } else
  47.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  48.       eeprom_write_byte_crc(EEDATA.x, 0xAB, WITHOUT_CRC);
  49.       eeprom_write_word_crc(EEDATA16.y, 0x1234, WITHOUT_CRC);
  50.       eeprom_write_dword_crc(EEDATA32.y, 0x12345678, WITHOUT_CRC);
  51.       EEDATA_UPDATE_CRC;
  52.     }
  53. #endif
  54.     ///TODO: Initialize hardware etc here
  55.  
  56.     // to use PCINt lib, call this function: (the callback function look as a timer callback function)
  57.     // Pcint_SetCallbackPin(sns_DHT11_PCINT, EXP_C , &sns_DHT11_pcint_callback);
  58.     Timer_SetTimeout(sns_DHT11_TIMER, 5000, TimerTypeFreeRunning, &ConvertTemperature_callback);
  59. }
  60.  
  61. void sns_DHT11_Process(void)
  62. {
  63.     if (sendNewTemp) {
  64.         uint16_t temp;
  65.         sendNewTemp = 0;
  66.  
  67.         StdCan_Msg_t txMsg;
  68.  
  69.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  70.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  71.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DHT11;
  72.         txMsg.Header.ModuleId = sns_DHT11_ID;
  73.         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_TEMPERATURE_CELSIUS;
  74.         txMsg.Length = 3;
  75.  
  76.         txMsg.Data[0] = 1;
  77.         temp = temperature*64;
  78.         txMsg.Data[1] = (uint8_t)(temp >> 8);
  79.         txMsg.Data[2] = (uint8_t)(temp & 0xff);
  80.         while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  81.  
  82.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  83.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  84.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DHT11;
  85.         txMsg.Header.ModuleId = sns_DHT11_ID;
  86.         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_HUMIDITY_PERCENT;
  87.         txMsg.Length = 3;
  88.  
  89.         txMsg.Data[0] = 1;
  90.         temp = humidity*64;
  91.         txMsg.Data[1] = (uint8_t)(temp >> 8);
  92.         txMsg.Data[2] = (uint8_t)(temp & 0xff);
  93.         while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  94.  
  95.     }
  96.  
  97.     ///TODO: Stuff that needs doing is done here
  98. }
  99.  
  100. void sns_DHT11_HandleMessage(StdCan_Msg_t *rxMsg)
  101. {
  102.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS && ///TODO: Change this to the actual class type
  103.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  104.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_DHT11 && ///TODO: Change this to the actual module type
  105.         rxMsg->Header.ModuleId == sns_DHT11_ID)
  106.     {
  107.         StdCan_Msg_t txMsg;
  108.         switch (rxMsg->Header.Command)
  109.         {
  110.             case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL:
  111.                 //if (rxMsg->Length > 0)
  112.                 //  sns_ds18x20_ReportInterval = rxMsg->Data[0];
  113.  
  114.  
  115.                 StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  116.                 StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  117.                 txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DHT11;
  118.                 txMsg.Header.ModuleId = sns_DHT11_ID;
  119.                 txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL;
  120.                 txMsg.Length = 1;
  121.  
  122.                 //txMsg.Data[0] = sns_ds18x20_ReportInterval;
  123.  
  124.                 StdCan_Put(&txMsg);
  125.                 break;
  126.         }
  127.     }
  128. }
  129.  
  130. void sns_DHT11_List(uint8_t ModuleSequenceNumber)
  131. {
  132.     StdCan_Msg_t txMsg;
  133.  
  134.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); ///TODO: Change this to the actual class type
  135.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  136.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DHT11; ///TODO: Change this to the actual module type
  137.     txMsg.Header.ModuleId = sns_DHT11_ID;
  138.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  139.     txMsg.Length = 6;
  140.  
  141.     uint32_t HwId=BIOS_GetHwId();
  142.     txMsg.Data[0] = HwId&0xff;
  143.     txMsg.Data[1] = (HwId>>8)&0xff;
  144.     txMsg.Data[2] = (HwId>>16)&0xff;
  145.     txMsg.Data[3] = (HwId>>24)&0xff;
  146.  
  147.     txMsg.Data[4] = NUMBER_OF_MODULES;
  148.     txMsg.Data[5] = ModuleSequenceNumber;
  149.  
  150.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  151. }
  152.