2018-01-12 03:56:23 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-12-09 02:29:51 +08:00
|
|
|
"time"
|
2018-01-12 03:56:23 +08:00
|
|
|
)
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
const bh1750DefaultAddress = 0x23
|
2018-01-12 03:56:23 +08:00
|
|
|
|
|
|
|
const (
|
|
|
|
BH1750_POWER_DOWN = 0x00
|
|
|
|
BH1750_POWER_ON = 0x01
|
|
|
|
BH1750_RESET = 0x07
|
|
|
|
BH1750_CONTINUOUS_HIGH_RES_MODE = 0x10
|
|
|
|
BH1750_CONTINUOUS_HIGH_RES_MODE_2 = 0x11
|
|
|
|
BH1750_CONTINUOUS_LOW_RES_MODE = 0x13
|
|
|
|
BH1750_ONE_TIME_HIGH_RES_MODE = 0x20
|
|
|
|
BH1750_ONE_TIME_HIGH_RES_MODE_2 = 0x21
|
|
|
|
BH1750_ONE_TIME_LOW_RES_MODE = 0x23
|
|
|
|
)
|
|
|
|
|
2018-01-12 23:26:21 +08:00
|
|
|
// BH1750Driver is a driver for the BH1750 digital Ambient Light Sensor IC for I²C bus interface.
|
2018-01-12 03:56:23 +08:00
|
|
|
type BH1750Driver struct {
|
2022-12-10 20:10:23 +08:00
|
|
|
*Driver
|
|
|
|
mode byte
|
2018-01-12 03:56:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBH1750Driver creates a new driver with specified i2c interface
|
|
|
|
// Params:
|
2023-10-21 02:50:42 +08:00
|
|
|
//
|
|
|
|
// c Connector - the Adaptor to use with this Driver
|
2018-01-12 03:56:23 +08:00
|
|
|
//
|
|
|
|
// Optional params:
|
|
|
|
//
|
2023-10-21 02:50:42 +08:00
|
|
|
// i2c.WithBus(int): bus to use with this driver
|
|
|
|
// i2c.WithAddress(int): address to use with this driver
|
2022-12-10 20:10:23 +08:00
|
|
|
func NewBH1750Driver(c Connector, options ...func(Config)) *BH1750Driver {
|
|
|
|
h := &BH1750Driver{
|
|
|
|
Driver: NewDriver(c, "BH1750", bh1750DefaultAddress),
|
|
|
|
mode: BH1750_CONTINUOUS_HIGH_RES_MODE,
|
2018-01-12 03:56:23 +08:00
|
|
|
}
|
2022-12-10 20:10:23 +08:00
|
|
|
h.afterStart = h.initialize
|
2018-01-12 03:56:23 +08:00
|
|
|
|
|
|
|
for _, option := range options {
|
2022-12-10 20:10:23 +08:00
|
|
|
option(h)
|
2018-01-12 03:56:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add commands for API
|
2022-12-10 20:10:23 +08:00
|
|
|
return h
|
2018-01-12 03:56:23 +08:00
|
|
|
}
|
|
|
|
|
2018-01-12 23:26:21 +08:00
|
|
|
// RawSensorData returns the raw value from the bh1750
|
2023-11-16 03:51:52 +08:00
|
|
|
func (h *BH1750Driver) RawSensorData() (int, error) {
|
2018-01-12 03:56:23 +08:00
|
|
|
buf := []byte{0, 0}
|
|
|
|
bytesRead, err := h.connection.Read(buf)
|
|
|
|
if err != nil {
|
2023-11-16 03:51:52 +08:00
|
|
|
return 0, err
|
2018-01-12 03:56:23 +08:00
|
|
|
}
|
2023-11-16 03:51:52 +08:00
|
|
|
if bytesRead != 2 {
|
|
|
|
return 0, errors.New("wrong number of bytes read")
|
|
|
|
}
|
|
|
|
|
|
|
|
level := int(buf[0])<<8 | int(buf[1])
|
2018-01-12 03:56:23 +08:00
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
return level, nil
|
2018-01-12 03:56:23 +08:00
|
|
|
}
|
|
|
|
|
2018-01-12 23:26:21 +08:00
|
|
|
// Lux returns the adjusted value from the bh1750
|
2023-11-16 03:51:52 +08:00
|
|
|
func (h *BH1750Driver) Lux() (int, error) {
|
|
|
|
rawLux, err := h.RawSensorData()
|
|
|
|
lux := int(float64(rawLux) / 1.2)
|
2018-01-12 03:56:23 +08:00
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
return lux, err
|
2018-01-12 03:56:23 +08:00
|
|
|
}
|
2022-12-10 20:10:23 +08:00
|
|
|
|
|
|
|
func (h *BH1750Driver) initialize() error {
|
|
|
|
err := h.connection.WriteByte(h.mode)
|
|
|
|
time.Sleep(10 * time.Microsecond)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|