i2c: refactor all i2c drivers to use Address property of Driver. Allows variations to override the default address as discussed in #14 (#30)

Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
Ron Evans
2019-04-07 10:50:34 +02:00
committed by GitHub
parent b8e278e08f
commit d9b426b90b
7 changed files with 65 additions and 50 deletions
+9 -7
View File
@@ -16,8 +16,9 @@ type SamplingMode byte
// Device wraps an I2C connection to a bh1750 device.
type Device struct {
bus machine.I2C
mode SamplingMode
bus machine.I2C
Address uint16
mode SamplingMode
}
// New creates a new bh1750 connection. The I2C bus must already be
@@ -26,14 +27,15 @@ type Device struct {
// This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C) Device {
return Device{
bus: bus,
mode: CONTINUOUS_HIGH_RES_MODE,
bus: bus,
Address: Address,
mode: CONTINUOUS_HIGH_RES_MODE,
}
}
// Configure sets up the device for communication
func (d *Device) Configure() {
d.bus.Tx(Address, []byte{POWER_ON}, nil)
d.bus.Tx(d.Address, []byte{POWER_ON}, nil)
d.SetMode(d.mode)
}
@@ -41,7 +43,7 @@ func (d *Device) Configure() {
func (d *Device) RawSensorData() uint16 {
buf := []byte{1, 0}
d.bus.Tx(Address, nil, buf)
d.bus.Tx(d.Address, nil, buf)
return (uint16(buf[0]) << 8) | uint16(buf[1])
}
@@ -65,6 +67,6 @@ func (d *Device) Illuminance() int32 {
// SetMode changes the reading mode for the sensor
func (d *Device) SetMode(mode SamplingMode) {
d.mode = mode
d.bus.Tx(Address, []byte{byte(d.mode)}, nil)
d.bus.Tx(d.Address, []byte{byte(d.mode)}, nil)
time.Sleep(10 * time.Millisecond)
}