HTU21x.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Unitemp - Universal temperature reader
  3. Copyright (C) 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 "HTU21x.h"
  16. #include "../interfaces/I2CSensor.h"
  17. const SensorType HTU21x = {
  18. .typename = "HTU21x",
  19. .altname = "HTU21x/SI70xx/SHT2x",
  20. .interface = &I2C,
  21. .datatype = UT_DATA_TYPE_TEMP_HUM,
  22. .pollingInterval = 250,
  23. .allocator = unitemp_HTU21x_alloc,
  24. .mem_releaser = unitemp_HTU21x_free,
  25. .initializer = unitemp_HTU21x_init,
  26. .deinitializer = unitemp_HTU21x_deinit,
  27. .updater = unitemp_HTU21x_update};
  28. static uint8_t checkCRC(uint16_t data) {
  29. for(uint8_t i = 0; i < 16; i++) {
  30. if(data & 0x8000)
  31. data = (data << 1) ^ 0x13100;
  32. else
  33. data <<= 1;
  34. }
  35. return (data >> 8);
  36. }
  37. bool unitemp_HTU21x_alloc(Sensor* sensor, char* args) {
  38. UNUSED(args);
  39. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  40. //Адреса на шине I2C (7 бит)
  41. i2c_sensor->minI2CAdr = 0x40 << 1;
  42. i2c_sensor->maxI2CAdr = 0x41 << 1;
  43. return true;
  44. }
  45. bool unitemp_HTU21x_free(Sensor* sensor) {
  46. //Нечего высвобождать, так как ничего не было выделено
  47. UNUSED(sensor);
  48. return true;
  49. }
  50. bool unitemp_HTU21x_init(Sensor* sensor) {
  51. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  52. UNUSED(i2c_sensor);
  53. return true;
  54. }
  55. bool unitemp_HTU21x_deinit(Sensor* sensor) {
  56. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  57. UNUSED(i2c_sensor);
  58. return true;
  59. }
  60. UnitempStatus unitemp_HTU21x_update(Sensor* sensor) {
  61. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  62. //Датчик может быть всего один, так что норм
  63. static bool temp_hum = false;
  64. uint8_t data[3];
  65. if(sensor->status == UT_SENSORSTATUS_POLLING) {
  66. if(!unitemp_i2c_readArray(i2c_sensor, 3, data)) return UT_SENSORSTATUS_TIMEOUT;
  67. uint16_t raw = ((uint16_t)data[0] << 8) | data[1];
  68. if(checkCRC(raw) != data[2]) return UT_SENSORSTATUS_BADCRC;
  69. if(temp_hum) {
  70. sensor->temp = (0.002681f * raw - 46.85f);
  71. } else {
  72. sensor->hum = ((0.001907 * (raw ^ 0x02)) - 6);
  73. }
  74. temp_hum = !temp_hum;
  75. if(temp_hum) return UT_SENSORSTATUS_EARLYPOOL;
  76. return UT_SENSORSTATUS_OK;
  77. }
  78. if(temp_hum) {
  79. //Запрос температуры
  80. data[0] = 0xF3;
  81. if(!unitemp_i2c_writeArray(i2c_sensor, 1, data)) return UT_SENSORSTATUS_TIMEOUT;
  82. } else {
  83. //Запрос влажности
  84. data[0] = 0xF5;
  85. if(!unitemp_i2c_writeArray(i2c_sensor, 1, data)) return UT_SENSORSTATUS_TIMEOUT;
  86. }
  87. return UT_SENSORSTATUS_POLLING;
  88. }