From 56b4a2bd7b61a154ed944ff567a08d934143cf25 Mon Sep 17 00:00:00 2001 From: Matthias Blankertz Date: Sun, 14 May 2023 12:52:40 +0200 Subject: [PATCH] - Allow other scripts / inputs to control AP heading too - Fix failure if rp2040_hid is connected above 32767 feet --- FlyWithLua/rpi2040_hid.lua | 16 +++++++++++++++- src/main.c | 6 +++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/FlyWithLua/rpi2040_hid.lua b/FlyWithLua/rpi2040_hid.lua index 97eb904..6587c62 100644 --- a/FlyWithLua/rpi2040_hid.lua +++ b/FlyWithLua/rpi2040_hid.lua @@ -130,6 +130,8 @@ else send_app_report(device, math.floor(ap_hdg + 0.5), build_leds(), math.floor(ap_alt + 0.5), -2, -2, status_machmode == 1) local prev_status_altmode = 0 local prev_status_machmode = status_machmode + local prev_ap_hdg = ap_hdg + local prev_report_ap_hdg = 0 function usb_app() local send_hdg = -1 local send_alt = -1 @@ -157,9 +159,15 @@ else mag_hdg_int = math.floor(mag_hdg + 0.5) ap_hdg = mag_hdg_int send_hdg = mag_hdg_int - elseif app_report_good and app_report[1] ~= 0xffff then + elseif prev_ap_hdg ~= ap_hdg then + send_hdg = ap_hdg + elseif app_report_good and app_report[1] ~= 0xffff and prev_report_ap_hdg ~= app_report[1] then ap_hdg = app_report[1] end + prev_ap_hdg = ap_hdg + if app_report_good and app_report[1] ~= 0xffff then + prev_report_ap_hdg = app_report[1] + end -- autopilot altitude if button(button_offset + button_alt_sync) and not last_button(button_offset + button_alt_sync) then cur_alt_int = math.floor(cur_alt + 0.5) @@ -213,4 +221,10 @@ else end do_every_frame("usb_app()") + + function exit_handler() + send_app_report(device, -2, 0, -2, -2, -2, 0) + end + + do_on_exit('exit_handler()') end diff --git a/src/main.c b/src/main.c index 81be171..f72f010 100644 --- a/src/main.c +++ b/src/main.c @@ -36,7 +36,7 @@ __attribute__((packed)) struct app_report { struct __attribute__((packed)) { int16_t hdg; int16_t leds; - int16_t alt; + uint16_t alt; int16_t vs; int16_t as; } update; @@ -228,7 +228,7 @@ static void hid_task(void) if (start_ms % 10 == 0 && send_heading_reports) { struct app_report report = { .type = APP_UPDATE | mach_mode ? APP_FLAG_MACH : 0, - .update = { .hdg = -1, .alt = -1, .vs = -1, .as = -1 } }; + .update = { .hdg = -1, .alt = 0xffff, .vs = -1, .as = -1 } }; if (send_heading_reports) report.update.hdg = hdg; if (send_alt_reports) @@ -271,7 +271,7 @@ static void app_update_report(struct app_report const *report) update_hdg_display(); send_heading_reports = true; } - if (report->update.alt >= 0) { + if (report->update.alt != 0xffff) { alt = report->update.alt; update_alt_display(); send_alt_reports = true;