Skip to content

Instantly share code, notes, and snippets.

@jamak
Last active August 29, 2015 14:17
Show Gist options
  • Save jamak/4770c740296268a5dc75 to your computer and use it in GitHub Desktop.
Save jamak/4770c740296268a5dc75 to your computer and use it in GitHub Desktop.
package main
import (
// "os"
"fmt"
"github.com/stianeikeland/go-rpio"
"log"
"net/http"
// "sync"
"time"
// "sync/atomic"
)
const (
LOCK_TIME time.Duration = 1 * time.Second
)
type Doorbot struct {
pin rpio.Pin
// mutex sync.Mutex turns out the pin logic has its own locks
}
type DoorbotHandler struct {
bot Doorbot
}
func NewDoorbot(pin_no int) *Doorbot {
pin := rpio.Pin(pin_no)
pin.Output()
return &Doorbot{
pin: pin,
// mutex: sync.Mutex{},
}
}
func (d *Doorbot) Unlock() {
d.pin.High()
time.Sleep(LOCK_TIME)
d.pin.Low()
}
func (d *Doorbot) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// d.mutex.Lock()
d.Unlock()
// d.mutex.Unlock()
fmt.Fprintf(w, "door unlocked!")
}
func main() {
doorbot := NewDoorbot(23) //Pin 23 corresponds to GPIO4
log.Fatal(http.ListenAndServe(":80", doorbot))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment