From 52972b810a9a42cef5c88f9947d64a0705f243ac Mon Sep 17 00:00:00 2001 From: xengineering Date: Thu, 17 Jun 2021 09:00:07 +0200 Subject: Implement single Picture Mode --- .gitignore | 1 + python/birdscan/__main__.py | 16 +++++++++++++-- src/Makefile | 2 +- src/camera.go | 49 +++++++++++++++++++++++++++++++++++++-------- src/main.go | 2 ++ src/state.go | 4 ++-- 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 } -- cgit v1.2.3-70-g09d2