Skip to content

Commit

Permalink
make zero value useful for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed May 27, 2023
1 parent d675bd4 commit 6288ba5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
6 changes: 2 additions & 4 deletions examples/mpu6050/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ func main() {

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

err := mpuDevice.Configure(mpu6050.Config{
AccelRange: mpu6050.ACCEL_RANGE_16,
GyroRange: mpu6050.GYRO_RANGE_2000,
})
// Configure the device with default configuration.
err := mpuDevice.Configure(mpu6050.Config{})
if err != nil {
panic(err.Error())
}
Expand Down
19 changes: 6 additions & 13 deletions mpu6050/mpu6050.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,13 @@ func (p *Device) setRangeGyro(gyroRange RangeGyro) (err error) {
p.gRange = 500
case RangeGyro1000:
p.gRange = 1000
case RangeGyro2000:
case RangeGyro2000, rangeGyroDefault:
gyroRange = RangeGyro2000
p.gRange = 2000
default:
return errInvalidRangeGyro
}
return p.writeMasked(_GYRO_CONFIG, _G_FS_SEL, uint8(gyroRange)<<_G_FS_SHIFT)
return p.writeMasked(_GYRO_CONFIG, _G_FS_SEL, uint8(gyroRange-1)<<_G_FS_SHIFT)
}

// setRangeAccel configures the full scale range of the accelerometer.
Expand All @@ -187,12 +188,13 @@ func (p *Device) setRangeAccel(accRange RangeAccel) (err error) {
p.aRange = 4
case RangeAccel8:
p.aRange = 8
case RangeAccel16:
case RangeAccel16, rangeAccelDefault:
accRange = RangeAccel16
p.aRange = 16
default:
return errInvalidRangeAccel
}
return p.writeMasked(_ACCEL_CONFIG, _AFS_SEL, uint8(accRange)<<_AFS_SHIFT)
return p.writeMasked(_ACCEL_CONFIG, _AFS_SEL, uint8(accRange-1)<<_AFS_SHIFT)
}

// Sleep sets the sleep bit on the power managment 1 field.
Expand All @@ -218,12 +220,3 @@ func b2u8(b bool) byte {
}
return 0
}

func DefaultConfig() Config {
return Config{
AccelRange: RangeAccel16,
GyroRange: RangeGyro2000,
sampleRatio: 0, // TODO add const values.
clkSel: 0,
}
}
6 changes: 4 additions & 2 deletions mpu6050/registers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ const (

// Gyroscope ranges for Init configuration
const (
rangeGyroDefault = iota
// 250°/s
RangeGyro250 RangeGyro = iota
RangeGyro250
// 500°/s
RangeGyro500
// 1000°/s
Expand All @@ -53,8 +54,9 @@ const (

// Accelerometer ranges for Init configuration
const (
rangeAccelDefault RangeAccel = iota
// 2g
RangeAccel2 RangeAccel = iota
RangeAccel2
// 4g
RangeAccel4
// 8g
Expand Down

0 comments on commit 6288ba5

Please sign in to comment.