Skip to content

Commit e6f82fa

Browse files
soypatdeadprogram
authored andcommitted
i2c iface refactor: Resolve 559
1 parent e20c6d0 commit e6f82fa

File tree

37 files changed

+313
-247
lines changed

37 files changed

+313
-247
lines changed

adt7410/adt7410.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"tinygo.org/x/drivers"
10+
"tinygo.org/x/drivers/internal/legacy"
1011
)
1112

1213
type Error uint8
@@ -54,7 +55,7 @@ func (d *Device) Configure() (err error) {
5455
// Connected returns whether sensor has been found.
5556
func (d *Device) Connected() bool {
5657
data := []byte{0}
57-
d.bus.ReadRegister(uint8(d.Address), RegID, data)
58+
legacy.ReadRegister(d.bus, uint8(d.Address), RegID, data)
5859
return data[0]&0xF8 == 0xC8
5960
}
6061

@@ -81,11 +82,11 @@ func (d *Device) writeByte(reg uint8, data byte) {
8182
}
8283

8384
func (d *Device) readByte(reg uint8) byte {
84-
d.bus.ReadRegister(d.Address, reg, d.buf)
85+
legacy.ReadRegister(d.bus, d.Address, reg, d.buf)
8586
return d.buf[0]
8687
}
8788

8889
func (d *Device) readUint16(reg uint8) uint16 {
89-
d.bus.ReadRegister(d.Address, reg, d.buf)
90+
legacy.ReadRegister(d.bus, d.Address, reg, d.buf)
9091
return uint16(d.buf[0])<<8 | uint16(d.buf[1])
9192
}

adxl345/adxl345.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
// Datasheet JP: http://www.analog.com/media/jp/technical-documentation/data-sheets/ADXL345_jp.pdf
66
package adxl345 // import "tinygo.org/x/drivers/adxl345"
77

8-
import "tinygo.org/x/drivers"
8+
import (
9+
"tinygo.org/x/drivers"
10+
"tinygo.org/x/drivers/internal/legacy"
11+
)
912

1013
type Range uint8
1114
type Rate uint8
@@ -68,21 +71,21 @@ func New(bus drivers.I2C) Device {
6871

6972
// Configure sets up the device for communication
7073
func (d *Device) Configure() {
71-
d.bus.WriteRegister(uint8(d.Address), REG_BW_RATE, []byte{d.bwRate.toByte()})
72-
d.bus.WriteRegister(uint8(d.Address), REG_POWER_CTL, []byte{d.powerCtl.toByte()})
73-
d.bus.WriteRegister(uint8(d.Address), REG_DATA_FORMAT, []byte{d.dataFormat.toByte()})
74+
legacy.WriteRegister(d.bus, uint8(d.Address), REG_BW_RATE, []byte{d.bwRate.toByte()})
75+
legacy.WriteRegister(d.bus, uint8(d.Address), REG_POWER_CTL, []byte{d.powerCtl.toByte()})
76+
legacy.WriteRegister(d.bus, uint8(d.Address), REG_DATA_FORMAT, []byte{d.dataFormat.toByte()})
7477
}
7578

7679
// Halt stops the sensor, values will not updated
7780
func (d *Device) Halt() {
7881
d.powerCtl.measure = 0
79-
d.bus.WriteRegister(uint8(d.Address), REG_POWER_CTL, []byte{d.powerCtl.toByte()})
82+
legacy.WriteRegister(d.bus, uint8(d.Address), REG_POWER_CTL, []byte{d.powerCtl.toByte()})
8083
}
8184

8285
// Restart makes reading the sensor working again after a halt
8386
func (d *Device) Restart() {
8487
d.powerCtl.measure = 1
85-
d.bus.WriteRegister(uint8(d.Address), REG_POWER_CTL, []byte{d.powerCtl.toByte()})
88+
legacy.WriteRegister(d.bus, uint8(d.Address), REG_POWER_CTL, []byte{d.powerCtl.toByte()})
8689
}
8790

8891
// ReadAcceleration reads the current acceleration from the device and returns
@@ -103,7 +106,7 @@ func (d *Device) ReadAcceleration() (x int32, y int32, z int32, err error) {
103106
// from the adxl345.
104107
func (d *Device) ReadRawAcceleration() (x int32, y int32, z int32) {
105108
data := []byte{0, 0, 0, 0, 0, 0}
106-
d.bus.ReadRegister(uint8(d.Address), REG_DATAX0, data)
109+
legacy.ReadRegister(d.bus, uint8(d.Address), REG_DATAX0, data)
107110

108111
x = readIntLE(data[0], data[1])
109112
y = readIntLE(data[2], data[3])
@@ -119,20 +122,20 @@ func (d *Device) UseLowPower(power bool) {
119122
} else {
120123
d.bwRate.lowPower = 0
121124
}
122-
d.bus.WriteRegister(uint8(d.Address), REG_BW_RATE, []byte{d.bwRate.toByte()})
125+
legacy.WriteRegister(d.bus, uint8(d.Address), REG_BW_RATE, []byte{d.bwRate.toByte()})
123126
}
124127

125128
// SetRate change the current rate of the sensor
126129
func (d *Device) SetRate(rate Rate) bool {
127130
d.bwRate.rate = rate & 0x0F
128-
d.bus.WriteRegister(uint8(d.Address), REG_BW_RATE, []byte{d.bwRate.toByte()})
131+
legacy.WriteRegister(d.bus, uint8(d.Address), REG_BW_RATE, []byte{d.bwRate.toByte()})
129132
return true
130133
}
131134

132135
// SetRange change the current range of the sensor
133136
func (d *Device) SetRange(sensorRange Range) bool {
134137
d.dataFormat.sensorRange = sensorRange & 0x03
135-
d.bus.WriteRegister(uint8(d.Address), REG_DATA_FORMAT, []byte{d.dataFormat.toByte()})
138+
legacy.WriteRegister(d.bus, uint8(d.Address), REG_DATA_FORMAT, []byte{d.dataFormat.toByte()})
136139
return true
137140
}
138141

amg88xx/amg88xx.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"tinygo.org/x/drivers"
11+
"tinygo.org/x/drivers/internal/legacy"
1112
)
1213

1314
// Device wraps an I2C connection to a AMG88xx device.
@@ -48,7 +49,7 @@ func (d *Device) Configure(cfg Config) {
4849

4950
// ReadPixels returns the 64 values (8x8 grid) of the sensor converted to millicelsius
5051
func (d *Device) ReadPixels(buffer *[64]int16) {
51-
d.bus.ReadRegister(uint8(d.Address), PIXEL_OFFSET, d.data)
52+
legacy.ReadRegister(d.bus, uint8(d.Address), PIXEL_OFFSET, d.data)
5253
for i := 0; i < 64; i++ {
5354
buffer[i] = int16((uint16(d.data[2*i+1]) << 8) | uint16(d.data[2*i]))
5455
if (buffer[i] & (1 << 11)) > 0 { // temperature negative
@@ -61,17 +62,17 @@ func (d *Device) ReadPixels(buffer *[64]int16) {
6162

6263
// SetPCTL sets the PCTL
6364
func (d *Device) SetPCTL(pctl uint8) {
64-
d.bus.WriteRegister(uint8(d.Address), PCTL, []byte{pctl})
65+
legacy.WriteRegister(d.bus, uint8(d.Address), PCTL, []byte{pctl})
6566
}
6667

6768
// SetReset sets the reset value
6869
func (d *Device) SetReset(rst uint8) {
69-
d.bus.WriteRegister(uint8(d.Address), RST, []byte{rst})
70+
legacy.WriteRegister(d.bus, uint8(d.Address), RST, []byte{rst})
7071
}
7172

7273
// SetFrameRate configures the frame rate
7374
func (d *Device) SetFrameRate(framerate uint8) {
74-
d.bus.WriteRegister(uint8(d.Address), FPSC, []byte{framerate & 0x01})
75+
legacy.WriteRegister(d.bus, uint8(d.Address), FPSC, []byte{framerate & 0x01})
7576
}
7677

7778
// SetMovingAverageMode sets the moving average mode
@@ -80,7 +81,7 @@ func (d *Device) SetMovingAverageMode(mode bool) {
8081
if mode {
8182
value = 1
8283
}
83-
d.bus.WriteRegister(uint8(d.Address), AVE, []byte{value << 5})
84+
legacy.WriteRegister(d.bus, uint8(d.Address), AVE, []byte{value << 5})
8485
}
8586

8687
// SetInterruptLevels sets the interrupt levels
@@ -97,8 +98,8 @@ func (d *Device) SetInterruptLevelsHysteresis(high int16, low int16, hysteresis
9798
if high > 4095 {
9899
high = 4095
99100
}
100-
d.bus.WriteRegister(uint8(d.Address), INTHL, []byte{uint8(high & 0xFF)})
101-
d.bus.WriteRegister(uint8(d.Address), INTHL, []byte{uint8((high & 0xFF) >> 4)})
101+
legacy.WriteRegister(d.bus, uint8(d.Address), INTHL, []byte{uint8(high & 0xFF)})
102+
legacy.WriteRegister(d.bus, uint8(d.Address), INTHL, []byte{uint8((high & 0xFF) >> 4)})
102103

103104
low = low / PIXEL_TEMP_CONVERSION
104105
if low < -4095 {
@@ -107,8 +108,8 @@ func (d *Device) SetInterruptLevelsHysteresis(high int16, low int16, hysteresis
107108
if low > 4095 {
108109
low = 4095
109110
}
110-
d.bus.WriteRegister(uint8(d.Address), INTHL, []byte{uint8(low & 0xFF)})
111-
d.bus.WriteRegister(uint8(d.Address), INTHL, []byte{uint8((low & 0xFF) >> 4)})
111+
legacy.WriteRegister(d.bus, uint8(d.Address), INTHL, []byte{uint8(low & 0xFF)})
112+
legacy.WriteRegister(d.bus, uint8(d.Address), INTHL, []byte{uint8((low & 0xFF) >> 4)})
112113

113114
hysteresis = hysteresis / PIXEL_TEMP_CONVERSION
114115
if hysteresis < -4095 {
@@ -117,32 +118,32 @@ func (d *Device) SetInterruptLevelsHysteresis(high int16, low int16, hysteresis
117118
if hysteresis > 4095 {
118119
hysteresis = 4095
119120
}
120-
d.bus.WriteRegister(uint8(d.Address), INTHL, []byte{uint8(hysteresis & 0xFF)})
121-
d.bus.WriteRegister(uint8(d.Address), INTHL, []byte{uint8((hysteresis & 0xFF) >> 4)})
121+
legacy.WriteRegister(d.bus, uint8(d.Address), INTHL, []byte{uint8(hysteresis & 0xFF)})
122+
legacy.WriteRegister(d.bus, uint8(d.Address), INTHL, []byte{uint8((hysteresis & 0xFF) >> 4)})
122123
}
123124

124125
// EnableInterrupt enables the interrupt pin on the device
125126
func (d *Device) EnableInterrupt() {
126127
d.interruptEnable = 1
127-
d.bus.WriteRegister(uint8(d.Address), INTC, []byte{((uint8(d.interruptMode) << 1) | d.interruptEnable) & 0x03})
128+
legacy.WriteRegister(d.bus, uint8(d.Address), INTC, []byte{((uint8(d.interruptMode) << 1) | d.interruptEnable) & 0x03})
128129
}
129130

130131
// DisableInterrupt disables the interrupt pin on the device
131132
func (d *Device) DisableInterrupt() {
132133
d.interruptEnable = 0
133-
d.bus.WriteRegister(uint8(d.Address), INTC, []byte{((uint8(d.interruptMode) << 1) | d.interruptEnable) & 0x03})
134+
legacy.WriteRegister(d.bus, uint8(d.Address), INTC, []byte{((uint8(d.interruptMode) << 1) | d.interruptEnable) & 0x03})
134135
}
135136

136137
// SetInterruptMode sets the interrupt mode
137138
func (d *Device) SetInterruptMode(mode InterruptMode) {
138139
d.interruptMode = mode
139-
d.bus.WriteRegister(uint8(d.Address), INTC, []byte{((uint8(d.interruptMode) << 1) | d.interruptEnable) & 0x03})
140+
legacy.WriteRegister(d.bus, uint8(d.Address), INTC, []byte{((uint8(d.interruptMode) << 1) | d.interruptEnable) & 0x03})
140141
}
141142

142143
// GetInterrupt reads the state of the triggered interrupts
143144
func (d *Device) GetInterrupt() []uint8 {
144145
data := make([]uint8, 8)
145-
d.bus.ReadRegister(uint8(d.Address), INT_OFFSET, data)
146+
legacy.ReadRegister(d.bus, uint8(d.Address), INT_OFFSET, data)
146147
return data
147148
}
148149

@@ -154,6 +155,6 @@ func (d *Device) ClearInterrupt() {
154155
// ReadThermistor reads the onboard thermistor
155156
func (d *Device) ReadThermistor() int16 {
156157
data := make([]uint8, 2)
157-
d.bus.ReadRegister(uint8(d.Address), TTHL, data)
158+
legacy.ReadRegister(d.bus, uint8(d.Address), TTHL, data)
158159
return (int16((uint16(data[1])<<8)|uint16(data[0])) * THERMISTOR_CONVERSION) / 10
159160
}

apds9960/apds9960.go

+26-25
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"tinygo.org/x/drivers"
11+
"tinygo.org/x/drivers/internal/legacy"
1112
)
1213

1314
// Device wraps an I2C connection to a APDS-9960 device.
@@ -68,7 +69,7 @@ func New(bus drivers.I2C) Device {
6869
// It does a "who am I" request and checks the response.
6970
func (d *Device) Connected() bool {
7071
data := []byte{0}
71-
d.bus.ReadRegister(d.Address, APDS9960_ID_REG, data)
72+
legacy.ReadRegister(d.bus, d.Address, APDS9960_ID_REG, data)
7273
return data[0] == 0xAB
7374
}
7475

@@ -80,21 +81,21 @@ func (d *Device) GetMode() uint8 {
8081
// DisableAll turns off the device and all functions
8182
func (d *Device) DisableAll() {
8283
d.enable(enableConfig{})
83-
d.bus.WriteRegister(d.Address, APDS9960_GCONF4_REG, []byte{0x00})
84+
legacy.WriteRegister(d.bus, d.Address, APDS9960_GCONF4_REG, []byte{0x00})
8485
d.mode = MODE_NONE
8586
d.gesture.detected = GESTURE_NONE
8687
}
8788

8889
// SetProximityPulse sets proximity pulse length (4, 8, 16, 32) and count (1~64)
8990
// default: 16, 64
9091
func (d *Device) SetProximityPulse(length, count uint8) {
91-
d.bus.WriteRegister(d.Address, APDS9960_PPULSE_REG, []byte{getPulseLength(length)<<6 | getPulseCount(count)})
92+
legacy.WriteRegister(d.bus, d.Address, APDS9960_PPULSE_REG, []byte{getPulseLength(length)<<6 | getPulseCount(count)})
9293
}
9394

9495
// SetGesturePulse sets gesture pulse length (4, 8, 16, 32) and count (1~64)
9596
// default: 16, 64
9697
func (d *Device) SetGesturePulse(length, count uint8) {
97-
d.bus.WriteRegister(d.Address, APDS9960_GPULSE_REG, []byte{getPulseLength(length)<<6 | getPulseCount(count)})
98+
legacy.WriteRegister(d.bus, d.Address, APDS9960_GPULSE_REG, []byte{getPulseLength(length)<<6 | getPulseCount(count)})
9899
}
99100

100101
// SetADCIntegrationCycles sets ALS/color ADC internal integration cycles (1~256, 1 cycle = 2.78 ms)
@@ -103,14 +104,14 @@ func (d *Device) SetADCIntegrationCycles(cycles uint16) {
103104
if cycles > 256 {
104105
cycles = 256
105106
}
106-
d.bus.WriteRegister(d.Address, APDS9960_ATIME_REG, []byte{uint8(256 - cycles)})
107+
legacy.WriteRegister(d.bus, d.Address, APDS9960_ATIME_REG, []byte{uint8(256 - cycles)})
107108
}
108109

109110
// SetGains sets proximity/gesture gain (1, 2, 4, 8x) and ALS/color gain (1, 4, 16, 64x)
110111
// default: 1, 1, 4
111112
func (d *Device) SetGains(proximityGain, gestureGain, colorGain uint8) {
112-
d.bus.WriteRegister(d.Address, APDS9960_CONTROL_REG, []byte{getProximityGain(proximityGain)<<2 | getALSGain(colorGain)})
113-
d.bus.WriteRegister(d.Address, APDS9960_GCONF2_REG, []byte{getProximityGain(gestureGain) << 5})
113+
legacy.WriteRegister(d.bus, d.Address, APDS9960_CONTROL_REG, []byte{getProximityGain(proximityGain)<<2 | getALSGain(colorGain)})
114+
legacy.WriteRegister(d.bus, d.Address, APDS9960_GCONF2_REG, []byte{getProximityGain(gestureGain) << 5})
114115
}
115116

116117
// LEDBoost sets proximity and gesture LED current level (100, 150, 200, 300 (%))
@@ -127,7 +128,7 @@ func (d *Device) LEDBoost(percent uint16) {
127128
case 300:
128129
v = 3
129130
}
130-
d.bus.WriteRegister(d.Address, APDS9960_CONFIG2_REG, []byte{0x01 | v<<4})
131+
legacy.WriteRegister(d.bus, d.Address, APDS9960_CONFIG2_REG, []byte{0x01 | v<<4})
131132
}
132133

133134
// Setthreshold sets threshold (0~255) for detecting gestures
@@ -168,7 +169,7 @@ func (d *Device) ReadProximity() (proximity int32) {
168169
return 0
169170
}
170171
data := []byte{0}
171-
d.bus.ReadRegister(d.Address, APDS9960_PDATA_REG, data)
172+
legacy.ReadRegister(d.bus, d.Address, APDS9960_PDATA_REG, data)
172173
return 255 - int32(data[0])
173174
}
174175

@@ -195,14 +196,14 @@ func (d *Device) ReadColor() (r int32, g int32, b int32, clear int32) {
195196
return
196197
}
197198
data := []byte{0, 0, 0, 0, 0, 0, 0, 0}
198-
d.bus.ReadRegister(d.Address, APDS9960_CDATAL_REG, data[:1])
199-
d.bus.ReadRegister(d.Address, APDS9960_CDATAH_REG, data[1:2])
200-
d.bus.ReadRegister(d.Address, APDS9960_RDATAL_REG, data[2:3])
201-
d.bus.ReadRegister(d.Address, APDS9960_RDATAH_REG, data[3:4])
202-
d.bus.ReadRegister(d.Address, APDS9960_GDATAL_REG, data[4:5])
203-
d.bus.ReadRegister(d.Address, APDS9960_GDATAH_REG, data[5:6])
204-
d.bus.ReadRegister(d.Address, APDS9960_BDATAL_REG, data[6:7])
205-
d.bus.ReadRegister(d.Address, APDS9960_BDATAH_REG, data[7:])
199+
legacy.ReadRegister(d.bus, d.Address, APDS9960_CDATAL_REG, data[:1])
200+
legacy.ReadRegister(d.bus, d.Address, APDS9960_CDATAH_REG, data[1:2])
201+
legacy.ReadRegister(d.bus, d.Address, APDS9960_RDATAL_REG, data[2:3])
202+
legacy.ReadRegister(d.bus, d.Address, APDS9960_RDATAH_REG, data[3:4])
203+
legacy.ReadRegister(d.bus, d.Address, APDS9960_GDATAL_REG, data[4:5])
204+
legacy.ReadRegister(d.bus, d.Address, APDS9960_GDATAH_REG, data[5:6])
205+
legacy.ReadRegister(d.bus, d.Address, APDS9960_BDATAL_REG, data[6:7])
206+
legacy.ReadRegister(d.bus, d.Address, APDS9960_BDATAH_REG, data[7:])
206207
clear = int32(uint16(data[1])<<8 | uint16(data[0]))
207208
r = int32(uint16(data[3])<<8 | uint16(data[2]))
208209
g = int32(uint16(data[5])<<8 | uint16(data[4]))
@@ -234,13 +235,13 @@ func (d *Device) GestureAvailable() bool {
234235
data := []byte{0, 0, 0, 0}
235236

236237
// check GVALID
237-
d.bus.ReadRegister(d.Address, APDS9960_GSTATUS_REG, data[:1])
238+
legacy.ReadRegister(d.bus, d.Address, APDS9960_GSTATUS_REG, data[:1])
238239
if data[0]&0x01 == 0 {
239240
return false
240241
}
241242

242243
// get number of data sets available in FIFO
243-
d.bus.ReadRegister(d.Address, APDS9960_GFLVL_REG, data[:1])
244+
legacy.ReadRegister(d.bus, d.Address, APDS9960_GFLVL_REG, data[:1])
244245
availableDataSets := data[0]
245246
if availableDataSets == 0 {
246247
return false
@@ -249,10 +250,10 @@ func (d *Device) GestureAvailable() bool {
249250
// read up, down, left and right proximity data from FIFO
250251
var dataSets [32][4]uint8
251252
for i := uint8(0); i < availableDataSets; i++ {
252-
d.bus.ReadRegister(d.Address, APDS9960_GFIFO_U_REG, data[:1])
253-
d.bus.ReadRegister(d.Address, APDS9960_GFIFO_D_REG, data[1:2])
254-
d.bus.ReadRegister(d.Address, APDS9960_GFIFO_L_REG, data[2:3])
255-
d.bus.ReadRegister(d.Address, APDS9960_GFIFO_R_REG, data[3:4])
253+
legacy.ReadRegister(d.bus, d.Address, APDS9960_GFIFO_U_REG, data[:1])
254+
legacy.ReadRegister(d.bus, d.Address, APDS9960_GFIFO_D_REG, data[1:2])
255+
legacy.ReadRegister(d.bus, d.Address, APDS9960_GFIFO_L_REG, data[2:3])
256+
legacy.ReadRegister(d.bus, d.Address, APDS9960_GFIFO_R_REG, data[3:4])
256257
for j := uint8(0); j < 4; j++ {
257258
dataSets[i][j] = data[j]
258259
}
@@ -385,7 +386,7 @@ func (d *Device) enable(cfg enableConfig) {
385386
}
386387

387388
data := []byte{gen<<6 | pien<<5 | aien<<4 | wen<<3 | pen<<2 | aen<<1 | pon}
388-
d.bus.WriteRegister(d.Address, APDS9960_ENABLE_REG, data)
389+
legacy.WriteRegister(d.bus, d.Address, APDS9960_ENABLE_REG, data)
389390

390391
if cfg.PON {
391392
time.Sleep(time.Millisecond * 10)
@@ -394,7 +395,7 @@ func (d *Device) enable(cfg enableConfig) {
394395

395396
func (d *Device) readStatus(param string) bool {
396397
data := []byte{0}
397-
d.bus.ReadRegister(d.Address, APDS9960_STATUS_REG, data)
398+
legacy.ReadRegister(d.bus, d.Address, APDS9960_STATUS_REG, data)
398399

399400
switch param {
400401
case "CPSAT":

as560x/i2c_register.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66

77
"tinygo.org/x/drivers"
8+
"tinygo.org/x/drivers/internal/legacy"
89
)
910

1011
// registerAttributes is a bitfield of attributes for a register
@@ -94,7 +95,7 @@ func (r *i2cRegister) readShiftAndMask(bus drivers.I2C, deviceAddress uint8, shi
9495
buf = buffer[:]
9596
}
9697
// Read the host register over I2C
97-
err := bus.ReadRegister(deviceAddress, r.host.address, buf)
98+
err := legacy.ReadRegister(bus, deviceAddress, r.host.address, buf)
9899
if nil != err {
99100
return 0, err
100101
}

0 commit comments

Comments
 (0)