88 "errors"
99
1010 "tinygo.org/x/drivers"
11- "tinygo.org/x/drivers/internal/legacy"
1211)
1312
1413type AccelRange uint8
@@ -26,7 +25,7 @@ type Device struct {
2625 accelSampleRate AccelSampleRate
2726 gyroRange GyroRange
2827 gyroSampleRate GyroSampleRate
29- buf [6 ]uint8
28+ buf [7 ]uint8 // up to 6 bytes for read + 1 byte for the register address
3029}
3130
3231// Configuration for LSM6DS3TR device.
@@ -84,30 +83,20 @@ func (d *Device) doConfigure(cfg Configuration) (err error) {
8483 d .gyroSampleRate = GYRO_SR_104
8584 }
8685
87- data := d .buf [:1 ]
88-
8986 // 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 ))
9288 if err != nil {
9389 return
9490 }
9591
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 )
10494 if err != nil {
10595 return
10696 }
10797
10898 // 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 ))
111100 if err != nil {
112101 return
113102 }
@@ -118,8 +107,10 @@ func (d *Device) doConfigure(cfg Configuration) (err error) {
118107// Connected returns whether a LSM6DS3TR has been found.
119108// It does a "who am I" request and checks the response.
120109func (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+ }
123114 return data [0 ] == 0x6A
124115}
125116
@@ -128,8 +119,7 @@ func (d *Device) Connected() bool {
128119// and the sensor is not moving the returned value will be around 1000000 or
129120// -1000000.
130121func (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 )
133123 if err != nil {
134124 return
135125 }
@@ -153,8 +143,7 @@ func (d *Device) ReadAcceleration() (x, y, z int32, err error) {
153143// rotation along one axis and while doing so integrate all values over time,
154144// you would get a value close to 360000000.
155145func (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 )
158147 if err != nil {
159148 return
160149 }
@@ -177,8 +166,7 @@ func (d *Device) ReadRotation() (x, y, z int32, err error) {
177166
178167// ReadTemperature returns the temperature in celsius milli degrees (°C/1000)
179168func (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 )
182170 if err != nil {
183171 return
184172 }
@@ -187,3 +175,26 @@ func (d *Device) ReadTemperature() (t int32, err error) {
187175 t = 25000 + (int32 (int16 ((int16 (data [1 ])<< 8 )| int16 (data [0 ])))* 125 )/ 32
188176 return
189177}
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