I2CSensor.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "I2CSensor.h"
  2. #include "../sensors/SensorsDriver.h"
  3. uint8_t readReg(I2CSensor* i2c_sensor, uint8_t reg) {
  4. //Блокировка шины
  5. furi_hal_i2c_acquire(i2c_sensor->i2c);
  6. uint8_t buff[1];
  7. furi_hal_i2c_read_mem(i2c_sensor->i2c, i2c_sensor->currentI2CAdr << 1, reg, buff, 1, 0xFF);
  8. furi_hal_i2c_release(i2c_sensor->i2c);
  9. return buff[0];
  10. }
  11. bool readRegArray(I2CSensor* i2c_sensor, uint8_t startReg, uint8_t len, uint8_t* data) {
  12. furi_hal_i2c_acquire(i2c_sensor->i2c);
  13. bool status = furi_hal_i2c_read_mem(
  14. i2c_sensor->i2c, i2c_sensor->currentI2CAdr << 1, startReg, data, len, 0xFF);
  15. furi_hal_i2c_release(i2c_sensor->i2c);
  16. return status;
  17. }
  18. bool writeReg(I2CSensor* i2c_sensor, uint8_t reg, uint8_t value) {
  19. //Блокировка шины
  20. furi_hal_i2c_acquire(i2c_sensor->i2c);
  21. uint8_t buff[1] = {value};
  22. bool status = furi_hal_i2c_write_mem(
  23. i2c_sensor->i2c, i2c_sensor->currentI2CAdr << 1, reg, buff, 1, 0xFF);
  24. furi_hal_i2c_release(i2c_sensor->i2c);
  25. return status;
  26. }
  27. bool unitemp_I2C_sensor_alloc(void* s, uint16_t* anotherValues) {
  28. Sensor* sensor = (Sensor*)s;
  29. bool status = false;
  30. I2CSensor* instance = malloc(sizeof(I2CSensor));
  31. if(instance == NULL) {
  32. FURI_LOG_E(APP_NAME, "Sensor %s instance allocation error", sensor->name);
  33. return false;
  34. }
  35. instance->i2c = &furi_hal_i2c_handle_external;
  36. sensor->instance = instance;
  37. //Указание функций инициализации, деинициализации и обновления данных, а так же адреса на шине I2C
  38. status = sensor->type->allocator(sensor, anotherValues);
  39. //Установка адреса шины I2C
  40. if(anotherValues[0] >= instance->minI2CAdr && anotherValues[0] <= instance->maxI2CAdr) {
  41. instance->currentI2CAdr = anotherValues[0];
  42. } else {
  43. instance->currentI2CAdr = instance->minI2CAdr;
  44. }
  45. return status;
  46. }
  47. bool unitemp_I2C_sensor_free(void* s) {
  48. Sensor* sensor = (Sensor*)s;
  49. bool status = sensor->type->mem_releaser(sensor);
  50. free(sensor->instance);
  51. return status;
  52. }
  53. UnitempStatus unitemp_I2C_sensor_update(void* s) {
  54. Sensor* sensor = (Sensor*)s;
  55. if(sensor->status != UT_OK) {
  56. sensor->type->initializer(sensor);
  57. }
  58. return sensor->type->updater(sensor);
  59. }