gobot.io/x/gobot/v2@v2.1.0/drivers/aio/analog_actuator_driver.go (about) 1 package aio 2 3 import ( 4 "strconv" 5 6 "gobot.io/x/gobot/v2" 7 ) 8 9 // AnalogActuatorDriver represents an analog actuator 10 type AnalogActuatorDriver struct { 11 name string 12 pin string 13 connection AnalogWriter 14 gobot.Eventer 15 gobot.Commander 16 scale func(input float64) (value int) 17 lastValue float64 18 lastRawValue int 19 } 20 21 // NewAnalogActuatorDriver returns a new AnalogActuatorDriver given by an AnalogWriter and pin. 22 // The driver supports customizable scaling from given float64 value to written int. 23 // The default scaling is 1:1. An adjustable linear scaler is provided by the driver. 24 // 25 // Adds the following API Commands: 26 // 27 // "Write" - See AnalogActuator.Write 28 // "RawWrite" - See AnalogActuator.RawWrite 29 func NewAnalogActuatorDriver(a AnalogWriter, pin string) *AnalogActuatorDriver { 30 d := &AnalogActuatorDriver{ 31 name: gobot.DefaultName("AnalogActuator"), 32 connection: a, 33 pin: pin, 34 Commander: gobot.NewCommander(), 35 scale: func(input float64) (value int) { return int(input) }, 36 } 37 38 d.AddCommand("Write", func(params map[string]interface{}) interface{} { 39 val, err := strconv.ParseFloat(params["val"].(string), 64) 40 if err != nil { 41 return err 42 } 43 return d.Write(val) 44 }) 45 46 d.AddCommand("RawWrite", func(params map[string]interface{}) interface{} { 47 val, _ := strconv.Atoi(params["val"].(string)) 48 return d.RawWrite(val) 49 }) 50 51 return d 52 } 53 54 // Start starts driver 55 func (a *AnalogActuatorDriver) Start() (err error) { return } 56 57 // Halt is for halt 58 func (a *AnalogActuatorDriver) Halt() (err error) { return } 59 60 // Name returns the drivers name 61 func (a *AnalogActuatorDriver) Name() string { return a.name } 62 63 // SetName sets the drivers name 64 func (a *AnalogActuatorDriver) SetName(n string) { a.name = n } 65 66 // Pin returns the drivers pin 67 func (a *AnalogActuatorDriver) Pin() string { return a.pin } 68 69 // Connection returns the drivers Connection 70 func (a *AnalogActuatorDriver) Connection() gobot.Connection { return a.connection.(gobot.Connection) } 71 72 // RawWrite write the given raw value to the actuator 73 func (a *AnalogActuatorDriver) RawWrite(val int) (err error) { 74 a.lastRawValue = val 75 return a.connection.AnalogWrite(a.Pin(), val) 76 } 77 78 // SetScaler substitute the default 1:1 return value function by a new scaling function 79 func (a *AnalogActuatorDriver) SetScaler(scaler func(float64) int) { 80 a.scale = scaler 81 } 82 83 // Write writes the given value to the actuator 84 func (a *AnalogActuatorDriver) Write(val float64) (err error) { 85 a.lastValue = val 86 rawValue := a.scale(val) 87 return a.RawWrite(rawValue) 88 } 89 90 // RawValue returns the last written raw value 91 func (a *AnalogActuatorDriver) RawValue() (val int) { 92 return a.lastRawValue 93 } 94 95 // Value returns the last written value 96 func (a *AnalogActuatorDriver) Value() (val float64) { 97 return a.lastValue 98 } 99 100 // AnalogActuatorLinearScaler creates a linear scaler function from the given values. 101 func AnalogActuatorLinearScaler(fromMin, fromMax float64, toMin, toMax int) func(input float64) (value int) { 102 m := float64(toMax-toMin) / (fromMax - fromMin) 103 n := float64(toMin) - m*fromMin 104 return func(input float64) (value int) { 105 if input <= fromMin { 106 return toMin 107 } 108 if input >= fromMax { 109 return toMax 110 } 111 return int(input*m + n) 112 } 113 }