I2CSensor.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_sensorInit(void* s) {
  28. Sensor* sensor = (Sensor*)s;
  29. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  30. //BMP280
  31. if(sensor->type == BMP280) {
  32. if(BMP280_init(i2c_sensor)) {
  33. sensor->status = UT_OK;
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. bool unitemp_I2C_sensorDeInit(void* s) {
  40. Sensor* sensor = (Sensor*)s;
  41. return sensor->deinitializer(s);
  42. }
  43. UnitempStatus unitemp_I2C_updateData(void* sensor) {
  44. if(((Sensor*)sensor)->status == UT_ERROR || ((Sensor*)sensor)->status == UT_TIMEOUT) {
  45. if(((Sensor*)sensor)->initializer(sensor) != true) return UT_ERROR;
  46. }
  47. BMP280_updateData(sensor);
  48. return UT_OK;
  49. }
  50. bool unitemp_I2C_sensorAlloc(Sensor* sensor, SensorType st, uint16_t* anotherValues) {
  51. bool status = false;
  52. I2CSensor* instance = malloc(sizeof(I2CSensor));
  53. instance->interface = I2C;
  54. instance->i2c = &furi_hal_i2c_handle_external;
  55. sensor->lastPollingTime = 0xFFFFFFFF;
  56. sensor->instance = instance;
  57. sensor->type = st;
  58. // //Настройки для BMP280
  59. // if(st == BMP280) {
  60. // instance->minI2CAdr = 0x76;
  61. // instance->maxI2CAdr = 0x77;
  62. // }
  63. if(st == LM75) {
  64. //Указание функций инициализации, деинициализации и обновления данных, а так же адреса на шине I2C
  65. status = unitemp_LM75_alloc(sensor);
  66. }
  67. if(anotherValues[0] >= instance->minI2CAdr && anotherValues[0] <= instance->maxI2CAdr) {
  68. instance->currentI2CAdr = anotherValues[0];
  69. } else {
  70. instance->currentI2CAdr = instance->minI2CAdr;
  71. }
  72. return status;
  73. }
  74. void unitemp_I2C_sensorFree(Sensor* sensor) {
  75. free(sensor->instance);
  76. }