Subversion Repositories HomeAutomation

Rev

Rev 2010 | 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[DHT11_NUM_SENSORS];
  5. uint8_t humidity[DHT11_NUM_SENSORS];
  6. uint8_t sns_DHT11_currentSensor = DHT11_NUM_SENSORS-1;
  7.  
  8. #if sns_DHT11_USEEEPROM==1
  9. #include "sns_DHT11_eeprom.h"
  10. struct eeprom_sns_DHT11 EEMEM eeprom_sns_DHT11 =
  11. {
  12.     {
  13.         ///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.
  14.         0xAB,       // x
  15.         0x1234      // y
  16.         0x12345678  // z
  17.     },
  18.     0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
  19. };
  20. #endif
  21.  
  22. void ConvertTemperature2_callback(uint8_t timer)
  23. {
  24.     uint8_t word[5];
  25.     if (dht11_readData((uint8_t*) word)) {
  26.         sendNewTemp = 1;
  27.         temperature[sns_DHT11_currentSensor] = word[2];
  28.         humidity[sns_DHT11_currentSensor] = word[0];
  29.     }
  30. }
  31.  
  32. void ConvertTemperature_callback_DHT11(uint8_t timer)
  33. {
  34. #ifndef DHT11_ONE_BUS
  35.     sns_DHT11_currentSensor++;
  36.     if (sns_DHT11_currentSensor >= DHT11_NUM_SENSORS)
  37.     {sns_DHT11_currentSensor = 0;}
  38.  
  39.     switch (sns_DHT11_currentSensor)
  40.     {
  41.         case 0:
  42.             dht11_set_bus(DHT11_PIN_ch0);
  43.             break;
  44.         case 1:
  45.             dht11_set_bus(DHT11_PIN_ch1);
  46.             break;
  47. #ifdef DHT11_PIN_ch2
  48.         case 2:
  49.             dht11_set_bus(DHT11_PIN_ch2);
  50.             break;
  51. #endif
  52. #ifdef DHT11_PIN_ch3
  53.         case 3:
  54.             dht11_set_bus(DHT11_PIN_ch3);
  55.             break;
  56. #endif
  57. #ifdef DHT11_PIN_ch4
  58.         case 4:
  59.             dht11_set_bus(DHT11_PIN_ch4);
  60.             break;
  61. #endif
  62. #ifdef DHT11_PIN_ch5
  63.         case 5:
  64.             dht11_set_bus(DHT11_PIN_ch5);
  65.             break;
  66. #endif
  67.     }
  68. #endif
  69.  
  70.     dht11_start();
  71.     Timer_SetTimeout(sns_DHT11_18MS_TIMER, 18, TimerTypeOneShot, &ConvertTemperature2_callback);
  72. }
  73.  
  74. void sns_DHT11_Init(void)
  75. {
  76. #if sns_DHT11_USEEEPROM==1
  77.     if (EEDATA_OK)
  78.     {
  79.       ///TODO: Use stored data to set initial values for the module
  80.       blablaX = eeprom_read_byte(EEDATA.x);
  81.       blablaY = eeprom_read_word(EEDATA16.y);
  82.       blablaZ = eeprom_read_dword(EEDATA32.y);
  83.     } else
  84.     {   //The CRC of the EEPROM is not correct, store default values and update CRC
  85.       eeprom_write_byte_crc(EEDATA.x, 0xAB, WITHOUT_CRC);
  86.       eeprom_write_word_crc(EEDATA16.y, 0x1234, WITHOUT_CRC);
  87.       eeprom_write_dword_crc(EEDATA32.y, 0x12345678, WITHOUT_CRC);
  88.       EEDATA_UPDATE_CRC;
  89.     }
  90. #endif
  91.     Timer_SetTimeout(sns_DHT11_TIMER, 10000/DHT11_NUM_SENSORS, TimerTypeFreeRunning, &ConvertTemperature_callback_DHT11);
  92. }
  93.  
  94. void sns_DHT11_Process(void)
  95. {
  96.     if (sendNewTemp) {
  97.         uint16_t temp;
  98.         sendNewTemp = 0;
  99.  
  100.         StdCan_Msg_t txMsg;
  101.  
  102.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  103.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  104.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DHT11;
  105.         txMsg.Header.ModuleId = sns_DHT11_ID;
  106.         txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_TEMPERATURE_CELSIUS;
  107.         txMsg.Length = 3;
  108.  
  109.         txMsg.Data[0] = sns_DHT11_currentSensor;
  110.         temp = temperature[sns_DHT11_currentSensor]*64;
  111.         txMsg.Data[1] = (uint8_t)(temp >> 8);
  112.         txMsg.Data[2] = (uint8_t)(temp & 0xff);
  113.         while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  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_PHYSICAL_HUMIDITY_PERCENT;
  120.         txMsg.Length = 3;
  121.  
  122.         txMsg.Data[0] = sns_DHT11_currentSensor;
  123.         temp = humidity[sns_DHT11_currentSensor]*64;
  124.         txMsg.Data[1] = (uint8_t)(temp >> 8);
  125.         txMsg.Data[2] = (uint8_t)(temp & 0xff);
  126.         while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  127.  
  128.     }
  129. }
  130.  
  131. void sns_DHT11_HandleMessage(StdCan_Msg_t *rxMsg)
  132. {
  133.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  134.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
  135.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_DHT11 &&
  136.         rxMsg->Header.ModuleId == sns_DHT11_ID)
  137.     {
  138.         StdCan_Msg_t txMsg;
  139.         switch (rxMsg->Header.Command)
  140.         {
  141.             case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL:
  142.                 //if (rxMsg->Length > 0)
  143.                 //  sns_ds18x20_ReportInterval = rxMsg->Data[0];
  144.  
  145.  
  146.                 StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  147.                 StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  148.                 txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DHT11;
  149.                 txMsg.Header.ModuleId = sns_DHT11_ID;
  150.                 txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL;
  151.                 txMsg.Length = 1;
  152.  
  153.                 //txMsg.Data[0] = sns_ds18x20_ReportInterval;
  154.  
  155.                 StdCan_Put(&txMsg);
  156.                 break;
  157.         }
  158.     }
  159. }
  160.  
  161. void sns_DHT11_List(uint8_t ModuleSequenceNumber)
  162. {
  163.     StdCan_Msg_t txMsg;
  164.  
  165.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  166.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  167.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DHT11;
  168.     txMsg.Header.ModuleId = sns_DHT11_ID;
  169.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  170.     txMsg.Length = 6;
  171.  
  172.     uint32_t HwId=BIOS_GetHwId();
  173.     txMsg.Data[0] = HwId&0xff;
  174.     txMsg.Data[1] = (HwId>>8)&0xff;
  175.     txMsg.Data[2] = (HwId>>16)&0xff;
  176.     txMsg.Data[3] = (HwId>>24)&0xff;
  177.  
  178.     txMsg.Data[4] = NUMBER_OF_MODULES;
  179.     txMsg.Data[5] = ModuleSequenceNumber;
  180.  
  181.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  182. }
  183.