1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 12:50:19 +01:00

style updates and using ticker to time steps

This commit is contained in:
Ben Schwartz 2015-03-30 19:10:19 -05:00
parent ae26e1d7f0
commit 2e3e23a155

View File

@ -14,15 +14,16 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/kidoman/embd"
_ "github.com/kidoman/embd/host/rpi"
"os" "os"
"os/signal" "os/signal"
"time" "time"
"github.com/kidoman/embd"
_ "github.com/kidoman/embd/host/rpi"
) )
func main() { func main() {
stepWait := flag.Int("step-delay", 10, "milliseconds between steps") stepDelay := flag.Int("step-delay", 10, "milliseconds between steps")
flag.Parse() flag.Parse()
if err := embd.InitGPIO(); err != nil { if err := embd.InitGPIO(); err != nil {
@ -30,7 +31,7 @@ func main() {
} }
defer embd.CloseGPIO() defer embd.CloseGPIO()
// Physical pins 11,15,16,18 // Physical pins 11,15,16,18 on rasp pi
// GPIO17,GPIO22,GPIO23,GPIO24 // GPIO17,GPIO22,GPIO23,GPIO24
stepPinNums := []int{17, 22, 23, 24} stepPinNums := []int{17, 22, 23, 24}
@ -57,8 +58,7 @@ func main() {
stepPins[i] = pin stepPins[i] = pin
} }
// Define advanced sequence // Define sequence described in manufacturer's datasheet
// as shown in manufacturers datasheet
seq := [][]int{ seq := [][]int{
[]int{1, 0, 0, 0}, []int{1, 0, 0, 0},
[]int{1, 1, 0, 0}, []int{1, 1, 0, 0},
@ -73,15 +73,19 @@ func main() {
stepCount := len(seq) - 1 stepCount := len(seq) - 1
stepDir := 2 // Set to 1 or 2 for clockwise, -1 or -2 for counter-clockwise stepDir := 2 // Set to 1 or 2 for clockwise, -1 or -2 for counter-clockwise
// Start main loop
quit := make(chan os.Signal, 1) quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, os.Kill) signal.Notify(quit, os.Interrupt, os.Kill)
defer signal.Stop(quit) defer signal.Stop(quit)
stepCounter := 0 // Start main loop
ticker := time.NewTicker(time.Duration(*stepDelay) * time.Millisecond)
var stepCounter int
for { for {
select { select {
case <-time.After(time.Duration(*stepWait) * time.Millisecond): case <-ticker.C:
// set pins to appropriate values for given position in the sequence
for i, pin := range stepPins { for i, pin := range stepPins {
if seq[stepCounter][i] != 0 { if seq[stepCounter][i] != 0 {
fmt.Printf("Enable pin %d, step %d\n", i, stepCounter) fmt.Printf("Enable pin %d, step %d\n", i, stepCounter)
@ -103,9 +107,10 @@ func main() {
} else if stepCounter < 0 { } else if stepCounter < 0 {
stepCounter = stepCount stepCounter = stepCount
} }
case <-quit: case <-quit:
ticker.Stop()
return return
} }
} }
} }