bh1750 i2c light sensor driver + pwm cleanup (#330)

Add bh1750 (I2C light sensor) driver + usage example
This commit is contained in:
andree182 2017-04-01 02:20:37 +02:00 committed by Ruslan V. Uss
parent 31ef50c9a9
commit b3f658bdbf
7 changed files with 170 additions and 7 deletions

3
examples/bh1750/Makefile Normal file
View file

@ -0,0 +1,3 @@
PROGRAM=bh1750_example
EXTRA_COMPONENTS = extras/i2c extras/bh1750
include ../../common.mk

View file

@ -0,0 +1,41 @@
#include <stdio.h>
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "i2c/i2c.h"
#include "bh1750/bh1750.h"
#define SCL_PIN 5
#define SDA_PIN 4
static void measure(void *pvParameters)
{
bh1750_configure(BH1750_ADDR_LO,
BH1750_CONTINUOUS_MODE | BH1750_HIGH_RES_MODE);
while (1) {
while(1) {
vTaskDelay(200 / portTICK_PERIOD_MS);
printf("Lux: %d\n", bh1750_read(BH1750_ADDR_LO));
}
}
}
void user_init(void)
{
uart_set_baud(0, 115200);
// Just some information
printf("\n");
printf("SDK version : %s\n", sdk_system_get_sdk_version());
printf("GIT version : %s\n", GITSHORTREV);
i2c_init(SCL_PIN, SDA_PIN);
xTaskCreate(measure, "measure_task", 256, NULL, 2, NULL);
}

28
extras/bh1750/bh1750.c Normal file
View 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
View 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_ */

View 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))

View file

@ -29,7 +29,7 @@ typedef struct pwmInfoDefinition
uint8_t running;
uint16_t freq;
uint16_t dutyCicle;
uint16_t dutyCycle;
/* private */
uint32_t _maxLoad;
@ -66,7 +66,7 @@ static void frc1_interrupt_handler(void)
pwmInfo._step = step;
}
void pwm_init(uint8_t npins, uint8_t* pins)
void pwm_init(uint8_t npins, const uint8_t* pins)
{
/* Assert number of pins is correct */
if (npins > MAX_PWM_PINS)
@ -127,7 +127,7 @@ void pwm_set_duty(uint16_t duty)
{
bool output;
pwmInfo.dutyCicle = duty;
pwmInfo.dutyCycle = duty;
if (duty > 0 && duty < UINT16_MAX) {
pwm_restart();
return;
@ -154,7 +154,7 @@ void pwm_restart()
void pwm_start()
{
pwmInfo._onLoad = pwmInfo.dutyCicle * pwmInfo._maxLoad / UINT16_MAX;
pwmInfo._onLoad = pwmInfo.dutyCycle * pwmInfo._maxLoad / UINT16_MAX;
pwmInfo._offLoad = pwmInfo._maxLoad - pwmInfo._onLoad;
pwmInfo._step = PERIOD_ON;

View file

@ -16,7 +16,7 @@
extern "C" {
#endif
void pwm_init(uint8_t npins, uint8_t* pins);
void pwm_init(uint8_t npins, const uint8_t* pins);
void pwm_set_freq(uint16_t freq);
void pwm_set_duty(uint16_t duty);