BH1750.h 2.4 KB

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