8
8
"errors"
9
9
10
10
"tinygo.org/x/drivers"
11
- "tinygo.org/x/drivers/internal/legacy"
12
11
)
13
12
14
13
type AccelRange uint8
@@ -26,7 +25,7 @@ type Device struct {
26
25
accelSampleRate AccelSampleRate
27
26
gyroRange GyroRange
28
27
gyroSampleRate GyroSampleRate
29
- buf [6 ]uint8
28
+ buf [7 ]uint8 // up to 6 bytes for read + 1 byte for the register address
30
29
}
31
30
32
31
// Configuration for LSM6DS3TR device.
@@ -84,30 +83,20 @@ func (d *Device) doConfigure(cfg Configuration) (err error) {
84
83
d .gyroSampleRate = GYRO_SR_104
85
84
}
86
85
87
- data := d .buf [:1 ]
88
-
89
86
// Configure accelerometer
90
- data [0 ] = uint8 (d .accelRange ) | uint8 (d .accelSampleRate )
91
- err = legacy .WriteRegister (d .bus , uint8 (d .Address ), CTRL1_XL , data )
87
+ err = d .writeByte (CTRL1_XL , uint8 (d .accelRange )| uint8 (d .accelSampleRate ))
92
88
if err != nil {
93
89
return
94
90
}
95
91
96
- // Set ODR bit
97
- err = legacy .ReadRegister (d .bus , uint8 (d .Address ), CTRL4_C , data )
98
- if err != nil {
99
- return
100
- }
101
- data [0 ] = data [0 ] &^ BW_SCAL_ODR_ENABLED
102
- data [0 ] |= BW_SCAL_ODR_ENABLED
103
- err = legacy .WriteRegister (d .bus , uint8 (d .Address ), CTRL4_C , data )
92
+ // Enable ODR scaling
93
+ err = d .setBits (CTRL4_C , BW_SCAL_ODR_ENABLED )
104
94
if err != nil {
105
95
return
106
96
}
107
97
108
98
// Configure gyroscope
109
- data [0 ] = uint8 (d .gyroRange ) | uint8 (d .gyroSampleRate )
110
- err = legacy .WriteRegister (d .bus , uint8 (d .Address ), CTRL2_G , data )
99
+ err = d .writeByte (CTRL2_G , uint8 (d .gyroRange )| uint8 (d .gyroSampleRate ))
111
100
if err != nil {
112
101
return
113
102
}
@@ -118,8 +107,10 @@ func (d *Device) doConfigure(cfg Configuration) (err error) {
118
107
// Connected returns whether a LSM6DS3TR has been found.
119
108
// It does a "who am I" request and checks the response.
120
109
func (d * Device ) Connected () bool {
121
- data := d .buf [:1 ]
122
- legacy .ReadRegister (d .bus , uint8 (d .Address ), WHO_AM_I , data )
110
+ data , err := d .readBytes (WHO_AM_I , 1 )
111
+ if err != nil {
112
+ return false
113
+ }
123
114
return data [0 ] == 0x6A
124
115
}
125
116
@@ -128,8 +119,7 @@ func (d *Device) Connected() bool {
128
119
// and the sensor is not moving the returned value will be around 1000000 or
129
120
// -1000000.
130
121
func (d * Device ) ReadAcceleration () (x , y , z int32 , err error ) {
131
- data := d .buf [:6 ]
132
- err = legacy .ReadRegister (d .bus , uint8 (d .Address ), OUTX_L_XL , data )
122
+ data , err := d .readBytes (OUTX_L_XL , 6 )
133
123
if err != nil {
134
124
return
135
125
}
@@ -153,8 +143,7 @@ func (d *Device) ReadAcceleration() (x, y, z int32, err error) {
153
143
// rotation along one axis and while doing so integrate all values over time,
154
144
// you would get a value close to 360000000.
155
145
func (d * Device ) ReadRotation () (x , y , z int32 , err error ) {
156
- data := d .buf [:6 ]
157
- err = legacy .ReadRegister (d .bus , uint8 (d .Address ), OUTX_L_G , data )
146
+ data , err := d .readBytes (OUTX_L_G , 6 )
158
147
if err != nil {
159
148
return
160
149
}
@@ -177,8 +166,7 @@ func (d *Device) ReadRotation() (x, y, z int32, err error) {
177
166
178
167
// ReadTemperature returns the temperature in celsius milli degrees (°C/1000)
179
168
func (d * Device ) ReadTemperature () (t int32 , err error ) {
180
- data := d .buf [:2 ]
181
- err = legacy .ReadRegister (d .bus , uint8 (d .Address ), OUT_TEMP_L , data )
169
+ data , err := d .readBytes (OUT_TEMP_L , 2 )
182
170
if err != nil {
183
171
return
184
172
}
@@ -187,3 +175,26 @@ func (d *Device) ReadTemperature() (t int32, err error) {
187
175
t = 25000 + (int32 (int16 ((int16 (data [1 ])<< 8 )| int16 (data [0 ])))* 125 )/ 32
188
176
return
189
177
}
178
+
179
+ func (d * Device ) readBytes (reg , size uint8 ) ([]byte , error ) {
180
+ d .buf [0 ] = reg
181
+ err := d .bus .Tx (d .Address , d .buf [0 :1 ], d .buf [1 :size + 1 ])
182
+ if err != nil {
183
+ return nil , err
184
+ }
185
+ return d .buf [1 : size + 1 ], nil
186
+ }
187
+
188
+ func (d * Device ) writeByte (reg , value uint8 ) error {
189
+ d .buf [0 ] = reg
190
+ d .buf [1 ] = value
191
+ return d .bus .Tx (d .Address , d .buf [0 :2 ], nil )
192
+ }
193
+
194
+ func (d * Device ) setBits (reg , bits uint8 ) error {
195
+ data , err := d .readBytes (reg , 1 )
196
+ if err != nil {
197
+ return err
198
+ }
199
+ return d .writeByte (reg , (data [0 ]&^bits )| bits )
200
+ }
0 commit comments