Skip to content

Instantly share code, notes, and snippets.

@kidoman kidoman/test.go
Created Nov 20, 2013

Embed
What would you like to do?
Test Go on RPi
package main
import (
rpio "github.com/stianeikeland/go-rpio"
"log"
"net/http"
)
func main() {
log.Print("Starting up...")
err := rpio.Open()
if err != nil {
panic(err)
}
defer func() {
log.Print("Cleaning up...")
rpio.Close()
}()
pin := rpio.Pin(10)
pin.Output()
defer func() {
log.Print("Setting pin 10 to low")
pin.Low()
}()
http.HandleFunc("/what", func(resp http.ResponseWriter, req *http.Request) {
defer pin.Output()
pin.Input()
state := pin.Read()
if state == rpio.Low {
resp.Write([]byte("Low"))
} else {
resp.Write([]byte("High"))
}
})
http.HandleFunc("/on", func(resp http.ResponseWriter, req *http.Request) {
pin.High()
})
http.HandleFunc("/off", func(resp http.ResponseWriter, req *http.Request) {
pin.Low()
})
quit := make(chan bool)
http.HandleFunc("/shutdown", func(resp http.ResponseWriter, req *http.Request) {
quit <- true
})
go http.ListenAndServe(":8080", nil)
<-quit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.