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
+13 -7
View File
@@ -9,7 +9,8 @@ import (
// Device wraps an I2C connection to a BlinkM device.
type Device struct {
bus machine.I2C
bus machine.I2C
Address uint16
}
// New creates a new BlinkM connection. The I2C bus must already be
@@ -17,38 +18,43 @@ 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}
return Device{bus, Address}
}
// Configure sets up the device for communication
func (d *Device) Configure() {
d.bus.Tx(d.Address, []byte{'o'}, nil)
}
// Version returns the version of firmware on the BlinkM.
func (d Device) Version() (major, minor byte, err error) {
version := []byte{0, 0}
d.bus.ReadRegister(Address, GET_FIRMWARE, version)
d.bus.Tx(d.Address, []byte{GET_FIRMWARE}, version)
return version[0], version[1], nil
}
// SetRGB sets the RGB color on the BlinkM.
func (d Device) SetRGB(r, g, b byte) error {
d.bus.WriteRegister(Address, TO_RGB, []byte{r, g, b})
d.bus.Tx(d.Address, []byte{TO_RGB, r, g, b}, nil)
return nil
}
// GetRGB gets the current RGB color on the BlinkM.
func (d Device) GetRGB() (r, g, b byte, err error) {
color := []byte{0, 0, 0}
d.bus.ReadRegister(Address, GET_RGB, color)
d.bus.Tx(d.Address, []byte{GET_RGB}, color)
return color[0], color[1], color[2], nil
}
// FadeToRGB sets the RGB color on the BlinkM by fading from the current color
// to the new color.
func (d Device) FadeToRGB(r, g, b byte) error {
d.bus.WriteRegister(Address, FADE_TO_RGB, []byte{r, g, b})
d.bus.Tx(d.Address, []byte{FADE_TO_RGB, r, g, b}, nil)
return nil
}
// StopScript stops whatever script is currently running on the BlinkM.
func (d Device) StopScript() error {
d.bus.WriteRegister(Address, STOP_SCRIPT, nil)
d.bus.Tx(d.Address, []byte{STOP_SCRIPT}, nil)
return nil
}