107 lines
1.8 KiB
Go
107 lines
1.8 KiB
Go
package main
|
|
|
|
import "fmt"
|
|
import "time"
|
|
import "github.com/warthog618/gpiod"
|
|
|
|
const wiegand_a = 17
|
|
const wiegand_b = 18
|
|
const wiegand_bit_timeout = time.Millisecond * 8
|
|
const solenoid = 21
|
|
|
|
var bits [64]bool
|
|
var card uint64 = 0
|
|
var bitnr uint64 = 0
|
|
var timeout *time.Timer
|
|
|
|
func openDoor(l *gpiod.Line) {
|
|
fmt.Println("Open")
|
|
l.SetValue(1)
|
|
d, _ := time.ParseDuration("500ms")
|
|
time.Sleep(d)
|
|
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 cardRunner() {
|
|
for {
|
|
// Wait for bit timeout
|
|
fmt.Printf("Waiting for bit timeout\n")
|
|
<-timeout.C
|
|
fmt.Printf("\n")
|
|
|
|
if bitnr != 64 {
|
|
fmt.Printf("We got less than 64 bits: %d\n", bitnr)
|
|
}
|
|
|
|
for i := 63; i != 0; i-- {
|
|
if bits[i] == true {
|
|
card |= 1 << (63 - i)
|
|
}
|
|
}
|
|
|
|
printCardId()
|
|
|
|
bitnr = 0
|
|
card = 0
|
|
}
|
|
}
|
|
|
|
func wiegandEvent(evt gpiod.LineEvent) {
|
|
timeout.Reset(wiegand_bit_timeout)
|
|
|
|
if evt.Offset == wiegand_b {
|
|
bits[bitnr] = true
|
|
fmt.Printf("1")
|
|
} else {
|
|
bits[bitnr] = false
|
|
fmt.Printf("0")
|
|
}
|
|
|
|
bitnr += 1
|
|
}
|
|
|
|
func main() {
|
|
timeout = time.NewTimer(wiegand_bit_timeout)
|
|
if !timeout.Stop() {
|
|
<-timeout.C
|
|
}
|
|
|
|
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)
|
|
_ = solenoid_line
|
|
go cardRunner()
|
|
fmt.Printf("Sleeping\n")
|
|
|
|
for {
|
|
time.Sleep(time.Second)
|
|
fmt.Printf(".")
|
|
}
|
|
wa.Close()
|
|
wb.Close()
|
|
fmt.Println("Done")
|
|
}
|