i2c: switch all i2c drivers definitions to use i2c bus interface type instead of machine package concrete type

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2020-08-27 12:45:17 +02:00
committed by Ron Evans
parent a07b20f2f9
commit db02cbb8a4
24 changed files with 86 additions and 87 deletions
+4 -3
View File
@@ -1,8 +1,9 @@
package adt7410 // import "tinygo.org/x/drivers/adt7410" package adt7410 // import "tinygo.org/x/drivers/adt7410"
import ( import (
"machine"
"time" "time"
"tinygo.org/x/drivers"
) )
type Error uint8 type Error uint8
@@ -21,7 +22,7 @@ func (e Error) Error() string {
} }
type Device struct { type Device struct {
bus *machine.I2C bus drivers.I2C
buf []byte buf []byte
addr uint8 addr uint8
} }
@@ -31,7 +32,7 @@ type Device struct {
// can be set using by connecting to the A1 and A0 pins to VDD or GND (for a // can be set using by connecting to the A1 and A0 pins to VDD or GND (for a
// total of up to 4 devices on a I2C bus). Also note that 10k pullups are // total of up to 4 devices on a I2C bus). Also note that 10k pullups are
// recommended for the SDA and SCL lines. // recommended for the SDA and SCL lines.
func New(i2c *machine.I2C, addressBits uint8) *Device { func New(i2c drivers.I2C, addressBits uint8) *Device {
return &Device{ return &Device{
bus: i2c, bus: i2c,
buf: make([]byte, 2), buf: make([]byte, 2),
+3 -5
View File
@@ -6,9 +6,7 @@
// //
package adxl345 // import "tinygo.org/x/drivers/adxl345" package adxl345 // import "tinygo.org/x/drivers/adxl345"
import ( import "tinygo.org/x/drivers"
"machine"
)
type Range uint8 type Range uint8
type Rate uint8 type Rate uint8
@@ -40,7 +38,7 @@ type bwRate struct {
// Device wraps an I2C connection to a ADXL345 device. // Device wraps an I2C connection to a ADXL345 device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
powerCtl powerCtl powerCtl powerCtl
dataFormat dataFormat dataFormat dataFormat
@@ -52,7 +50,7 @@ type Device struct {
// //
// This function only creates the Device object, it does not init the device. // This function only creates the Device object, it does not init the device.
// To do that you must call the Configure() method on the Device before using it. // To do that you must call the Configure() method on the Device before using it.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{ return Device{
bus: bus, bus: bus,
powerCtl: powerCtl{ powerCtl: powerCtl{
+4 -3
View File
@@ -5,13 +5,14 @@
package amg88xx // import "tinygo.org/x/drivers/amg88xx" package amg88xx // import "tinygo.org/x/drivers/amg88xx"
import ( import (
"machine"
"time" "time"
"tinygo.org/x/drivers"
) )
// Device wraps an I2C connection to a AMG88xx device. // Device wraps an I2C connection to a AMG88xx device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
data []uint8 data []uint8
interruptMode InterruptMode interruptMode InterruptMode
@@ -27,7 +28,7 @@ type Config struct {
// configured. // configured.
// //
// This function only creates the Device object, it does not touch the device. // This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{ return Device{
bus: bus, bus: bus,
Address: AddressHigh, Address: AddressHigh,
+4 -3
View File
@@ -6,13 +6,14 @@ package at24cx // import "tinygo.org/x/drivers/at24cx"
import ( import (
"errors" "errors"
"machine"
"time" "time"
"tinygo.org/x/drivers"
) )
// Device wraps an I2C connection to a DS3231 device. // Device wraps an I2C connection to a DS3231 device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
pageSize uint16 pageSize uint16
currentRAMAddress uint16 currentRAMAddress uint16
@@ -30,7 +31,7 @@ type Config struct {
// configured. // configured.
// //
// This function only creates the Device object, it does not touch the device. // This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{ return Device{
bus: bus, bus: bus,
Address: Address, Address: Address,
+3 -3
View File
@@ -8,7 +8,7 @@ package bh1750 // import "tinygo.org/x/drivers/bh1750"
import ( import (
"time" "time"
"machine" "tinygo.org/x/drivers"
) )
// SamplingMode is the sampling's resolution of the measurement // SamplingMode is the sampling's resolution of the measurement
@@ -16,7 +16,7 @@ type SamplingMode byte
// Device wraps an I2C connection to a bh1750 device. // Device wraps an I2C connection to a bh1750 device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
mode SamplingMode mode SamplingMode
} }
@@ -25,7 +25,7 @@ type Device struct {
// configured. // configured.
// //
// This function only creates the Device object, it does not touch the device. // This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{ return Device{
bus: bus, bus: bus,
Address: Address, Address: Address,
+4 -5
View File
@@ -1,15 +1,14 @@
// Package blinkm implements a driver for the BlinkM I2C RGB LED. // Package blinkm implements a driver for the BlinkM I2C RGB LED.
// //
// Datasheet: http://thingm.com/fileadmin/thingm/downloads/BlinkM_datasheet.pdf // Datasheet: http://thingm.com/fileadmin/thingm/downloads/BlinkM_datasheet.pdf
//
package blinkm // import "tinygo.org/x/drivers/blinkm" package blinkm // import "tinygo.org/x/drivers/blinkm"
import ( import "tinygo.org/x/drivers"
"machine"
)
// Device wraps an I2C connection to a BlinkM device. // Device wraps an I2C connection to a BlinkM device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
} }
@@ -17,7 +16,7 @@ type Device struct {
// configured. // configured.
// //
// This function only creates the Device object, it does not touch the device. // This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{bus, Address} return Device{bus, Address}
} }
+4 -3
View File
@@ -7,8 +7,9 @@
package bme280 package bme280
import ( import (
"machine"
"math" "math"
"tinygo.org/x/drivers"
) )
// calibrationCoefficients reads at startup and stores the calibration coefficients // calibrationCoefficients reads at startup and stores the calibration coefficients
@@ -35,7 +36,7 @@ type calibrationCoefficients struct {
// Device wraps an I2C connection to a BME280 device. // Device wraps an I2C connection to a BME280 device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
calibrationCoefficients calibrationCoefficients calibrationCoefficients calibrationCoefficients
} }
@@ -44,7 +45,7 @@ type Device struct {
// configured. // configured.
// //
// This function only creates the Device object, it does not touch the device. // This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{ return Device{
bus: bus, bus: bus,
Address: Address, Address: Address,
+3 -3
View File
@@ -9,7 +9,7 @@ package bmp180 // import "tinygo.org/x/drivers/bmp180"
import ( import (
"time" "time"
"machine" "tinygo.org/x/drivers"
) )
// OversamplingMode is the oversampling ratio of the pressure measurement. // OversamplingMode is the oversampling ratio of the pressure measurement.
@@ -32,7 +32,7 @@ type calibrationCoefficients struct {
// Device wraps an I2C connection to a BMP180 device. // Device wraps an I2C connection to a BMP180 device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
mode OversamplingMode mode OversamplingMode
calibrationCoefficients calibrationCoefficients calibrationCoefficients calibrationCoefficients
@@ -43,7 +43,7 @@ type Device struct {
// //
// This function only creates the Device object, it does not initialize the device. // This function only creates the Device object, it does not initialize the device.
// You must call Configure() first in order to use the device itself. // You must call Configure() first in order to use the device itself.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{ return Device{
bus: bus, bus: bus,
Address: Address, Address: Address,
+4 -3
View File
@@ -1,8 +1,9 @@
package bmp280 package bmp280
import ( import (
"machine"
"time" "time"
"tinygo.org/x/drivers"
) )
// OversamplingMode is the oversampling ratio of the temperature or pressure measurement. // OversamplingMode is the oversampling ratio of the temperature or pressure measurement.
@@ -19,7 +20,7 @@ type Filter uint
// Device wraps an I2C connection to a BMP280 device. // Device wraps an I2C connection to a BMP280 device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
cali calibrationCoefficients cali calibrationCoefficients
Temperature Oversampling Temperature Oversampling
@@ -52,7 +53,7 @@ type calibrationCoefficients struct {
// //
// This function only creates the Device object, it does not initialize the device. // This function only creates the Device object, it does not initialize the device.
// You must call Configure() first in order to use the device itself. // You must call Configure() first in order to use the device itself.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{ return Device{
bus: bus, bus: bus,
Address: Address, Address: Address,
+3 -3
View File
@@ -9,18 +9,18 @@ import (
"errors" "errors"
"time" "time"
"machine" "tinygo.org/x/drivers"
) )
// Device wraps an I2C connection to a DS1307 device. // Device wraps an I2C connection to a DS1307 device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint8 Address uint8
AddressSRAM uint8 AddressSRAM uint8
} }
// New creates a new DS1307 connection. I2C bus must be already configured. // New creates a new DS1307 connection. I2C bus must be already configured.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{bus: bus, return Device{bus: bus,
Address: uint8(I2CAddress), Address: uint8(I2CAddress),
AddressSRAM: SRAMBeginAddres, AddressSRAM: SRAMBeginAddres,
+4 -3
View File
@@ -5,15 +5,16 @@
package ds3231 // import "tinygo.org/x/drivers/ds3231" package ds3231 // import "tinygo.org/x/drivers/ds3231"
import ( import (
"machine"
"time" "time"
"tinygo.org/x/drivers"
) )
type Mode uint8 type Mode uint8
// Device wraps an I2C connection to a DS3231 device. // Device wraps an I2C connection to a DS3231 device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
} }
@@ -21,7 +22,7 @@ type Device struct {
// configured. // configured.
// //
// This function only creates the Device object, it does not touch the device. // This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{ return Device{
bus: bus, bus: bus,
Address: Address, Address: Address,
+4 -2
View File
@@ -7,6 +7,8 @@ import (
"machine" "machine"
"strings" "strings"
"time" "time"
"tinygo.org/x/drivers"
) )
var ( var (
@@ -20,7 +22,7 @@ type Device struct {
bufIdx int bufIdx int
sentence strings.Builder sentence strings.Builder
uart *machine.UART uart *machine.UART
bus *machine.I2C bus drivers.I2C
address uint16 address uint16
} }
@@ -35,7 +37,7 @@ func NewUART(uart *machine.UART) Device {
} }
// NewI2C creates a new I2C GPS connection. // NewI2C creates a new I2C GPS connection.
func NewI2C(bus *machine.I2C) Device { func NewI2C(bus drivers.I2C) Device {
return Device{ return Device{
bus: bus, bus: bus,
address: I2C_ADDRESS, address: I2C_ADDRESS,
+4 -3
View File
@@ -7,13 +7,14 @@ package hd44780i2c
import ( import (
"errors" "errors"
"machine"
"time" "time"
"tinygo.org/x/drivers"
) )
// Device wraps an I2C connection to a HD44780 I2C LCD with related data. // Device wraps an I2C connection to a HD44780 I2C LCD with related data.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
addr uint8 addr uint8
width uint8 width uint8
height uint8 height uint8
@@ -41,7 +42,7 @@ type Config struct {
// configured. // configured.
// //
// This function only creates the Device object, it does not touch the device. // This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C, addr uint8) Device { func New(bus drivers.I2C, addr uint8) Device {
if addr == 0 { if addr == 0 {
addr = 0x27 addr = 0x27
} }
+3 -5
View File
@@ -4,13 +4,11 @@
// //
package lis3dh // import "tinygo.org/x/drivers/lis3dh" package lis3dh // import "tinygo.org/x/drivers/lis3dh"
import ( import "tinygo.org/x/drivers"
"machine"
)
// Device wraps an I2C connection to a LIS3DH device. // Device wraps an I2C connection to a LIS3DH device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
r Range r Range
} }
@@ -18,7 +16,7 @@ type Device struct {
// New creates a new LIS3DH connection. The I2C bus must already be configured. // New creates a new LIS3DH connection. The I2C bus must already be configured.
// //
// This function only creates the Device object, it does not touch the device. // This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{bus: bus, Address: Address0} return Device{bus: bus, Address: Address0}
} }
+4 -3
View File
@@ -6,13 +6,14 @@
package lsm303agr // import "tinygo.org/x/drivers/lsm303agr" package lsm303agr // import "tinygo.org/x/drivers/lsm303agr"
import ( import (
"machine"
"math" "math"
"tinygo.org/x/drivers"
) )
// Device wraps an I2C connection to a LSM303AGR device. // Device wraps an I2C connection to a LSM303AGR device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
AccelAddress uint8 AccelAddress uint8
MagAddress uint8 MagAddress uint8
AccelPowerMode uint8 AccelPowerMode uint8
@@ -37,7 +38,7 @@ type Configuration struct {
// configured. // configured.
// //
// This function only creates the Device object, it does not touch the device. // This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{bus: bus, AccelAddress: ACCEL_ADDRESS, MagAddress: MAG_ADDRESS} return Device{bus: bus, AccelAddress: ACCEL_ADDRESS, MagAddress: MAG_ADDRESS}
} }
+3 -5
View File
@@ -5,9 +5,7 @@
// //
package lsm6ds3 // import "tinygo.org/x/drivers/lsm6ds3" package lsm6ds3 // import "tinygo.org/x/drivers/lsm6ds3"
import ( import "tinygo.org/x/drivers"
"machine"
)
type AccelRange uint8 type AccelRange uint8
type AccelSampleRate uint8 type AccelSampleRate uint8
@@ -18,7 +16,7 @@ type GyroSampleRate uint8
// Device wraps an I2C connection to a LSM6DS3 device. // Device wraps an I2C connection to a LSM6DS3 device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
accelRange AccelRange accelRange AccelRange
accelSampleRate AccelSampleRate accelSampleRate AccelSampleRate
@@ -44,7 +42,7 @@ type Configuration struct {
// configured. // configured.
// //
// This function only creates the Device object, it does not touch the device. // This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{bus: bus, Address: Address} return Device{bus: bus, Address: Address}
} }
+3 -5
View File
@@ -5,13 +5,11 @@
// //
package mag3110 // import "tinygo.org/x/drivers/mag3110" package mag3110 // import "tinygo.org/x/drivers/mag3110"
import ( import "tinygo.org/x/drivers"
"machine"
)
// Device wraps an I2C connection to a MAG3110 device. // Device wraps an I2C connection to a MAG3110 device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
} }
@@ -19,7 +17,7 @@ type Device struct {
// configured. // configured.
// //
// This function only creates the Device object, it does not touch the device. // This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{bus, Address} return Device{bus, Address}
} }
+3 -5
View File
@@ -6,13 +6,11 @@
// //
package mma8653 // import "tinygo.org/x/drivers/mma8653" package mma8653 // import "tinygo.org/x/drivers/mma8653"
import ( import "tinygo.org/x/drivers"
"machine"
)
// Device wraps an I2C connection to a MMA8653 device. // Device wraps an I2C connection to a MMA8653 device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
sensitivity Sensitivity sensitivity Sensitivity
} }
@@ -21,7 +19,7 @@ type Device struct {
// configured. // configured.
// //
// This function only creates the Device object, it does not touch the device. // This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{bus, Address, Sensitivity2G} return Device{bus, Address, Sensitivity2G}
} }
+3 -5
View File
@@ -7,13 +7,11 @@
// //
package mpu6050 // import "tinygo.org/x/drivers/mpu6050" package mpu6050 // import "tinygo.org/x/drivers/mpu6050"
import ( import "tinygo.org/x/drivers"
"machine"
)
// Device wraps an I2C connection to a MPU6050 device. // Device wraps an I2C connection to a MPU6050 device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
} }
@@ -21,7 +19,7 @@ type Device struct {
// configured. // configured.
// //
// This function only creates the Device object, it does not touch the device. // This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{bus, Address} return Device{bus, Address}
} }
+4 -3
View File
@@ -7,13 +7,14 @@
package sht3x // import "tinygo.org/x/drivers/sht3x" package sht3x // import "tinygo.org/x/drivers/sht3x"
import ( import (
"machine"
"time" "time"
"tinygo.org/x/drivers"
) )
// Device wraps an I2C connection to a SHT31 device. // Device wraps an I2C connection to a SHT31 device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
} }
@@ -22,7 +23,7 @@ type Device struct {
// //
// This function only creates the Device object, it does not initialize the device. // This function only creates the Device object, it does not initialize the device.
// You must call Configure() first in order to use the device itself. // You must call Configure() first in order to use the device itself.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{ return Device{
bus: bus, bus: bus,
Address: AddressA, Address: AddressA,
+4 -2
View File
@@ -9,6 +9,8 @@ import (
"image/color" "image/color"
"machine" "machine"
"time" "time"
"tinygo.org/x/drivers"
) )
// Device wraps an SPI connection. // Device wraps an SPI connection.
@@ -30,7 +32,7 @@ type Config struct {
} }
type I2CBus struct { type I2CBus struct {
wire machine.I2C wire drivers.I2C
Address uint16 Address uint16
} }
@@ -50,7 +52,7 @@ type Buser interface {
type VccMode uint8 type VccMode uint8
// NewI2C creates a new SSD1306 connection. The I2C wire must already be configured. // NewI2C creates a new SSD1306 connection. The I2C wire must already be configured.
func NewI2C(bus machine.I2C) Device { func NewI2C(bus drivers.I2C) Device {
return Device{ return Device{
bus: &I2CBus{ bus: &I2CBus{
wire: bus, wire: bus,
+3 -5
View File
@@ -4,13 +4,11 @@
package tmp102 // import "tinygo.org/x/drivers/tmp102" package tmp102 // import "tinygo.org/x/drivers/tmp102"
import ( import "tinygo.org/x/drivers"
"machine"
)
// Device holds the already configured I2C bus and the address of the sensor. // Device holds the already configured I2C bus and the address of the sensor.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
address uint8 address uint8
} }
@@ -20,7 +18,7 @@ type Config struct {
} }
// New creates a new TMP102 connection. The I2C bus must already be configured. // New creates a new TMP102 connection. The I2C bus must already be configured.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{ return Device{
bus: bus, bus: bus,
} }
+4 -4
View File
@@ -11,12 +11,12 @@ package veml6070 // import "tinygo.org/x/drivers/veml6070"
import ( import (
"time" "time"
"machine" "tinygo.org/x/drivers"
) )
// Device wraps an I2C connection to a VEML6070 device. // Device wraps an I2C connection to a VEML6070 device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
AddressLow uint16 AddressLow uint16
AddressHigh uint16 AddressHigh uint16
RSET uint32 RSET uint32
@@ -28,7 +28,7 @@ type Device struct {
// //
// This function only creates the Device object, it does not initialize the device. // This function only creates the Device object, it does not initialize the device.
// You must call Configure() first in order to use the device itself. // You must call Configure() first in order to use the device itself.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{ return Device{
bus: bus, bus: bus,
AddressLow: ADDR_L, AddressLow: ADDR_L,
@@ -119,7 +119,7 @@ func (d *Device) enable() error {
func (d *Device) readData(address uint16) (byte, error) { func (d *Device) readData(address uint16) (byte, error) {
data := []byte{0} data := []byte{0}
err := machine.I2C0.Tx(address, []byte{}, data) err := d.bus.Tx(address, []byte{}, data)
return data[0], err return data[0], err
} }
+4 -3
View File
@@ -10,8 +10,9 @@
package vl53l1x // import "tinygo.org/x/drivers/vl53l1x" package vl53l1x // import "tinygo.org/x/drivers/vl53l1x"
import ( import (
"machine"
"time" "time"
"tinygo.org/x/drivers"
) )
type DistanceMode uint8 type DistanceMode uint8
@@ -35,7 +36,7 @@ type resultBuffer struct {
// Device wraps an I2C connection to a VL53L1X device. // Device wraps an I2C connection to a VL53L1X device.
type Device struct { type Device struct {
bus machine.I2C bus drivers.I2C
Address uint16 Address uint16
mode DistanceMode mode DistanceMode
timeout uint32 timeout uint32
@@ -52,7 +53,7 @@ type Device struct {
// configured. // configured.
// //
// This function only creates the Device object, it does not touch the device. // This function only creates the Device object, it does not touch the device.
func New(bus machine.I2C) Device { func New(bus drivers.I2C) Device {
return Device{ return Device{
bus: bus, bus: bus,
Address: Address, Address: Address,