From 24de9c0fb1708fdfda646d544eb49663c90ecc81 Mon Sep 17 00:00:00 2001 From: Dhruva Sagar Date: Mon, 28 May 2018 22:48:12 +0530 Subject: [PATCH] Improve Stepper Driver * Ensure StepperDriver embeds gobot.Commander to allow for command driven workflow * Add a few core commands available out of the box for control via command workflow --- drivers/gpio/stepper_driver.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/gpio/stepper_driver.go b/drivers/gpio/stepper_driver.go index dfb6896bb..cea0cf820 100644 --- a/drivers/gpio/stepper_driver.go +++ b/drivers/gpio/stepper_driver.go @@ -3,6 +3,7 @@ package gpio import ( "errors" "math" + "strconv" "strings" "sync" "time" @@ -57,6 +58,7 @@ type StepperDriver struct { stepNum int speed uint mutex *sync.Mutex + gobot.Commander } // NewStepperDriver returns a new StepperDriver given a @@ -76,9 +78,21 @@ func NewStepperDriver(a DigitalWriter, pins [4]string, phase phase, stepsPerRev stepNum: 0, speed: 1, mutex: &sync.Mutex{}, + Commander: gobot.NewCommander(), } - s.speed = s.GetMaxSpeed() + + s.AddCommand("Move", func(params map[string]interface{}) interface{} { + steps, _ := strconv.Atoi(params["steps"].(string)) + return s.Move(steps) + }) + s.AddCommand("Run", func(params map[string]interface{}) interface{} { + return s.Run() + }) + s.AddCommand("Halt", func(params map[string]interface{}) interface{} { + return s.Halt() + }) + return s }