bh1750 i2c light sensor driver + pwm cleanup (#330)
Add bh1750 (I2C light sensor) driver + usage example
This commit is contained in:
parent
31ef50c9a9
commit
b3f658bdbf
7 changed files with 170 additions and 7 deletions
28
extras/bh1750/bh1750.c
Normal file
28
extras/bh1750/bh1750.c
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Driver for BH1750 light sensor
|
||||
*
|
||||
* Part of esp-open-rtos
|
||||
* Copyright (C) 2017 Andrej Krutak <dev@andree.sk>
|
||||
* BSD Licensed as described in the file LICENSE
|
||||
*/
|
||||
#include "bh1750.h"
|
||||
#include <i2c/i2c.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
82
extras/bh1750/bh1750.h
Normal file
82
extras/bh1750/bh1750.h
Normal file
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Driver for BH1750 light sensor
|
||||
*
|
||||
* Part of esp-open-rtos
|
||||
* Copyright (C) 2017 Andrej Krutak <dev@andree.sk>
|
||||
* BSD Licensed as described in the file LICENSE
|
||||
*
|
||||
* ROHM Semiconductor bh1750fvi-e.pdf
|
||||
*/
|
||||
#ifndef EXTRAS_BH1750_H_
|
||||
#define EXTRAS_BH1750_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
|
||||
#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_ */
|
9
extras/bh1750/component.mk
Normal file
9
extras/bh1750/component.mk
Normal file
|
@ -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))
|
Loading…
Add table
Add a link
Reference in a new issue