From 174dba8c8bf833a4825c6ed8226895174898c595 Mon Sep 17 00:00:00 2001 From: Andrej Krutak Date: Thu, 26 Jan 2017 12:10:02 +0100 Subject: [PATCH] Add bh1750 (I2C light sensor) driver --- extras/bh1750/bh1750.c | 28 +++++++++++++ extras/bh1750/bh1750.h | 82 ++++++++++++++++++++++++++++++++++++++ extras/bh1750/component.mk | 9 +++++ 3 files changed, 119 insertions(+) create mode 100644 extras/bh1750/bh1750.c create mode 100644 extras/bh1750/bh1750.h create mode 100644 extras/bh1750/component.mk diff --git a/extras/bh1750/bh1750.c b/extras/bh1750/bh1750.c new file mode 100644 index 0000000..68e623a --- /dev/null +++ b/extras/bh1750/bh1750.c @@ -0,0 +1,28 @@ +/* + * Driver for BH1750 light sensor + * + * Part of esp-open-rtos + * Copyright (C) 2017 Andrej Krutak + * BSD Licensed as described in the file LICENSE + */ +#include "bh1750.h" +#include +#include + +void bh1750_configure(uint8_t addr, uint8_t mode) +{ + i2c_slave_write(addr, NULL, &mode, 1); +} + +uint16_t bh1750_read(uint8_t addr) +{ + uint8_t buf[2]; + uint16_t level; + + i2c_slave_read(addr, NULL, buf, 2); + + level = buf[0] << 8 | buf[1]; + level = (level * 10) / 12; // convert to LUX + + return level; +} diff --git a/extras/bh1750/bh1750.h b/extras/bh1750/bh1750.h new file mode 100644 index 0000000..b280c45 --- /dev/null +++ b/extras/bh1750/bh1750.h @@ -0,0 +1,82 @@ +/* + * Driver for BH1750 light sensor + * + * Part of esp-open-rtos + * Copyright (C) 2017 Andrej Krutak + * BSD Licensed as described in the file LICENSE + * + * ROHM Semiconductor bh1750fvi-e.pdf + */ +#ifndef EXTRAS_BH1750_H_ +#define EXTRAS_BH1750_H_ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Possible chip addresses */ +#define BH1750_ADDR_LO 0x23 // ADDR pin floating/low +#define BH1750_ADDR_HI 0x5c + + +/* Configuration options */ + +// No active state +#define BH1750_POWER_DOWN 0x00 + +// Wating for measurement command +#define BH1750_POWER_ON 0x01 + +// Reset data register value - not accepted in POWER_DOWN mode +#define BH1750_RESET 0x07 + + +/* Measurement modes */ + +#define BH1750_CONTINUOUS_MODE 0x10 +#define BH1750_ONE_TIME_MODE 0x20 + +// Start measurement at 1 lx resolution (measurement time typically 120ms) +#define BH1750_HIGH_RES_MODE 0x00 +// Start measurement at 0.5 lx resolution (measurement time typically 120ms) +#define BH1750_HIGH_RES_MODE2 0x01 +// Start measurement at 4 lx resolution (measurement time typically 16ms) +#define BH1750_LOW_RES_MODE 0x03 + +/* Adjust measurement time to account for optical window size (see datasheet). + * Procedure from datasheet suggests order Hi, Low and finally measurement mode + */ +#define BH1750_MEASURE_TIME_HI(mt) (0x40 | (((mt) >> 5) & 0x7)) +#define BH1750_MEASURE_TIME_LO(mt) (0x60 | ((mt) & 0x1f)) +#define BH1750_DEFAULT_MEASURE_TIME 0x45 + + +/** + * Configure the device. + * @param addr Device address + * @param mode Combination of BH1750_* flags + * + * May be called multiple times e.g. to configure the measurement time and + * the readout mode afterwards - or if one time mode is used consecutively. + * + * Example: BH1750_ADDR_LO, BH1750_CONTINUOUS_MODE | BH1750_HIGH_RES_MODE + */ +void bh1750_configure(uint8_t addr, uint8_t mode); + +/** + * Read LUX value from the device. + * + * @param addr Device address + * @returns read value in lux units + */ +uint16_t bh1750_read(uint8_t addr); + +#ifdef __cplusplus +} +#endif + +#endif /* EXTRAS_BH1750_H_ */ diff --git a/extras/bh1750/component.mk b/extras/bh1750/component.mk new file mode 100644 index 0000000..0a57b31 --- /dev/null +++ b/extras/bh1750/component.mk @@ -0,0 +1,9 @@ +# Component makefile for extras/bh1750 + +# expected anyone using RTC driver includes it as 'bh1750/bh1750.h' +INC_DIRS += $(bh1750_ROOT).. + +# args for passing into compile rule generation +bh1750_SRC_DIR = $(bh1750_ROOT) + +$(eval $(call component_compile_rules,bh1750))