summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <mail2xengineering@protonmail.com>2021-06-17 09:00:07 +0200
committerxengineering <mail2xengineering@protonmail.com>2021-06-17 11:27:05 +0200
commit52972b810a9a42cef5c88f9947d64a0705f243ac (patch)
tree6ce0307a030bb5279a695506868a424f6ab6599c
parent591097e5a3741432e2acd9c41fadd4eb10678674 (diff)
downloadbirdscan-52972b810a9a42cef5c88f9947d64a0705f243ac.tar
birdscan-52972b810a9a42cef5c88f9947d64a0705f243ac.tar.zst
birdscan-52972b810a9a42cef5c88f9947d64a0705f243ac.zip
Implement single Picture Mode
-rw-r--r--.gitignore1
-rw-r--r--python/birdscan/__main__.py16
-rw-r--r--src/Makefile2
-rw-r--r--src/camera.go49
-rw-r--r--src/main.go2
-rw-r--r--src/state.go4
6 files changed, 61 insertions, 13 deletions
diff --git a/.gitignore b/.gitignore
index 378eac2..7d75298 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
build
+__pycache__
diff --git a/python/birdscan/__main__.py b/python/birdscan/__main__.py
index a25dee2..77fae73 100644
--- a/python/birdscan/__main__.py
+++ b/python/birdscan/__main__.py
@@ -8,15 +8,27 @@ This is the main executable of the birdscan package.
"""
+import sys
import time
def main():
"""Main function of this module"""
- while True:
- print("Active!")
+ if len(sys.argv) == 2: # check if argument is given (--debug)
time.sleep(1)
+ sys.stdout.write("ok")
+ else:
+ from picamera import PiCamera
+ camera = PiCamera()
+ camera.resolution = "3280x2464"
+ camera.start_preview()
+ # Camera warm-up time
+ time.sleep(2)
+ camera.capture("/var/lib/birdscan/{}.jpg".format(
+ time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()))
+ )
+ sys.stdout.write("ok")
if __name__ == "__main__":
diff --git a/src/Makefile b/src/Makefile
index 3cb219f..df30260 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -23,5 +23,5 @@ install: all
install -Dm 755 build/birdscan $(DESTDIR)$(PREFIX)/bin/birdscan
debug: all
- go run ./... -c ../config/default.json
+ go run ./... -c ../config/default.json -d true
diff --git a/src/camera.go b/src/camera.go
index 197aa21..cc29f54 100644
--- a/src/camera.go
+++ b/src/camera.go
@@ -3,7 +3,9 @@
package main
import (
- "time"
+ "log"
+ "os/exec"
+ "io"
)
type Camera struct {
@@ -38,15 +40,46 @@ func NewCamera() Camera {
}
}
-func runCameraHooks(last string, next string) {
-
+func runCameraHooks(last string, next string, m *Machine) {
if last == "idle" && next == "single_picture" {
- // TODO implement launch of python subprocess here
- go func() {
- time.Sleep(1 * time.Second)
- camera.statemachine.SendEvent("single_picture_taken")
- }()
+ go singlePicture(m)
+ }
+}
+
+func singlePicture(m *Machine) {
+
+ // create command
+ var cmd *exec.Cmd
+ if !debug {
+ cmd = exec.Command("python3", "/usr/lib/python3.9/site-packages/birdscan/")
+ } else {
+ cmd = exec.Command("python3", "../python/birdscan", "--debug")
+ }
+
+ // connect stdout of python process
+ stdout,err := cmd.StdoutPipe()
+ if err != nil {
+ log.Print(err)
+ }
+ defer stdout.Close()
+
+ // run command
+ err = cmd.Start()
+ if err != nil {
+ log.Print(err)
}
+ data,err := io.ReadAll(stdout)
+ if err != nil {
+ log.Print(err)
+ }
+ err = cmd.Wait() // wait until command execution and io is complete
+ if err != nil {
+ log.Print(err)
+ }
+
+ // process result
+ log.Println(string(data))
+ m.SendEvent("single_picture_taken")
}
func (cam *Camera) run() {
diff --git a/src/main.go b/src/main.go
index 768920a..01581ff 100644
--- a/src/main.go
+++ b/src/main.go
@@ -12,6 +12,7 @@ import (
var (
camera Camera
+ debug bool
)
type config struct {
@@ -44,6 +45,7 @@ func main() {
func readFlags() string {
var retval string
flag.StringVar(&retval, "c", "/etc/birdscan/config.json", "Path to birdscan configuration file")
+ flag.BoolVar(&debug, "d", false, "A debug flag to be used by source repository Makefile")
flag.Parse()
return retval
}
diff --git a/src/state.go b/src/state.go
index 10c35ef..eaf3bfa 100644
--- a/src/state.go
+++ b/src/state.go
@@ -29,7 +29,7 @@ type MachineTransition struct {
to string
}
-type HookFunction func(string, string)
+type HookFunction func(string, string, *Machine)
// This will run the statemachine (blocking).
func (m *Machine) Run() {
@@ -110,7 +110,7 @@ func (m *Machine) processEvent(event string) string {
*listener <- next
}
- m.hook(current, next) // execute registered callback function
+ m.hook(current, next, m) // execute registered callback function
m.current = next // set new state
return next
}