Sensors.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. #include "Sensors.h"
  2. #include <furi_hal_power.h>
  3. #include <m-string.h>
  4. //Порты ввода/вывода, которые не были обозначены в общем списке
  5. const GpioPin SWC_10 = {.pin = LL_GPIO_PIN_14, .port = GPIOA};
  6. const GpioPin SIO_12 = {.pin = LL_GPIO_PIN_13, .port = GPIOA};
  7. const GpioPin TX_13 = {.pin = LL_GPIO_PIN_6, .port = GPIOB};
  8. const GpioPin RX_14 = {.pin = LL_GPIO_PIN_7, .port = GPIOB};
  9. //Количество доступных портов ввода/вывода
  10. #define GPIO_ITEMS (sizeof(GPIOList) / sizeof(GPIO))
  11. //Количество интерфейсов
  12. #define INTERFACES_TYPES_COUNT (int)(sizeof(interfaces) / sizeof(const Interface*))
  13. //Количество типов датчиков
  14. #define SENSOR_TYPES_COUNT (int)(sizeof(sensorTypes) / sizeof(const SensorType*))
  15. //Перечень достуных портов ввода/вывода
  16. static const GPIO GPIOList[] = {
  17. {2, "2 (A7)", &gpio_ext_pa7},
  18. {3, "3 (A6)", &gpio_ext_pa6},
  19. {4, "4 (A4)", &gpio_ext_pa4},
  20. {5, "5 (B3)", &gpio_ext_pb3},
  21. {6, "6 (B2)", &gpio_ext_pb2},
  22. {7, "7 (C3)", &gpio_ext_pc3},
  23. {10, " 10(SWC) ", &SWC_10},
  24. {12, "12 (SIO)", &SIO_12},
  25. {13, "13 (TX)", &TX_13},
  26. {14, "14 (RX)", &RX_14},
  27. {15, "15 (C1)", &gpio_ext_pc1},
  28. {16, "16 (C0)", &gpio_ext_pc0},
  29. {17, "17 (1W)", &ibutton_gpio}};
  30. //Список интерфейсов, которые прикреплены к GPIO (определяется индексом)
  31. //NULL - порт свободен, указатель на интерфейс - порт занят этим интерфейсом
  32. static const Interface* gpio_interfaces_list[GPIO_ITEMS] = {0};
  33. const Interface SINGLE_WIRE = {
  34. .name = "Single wire",
  35. .allocator = unitemp_singlewire_alloc,
  36. .mem_releaser = unitemp_singlewire_free,
  37. .updater = unitemp_singlewire_update};
  38. const Interface I2C = {
  39. .name = "I2C",
  40. .allocator = unitemp_I2C_sensor_alloc,
  41. .mem_releaser = unitemp_I2C_sensor_free,
  42. .updater = unitemp_I2C_sensor_update};
  43. const Interface ONE_WIRE = {
  44. .name = "One wire",
  45. .allocator = unitemp_onewire_sensor_alloc,
  46. .mem_releaser = unitemp_onewire_sensor_free,
  47. .updater = unitemp_onewire_sensor_update};
  48. //Перечень интерфейсов подключения
  49. //static const Interface* interfaces[] = {&SINGLE_WIRE, &I2C, &ONE_WIRE};
  50. //Перечень датчиков
  51. static const SensorType* sensorTypes[] =
  52. {&DHT11, &DHT12_SW, &DHT21, &DHT22, &AM2320_SW, &LM75, &BMP280, &Dallas};
  53. const SensorType* unitemp_sensors_getTypeFromInt(uint8_t index) {
  54. if(index > SENSOR_TYPES_COUNT) return NULL;
  55. return sensorTypes[index];
  56. }
  57. const SensorType* unitemp_sensors_getTypeFromStr(char* str) {
  58. UNUSED(str);
  59. if(str == NULL) return NULL;
  60. for(uint8_t i = 0; i < unitemp_sensors_getTypesCount(); i++) {
  61. if(!strcmp(str, sensorTypes[i]->typename)) {
  62. return sensorTypes[i];
  63. }
  64. }
  65. return NULL;
  66. }
  67. uint8_t unitemp_sensors_getTypesCount(void) {
  68. return SENSOR_TYPES_COUNT;
  69. }
  70. const SensorType** unitemp_sensors_getTypes(void) {
  71. return sensorTypes;
  72. }
  73. int unitemp_getIntFromType(const SensorType* type) {
  74. for(int i = 0; i < SENSOR_TYPES_COUNT; i++) {
  75. if(!strcmp(type->typename, sensorTypes[i]->typename)) {
  76. return i;
  77. }
  78. }
  79. return 255;
  80. }
  81. const GPIO* unitemp_gpio_getFromInt(uint8_t name) {
  82. for(uint8_t i = 0; i < GPIO_ITEMS; i++) {
  83. if(GPIOList[i].num == name) {
  84. return &GPIOList[i];
  85. }
  86. }
  87. return NULL;
  88. }
  89. const GPIO* unitemp_gpio_getFromIndex(uint8_t index) {
  90. return &GPIOList[index];
  91. }
  92. uint8_t unitemp_gpio_toInt(const GPIO* gpio) {
  93. if(gpio == NULL) return 255;
  94. for(uint8_t i = 0; i < GPIO_ITEMS; i++) {
  95. if(GPIOList[i].pin->pin == gpio->pin->pin && GPIOList[i].pin->port == gpio->pin->port) {
  96. return GPIOList[i].num;
  97. }
  98. }
  99. return 255;
  100. }
  101. uint8_t unitemp_gpio_to_index(const GpioPin* gpio) {
  102. if(gpio == NULL) return 255;
  103. for(uint8_t i = 0; i < GPIO_ITEMS; i++) {
  104. if(GPIOList[i].pin->pin == gpio->pin && GPIOList[i].pin->port == gpio->port) {
  105. return i;
  106. }
  107. }
  108. return 255;
  109. }
  110. uint8_t unitemp_gpio_getAviablePortsCount(const Interface* interface, const GPIO* extraport) {
  111. uint8_t aviable_ports_count = 0;
  112. for(uint8_t i = 0; i < GPIO_ITEMS; i++) {
  113. //Проверка для one wire
  114. if(interface == &ONE_WIRE) {
  115. if(((gpio_interfaces_list[i] == NULL || gpio_interfaces_list[i] == &ONE_WIRE) &&
  116. (i != 12)) || //Почему-то не работает на 17 порте
  117. (unitemp_gpio_getFromIndex(i) == extraport)) {
  118. aviable_ports_count++;
  119. }
  120. }
  121. //Проверка для single wire
  122. if(interface == &SINGLE_WIRE) {
  123. if(gpio_interfaces_list[i] == NULL || (unitemp_gpio_getFromIndex(i) == extraport)) {
  124. aviable_ports_count++;
  125. }
  126. }
  127. if(interface == &I2C) {
  128. //У I2C два фиксированых порта
  129. return 0;
  130. }
  131. }
  132. return aviable_ports_count;
  133. }
  134. void unitemp_gpio_lock(const GPIO* gpio, const Interface* interface) {
  135. uint8_t i = unitemp_gpio_to_index(gpio->pin);
  136. if(i == 255) return;
  137. gpio_interfaces_list[i] = interface;
  138. }
  139. void unitemp_gpio_unlock(const GPIO* gpio) {
  140. uint8_t i = unitemp_gpio_to_index(gpio->pin);
  141. if(i == 255) return;
  142. gpio_interfaces_list[i] = NULL;
  143. }
  144. const GPIO*
  145. unitemp_gpio_getAviablePort(const Interface* interface, uint8_t index, const GPIO* extraport) {
  146. //Проверка для I2C
  147. if(interface == &I2C) {
  148. if((gpio_interfaces_list[10] == NULL || gpio_interfaces_list[10] == &I2C) &&
  149. (gpio_interfaces_list[11] == NULL || gpio_interfaces_list[11] == &I2C)) {
  150. //Возврат истины
  151. return unitemp_gpio_getFromIndex(0);
  152. } else {
  153. //Возврат лжи
  154. return NULL;
  155. }
  156. }
  157. uint8_t aviable_index = 0;
  158. for(uint8_t i = 0; i < GPIO_ITEMS; i++) {
  159. //Проверка для one wire
  160. if(interface == &ONE_WIRE) {
  161. //Почему-то не работает на 17 порте
  162. if(((gpio_interfaces_list[i] == NULL || gpio_interfaces_list[i] == &ONE_WIRE) &&
  163. (i != 12)) || //Почему-то не работает на 17 порте
  164. (unitemp_gpio_getFromIndex(i) == extraport)) {
  165. if(aviable_index == index) {
  166. return unitemp_gpio_getFromIndex(i);
  167. } else {
  168. aviable_index++;
  169. }
  170. }
  171. }
  172. //Проверка для single wire
  173. if(interface == &SINGLE_WIRE) {
  174. if(gpio_interfaces_list[i] == NULL || unitemp_gpio_getFromIndex(i) == extraport) {
  175. if(aviable_index == index) {
  176. return unitemp_gpio_getFromIndex(i);
  177. } else {
  178. aviable_index++;
  179. }
  180. }
  181. }
  182. }
  183. return NULL;
  184. }
  185. void unitemp_sensor_delete(Sensor* sensor) {
  186. for(uint8_t i = 0; i < app->sensors_count; i++) {
  187. if(app->sensors[i] == sensor) {
  188. app->sensors[i]->status = UT_INACTIVE;
  189. unitemp_sensors_save();
  190. unitemp_sensors_reload();
  191. return;
  192. }
  193. }
  194. }
  195. Sensor* unitemp_sensor_getActive(uint8_t index) {
  196. uint8_t aviable_index = 0;
  197. for(uint8_t i = 0; i < app->sensors_count; i++) {
  198. if(app->sensors[i]->status != UT_INACTIVE) {
  199. if(aviable_index == index) {
  200. return app->sensors[i];
  201. } else {
  202. aviable_index++;
  203. }
  204. }
  205. }
  206. return NULL;
  207. }
  208. uint8_t unitemp_sensors_getCount(void) {
  209. if(app->sensors == NULL) return 0;
  210. return app->sensors_count;
  211. }
  212. uint8_t unitemp_sensors_getActiveCount(void) {
  213. if(app->sensors == NULL) return 0;
  214. uint8_t counter = 0;
  215. for(uint8_t i = 0; i < unitemp_sensors_getCount(); i++) {
  216. if(app->sensors[i]->status != UT_INACTIVE) counter++;
  217. }
  218. return counter;
  219. }
  220. void unitemp_sensors_add(Sensor* sensor) {
  221. app->sensors =
  222. (Sensor**)realloc(app->sensors, (unitemp_sensors_getCount() + 1) * sizeof(Sensor*));
  223. app->sensors[unitemp_sensors_getCount()] = sensor;
  224. app->sensors_count++;
  225. }
  226. bool unitemp_sensors_load(void) {
  227. FURI_LOG_D(APP_NAME, "Loading sensors...");
  228. //Выделение памяти на поток
  229. app->file_stream = file_stream_alloc(app->storage);
  230. //Переменная пути к файлу
  231. FuriString* filepath = furi_string_alloc();
  232. //Составление пути к файлу
  233. furi_string_printf(filepath, "%s/%s", APP_PATH_FOLDER, APP_FILENAME_SENSORS);
  234. //Открытие потока к файлу с датчиками
  235. if(!file_stream_open(
  236. app->file_stream, furi_string_get_cstr(filepath), FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) {
  237. if(file_stream_get_error(app->file_stream) == FSE_NOT_EXIST) {
  238. FURI_LOG_W(APP_NAME, "Missing sensors file");
  239. //Закрытие потока и освобождение памяти
  240. file_stream_close(app->file_stream);
  241. stream_free(app->file_stream);
  242. return false;
  243. } else {
  244. FURI_LOG_E(
  245. APP_NAME,
  246. "An error occurred while loading the sensors file: %d",
  247. file_stream_get_error(app->file_stream));
  248. //Закрытие потока и освобождение памяти
  249. file_stream_close(app->file_stream);
  250. stream_free(app->file_stream);
  251. return false;
  252. }
  253. }
  254. //Вычисление размера файла
  255. uint16_t file_size = stream_size(app->file_stream);
  256. //Если файл пустой, то:
  257. if(file_size == (uint8_t)0) {
  258. FURI_LOG_W(APP_NAME, "Sensors file is empty");
  259. //Закрытие потока и освобождение памяти
  260. file_stream_close(app->file_stream);
  261. stream_free(app->file_stream);
  262. return false;
  263. }
  264. //Выделение памяти под загрузку файла
  265. uint8_t* file_buf = malloc(file_size);
  266. //Опустошение буфера файла
  267. memset(file_buf, 0, file_size);
  268. //Загрузка файла
  269. if(stream_read(app->file_stream, file_buf, file_size) != file_size) {
  270. //Выход при ошибке чтения
  271. FURI_LOG_E(APP_NAME, "Error reading sensors file");
  272. //Закрытие потока и освобождение памяти
  273. file_stream_close(app->file_stream);
  274. stream_free(app->file_stream);
  275. free(file_buf);
  276. return false;
  277. }
  278. //Указатель на начало строки
  279. FuriString* file = furi_string_alloc_set_str((char*)file_buf);
  280. //Сколько байт до конца строки
  281. size_t line_end = 0;
  282. while(line_end != STRING_FAILURE && line_end != (size_t)(file_size - 1)) {
  283. //Имя датчика
  284. char name[11] = {0};
  285. //Тип датчика
  286. char type[11] = {0};
  287. //Смещение по строке для отделения аргументов
  288. int offset = 0;
  289. //Чтение из строки
  290. sscanf(((char*)(file_buf + line_end)), "%s %s %n", name, type, &offset);
  291. //Ограничение длины имени
  292. name[10] = '\0';
  293. char* args = ((char*)(file_buf + line_end + offset));
  294. const SensorType* stype = unitemp_sensors_getTypeFromStr(type);
  295. //Проверка типа датчика
  296. if(stype != NULL && sizeof(name) > 0 && sizeof(name) <= 11) {
  297. Sensor* sensor =
  298. unitemp_sensor_alloc(name, unitemp_sensors_getTypeFromStr(type), args);
  299. if(sensor != NULL) {
  300. unitemp_sensors_add(sensor);
  301. } else {
  302. FURI_LOG_E(APP_NAME, "Failed sensor (%s:%s) mem allocation", name, type);
  303. }
  304. } else {
  305. FURI_LOG_E(APP_NAME, "Unsupported sensor name (%s) or sensor type (%s)", name, type);
  306. }
  307. //Вычисление конца строки
  308. line_end = furi_string_search_char(file, '\n', line_end + 1);
  309. }
  310. free(file_buf);
  311. file_stream_close(app->file_stream);
  312. stream_free(app->file_stream);
  313. FURI_LOG_I(APP_NAME, "Sensors have been successfully loaded");
  314. return true;
  315. }
  316. bool unitemp_sensors_save(void) {
  317. FURI_LOG_D(APP_NAME, "Saving sensors...");
  318. //Выделение памяти для потока
  319. app->file_stream = file_stream_alloc(app->storage);
  320. //Переменная пути к файлу
  321. FuriString* filepath = furi_string_alloc();
  322. //Составление пути к файлу
  323. furi_string_printf(filepath, "%s/%s", APP_PATH_FOLDER, APP_FILENAME_SENSORS);
  324. //Создание папки плагина
  325. storage_common_mkdir(app->storage, APP_PATH_FOLDER);
  326. //Открытие потока
  327. if(!file_stream_open(
  328. app->file_stream, furi_string_get_cstr(filepath), FSAM_READ_WRITE, FSOM_CREATE_ALWAYS)) {
  329. FURI_LOG_E(
  330. APP_NAME,
  331. "An error occurred while saving the sensors file: %d",
  332. file_stream_get_error(app->file_stream));
  333. //Закрытие потока и освобождение памяти
  334. file_stream_close(app->file_stream);
  335. stream_free(app->file_stream);
  336. return false;
  337. }
  338. //Сохранение датчиков
  339. for(uint8_t i = 0; i < unitemp_sensors_getActiveCount(); i++) {
  340. if(unitemp_sensor_getActive(i)->type->interface == &SINGLE_WIRE) {
  341. stream_write_format(
  342. app->file_stream,
  343. "%s %s %d\n",
  344. unitemp_sensor_getActive(i)->name,
  345. unitemp_sensor_getActive(i)->type->typename,
  346. unitemp_singlewire_sensorGetGPIO(unitemp_sensor_getActive(i))->num);
  347. }
  348. if(unitemp_sensor_getActive(i)->type->interface == &I2C) {
  349. stream_write_format(
  350. app->file_stream,
  351. "%s %s %X\n",
  352. unitemp_sensor_getActive(i)->name,
  353. unitemp_sensor_getActive(i)->type->typename,
  354. ((I2CSensor*)unitemp_sensor_getActive(i)->instance)->currentI2CAdr);
  355. }
  356. if(unitemp_sensor_getActive(i)->type->interface == &ONE_WIRE) {
  357. stream_write_format(
  358. app->file_stream,
  359. "%s %s %d %02X%02X%02X%02X%02X%02X%02X%02X\n",
  360. unitemp_sensor_getActive(i)->name,
  361. unitemp_sensor_getActive(i)->type->typename,
  362. ((OneWireSensor*)unitemp_sensor_getActive(i)->instance)->bus->gpio->num,
  363. ((OneWireSensor*)unitemp_sensor_getActive(i)->instance)->deviceID[0],
  364. ((OneWireSensor*)unitemp_sensor_getActive(i)->instance)->deviceID[1],
  365. ((OneWireSensor*)unitemp_sensor_getActive(i)->instance)->deviceID[2],
  366. ((OneWireSensor*)unitemp_sensor_getActive(i)->instance)->deviceID[3],
  367. ((OneWireSensor*)unitemp_sensor_getActive(i)->instance)->deviceID[4],
  368. ((OneWireSensor*)unitemp_sensor_getActive(i)->instance)->deviceID[5],
  369. ((OneWireSensor*)unitemp_sensor_getActive(i)->instance)->deviceID[6],
  370. ((OneWireSensor*)unitemp_sensor_getActive(i)->instance)->deviceID[7]);
  371. }
  372. }
  373. //Закрытие потока и освобождение памяти
  374. file_stream_close(app->file_stream);
  375. stream_free(app->file_stream);
  376. FURI_LOG_I(APP_NAME, "Sensors have been successfully saved");
  377. return true;
  378. }
  379. void unitemp_sensors_reload(void) {
  380. unitemp_sensors_deInit();
  381. unitemp_sensors_free();
  382. unitemp_sensors_load();
  383. unitemp_sensors_init();
  384. }
  385. bool unitemp_sensor_isContains(Sensor* sensor) {
  386. for(uint8_t i = 0; i < unitemp_sensors_getCount(); i++) {
  387. if(unitemp_sensor_getActive(i) == sensor) return true;
  388. }
  389. return false;
  390. }
  391. Sensor* unitemp_sensor_alloc(char* name, const SensorType* type, char* args) {
  392. if(name == NULL || type == NULL) return NULL;
  393. bool status = false;
  394. //Выделение памяти под датчик
  395. Sensor* sensor = malloc(sizeof(Sensor));
  396. if(sensor == NULL) {
  397. FURI_LOG_E(APP_NAME, "Sensor %s allocation error", name);
  398. return false;
  399. }
  400. //Выделение памяти под имя
  401. sensor->name = malloc(11);
  402. if(sensor->name == NULL) {
  403. FURI_LOG_E(APP_NAME, "Sensor %s name allocation error", name);
  404. return false;
  405. }
  406. //Запись имени датчка
  407. strcpy(sensor->name, name);
  408. //Тип датчика
  409. sensor->type = type;
  410. //Статус датчика по умолчанию - ошибка
  411. sensor->status = UT_ERROR;
  412. //Время последнего опроса
  413. sensor->lastPollingTime =
  414. furi_get_tick() - 10000; //чтобы первый опрос произошёл как можно раньше
  415. sensor->temp = -128.0f;
  416. sensor->hum = -128.0f;
  417. sensor->pressure = -128.0f;
  418. //Выделение памяти под инстанс датчика в зависимости от его интерфейса
  419. status = sensor->type->interface->allocator(sensor, args);
  420. //Выход если датчик успешно развёрнут
  421. if(status) {
  422. FURI_LOG_I(APP_NAME, "Sensor %s allocated", name);
  423. return sensor;
  424. }
  425. //Выход с очисткой если память для датчика не была выделена
  426. free(sensor->name);
  427. free(sensor);
  428. FURI_LOG_E(APP_NAME, "Sensor %s(%s) allocation error", name, type->typename);
  429. return NULL;
  430. }
  431. void unitemp_sensor_free(Sensor* sensor) {
  432. if(sensor == NULL) {
  433. FURI_LOG_E(APP_NAME, "Null pointer sensor releasing");
  434. return;
  435. }
  436. if(sensor->type == NULL) {
  437. FURI_LOG_E(APP_NAME, "Sensor type is null");
  438. return;
  439. }
  440. if(sensor->type->mem_releaser == NULL) {
  441. FURI_LOG_E(APP_NAME, "Sensor releaser is null");
  442. return;
  443. }
  444. bool status = false;
  445. //Высвобождение памяти под инстанс
  446. status = sensor->type->interface->mem_releaser(sensor);
  447. if(status) {
  448. FURI_LOG_D(APP_NAME, "Sensor %s memory successfully released", sensor->name);
  449. } else {
  450. FURI_LOG_E(APP_NAME, "Sensor %s memory is not released", sensor->name);
  451. }
  452. free(sensor->name);
  453. //free(sensor);
  454. }
  455. void unitemp_sensors_free(void) {
  456. for(uint8_t i = 0; i < unitemp_sensors_getCount(); i++) {
  457. unitemp_sensor_free(app->sensors[i]);
  458. }
  459. app->sensors_count = 0;
  460. }
  461. bool unitemp_sensors_init(void) {
  462. bool result = true;
  463. //Перебор датчиков из списка
  464. for(uint8_t i = 0; i < unitemp_sensors_getCount(); i++) {
  465. //Включение 5V если на порту 1 FZ его нет
  466. //Может пропасть при отключении USB
  467. if(furi_hal_power_is_otg_enabled() != true) {
  468. furi_hal_power_enable_otg();
  469. FURI_LOG_D(APP_NAME, "OTG enabled");
  470. }
  471. if(!(*app->sensors[i]->type->initializer)(app->sensors[i])) {
  472. FURI_LOG_E(
  473. APP_NAME,
  474. "An error occurred during sensor initialization %s",
  475. app->sensors[i]->name);
  476. result = false;
  477. }
  478. FURI_LOG_D(APP_NAME, "Sensor %s successfully initialized", app->sensors[i]->name);
  479. }
  480. app->sensors_ready = true;
  481. return result;
  482. }
  483. bool unitemp_sensors_deInit(void) {
  484. bool result = true;
  485. //Выключение 5 В если до этого оно не было включено
  486. if(app->settings.lastOTGState != true) {
  487. furi_hal_power_disable_otg();
  488. FURI_LOG_D(APP_NAME, "OTG disabled");
  489. }
  490. //Перебор датчиков из списка
  491. for(uint8_t i = 0; i < unitemp_sensors_getCount(); i++) {
  492. if(!(*app->sensors[i]->type->deinitializer)(app->sensors[i])) {
  493. FURI_LOG_E(
  494. APP_NAME,
  495. "An error occurred during sensor deinitialization %s",
  496. app->sensors[i]->name);
  497. result = false;
  498. }
  499. }
  500. return result;
  501. }
  502. UnitempStatus unitemp_sensor_updateData(Sensor* sensor) {
  503. if(sensor == NULL) return UT_ERROR;
  504. //Проверка на допустимость опроса датчика
  505. if(furi_get_tick() - sensor->lastPollingTime < sensor->type->pollingInterval) {
  506. //Возврат ошибки если последний опрос датчика был неудачным
  507. if(sensor->status == UT_TIMEOUT) {
  508. return UT_TIMEOUT;
  509. }
  510. return UT_EARLYPOOL;
  511. }
  512. sensor->lastPollingTime = furi_get_tick();
  513. if(!furi_hal_power_is_otg_enabled()) {
  514. furi_hal_power_enable_otg();
  515. }
  516. sensor->status = sensor->type->interface->updater(sensor);
  517. if(sensor->status != UT_OK && sensor->status != UT_POLLING)
  518. FURI_LOG_D(APP_NAME, "Sensor %s update status %d", sensor->name, sensor->status);
  519. if(app->settings.unit == FAHRENHEIT && sensor->status == UT_OK)
  520. uintemp_celsiumToFarengate(sensor);
  521. if(sensor->status == UT_OK) {
  522. unitemp_pascalToMmHg(sensor);
  523. }
  524. return sensor->status;
  525. }
  526. void unitemp_sensors_updateValues(void) {
  527. for(uint8_t i = 0; i < unitemp_sensors_getCount(); i++) {
  528. unitemp_sensor_updateData(unitemp_sensor_getActive(i));
  529. }
  530. }