AM2320.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "AM2320.h"
  16. #include "../interfaces/I2CSensor.h"
  17. const SensorType AM2320_I2C = {
  18. .typename = "AM2320_I2C",
  19. .altname = "AM2320 (I2C)",
  20. .interface = &I2C,
  21. .datatype = UT_TEMPERATURE | UT_HUMIDITY,
  22. .pollingInterval = 2000,
  23. .allocator = unitemp_AM2320_I2C_alloc,
  24. .mem_releaser = unitemp_AM2320_I2C_free,
  25. .initializer = unitemp_AM2320_init,
  26. .deinitializer = unitemp_AM2320_I2C_deinit,
  27. .updater = unitemp_AM2320_I2C_update};
  28. static uint16_t AM2320_calc_CRC(uint8_t* ptr, uint8_t len) {
  29. uint16_t crc = 0xFFFF;
  30. uint8_t i;
  31. while(len--) {
  32. crc ^= *ptr++;
  33. for(i = 0; i < 8; i++) {
  34. if(crc & 0x01) {
  35. crc >>= 1;
  36. crc ^= 0xA001;
  37. } else {
  38. crc >>= 1;
  39. }
  40. }
  41. }
  42. return crc;
  43. }
  44. bool unitemp_AM2320_I2C_alloc(Sensor* sensor, char* args) {
  45. UNUSED(args);
  46. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  47. //Адреса на шине I2C (7 бит)
  48. i2c_sensor->minI2CAdr = 0x5C << 1;
  49. i2c_sensor->maxI2CAdr = 0x5C << 1;
  50. return true;
  51. }
  52. bool unitemp_AM2320_I2C_free(Sensor* sensor) {
  53. //Нечего высвобождать, так как ничего не было выделено
  54. UNUSED(sensor);
  55. return true;
  56. }
  57. bool unitemp_AM2320_init(Sensor* sensor) {
  58. //Нечего инициализировать
  59. UNUSED(sensor);
  60. return true;
  61. }
  62. bool unitemp_AM2320_I2C_deinit(Sensor* sensor) {
  63. //Нечего деинициализировать
  64. UNUSED(sensor);
  65. return true;
  66. }
  67. UnitempStatus unitemp_AM2320_I2C_update(Sensor* sensor) {
  68. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  69. uint8_t data[8] = {0x03, 0x00, 0x04};
  70. //Wake up
  71. unitemp_i2c_isDeviceReady(i2c_sensor);
  72. furi_delay_ms(1);
  73. //Запрос
  74. if(!unitemp_i2c_writeArray(i2c_sensor, 3, data)) return UT_SENSORSTATUS_TIMEOUT;
  75. furi_delay_ms(2);
  76. //Ответ
  77. if(!unitemp_i2c_readArray(i2c_sensor, 8, data)) return UT_SENSORSTATUS_TIMEOUT;
  78. if(AM2320_calc_CRC(data, 6) != ((data[7] << 8) | data[6])) {
  79. return UT_SENSORSTATUS_BADCRC;
  80. }
  81. sensor->hum = (float)(((uint16_t)data[2] << 8) | data[3]) / 10;
  82. //Проверка на отрицательность температуры
  83. if(!(data[4] & (1 << 7))) {
  84. sensor->temp = (float)(((uint16_t)data[4] << 8) | data[5]) / 10;
  85. } else {
  86. data[4] &= ~(1 << 7);
  87. sensor->temp = (float)(((uint16_t)data[4] << 8) | data[5]) / -10;
  88. }
  89. return UT_SENSORSTATUS_OK;
  90. }