adjust analog sensor driver to prevent ReadValue() to get float64

This commit is contained in:
Thomas Kohler 2022-10-18 09:34:01 +02:00 committed by GitHub
commit cab50412e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 27 deletions

View File

@ -29,8 +29,8 @@ type AnalogSensorDriver struct {
// time.Duration: Interval at which the AnalogSensor is polled for new information // time.Duration: Interval at which the AnalogSensor is polled for new information
// //
// Adds the following API Commands: // Adds the following API Commands:
// "Read" - See AnalogDriverSensor.Read // "Read" - See AnalogDriverSensor.Read
// "ReadValue" - See AnalogDriverSensor.ReadValue // "ReadRaw" - See AnalogDriverSensor.ReadRaw
func NewAnalogSensorDriver(a AnalogReader, pin string, v ...time.Duration) *AnalogSensorDriver { func NewAnalogSensorDriver(a AnalogReader, pin string, v ...time.Duration) *AnalogSensorDriver {
d := &AnalogSensorDriver{ d := &AnalogSensorDriver{
name: gobot.DefaultName("AnalogSensor"), name: gobot.DefaultName("AnalogSensor"),
@ -56,8 +56,8 @@ func NewAnalogSensorDriver(a AnalogReader, pin string, v ...time.Duration) *Anal
return map[string]interface{}{"val": val, "err": err} return map[string]interface{}{"val": val, "err": err}
}) })
d.AddCommand("ReadValue", func(params map[string]interface{}) interface{} { d.AddCommand("ReadRaw", func(params map[string]interface{}) interface{} {
val, err := d.ReadValue() val, err := d.ReadRaw()
return map[string]interface{}{"val": val, "err": err} return map[string]interface{}{"val": val, "err": err}
}) })
@ -80,7 +80,7 @@ func (a *AnalogSensorDriver) Start() (err error) {
timer := time.NewTimer(a.interval) timer := time.NewTimer(a.interval)
timer.Stop() timer.Stop()
for { for {
_, err := a.ReadValue() _, err := a.Read()
if err != nil { if err != nil {
a.Publish(a.Event(Error), err) a.Publish(a.Event(Error), err)
} else { } else {
@ -128,8 +128,17 @@ func (a *AnalogSensorDriver) Pin() string { return a.pin }
// Connection returns the AnalogSensorDrivers Connection // Connection returns the AnalogSensorDrivers Connection
func (a *AnalogSensorDriver) Connection() gobot.Connection { return a.connection.(gobot.Connection) } func (a *AnalogSensorDriver) Connection() gobot.Connection { return a.connection.(gobot.Connection) }
// Read returns the current reading from the sensor without scaling // Read returns the current reading from the sensor
func (a *AnalogSensorDriver) Read() (val int, err error) { func (a *AnalogSensorDriver) Read() (val float64, err error) {
if a.rawValue, err = a.ReadRaw(); err != nil {
return
}
a.value = a.scale(a.rawValue)
return a.value, nil
}
// ReadRaw returns the current reading from the sensor without scaling
func (a *AnalogSensorDriver) ReadRaw() (val int, err error) {
return a.connection.AnalogRead(a.Pin()) return a.connection.AnalogRead(a.Pin())
} }
@ -138,15 +147,6 @@ func (a *AnalogSensorDriver) SetScaler(scaler func(int) float64) {
a.scale = scaler a.scale = scaler
} }
// ReadValue returns the current reading from the sensor
func (a *AnalogSensorDriver) ReadValue() (val float64, err error) {
if a.rawValue, err = a.Read(); err != nil {
return
}
a.value = a.scale(a.rawValue)
return a.value, nil
}
// Value returns the last read value from the sensor // Value returns the last read value from the sensor
func (a *AnalogSensorDriver) Value() float64 { func (a *AnalogSensorDriver) Value() float64 {
return a.value return a.value

View File

@ -31,11 +31,11 @@ func TestAnalogSensorDriver(t *testing.T) {
val = 100 val = 100
return return
}) })
ret := d.Command("Read")(nil).(map[string]interface{}) ret := d.Command("ReadRaw")(nil).(map[string]interface{})
gobottest.Assert(t, ret["val"].(int), 100) gobottest.Assert(t, ret["val"].(int), 100)
gobottest.Assert(t, ret["err"], nil) gobottest.Assert(t, ret["err"], nil)
ret = d.Command("ReadValue")(nil).(map[string]interface{}) ret = d.Command("Read")(nil).(map[string]interface{})
gobottest.Assert(t, ret["val"].(float64), 247.0) gobottest.Assert(t, ret["val"].(float64), 247.0)
gobottest.Assert(t, ret["err"], nil) gobottest.Assert(t, ret["err"], nil)
@ -47,7 +47,7 @@ func TestAnalogSensorDriver(t *testing.T) {
return return
}) })
gobottest.Assert(t, d.Value(), 0.0) gobottest.Assert(t, d.Value(), 0.0)
val, err := d.ReadValue() val, err := d.Read()
gobottest.Assert(t, err, nil) gobottest.Assert(t, err, nil)
gobottest.Assert(t, val, 150.0) gobottest.Assert(t, val, 150.0)
gobottest.Assert(t, d.Value(), 150.0) gobottest.Assert(t, d.Value(), 150.0)
@ -82,7 +82,7 @@ func TestAnalogSensorDriverWithLinearScaler(t *testing.T) {
return tt.input, nil return tt.input, nil
}) })
// act // act
got, err := d.ReadValue() got, err := d.Read()
// assert // assert
gobottest.Assert(t, err, nil) gobottest.Assert(t, err, nil)
gobottest.Assert(t, got, tt.want) gobottest.Assert(t, got, tt.want)

View File

@ -45,7 +45,7 @@ func TestGroveTemperatureSensorDriverScaling(t *testing.T) {
return return
}) })
// act // act
got, err := d.ReadValue() got, err := d.Read()
// assert // assert
gobottest.Assert(t, err, nil) gobottest.Assert(t, err, nil)
gobottest.Assert(t, got, tt.want) gobottest.Assert(t, got, tt.want)

View File

@ -46,7 +46,7 @@ func TestTemperatureSensorDriverNtcScaling(t *testing.T) {
return return
}) })
// act // act
got, err := d.ReadValue() got, err := d.Read()
// assert // assert
gobottest.Assert(t, err, nil) gobottest.Assert(t, err, nil)
gobottest.Assert(t, got, tt.want) gobottest.Assert(t, got, tt.want)
@ -81,7 +81,7 @@ func TestTemperatureSensorDriverLinearScaling(t *testing.T) {
return return
}) })
// act // act
got, err := d.ReadValue() got, err := d.Read()
// assert // assert
gobottest.Assert(t, err, nil) gobottest.Assert(t, err, nil)
gobottest.Assert(t, got, tt.want) gobottest.Assert(t, got, tt.want)

View File

@ -241,13 +241,13 @@ func (y *YL40Driver) Halt() (err error) {
func (y *YL40Driver) Read(pin YL40Pin) (val float64, err error) { func (y *YL40Driver) Read(pin YL40Pin) (val float64, err error) {
switch pin { switch pin {
case YL40Bri: case YL40Bri:
return y.aBri.ReadValue() return y.aBri.Read()
case YL40Temp: case YL40Temp:
return y.aTemp.ReadValue() return y.aTemp.Read()
case YL40AIN2: case YL40AIN2:
return y.aAIN2.ReadValue() return y.aAIN2.Read()
case YL40Poti: case YL40Poti:
return y.aPoti.ReadValue() return y.aPoti.Read()
case YL40AOUT: case YL40AOUT:
return y.aOut.Value(), nil return y.aOut.Value(), nil
default: default: