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)
}
+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
}
+12 -10
View File
@@ -33,6 +33,7 @@ type calibrationCoefficients struct {
// Device wraps an I2C connection to a BMP180 device.
type Device struct {
bus machine.I2C
Address uint16
mode OversamplingMode
calibrationCoefficients calibrationCoefficients
}
@@ -43,8 +44,9 @@ 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: ULTRAHIGHRESOLUTION,
bus: bus,
Address: Address,
mode: ULTRAHIGHRESOLUTION,
}
}
@@ -52,7 +54,7 @@ func New(bus machine.I2C) Device {
// It does a "who am I" request and checks the response.
func (d *Device) Connected() bool {
data := []byte{0}
d.bus.ReadRegister(Address, WHO_AM_I, data)
d.bus.ReadRegister(uint8(d.Address), WHO_AM_I, data)
return data[0] == CHIP_ID
}
@@ -60,7 +62,7 @@ func (d *Device) Connected() bool {
// read the calibration coefficientes.
func (d *Device) Configure() {
data := make([]byte, 22)
err := d.bus.ReadRegister(Address, AC1_MSB, data)
err := d.bus.ReadRegister(uint8(d.Address), AC1_MSB, data)
if err != nil {
return
}
@@ -85,7 +87,7 @@ func (d *Device) Temperature() (temperature int32, err error) {
}
b5 := d.calculateB5(rawTemp)
t := (b5 + 8) >> 4
return 100*t, nil
return 100 * t, nil
}
// Pressure returns the pressure in milli pascals (mPa)
@@ -118,15 +120,15 @@ func (d *Device) Pressure() (pressure int32, err error) {
x1 = (p >> 8) * (p >> 8)
x1 = (x1 * 3038) >> 16
x2 = (-7357 * p) >> 16
return 1000*(p + ((x1 + x2 + 3791) >> 4)), nil
return 1000 * (p + ((x1 + x2 + 3791) >> 4)), nil
}
// rawTemp returns the sensor's raw values of the temperature
func (d *Device) rawTemp() (int16, error) {
d.bus.WriteRegister(Address, REG_CTRL, []byte{CMD_TEMP})
d.bus.WriteRegister(uint8(d.Address), REG_CTRL, []byte{CMD_TEMP})
time.Sleep(5 * time.Millisecond)
data := make([]byte, 2)
err := d.bus.ReadRegister(Address, REG_TEMP_MSB, data)
err := d.bus.ReadRegister(uint8(d.Address), REG_TEMP_MSB, data)
if err != nil {
return 0, err
}
@@ -142,10 +144,10 @@ func (d *Device) calculateB5(rawTemp int16) int32 {
// rawPressure returns the sensor's raw values of the pressure
func (d *Device) rawPressure(mode OversamplingMode) (int32, error) {
d.bus.WriteRegister(Address, REG_CTRL, []byte{CMD_PRESSURE + byte(mode<<6)})
d.bus.WriteRegister(uint8(d.Address), REG_CTRL, []byte{CMD_PRESSURE + byte(mode<<6)})
time.Sleep(pauseForReading(mode))
data := make([]byte, 3)
err := d.bus.ReadRegister(Address, REG_PRESSURE_MSB, data)
err := d.bus.ReadRegister(uint8(d.Address), REG_PRESSURE_MSB, data)
if err != nil {
return 0, err
}
+8 -7
View File
@@ -10,7 +10,8 @@ import (
// Device wraps an I2C connection to a MAG3110 device.
type Device struct {
bus machine.I2C
bus machine.I2C
Address uint16
}
// New creates a new MAG3110 connection. The I2C bus must already be
@@ -18,29 +19,29 @@ 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}
}
// Connected returns whether a MAG3110 has been found.
// It does a "who am I" request and checks the response.
func (d Device) Connected() bool {
data := []byte{0}
d.bus.ReadRegister(Address, WHO_AM_I, data)
d.bus.ReadRegister(uint8(d.Address), WHO_AM_I, data)
return data[0] == 0xC4
}
// Configure sets up the device for communication.
func (d Device) Configure() {
d.bus.WriteRegister(Address, CTRL_REG2, []uint8{0x80}) // Power down when not used
d.bus.WriteRegister(uint8(d.Address), CTRL_REG2, []uint8{0x80}) // Power down when not used
}
// ReadMagnetic reads the vectors of the magnetic field of the device and
// returns it.
func (d Device) ReadMagnetic() (x int16, y int16, z int16) {
d.bus.WriteRegister(Address, CTRL_REG1, []uint8{0x1a}) // Request a measurement
d.bus.WriteRegister(uint8(d.Address), CTRL_REG1, []uint8{0x1a}) // Request a measurement
data := make([]byte, 6)
d.bus.ReadRegister(Address, OUT_X_MSB, data)
d.bus.ReadRegister(uint8(d.Address), OUT_X_MSB, data)
x = int16((uint16(data[0]) << 8) | uint16(data[1]))
y = int16((uint16(data[2]) << 8) | uint16(data[3]))
z = int16((uint16(data[4]) << 8) | uint16(data[5]))
@@ -50,6 +51,6 @@ func (d Device) ReadMagnetic() (x int16, y int16, z int16) {
// ReadTemperature reads the current die temperature in degrees Celsius.
func (d Device) ReadTemperature() (temp int8) {
data := make([]byte, 1)
d.bus.ReadRegister(Address, DIE_TEMP, data)
d.bus.ReadRegister(uint8(d.Address), DIE_TEMP, data)
return int8(data[0])
}
+7 -6
View File
@@ -12,6 +12,7 @@ import (
// Device wraps an I2C connection to a MMA8653 device.
type Device struct {
bus machine.I2C
Address uint16
sensitivity Sensitivity
}
@@ -20,34 +21,34 @@ 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, Sensitivity2G}
return Device{bus, Address, Sensitivity2G}
}
// Connected returns whether a MMA8653 has been found.
// It does a "who am I" request and checks the response.
func (d Device) Connected() bool {
data := []byte{0}
d.bus.ReadRegister(Address, WHO_AM_I, data)
d.bus.ReadRegister(uint8(d.Address), WHO_AM_I, data)
return data[0] == 0x5A
}
// Configure sets up the device for communication.
func (d *Device) Configure(speed DataRate, sensitivity Sensitivity) error {
// Set mode to STANDBY to be able to change the sensitivity.
err := d.bus.WriteRegister(Address, CTRL_REG1, []uint8{0})
err := d.bus.WriteRegister(uint8(d.Address), CTRL_REG1, []uint8{0})
if err != nil {
return err
}
// Set sensitivity (2G, 4G, 8G).
err = d.bus.WriteRegister(Address, XYZ_DATA_CFG, []uint8{uint8(sensitivity)})
err = d.bus.WriteRegister(uint8(d.Address), XYZ_DATA_CFG, []uint8{uint8(sensitivity)})
if err != nil {
return err
}
d.sensitivity = sensitivity
// Set mode to ACTIVE and set the data rate.
err = d.bus.WriteRegister(Address, CTRL_REG1, []uint8{(uint8(speed) << 3) | 1})
err = d.bus.WriteRegister(uint8(d.Address), CTRL_REG1, []uint8{(uint8(speed) << 3) | 1})
if err != nil {
return err
}
@@ -60,7 +61,7 @@ func (d *Device) Configure(speed DataRate, sensitivity Sensitivity) error {
// -1000000.
func (d Device) ReadAcceleration() (x int32, y int32, z int32, err error) {
data := make([]byte, 6)
err = d.bus.ReadRegister(Address, OUT_X_MSB, data)
err = d.bus.ReadRegister(uint8(d.Address), OUT_X_MSB, data)
shift := uint32(8)
switch d.sensitivity {
case Sensitivity4G:
+7 -6
View File
@@ -12,7 +12,8 @@ import (
// Device wraps an I2C connection to a MPU6050 device.
type Device struct {
bus machine.I2C
bus machine.I2C
Address uint16
}
// New creates a new MPU6050 connection. The I2C bus must already be
@@ -20,27 +21,27 @@ 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}
}
// Connected returns whether a MPU6050 has been found.
// It does a "who am I" request and checks the response.
func (d Device) Connected() bool {
data := []byte{0}
d.bus.ReadRegister(Address, WHO_AM_I, data)
d.bus.ReadRegister(uint8(d.Address), WHO_AM_I, data)
return data[0] == 0x68
}
// Configure sets up the device for communication.
func (d Device) Configure() {
d.bus.WriteRegister(Address, PWR_MGMT_1, []uint8{0})
d.bus.WriteRegister(uint8(d.Address), PWR_MGMT_1, []uint8{0})
}
// ReadAcceleration reads the current acceleration from the device and returns
// it.
func (d Device) ReadAcceleration() (x int16, y int16, z int16) {
data := make([]byte, 6)
d.bus.ReadRegister(Address, ACCEL_XOUT_H, data)
d.bus.ReadRegister(uint8(d.Address), ACCEL_XOUT_H, data)
x = int16((uint16(data[0]) << 8) | uint16(data[1]))
y = int16((uint16(data[2]) << 8) | uint16(data[3]))
z = int16((uint16(data[4]) << 8) | uint16(data[5]))
@@ -50,7 +51,7 @@ func (d Device) ReadAcceleration() (x int16, y int16, z int16) {
// ReadRotation reads the current rotation from the device and returns it.
func (d Device) ReadRotation() (x int16, y int16, z int16) {
data := make([]byte, 6)
d.bus.ReadRegister(Address, GYRO_XOUT_H, data)
d.bus.ReadRegister(uint8(d.Address), GYRO_XOUT_H, data)
x = int16((uint16(data[0]) << 8) | uint16(data[1]))
y = int16((uint16(data[2]) << 8) | uint16(data[3]))
z = int16((uint16(data[4]) << 8) | uint16(data[5]))
+9 -7
View File
@@ -35,6 +35,7 @@ type resultBuffer struct {
// Device wraps an I2C connection to a VL53L1X device.
type Device struct {
bus machine.I2C
Address uint16
mode DistanceMode
timeout uint32
fastOscillatorFreq uint16
@@ -53,6 +54,7 @@ type Device struct {
func New(bus machine.I2C) Device {
return Device{
bus: bus,
Address: Address,
mode: LONG,
timeout: 500,
}
@@ -287,7 +289,7 @@ func (d *Device) readResults() {
data := make([]byte, 17)
msb := byte((RESULT_RANGE_STATUS >> 8) & 0xFF)
lsb := byte(RESULT_RANGE_STATUS & 0xFF)
d.bus.Tx(Address, []byte{msb, lsb}, data)
d.bus.Tx(d.Address, []byte{msb, lsb}, data)
d.results.status = data[0]
// data[1] report_status : not used
d.results.streamCount = data[2]
@@ -430,7 +432,7 @@ func (d *Device) StopContinuous() {
func (d *Device) writeReg(reg uint16, value uint8) {
msb := byte((reg >> 8) & 0xFF)
lsb := byte(reg & 0xFF)
d.bus.Tx(Address, []byte{msb, lsb, value}, nil)
d.bus.Tx(d.Address, []byte{msb, lsb, value}, nil)
}
// writeReg16Bit sends two bytes to the specified register address
@@ -440,7 +442,7 @@ func (d *Device) writeReg16Bit(reg uint16, value uint16) {
data[1] = byte(reg & 0xFF)
data[2] = byte((value >> 8) & 0xFF)
data[3] = byte(value & 0xFF)
d.bus.Tx(Address, data, nil)
d.bus.Tx(d.Address, data, nil)
}
// writeReg32Bit sends four bytes to the specified register address
@@ -452,7 +454,7 @@ func (d *Device) writeReg32Bit(reg uint16, value uint32) {
data[3] = byte((value >> 16) & 0xFF)
data[4] = byte((value >> 8) & 0xFF)
data[5] = byte(value & 0xFF)
d.bus.Tx(Address, data, nil)
d.bus.Tx(d.Address, data, nil)
}
// readReg reads a single byte from the specified address
@@ -460,7 +462,7 @@ func (d *Device) readReg(reg uint16) uint8 {
data := []byte{0}
msb := byte((reg >> 8) & 0xFF)
lsb := byte(reg & 0xFF)
d.bus.Tx(Address, []byte{msb, lsb}, data)
d.bus.Tx(d.Address, []byte{msb, lsb}, data)
return data[0]
}
@@ -470,7 +472,7 @@ func (d *Device) readReg16Bit(reg uint16) uint16 {
data := []byte{0, 0}
msb := byte((reg >> 8) & 0xFF)
lsb := byte(reg & 0xFF)
d.bus.Tx(Address, []byte{msb, lsb}, data)
d.bus.Tx(d.Address, []byte{msb, lsb}, data)
return readUint(data[0], data[1])
}
@@ -480,7 +482,7 @@ func (d *Device) readReg32Bit(reg uint16) uint32 {
data := make([]byte, 4)
msb := byte((reg >> 8) & 0xFF)
lsb := byte(reg & 0xFF)
d.bus.Tx(Address, []byte{msb, lsb}, data)
d.bus.Tx(d.Address, []byte{msb, lsb}, data)
return readUint32(data)
}