Read many cards

This commit is contained in:
Arti Zirk 2022-04-03 16:50:21 +03:00
parent ffa2fc96d7
commit be22e89f66

View File

@ -6,16 +6,19 @@ import "github.com/warthog618/gpiod"
const wiegand_a = 17 const wiegand_a = 17
const wiegand_b = 18 const wiegand_b = 18
const wiegand_bit_timeout = time.Millisecond * 8
const solenoid = 21 const solenoid = 21
var bits [64]bool var bits [64]bool
var card uint64 = 0 var card uint64 = 0
var bitnr uint64 = 0 var bitnr uint64 = 0
var timeout *time.Timer
func openDoor(l *gpiod.Line) { func openDoor(l *gpiod.Line) {
fmt.Println("Open") fmt.Println("Open")
l.SetValue(1) l.SetValue(1)
time.Sleep(500000000) d, _ := time.ParseDuration("500ms")
time.Sleep(d)
l.SetValue(0) l.SetValue(0)
fmt.Println("Close") fmt.Println("Close")
} }
@ -28,7 +31,17 @@ func printCardId() {
fmt.Printf("\n") fmt.Printf("\n")
} }
func cardEvent() { 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-- { for i := 63; i != 0; i-- {
if bits[i] == true { if bits[i] == true {
card |= 1 << (63 - i) card |= 1 << (63 - i)
@ -39,9 +52,12 @@ func cardEvent() {
bitnr = 0 bitnr = 0
card = 0 card = 0
}
} }
func wiegandEvent(evt gpiod.LineEvent) { func wiegandEvent(evt gpiod.LineEvent) {
timeout.Reset(wiegand_bit_timeout)
if evt.Offset == wiegand_b { if evt.Offset == wiegand_b {
bits[bitnr] = true bits[bitnr] = true
fmt.Printf("1") fmt.Printf("1")
@ -49,16 +65,16 @@ func wiegandEvent(evt gpiod.LineEvent) {
bits[bitnr] = false bits[bitnr] = false
fmt.Printf("0") fmt.Printf("0")
} }
bitnr += 1 bitnr += 1
if bitnr == 64 {
fmt.Printf("\n")
cardEvent()
}
} }
func main() { func main() {
timeout = time.NewTimer(wiegand_bit_timeout)
if !timeout.Stop() {
<-timeout.C
}
wa, err := gpiod.RequestLine("gpiochip0", wiegand_a, gpiod.AsInput, wa, err := gpiod.RequestLine("gpiochip0", wiegand_a, gpiod.AsInput,
gpiod.WithFallingEdge, gpiod.WithEventHandler(wiegandEvent)) gpiod.WithFallingEdge, gpiod.WithEventHandler(wiegandEvent))
if err != nil { if err != nil {
@ -75,9 +91,15 @@ func main() {
panic(err) panic(err)
} }
openDoor(solenoid_line) //openDoor(solenoid_line)
_ = solenoid_line
go cardRunner()
fmt.Printf("Sleeping\n")
time.Sleep(time.Minute) for {
time.Sleep(time.Second)
fmt.Printf(".")
}
wa.Close() wa.Close()
wb.Close() wb.Close()
fmt.Println("Done") fmt.Println("Done")