From c56943e6e12da3bd98dd7824a5901bac42da348f Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Mon, 30 Jun 2025 20:30:23 +0100 Subject: [PATCH] use cqueues for gnss socket This is to avoid a weird bug in the previous glib iochannel implementation, where the socket was randomly being closed after about a minute run time. But it's also less code, so that's OK --- pkgs/maps/Makefile | 2 +- pkgs/maps/main.fnl | 64 ++++++++++++++++++++-------------------------- 2 files changed, 29 insertions(+), 37 deletions(-) diff --git a/pkgs/maps/Makefile b/pkgs/maps/Makefile index c220aa6..ca7f206 100644 --- a/pkgs/maps/Makefile +++ b/pkgs/maps/Makefile @@ -12,7 +12,7 @@ $(NAME): $(patsubst %.fnl,%.lua,$(MODULES)) Makefile chmod +x $@ run: - ( fennel fake-nmea.fnl commute.nmea /tmp/gnss & ) ; sleep 1; fennel -e '((. (require :main) :run) "/tmp/gnss")' + fennel -e '((. (require :main) :run) "/tmp/gnss")' test: fennel run-tests.fnl $(MODULES) diff --git a/pkgs/maps/main.fnl b/pkgs/maps/main.fnl index 67a6e76..18a98b6 100644 --- a/pkgs/maps/main.fnl +++ b/pkgs/maps/main.fnl @@ -2,6 +2,7 @@ (local { : fdopen } (require :posix.stdio)) (local ptime (require :posix.time)) (local cqueues (require :cqueues)) +(local socket (require :cqueues.socket)) (local nmea (require :nmea)) (local tiles (require :tiles)) @@ -452,32 +453,29 @@ label.readout { (> b c) c b)) -(fn read-gnss [socket] - (each [l #(socket:read "l")] - ; (print "gnss" l) - (if (not (= l "")) - (let [message (nmea.parse l)] - (case message - { : lat : lon : utc} +(fn read-gnss-sentence [l] + (if (not (= l "")) + (let [message (nmea.parse l)] + (case message + { : lat : lon : utc} + (update-app-state + { + : lat : lon + :time-of-day + (let [(h m s) (string.match utc "(..)(..)(..)")] + (+ s (* m 60) (* h 60 60))) + } + ) + { : speed-knots } + (update-app-state { :speed (* speed-knots knot-in-m-s) })) + (when message.bearing-true + (let [c1 message.bearing-true + c2 (. app-state.courses 1) + c3 (. app-state.courses 2)] (update-app-state - { - : lat : lon - :time-of-day - (let [(h m s) (string.match utc "(..)(..)(..)")] - (+ s (* m 60) (* h 60 60))) - } - ) - { : speed-knots } - (update-app-state { :speed (* speed-knots knot-in-m-s) })) - (when message.bearing-true - (let [c1 message.bearing-true - c2 (. app-state.courses 1) - c3 (. app-state.courses 2)] - (update-app-state - { :course (clamp c1 c2 c3) - :courses [c1 c2 c3] - })))))) - true) + { :course (clamp c1 c2 c3) + :courses [c1 c2 c3] + })))))) (fn collect-profile [] (GLib.timeout_add @@ -488,17 +486,11 @@ label.readout { (profile.start 0)) (fn watch-gnss-socket [socket-path] - (let [gnss-socket - (let [addr (Gio.UnixSocketAddress { - :path socket-path - })] - (: (Gio.SocketClient) :connect addr nil)) - sock (gnss-socket:get_socket) - fd (sock:get_fd) - events [ GLib.IOCondition.IN GLib.IOCondition.HUP] - channel (GLib.IOChannel.unix_new fd) - handle (fdopen fd :r)] - (GLib.io_add_watch channel 0 events #(read-gnss handle)))) + (let [sock (socket.connect { :path socket-path :unlink true })] + (cq:wrap + #(each [l (sock:lines "*L")] + (read-gnss-sentence l))))) + (fn run [gnss-socket] (watch-gnss-socket (or gnss-socket "/var/run/gnss-share.sock"))