@@ -84,31 +84,19 @@ func (d *Device) doConfigure(cfg Configuration) (err error) {
84
84
}
85
85
86
86
// 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 ))
90
88
if err != nil {
91
89
return
92
90
}
93
91
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 )
104
94
if err != nil {
105
95
return
106
96
}
107
97
108
98
// 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 ))
112
100
if err != nil {
113
101
return
114
102
}
@@ -119,8 +107,10 @@ func (d *Device) doConfigure(cfg Configuration) (err error) {
119
107
// Connected returns whether a LSM6DS3TR has been found.
120
108
// It does a "who am I" request and checks the response.
121
109
func (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
+ }
124
114
return d .buf [1 ] == 0x6A
125
115
}
126
116
@@ -129,8 +119,7 @@ func (d *Device) Connected() bool {
129
119
// and the sensor is not moving the returned value will be around 1000000 or
130
120
// -1000000.
131
121
func (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 )
134
123
if err != nil {
135
124
return
136
125
}
@@ -154,8 +143,7 @@ func (d *Device) ReadAcceleration() (x, y, z int32, err error) {
154
143
// rotation along one axis and while doing so integrate all values over time,
155
144
// you would get a value close to 360000000.
156
145
func (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 )
159
147
if err != nil {
160
148
return
161
149
}
@@ -178,8 +166,7 @@ func (d *Device) ReadRotation() (x, y, z int32, err error) {
178
166
179
167
// ReadTemperature returns the temperature in celsius milli degrees (°C/1000)
180
168
func (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 )
183
170
if err != nil {
184
171
return
185
172
}
@@ -188,3 +175,22 @@ func (d *Device) ReadTemperature() (t int32, err error) {
188
175
t = 25000 + (int32 (int16 ((int16 (d .buf [2 ])<< 8 )| int16 (d .buf [1 ])))* 125 )/ 32
189
176
return
190
177
}
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