godoor/wiegand.go

150 lines
2.9 KiB
Go
Raw Permalink Normal View History

2022-04-03 16:27:16 +00:00
package main
import (
"fmt"
2023-07-29 17:04:19 +00:00
"log"
2023-07-30 10:38:47 +00:00
"sync"
2022-04-03 16:27:16 +00:00
"time"
2022-04-03 16:30:56 +00:00
"github.com/warthog618/gpiod"
)
2023-07-28 22:22:26 +00:00
type Wiegand interface {
GetCardUid() (uint64, error)
2023-07-29 17:04:19 +00:00
OpenDoor() error
CloseDoor() error
IsDoorOpen() (bool, error)
2023-07-28 22:22:26 +00:00
}
type WiegandHW struct {
2022-04-03 16:27:16 +00:00
aLine *gpiod.Line
bLine *gpiod.Line
bits [64]bool
bitNr int
bitTimeout time.Duration
bitTimeoutTimer *time.Timer
solenoidLine *gpiod.Line
2023-07-30 10:38:47 +00:00
lock sync.RWMutex
2022-04-03 16:27:16 +00:00
}
2023-07-29 17:04:19 +00:00
func (w *WiegandHW) OpenDoor() error {
2023-07-30 10:38:47 +00:00
w.lock.Lock()
defer w.lock.Unlock()
open, _ := w.isDoorOpen()
if open {
return nil
}
2023-07-29 17:04:19 +00:00
return w.solenoidLine.SetValue(1)
2022-04-03 16:27:16 +00:00
}
2023-07-29 17:04:19 +00:00
func (w *WiegandHW) CloseDoor() error {
2023-07-30 10:38:47 +00:00
w.lock.Lock()
defer w.lock.Unlock()
open, _ := w.isDoorOpen()
if !open {
return nil
}
2023-07-29 17:04:19 +00:00
return w.solenoidLine.SetValue(0)
}
func (w *WiegandHW) IsDoorOpen() (bool, error) {
2023-07-30 10:38:47 +00:00
w.lock.RLock()
defer w.lock.RUnlock()
return w.isDoorOpen()
}
func (w *WiegandHW) isDoorOpen() (bool, error) {
2023-07-29 17:04:19 +00:00
i, err := w.solenoidLine.Value()
if err != nil {
return false, err
}
return i == 1, nil
}
2022-04-03 16:27:16 +00:00
2023-07-28 22:22:26 +00:00
func (w *WiegandHW) GetCardUid() (uint64, error) {
// Wait for bit timeout
2023-07-29 17:04:19 +00:00
log.Printf("Waiting for bit timeout\n")
2022-04-03 16:27:16 +00:00
2023-07-28 22:22:26 +00:00
<-w.bitTimeoutTimer.C
2022-04-03 16:27:16 +00:00
2023-07-28 22:22:26 +00:00
defer func() { w.bitNr = 0 }()
2022-04-03 17:34:46 +00:00
2023-07-28 22:22:26 +00:00
if w.bitNr != 64 {
return 0, fmt.Errorf("We got less than 64 bits: %d\n", w.bitNr)
}
2022-04-03 17:34:46 +00:00
2023-07-28 22:22:26 +00:00
var card uint64 = 0
2023-07-30 10:38:47 +00:00
for i := 63; i >= 0; i-- {
2023-07-28 22:22:26 +00:00
if w.bits[i] == true {
card |= 1 << (63 - i)
2022-04-03 17:34:46 +00:00
}
2022-04-03 16:27:16 +00:00
}
2023-07-28 22:22:26 +00:00
return card, nil
2022-04-03 16:27:16 +00:00
}
2023-07-28 22:22:26 +00:00
func (w *WiegandHW) wiegandAEvent(evt gpiod.LineEvent) {
2022-04-03 16:27:16 +00:00
w.bitTimeoutTimer.Reset(w.bitTimeout)
2023-07-30 10:38:47 +00:00
w.bits[w.bitNr] = true
2023-08-03 08:03:43 +00:00
//fmt.Printf("1")
2022-04-03 16:27:16 +00:00
w.bitNr += 1
}
2023-07-28 22:22:26 +00:00
func (w *WiegandHW) wiegandBEvent(evt gpiod.LineEvent) {
2022-04-03 16:27:16 +00:00
w.bitTimeoutTimer.Reset(w.bitTimeout)
2023-07-30 10:38:47 +00:00
w.bits[w.bitNr] = false
2023-08-03 08:03:43 +00:00
//fmt.Printf("0")
2022-04-03 16:27:16 +00:00
w.bitNr += 1
}
2023-07-28 22:22:26 +00:00
func WiegandSetup(a int, b int, bitTimeout time.Duration, solenoid int) *WiegandHW {
2022-04-03 16:27:16 +00:00
2023-08-06 13:48:17 +00:00
log.Printf("Wiegand GPIO-s: A:%d B:%d Solenoid:%d", a, b, solenoid)
2023-07-28 22:22:26 +00:00
var wiegand WiegandHW
2022-04-03 16:27:16 +00:00
wiegand.bitTimeout = bitTimeout
wiegand.bitTimeoutTimer = time.NewTimer(wiegand.bitTimeout)
if !wiegand.bitTimeoutTimer.Stop() {
<-wiegand.bitTimeoutTimer.C
}
wa, err := gpiod.RequestLine("gpiochip0", a, gpiod.AsInput,
gpiod.WithFallingEdge, gpiod.LineBiasPullUp, gpiod.WithEventHandler(wiegand.wiegandAEvent))
2022-04-03 16:27:16 +00:00
if err != nil {
panic(err)
}
wiegand.aLine = wa
wb, err := gpiod.RequestLine("gpiochip0", b, gpiod.AsInput,
gpiod.WithFallingEdge, gpiod.LineBiasPullUp, gpiod.WithEventHandler(wiegand.wiegandBEvent))
2022-04-03 16:27:16 +00:00
if err != nil {
panic(err)
}
wiegand.bLine = wb
2022-04-03 16:30:56 +00:00
solenoidLine, err := gpiod.RequestLine("gpiochip0", solenoid, gpiod.AsOutput(0))
2022-04-03 16:27:16 +00:00
if err != nil {
panic(err)
}
2022-04-03 16:30:56 +00:00
wiegand.solenoidLine = solenoidLine
2022-04-03 16:27:16 +00:00
return &wiegand
}
2022-04-03 16:30:56 +00:00
2023-07-28 22:22:26 +00:00
func (w *WiegandHW) WiegandClose() {
2022-04-03 16:30:56 +00:00
w.aLine.Close()
w.bLine.Close()
w.solenoidLine.Close()
}
2023-07-30 10:38:47 +00:00
func printCardId(card uint64) {
for i := 0; i < 7; i++ {
fmt.Printf("%02x", (card>>(8*i))&0xff)
}
fmt.Printf("\n")
}