forked from hybridgroup/gobot
/
bh1750_driver.go
113 lines (91 loc) · 2.59 KB
/
bh1750_driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package i2c
import (
"time"
"errors"
"gobot.io/x/gobot"
)
const bh1750Address = 0x23
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
)
// BH1750Driver is a driver for the BH1750 digital Ambient Light Sensor IC for I²C bus interface.
//
type BH1750Driver struct {
name string
connector Connector
connection Connection
mode byte
Config
}
// NewBH1750Driver creates a new driver with specified i2c interface
// Params:
// conn Connector - the Adaptor to use with this Driver
//
// Optional params:
// i2c.WithBus(int): bus to use with this driver
// i2c.WithAddress(int): address to use with this driver
//
func NewBH1750Driver(a Connector, options ...func(Config)) *BH1750Driver {
m := &BH1750Driver{
name: gobot.DefaultName("BH1750"),
connector: a,
Config: NewConfig(),
mode: BH1750_CONTINUOUS_HIGH_RES_MODE,
}
for _, option := range options {
option(m)
}
// TODO: add commands for API
return m
}
// Name returns the Name for the Driver
func (h *BH1750Driver) Name() string { return h.name }
// SetName sets the Name for the Driver
func (h *BH1750Driver) SetName(n string) { h.name = n }
// Connection returns the connection for the Driver
func (h *BH1750Driver) Connection() gobot.Connection { return h.connector.(gobot.Connection) }
// Start initialized the bh1750
func (h *BH1750Driver) Start() (err error) {
bus := h.GetBusOrDefault(h.connector.GetDefaultBus())
address := h.GetAddressOrDefault(bh1750Address)
h.connection, err = h.connector.GetConnection(address, bus)
if err != nil {
return err
}
err = h.connection.WriteByte(h.mode)
time.Sleep(10 * time.Microsecond)
if err != nil {
return err
}
return
}
// Halt returns true if devices is halted successfully
func (h *BH1750Driver) Halt() (err error) { return }
// RawSensorData returns the raw value from the bh1750
func (h *BH1750Driver) RawSensorData() (level int, err error) {
buf := []byte{0, 0}
bytesRead, err := h.connection.Read(buf)
if bytesRead != 2 {
err = errors.New("wrong number of bytes read")
return
}
if err != nil {
return
}
level = int(buf[0])<<8 | int(buf[1])
return
}
// Lux returns the adjusted value from the bh1750
func (h *BH1750Driver) Lux() (lux int, err error) {
lux, err = h.RawSensorData()
lux = int(float64(lux) / 1.2)
return
}