Skip to content

Instantly share code, notes, and snippets.

@chrissnell
Created July 25, 2014 05:09
Show Gist options
  • Save chrissnell/1134b7687e5940aeba11 to your computer and use it in GitHub Desktop.
Save chrissnell/1134b7687e5940aeba11 to your computer and use it in GitHub Desktop.
Toggler
// A little blurb I wrote to toggle something on and off, always making sure to toggle off before shutdown
toggle := make(chan bool, 1)
outputPin, err := hwio.GetPinWithMode(pin, hwio.OUTPUT)
if err != nil {
log.Printf("Error getting GPIO pin: %v\n", err)
}
go func() {
for {
timer := time.NewTimer(time.Second * 3)
<-timer.C
fmt.Println("Toggling on")
toggle <- true
timer2 := time.NewTimer(time.Second * 3)
<-timer2.C
fmt.Println("Toggling off")
toggle <- false
}
}()
for {
select {
case <-shutdownFlight:
fmt.Println("--- Break")
hwio.DigitalWrite(outputPin, hwio.LOW)
hwio.CloseAll()
fmt.Println("Closed all pins")
shutdownComplete <- true
break
case t := <-toggle:
aprsMessage <- "Preparing to cutdown in 30 sec"
fmt.Printf("Toggling: %v\n", t)
if t {
hwio.DigitalWrite(outputPin, hwio.HIGH)
} else {
hwio.DigitalWrite(outputPin, hwio.LOW)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment