@@ -84,31 +84,19 @@ func (d *Device) doConfigure(cfg Configuration) (err error) {
8484 }
8585
8686 // Configure accelerometer
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 )
87+ err = d .writeValue (CTRL1_XL , uint8 (d .accelRange )| uint8 (d .accelSampleRate ))
9088 if err != nil {
9189 return
9290 }
9391
94- // Set ODR bit
95- d .buf [0 ] = CTRL4_C
96- err = d .bus .Tx (d .Address , d .buf [0 :1 ], d .buf [1 :2 ])
97- if err != nil {
98- return
99- }
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 )
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- 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 )
99+ err = d .writeValue (CTRL2_G , uint8 (d .gyroRange )| uint8 (d .gyroSampleRate ))
112100 if err != nil {
113101 return
114102 }
@@ -119,8 +107,10 @@ func (d *Device) doConfigure(cfg Configuration) (err error) {
119107// Connected returns whether a LSM6DS3TR has been found.
120108// It does a "who am I" request and checks the response.
121109func (d * Device ) Connected () bool {
122- d .buf [0 ] = WHO_AM_I
123- d .bus .Tx (d .Address , d .buf [0 :1 ], d .buf [1 :2 ])
110+ err := d .readValue (WHO_AM_I , 1 )
111+ if err != nil {
112+ return false
113+ }
124114 return d .buf [1 ] == 0x6A
125115}
126116
@@ -129,8 +119,7 @@ func (d *Device) Connected() bool {
129119// and the sensor is not moving the returned value will be around 1000000 or
130120// -1000000.
131121func (d * Device ) ReadAcceleration () (x , y , z int32 , err error ) {
132- d .buf [0 ] = OUTX_L_XL
133- err = d .bus .Tx (d .Address , d .buf [0 :1 ], d .buf [1 :7 ])
122+ err = d .readValue (OUTX_L_XL , 6 )
134123 if err != nil {
135124 return
136125 }
@@ -154,8 +143,7 @@ func (d *Device) ReadAcceleration() (x, y, z int32, err error) {
154143// rotation along one axis and while doing so integrate all values over time,
155144// you would get a value close to 360000000.
156145func (d * Device ) ReadRotation () (x , y , z int32 , err error ) {
157- d .buf [0 ] = OUTX_L_G
158- err = d .bus .Tx (d .Address , d .buf [0 :1 ], d .buf [1 :7 ])
146+ err = d .readValue (OUTX_L_G , 6 )
159147 if err != nil {
160148 return
161149 }
@@ -178,8 +166,7 @@ func (d *Device) ReadRotation() (x, y, z int32, err error) {
178166
179167// ReadTemperature returns the temperature in celsius milli degrees (°C/1000)
180168func (d * Device ) ReadTemperature () (t int32 , err error ) {
181- d .buf [0 ] = OUT_TEMP_L
182- err = d .bus .Tx (d .Address , d .buf [0 :1 ], d .buf [1 :3 ])
169+ err = d .readValue (OUT_TEMP_L , 2 )
183170 if err != nil {
184171 return
185172 }
@@ -188,3 +175,22 @@ func (d *Device) ReadTemperature() (t int32, err error) {
188175 t = 25000 + (int32 (int16 ((int16 (d .buf [2 ])<< 8 )| int16 (d .buf [1 ])))* 125 )/ 32
189176 return
190177}
178+
179+ func (d * Device ) readValue (reg , size uint8 ) error {
180+ d .buf [0 ] = reg
181+ return d .bus .Tx (d .Address , d .buf [0 :1 ], d .buf [1 :size + 1 ])
182+ }
183+
184+ func (d * Device ) writeValue (reg , value uint8 ) error {
185+ d .buf [0 ] = reg
186+ d .buf [1 ] = value
187+ return d .bus .Tx (d .Address , d .buf [0 :2 ], nil )
188+ }
189+
190+ func (d * Device ) setBits (reg , bits uint8 ) (err error ) {
191+ err = d .readValue (reg , 1 )
192+ if err != nil {
193+ return
194+ }
195+ return d .writeValue (reg , (d .buf [1 ]&^bits )| bits )
196+ }
0 commit comments