Make code look more like go

This commit is contained in:
Arti Zirk 2022-04-03 19:27:16 +03:00
parent be22e89f66
commit fcd8d3fbfd
2 changed files with 107 additions and 85 deletions

View File

@ -2,105 +2,22 @@ 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)
}
wiegand := WiegandSetup(wiegand_a, wiegand_b, wiegand_bit_timeout, solenoid)
//openDoor(solenoid_line)
_ = solenoid_line
go cardRunner()
go wiegand.cardRunner()
fmt.Printf("Sleeping\n")
for {
time.Sleep(time.Second)
fmt.Printf(".")
}
wa.Close()
wb.Close()
fmt.Println("Done")
}

105
wiegand.go Normal file
View File

@ -0,0 +1,105 @@
package main
import (
"fmt"
"github.com/warthog618/gpiod"
"time"
)
type Wiegand struct {
aLine *gpiod.Line
bLine *gpiod.Line
bits [64]bool
bitNr int
bitTimeout time.Duration
bitTimeoutTimer *time.Timer
solenoidLine *gpiod.Line
}
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(card uint64) {
for i := 0; i < 7; i++ {
fmt.Printf("%02x", (card>>(8*i))&0xff)
}
fmt.Printf("\n")
}
func (w *Wiegand) cardRunner() {
for {
// Wait for bit timeout
fmt.Printf("Waiting for bit timeout\n")
<-w.bitTimeoutTimer.C
fmt.Printf("\n")
if w.bitNr != 64 {
fmt.Printf("We got less than 64 bits: %d\n", w.bitNr)
}
var card uint64 = 0
for i := 63; i != 0; i-- {
if w.bits[i] == true {
card |= 1 << (63 - i)
}
}
printCardId(card)
w.bitNr = 0
}
}
func (w *Wiegand) wiegandAEvent(evt gpiod.LineEvent) {
w.bitTimeoutTimer.Reset(w.bitTimeout)
w.bits[w.bitNr] = false
fmt.Printf("0")
w.bitNr += 1
}
func (w *Wiegand) wiegandBEvent(evt gpiod.LineEvent) {
w.bitTimeoutTimer.Reset(w.bitTimeout)
w.bits[w.bitNr] = true
fmt.Printf("1")
w.bitNr += 1
}
func WiegandSetup(a int, b int, bitTimeout time.Duration, solenoid int) *Wiegand {
var wiegand Wiegand
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.WithEventHandler(wiegand.wiegandAEvent))
if err != nil {
panic(err)
}
wiegand.aLine = wa
wb, err := gpiod.RequestLine("gpiochip0", b, gpiod.AsInput,
gpiod.WithFallingEdge, gpiod.WithEventHandler(wiegand.wiegandBEvent))
if err != nil {
panic(err)
}
wiegand.bLine = wb
solenoid_line, err := gpiod.RequestLine("gpiochip0", solenoid, gpiod.AsOutput(0))
if err != nil {
panic(err)
}
wiegand.solenoidLine = solenoid_line
return &wiegand
}