BH1750.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 Reset all registers to the default value.
  44. *
  45. * @return BH1750_STATUS
  46. */
  47. BH1750_STATUS bh1750_reset();
  48. /**
  49. * @brief Sets the power state. 1 - running; 0 - sleep, low power.
  50. *
  51. * @param PowerOn sensor state.
  52. * @return BH1750_STATUS
  53. */
  54. BH1750_STATUS bh1750_set_power_state(uint8_t PowerOn);
  55. /**
  56. * @brief Set the Measurement Time register. It allows to increase or decrease the sensitivity.
  57. *
  58. * @param MTreg value from 31 to 254, defaults to 69.
  59. *
  60. * @return BH1750_STATUS
  61. */
  62. BH1750_STATUS bh1750_set_mt_reg(uint8_t MTreg);
  63. /**
  64. * @brief Set the mode of converting. Look into the bh1750_mode enum.
  65. *
  66. * @param Mode mode enumerator
  67. * @return BH1750_STATUS
  68. */
  69. BH1750_STATUS bh1750_set_mode(BH1750_mode Mode);
  70. /**
  71. * @brief Trigger the conversion in manual modes.
  72. *
  73. * @details a low-resolution mode, the conversion time is typically 16 ms, and for a high-resolution
  74. * mode is 120 ms. You need to wait until reading the measurement value. There is no need
  75. * to exit low-power mode for manual conversion. It makes automatically.
  76. *
  77. * @return BH1750_STATUS
  78. */
  79. BH1750_STATUS bh1750_trigger_manual_conversion();
  80. /**
  81. * @brief Read the converted value and calculate the result.
  82. *
  83. * @param Result stores received value to this variable.
  84. * @return BH1750_STATUS
  85. */
  86. BH1750_STATUS bh1750_read_light(float* Result);
  87. #endif /* BH1750_H_ */