Skip to content

Commit 6288ba5

Browse files
committed
make zero value useful for configuration
1 parent d675bd4 commit 6288ba5

File tree

3 files changed

+12
-19
lines changed

3 files changed

+12
-19
lines changed

examples/mpu6050/main.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ func main() {
1313

1414
mpuDevice := mpu6050.New(machine.I2C0, mpu6050.DefaultAddress)
1515

16-
err := mpuDevice.Configure(mpu6050.Config{
17-
AccelRange: mpu6050.ACCEL_RANGE_16,
18-
GyroRange: mpu6050.GYRO_RANGE_2000,
19-
})
16+
// Configure the device with default configuration.
17+
err := mpuDevice.Configure(mpu6050.Config{})
2018
if err != nil {
2119
panic(err.Error())
2220
}

mpu6050/mpu6050.go

+6-13
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,13 @@ func (p *Device) setRangeGyro(gyroRange RangeGyro) (err error) {
167167
p.gRange = 500
168168
case RangeGyro1000:
169169
p.gRange = 1000
170-
case RangeGyro2000:
170+
case RangeGyro2000, rangeGyroDefault:
171+
gyroRange = RangeGyro2000
171172
p.gRange = 2000
172173
default:
173174
return errInvalidRangeGyro
174175
}
175-
return p.writeMasked(_GYRO_CONFIG, _G_FS_SEL, uint8(gyroRange)<<_G_FS_SHIFT)
176+
return p.writeMasked(_GYRO_CONFIG, _G_FS_SEL, uint8(gyroRange-1)<<_G_FS_SHIFT)
176177
}
177178

178179
// setRangeAccel configures the full scale range of the accelerometer.
@@ -187,12 +188,13 @@ func (p *Device) setRangeAccel(accRange RangeAccel) (err error) {
187188
p.aRange = 4
188189
case RangeAccel8:
189190
p.aRange = 8
190-
case RangeAccel16:
191+
case RangeAccel16, rangeAccelDefault:
192+
accRange = RangeAccel16
191193
p.aRange = 16
192194
default:
193195
return errInvalidRangeAccel
194196
}
195-
return p.writeMasked(_ACCEL_CONFIG, _AFS_SEL, uint8(accRange)<<_AFS_SHIFT)
197+
return p.writeMasked(_ACCEL_CONFIG, _AFS_SEL, uint8(accRange-1)<<_AFS_SHIFT)
196198
}
197199

198200
// Sleep sets the sleep bit on the power managment 1 field.
@@ -218,12 +220,3 @@ func b2u8(b bool) byte {
218220
}
219221
return 0
220222
}
221-
222-
func DefaultConfig() Config {
223-
return Config{
224-
AccelRange: RangeAccel16,
225-
GyroRange: RangeGyro2000,
226-
sampleRatio: 0, // TODO add const values.
227-
clkSel: 0,
228-
}
229-
}

mpu6050/registers.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ const (
4141

4242
// Gyroscope ranges for Init configuration
4343
const (
44+
rangeGyroDefault = iota
4445
// 250°/s
45-
RangeGyro250 RangeGyro = iota
46+
RangeGyro250
4647
// 500°/s
4748
RangeGyro500
4849
// 1000°/s
@@ -53,8 +54,9 @@ const (
5354

5455
// Accelerometer ranges for Init configuration
5556
const (
57+
rangeAccelDefault RangeAccel = iota
5658
// 2g
57-
RangeAccel2 RangeAccel = iota
59+
RangeAccel2
5860
// 4g
5961
RangeAccel4
6062
// 8g

0 commit comments

Comments
 (0)