HDC1080.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "HDC1080.h"
  16. #include "../interfaces/I2CSensor.h"
  17. const SensorType HDC1080 = {
  18. .typename = "HDC1080",
  19. .interface = &I2C,
  20. .datatype = UT_DATA_TYPE_TEMP_HUM,
  21. .pollingInterval = 250,
  22. .allocator = unitemp_HDC1080_alloc,
  23. .mem_releaser = unitemp_HDC1080_free,
  24. .initializer = unitemp_HDC1080_init,
  25. .deinitializer = unitemp_HDC1080_deinit,
  26. .updater = unitemp_HDC1080_update};
  27. bool unitemp_HDC1080_alloc(Sensor* sensor, char* args) {
  28. UNUSED(args);
  29. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  30. //Адреса на шине I2C (7 бит)
  31. i2c_sensor->minI2CAdr = 0x40 << 1;
  32. i2c_sensor->maxI2CAdr = 0x40 << 1;
  33. return true;
  34. }
  35. bool unitemp_HDC1080_free(Sensor* sensor) {
  36. //Нечего высвобождать, так как ничего не было выделено
  37. UNUSED(sensor);
  38. return true;
  39. }
  40. bool unitemp_HDC1080_init(Sensor* sensor) {
  41. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  42. uint8_t data[2];
  43. if(!unitemp_i2c_readRegArray(i2c_sensor, 0xFF, 2, data)) return UT_SENSORSTATUS_TIMEOUT;
  44. uint16_t device_id = ((uint16_t)data[0] << 8) | data[1];
  45. if(device_id != 0x1050) {
  46. FURI_LOG_E(
  47. APP_NAME,
  48. "Sensor %s returned wrong ID 0x%02X, expected 0x1050",
  49. sensor->name,
  50. device_id);
  51. return false;
  52. }
  53. data[0] = 0b0001000;
  54. data[1] = 0;
  55. //Установка режима работы и разрядности измерений
  56. if(!unitemp_i2c_writeRegArray(i2c_sensor, 0x02, 2, data)) return UT_SENSORSTATUS_TIMEOUT;
  57. return true;
  58. }
  59. bool unitemp_HDC1080_deinit(Sensor* sensor) {
  60. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  61. UNUSED(i2c_sensor);
  62. return true;
  63. }
  64. UnitempStatus unitemp_HDC1080_update(Sensor* sensor) {
  65. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  66. uint8_t data[2] = {0};
  67. //Запуск измерения
  68. if(!unitemp_i2c_writeArray(i2c_sensor, 1, data)) return UT_SENSORSTATUS_TIMEOUT;
  69. furi_delay_ms(10);
  70. if(!unitemp_i2c_readArray(i2c_sensor, 2, data)) return UT_SENSORSTATUS_TIMEOUT;
  71. sensor->temp = ((float)(((uint16_t)data[0] << 8) | data[1]) / 65536) * 165 - 40;
  72. data[0] = 1;
  73. if(!unitemp_i2c_writeArray(i2c_sensor, 1, data)) return UT_SENSORSTATUS_TIMEOUT;
  74. furi_delay_ms(10);
  75. if(!unitemp_i2c_readArray(i2c_sensor, 2, data)) return UT_SENSORSTATUS_TIMEOUT;
  76. sensor->hum = ((float)(((uint16_t)data[0] << 8) | data[1]) / 65536) * 100;
  77. return UT_SENSORSTATUS_OK;
  78. }