Init
This commit is contained in:
84
godoor.go
Normal file
84
godoor.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import "time"
|
||||
import "github.com/warthog618/gpiod"
|
||||
|
||||
const wiegand_a = 17
|
||||
const wiegand_b = 18
|
||||
const solenoid = 21
|
||||
|
||||
var bits [64]bool
|
||||
var card uint64 = 0
|
||||
var bitnr uint64 = 0
|
||||
|
||||
func openDoor(l *gpiod.Line) {
|
||||
fmt.Println("Open")
|
||||
l.SetValue(1)
|
||||
time.Sleep(500000000)
|
||||
l.SetValue(0)
|
||||
fmt.Println("Close")
|
||||
}
|
||||
|
||||
func printCardId() {
|
||||
fmt.Printf("%d, ", bitnr)
|
||||
for i := 0; i < 7; i++ {
|
||||
fmt.Printf("%02x", (card>>(8*i))&0xff)
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
}
|
||||
|
||||
func cardEvent() {
|
||||
for i := 63; i != 0; i-- {
|
||||
if bits[i] == true {
|
||||
card |= 1 << (63 - i)
|
||||
}
|
||||
}
|
||||
|
||||
printCardId()
|
||||
|
||||
bitnr = 0
|
||||
card = 0
|
||||
}
|
||||
|
||||
func wiegandEvent(evt gpiod.LineEvent) {
|
||||
if evt.Offset == wiegand_b {
|
||||
bits[bitnr] = true
|
||||
fmt.Printf("1")
|
||||
} else {
|
||||
bits[bitnr] = false
|
||||
fmt.Printf("0")
|
||||
}
|
||||
bitnr += 1
|
||||
|
||||
if bitnr == 64 {
|
||||
fmt.Printf("\n")
|
||||
cardEvent()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
wa, err := gpiod.RequestLine("gpiochip0", wiegand_a, gpiod.AsInput,
|
||||
gpiod.WithFallingEdge, gpiod.WithEventHandler(wiegandEvent))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
wb, err := gpiod.RequestLine("gpiochip0", wiegand_b, gpiod.AsInput,
|
||||
gpiod.WithFallingEdge, gpiod.WithEventHandler(wiegandEvent))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
solenoid_line, err := gpiod.RequestLine("gpiochip0", solenoid, gpiod.AsOutput(0))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
openDoor(solenoid_line)
|
||||
|
||||
time.Sleep(time.Minute)
|
||||
wa.Close()
|
||||
wb.Close()
|
||||
fmt.Println("Done")
|
||||
}
|
||||
Reference in New Issue
Block a user