1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-02 22:17:12 +08:00

i2c: small refactor and increase test coverage for l3gd20h

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-04-12 17:37:05 +02:00
parent 4b5fb71afb
commit 6ae2003622
2 changed files with 42 additions and 32 deletions

View File

@ -102,29 +102,6 @@ func (d *L3GD20HDriver) Start() (err error) {
return nil return nil
} }
func (d *L3GD20HDriver) initialization() (err error) {
bus := d.GetBusOrDefault(d.connector.GetDefaultBus())
address := d.GetAddressOrDefault(l3gd20hAddress)
d.connection, err = d.connector.GetConnection(address, bus)
if err != nil {
return err
}
// reset the gyroscope.
if _, err := d.connection.Write([]byte{l3gd20hRegisterCtl1, 0x00}); err != nil {
return err
}
// Enable Z, Y and X axis.
if _, err := d.connection.Write([]byte{l3gd20hRegisterCtl1, l3gd20hNormalMode | l3gd20hEnableZ | l3gd20hEnableY | l3gd20hEnableX}); err != nil {
return err
}
// Set the sensitivity scale.
if _, err := d.connection.Write([]byte{l3gd20hRegisterCtl4, byte(d.scale)}); err != nil {
return err
}
return nil
}
// Halt halts the device. // Halt halts the device.
func (d *L3GD20HDriver) Halt() (err error) { func (d *L3GD20HDriver) Halt() (err error) {
return nil return nil
@ -149,15 +126,42 @@ func (d *L3GD20HDriver) XYZ() (x float32, y float32, z float32, err error) {
binary.Read(buf, binary.LittleEndian, &rawZ) binary.Read(buf, binary.LittleEndian, &rawZ)
// Sensitivity values from the mechanical characteristics in the datasheet. // Sensitivity values from the mechanical characteristics in the datasheet.
var sensitivity float32 sensitivity := d.getSensitivity()
switch d.scale {
case L3GD20HScale250dps:
sensitivity = 0.00875
case L3GD20HScale500dps:
sensitivity = 0.0175
case L3GD20HScale2000dps:
sensitivity = 0.07
}
return float32(rawX) * sensitivity, float32(rawY) * sensitivity, float32(rawZ) * sensitivity, nil return float32(rawX) * sensitivity, float32(rawY) * sensitivity, float32(rawZ) * sensitivity, nil
} }
func (d *L3GD20HDriver) initialization() (err error) {
bus := d.GetBusOrDefault(d.connector.GetDefaultBus())
address := d.GetAddressOrDefault(l3gd20hAddress)
d.connection, err = d.connector.GetConnection(address, bus)
if err != nil {
return err
}
// reset the gyroscope.
if _, err := d.connection.Write([]byte{l3gd20hRegisterCtl1, 0x00}); err != nil {
return err
}
// Enable Z, Y and X axis.
if _, err := d.connection.Write([]byte{l3gd20hRegisterCtl1, l3gd20hNormalMode | l3gd20hEnableZ | l3gd20hEnableY | l3gd20hEnableX}); err != nil {
return err
}
// Set the sensitivity scale.
if _, err := d.connection.Write([]byte{l3gd20hRegisterCtl4, byte(d.scale)}); err != nil {
return err
}
return nil
}
func (d *L3GD20HDriver) getSensitivity() float32 {
switch d.scale {
case L3GD20HScale250dps:
return 0.00875
case L3GD20HScale500dps:
return 0.0175
case L3GD20HScale2000dps:
return 0.07
}
return 0
}

View File

@ -69,9 +69,15 @@ func TestL3GD20HDriverHalt(t *testing.T) {
func TestL3GD20HDriverScale(t *testing.T) { func TestL3GD20HDriverScale(t *testing.T) {
d := initTestL3GD20HDriver() d := initTestL3GD20HDriver()
gobottest.Assert(t, d.Scale(), L3GD20HScale250dps) gobottest.Assert(t, d.Scale(), L3GD20HScale250dps)
gobottest.Assert(t, d.getSensitivity(), float32(0.00875))
d.SetScale(L3GD20HScale500dps) d.SetScale(L3GD20HScale500dps)
gobottest.Assert(t, d.Scale(), L3GD20HScale500dps) gobottest.Assert(t, d.Scale(), L3GD20HScale500dps)
gobottest.Assert(t, d.getSensitivity(), float32(0.0175))
d.SetScale(L3GD20HScale2000dps)
gobottest.Assert(t, d.Scale(), L3GD20HScale2000dps)
gobottest.Assert(t, d.getSensitivity(), float32(0.07))
} }
func TestL3GD20HDriverMeasurement(t *testing.T) { func TestL3GD20HDriverMeasurement(t *testing.T) {