From ffa2fc96d74ece11450e02cba7110ddc30d02244 Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Sat, 2 Apr 2022 21:14:21 +0300 Subject: [PATCH] Init --- .gitignore | 2 ++ LICENSE | 21 ++++++++++++++ README.md | 14 +++++++++ build.sh | 3 ++ go.mod | 8 ++++++ go.sum | 4 +++ godoor.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 136 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 build.sh create mode 100644 go.mod create mode 100644 go.sum create mode 100644 godoor.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e55c5c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +godoor diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a6a32bb --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Arti Zirk + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ac14faf --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# GoDoor + +[kdoorpi](https://git.k-space.ee/arti/kdoorpi) but in Go + +Uses Go native [libgpiod](https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/about/) +library to access the gpio pins in cross platform way. + +# Cross compile + + GOOS=linux GOARCH=arm64 go build + +# Test deploy + + scp godoor rpi4b: diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..879b55f --- /dev/null +++ b/build.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +GOOS=linux GOARCH=arm64 go build && scp godoor rpi4b: diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4cb4aff --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module godoor + +go 1.18 + +require ( + github.com/warthog618/gpiod v0.8.0 // indirect + golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d8aee82 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/warthog618/gpiod v0.8.0 h1:qxH9XVvWHpTxzWFSndBcujFyNH5zVRzHM63tcmm85o4= +github.com/warthog618/gpiod v0.8.0/go.mod h1:a7Csa+IJtDBZ39++zC/6Srjo01qWejt/5velrDWuNkY= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/godoor.go b/godoor.go new file mode 100644 index 0000000..a92c544 --- /dev/null +++ b/godoor.go @@ -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") +}