HTU21x.c 3.2 KB

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