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 // cmd + up to 6 bytes of data
3029}
3130
3231// Configuration for LSM6DS3TR device.
@@ -84,30 +83,32 @@ 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+ d .buf [0 ] = CTRL1_XL
88+ d .buf [1 ] = uint8 (d .accelRange ) | uint8 (d .accelSampleRate )
89+ err = d .bus .Tx (d .Address , d .buf [0 :2 ], nil )
9290 if err != nil {
9391 return
9492 }
9593
9694 // Set ODR bit
97- err = legacy .ReadRegister (d .bus , uint8 (d .Address ), CTRL4_C , data )
95+ d .buf [0 ] = CTRL4_C
96+ err = d .bus .Tx (d .Address , d .buf [0 :1 ], d .buf [1 :2 ])
9897 if err != nil {
9998 return
10099 }
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 )
100+ d .buf [0 ] = CTRL4_C
101+ d .buf [1 ] = d .buf [1 ] &^ BW_SCAL_ODR_ENABLED
102+ d .buf [1 ] |= BW_SCAL_ODR_ENABLED
103+ err = d .bus .Tx (d .Address , d .buf [0 :2 ], nil )
104104 if err != nil {
105105 return
106106 }
107107
108108 // Configure gyroscope
109- data [0 ] = uint8 (d .gyroRange ) | uint8 (d .gyroSampleRate )
110- err = legacy .WriteRegister (d .bus , uint8 (d .Address ), CTRL2_G , data )
109+ d .buf [0 ] = CTRL2_G
110+ d .buf [1 ] = uint8 (d .gyroRange ) | uint8 (d .gyroSampleRate )
111+ err = d .bus .Tx (d .Address , d .buf [0 :2 ], nil )
111112 if err != nil {
112113 return
113114 }
@@ -118,18 +119,18 @@ func (d *Device) doConfigure(cfg Configuration) (err error) {
118119// Connected returns whether a LSM6DS3TR has been found.
119120// It does a "who am I" request and checks the response.
120121func (d * Device ) Connected () bool {
121- data := d .buf [: 1 ]
122- legacy . ReadRegister ( d .bus , uint8 (d .Address ), WHO_AM_I , data )
123- return data [ 0 ] == 0x6A
122+ d .buf [0 ] = WHO_AM_I
123+ d .bus . Tx (d .Address , d . buf [ 0 : 1 ], d . buf [ 1 : 2 ] )
124+ return d . buf [ 1 ] == 0x6A
124125}
125126
126127// ReadAcceleration reads the current acceleration from the device and returns
127128// it in µg (micro-gravity). When one of the axes is pointing straight to Earth
128129// and the sensor is not moving the returned value will be around 1000000 or
129130// -1000000.
130131func (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 )
132+ d .buf [0 ] = OUTX_L_XL
133+ err = d .bus . Tx (d .Address , d . buf [ 0 : 1 ], d . buf [ 1 : 7 ] )
133134 if err != nil {
134135 return
135136 }
@@ -142,9 +143,9 @@ func (d *Device) ReadAcceleration() (x, y, z int32, err error) {
142143 } else if d .accelRange == ACCEL_16G {
143144 k = 488
144145 }
145- x = int32 (int16 ((uint16 (data [ 1 ])<< 8 )| uint16 (data [ 0 ]))) * k
146- y = int32 (int16 ((uint16 (data [ 3 ])<< 8 )| uint16 (data [ 2 ]))) * k
147- z = int32 (int16 ((uint16 (data [ 5 ])<< 8 )| uint16 (data [ 4 ]))) * k
146+ x = int32 (int16 ((uint16 (d . buf [ 2 ])<< 8 )| uint16 (d . buf [ 1 ]))) * k
147+ y = int32 (int16 ((uint16 (d . buf [ 4 ])<< 8 )| uint16 (d . buf [ 3 ]))) * k
148+ z = int32 (int16 ((uint16 (d . buf [ 6 ])<< 8 )| uint16 (d . buf [ 5 ]))) * k
148149 return
149150}
150151
@@ -153,8 +154,8 @@ func (d *Device) ReadAcceleration() (x, y, z int32, err error) {
153154// rotation along one axis and while doing so integrate all values over time,
154155// you would get a value close to 360000000.
155156func (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 )
157+ d .buf [0 ] = OUTX_L_G
158+ err = d .bus . Tx (d .Address , d . buf [ 0 : 1 ], d . buf [ 1 : 7 ] )
158159 if err != nil {
159160 return
160161 }
@@ -169,21 +170,21 @@ func (d *Device) ReadRotation() (x, y, z int32, err error) {
169170 } else if d .gyroRange == GYRO_2000DPS {
170171 k = 70000
171172 }
172- x = int32 (int16 ((uint16 (data [ 1 ])<< 8 )| uint16 (data [ 0 ]))) * k
173- y = int32 (int16 ((uint16 (data [ 3 ])<< 8 )| uint16 (data [ 2 ]))) * k
174- z = int32 (int16 ((uint16 (data [ 5 ])<< 8 )| uint16 (data [ 4 ]))) * k
173+ x = int32 (int16 ((uint16 (d . buf [ 2 ])<< 8 )| uint16 (d . buf [ 1 ]))) * k
174+ y = int32 (int16 ((uint16 (d . buf [ 4 ])<< 8 )| uint16 (d . buf [ 3 ]))) * k
175+ z = int32 (int16 ((uint16 (d . buf [ 6 ])<< 8 )| uint16 (d . buf [ 5 ]))) * k
175176 return
176177}
177178
178179// ReadTemperature returns the temperature in celsius milli degrees (°C/1000)
179180func (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 )
181+ d .buf [0 ] = OUT_TEMP_L
182+ err = d .bus . Tx (d .Address , d . buf [ 0 : 1 ], d . buf [ 1 : 3 ] )
182183 if err != nil {
183184 return
184185 }
185186 // From "Table 5. Temperature sensor characteristics"
186187 // temp = value/256 + 25
187- t = 25000 + (int32 (int16 ((int16 (data [ 1 ])<< 8 )| int16 (data [ 0 ])))* 125 )/ 32
188+ t = 25000 + (int32 (int16 ((int16 (d . buf [ 2 ])<< 8 )| int16 (d . buf [ 1 ])))* 125 )/ 32
188189 return
189190}
0 commit comments