BH1750.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @file BH1750.h
  3. * @author Oleksii Kutuzov (oleksii.kutuzov@icloud.com)
  4. * @brief
  5. * @version 0.1
  6. * @date 2022-11-06
  7. *
  8. * @copyright Copyright (c) 2022
  9. *
  10. * Ported from:
  11. * https://github.com/lamik/Light_Sensors_STM32
  12. */
  13. #include <furi.h>
  14. #include <furi_hal.h>
  15. #ifndef BH1750_H_
  16. #define BH1750_H_
  17. // I2C BUS
  18. #define I2C_BUS &furi_hal_i2c_handle_external
  19. #define I2C_TIMEOUT 10
  20. #define BH1750_ADDRESS (0x23 << 1)
  21. #define BH1750_POWER_DOWN 0x00
  22. #define BH1750_POWER_ON 0x01
  23. #define BH1750_RESET 0x07
  24. #define BH1750_DEFAULT_MTREG 69
  25. #define BH1750_DEFAULT_MODE ONETIME_HIGH_RES_MODE
  26. #define BH1750_CONVERSION_FACTOR 1.2
  27. typedef enum { BH1750_OK = 0, BH1750_ERROR = 1 } BH1750_STATUS;
  28. typedef enum {
  29. CONTINUOUS_HIGH_RES_MODE = 0x10,
  30. CONTINUOUS_HIGH_RES_MODE_2 = 0x11,
  31. CONTINUOUS_LOW_RES_MODE = 0x13,
  32. ONETIME_HIGH_RES_MODE = 0x20,
  33. ONETIME_HIGH_RES_MODE_2 = 0x21,
  34. ONETIME_LOW_RES_MODE = 0x23
  35. } BH1750_mode;
  36. /**
  37. * @brief Initialize the sensor. Sends the reset command and sets the measurement register to the default value.
  38. *
  39. * @return BH1750_STATUS
  40. */
  41. BH1750_STATUS bh1750_init();
  42. /**
  43. * @brief Change the I2C device address and then initialize the sensor.
  44. *
  45. * @return BH1750_STATUS
  46. */
  47. BH1750_STATUS bh1750_init_with_addr(uint8_t addr);
  48. /**
  49. * @brief Reset all registers to the default value.
  50. *
  51. * @return BH1750_STATUS
  52. */
  53. BH1750_STATUS bh1750_reset();
  54. /**
  55. * @brief Sets the power state. 1 - running; 0 - sleep, low power.
  56. *
  57. * @param PowerOn sensor state.
  58. * @return BH1750_STATUS
  59. */
  60. BH1750_STATUS bh1750_set_power_state(uint8_t PowerOn);
  61. /**
  62. * @brief Set the Measurement Time register. It allows to increase or decrease the sensitivity.
  63. *
  64. * @param MTreg value from 31 to 254, defaults to 69.
  65. *
  66. * @return BH1750_STATUS
  67. */
  68. BH1750_STATUS bh1750_set_mt_reg(uint8_t MTreg);
  69. /**
  70. * @brief Set the mode of converting. Look into the bh1750_mode enum.
  71. *
  72. * @param Mode mode enumerator
  73. * @return BH1750_STATUS
  74. */
  75. BH1750_STATUS bh1750_set_mode(BH1750_mode Mode);
  76. /**
  77. * @brief Trigger the conversion in manual modes.
  78. *
  79. * @details a low-resolution mode, the conversion time is typically 16 ms, and for a high-resolution
  80. * mode is 120 ms. You need to wait until reading the measurement value. There is no need
  81. * to exit low-power mode for manual conversion. It makes automatically.
  82. *
  83. * @return BH1750_STATUS
  84. */
  85. BH1750_STATUS bh1750_trigger_manual_conversion();
  86. /**
  87. * @brief Read the converted value and calculate the result.
  88. *
  89. * @param Result stores received value to this variable.
  90. * @return BH1750_STATUS
  91. */
  92. BH1750_STATUS bh1750_read_light(float* Result);
  93. #endif /* BH1750_H_ */