SHT30.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Unitemp - Universal temperature reader
  3. Copyright (C) 2022-2023 Victor Nikitchuk (https://github.com/quen0n)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "SHT30.h"
  16. #include "../interfaces/I2CSensor.h"
  17. const SensorType SHT30 = {
  18. .typename = "SHT30",
  19. .altname = "SHT30/31/35",
  20. .interface = &I2C,
  21. .datatype = UT_TEMPERATURE | UT_HUMIDITY,
  22. .pollingInterval = 1000,
  23. .allocator = unitemp_SHT30_I2C_alloc,
  24. .mem_releaser = unitemp_SHT30_I2C_free,
  25. .initializer = unitemp_SHT30_init,
  26. .deinitializer = unitemp_SHT30_I2C_deinit,
  27. .updater = unitemp_SHT30_I2C_update};
  28. const SensorType GXHT30 = {
  29. .typename = "GXHT30",
  30. .altname = "GXHT30/31/35",
  31. .interface = &I2C,
  32. .datatype = UT_TEMPERATURE | UT_HUMIDITY,
  33. .pollingInterval = 1000,
  34. .allocator = unitemp_SHT30_I2C_alloc,
  35. .mem_releaser = unitemp_SHT30_I2C_free,
  36. .initializer = unitemp_GXHT30_init,
  37. .deinitializer = unitemp_SHT30_I2C_deinit,
  38. .updater = unitemp_SHT30_I2C_update};
  39. bool unitemp_SHT30_I2C_alloc(Sensor* sensor, char* args) {
  40. UNUSED(args);
  41. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  42. //Адреса на шине I2C (7 бит)
  43. i2c_sensor->minI2CAdr = 0x44 << 1;
  44. i2c_sensor->maxI2CAdr = 0x45 << 1;
  45. return true;
  46. }
  47. bool unitemp_SHT30_I2C_free(Sensor* sensor) {
  48. //Нечего высвобождать, так как ничего не было выделено
  49. UNUSED(sensor);
  50. return true;
  51. }
  52. bool unitemp_SHT30_init(Sensor* sensor) {
  53. UNUSED(sensor);
  54. return true;
  55. }
  56. bool unitemp_GXHT30_init(Sensor* sensor) {
  57. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  58. //Включение режима автоматического преобразования 2 раза в сек
  59. uint8_t data[2] = {0x22, 0x36};
  60. if(!unitemp_i2c_writeArray(i2c_sensor, 2, data)) return false;
  61. return true;
  62. }
  63. bool unitemp_SHT30_I2C_deinit(Sensor* sensor) {
  64. //Нечего деинициализировать
  65. UNUSED(sensor);
  66. return true;
  67. }
  68. UnitempStatus unitemp_SHT30_I2C_update(Sensor* sensor) {
  69. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  70. //Получение данных
  71. uint8_t data[6] = {0xE0, 0x00};
  72. if(!unitemp_i2c_writeArray(i2c_sensor, 2, data)) return UT_SENSORSTATUS_TIMEOUT;
  73. if(!unitemp_i2c_readArray(i2c_sensor, 6, data)) return UT_SENSORSTATUS_TIMEOUT;
  74. sensor->temp = -45 + 175 * (((uint16_t)(data[0] << 8) | data[1]) / 65535.0f);
  75. sensor->hum = 100 * (((uint16_t)(data[3] << 8) | data[4]) / 65535.0f);
  76. return UT_SENSORSTATUS_OK;
  77. }