From 40046b1f71431ce70a32f6f3e7e8dddc8e40a359 Mon Sep 17 00:00:00 2001 From: Hayden Donald Date: Tue, 23 Jan 2024 16:44:46 +1100 Subject: [PATCH] AP_Scripting: Add send_text to display binding Added a notify:send_text binding to override the text displayed on a display with custom text --- libraries/AP_Scripting/docs/docs.lua | 4 ++ .../examples/hello_world_display.lua | 59 +++++++++++++++++++ .../generator/description/bindings.desc | 2 + 3 files changed, 65 insertions(+) create mode 100644 libraries/AP_Scripting/examples/hello_world_display.lua diff --git a/libraries/AP_Scripting/docs/docs.lua b/libraries/AP_Scripting/docs/docs.lua index 0d4ac3d3a7d1d7..4deb6bbc4f5189 100644 --- a/libraries/AP_Scripting/docs/docs.lua +++ b/libraries/AP_Scripting/docs/docs.lua @@ -2765,6 +2765,10 @@ function notify:handle_rgb(red, green, blue, rate_hz) end ---@param tune string function notify:play_tune(tune) end +-- desc +---@param text string +---@param row integer +function notify:send_text(text, row) end -- desc ---@class gps diff --git a/libraries/AP_Scripting/examples/hello_world_display.lua b/libraries/AP_Scripting/examples/hello_world_display.lua new file mode 100644 index 00000000000000..d3f75cd7ca5522 --- /dev/null +++ b/libraries/AP_Scripting/examples/hello_world_display.lua @@ -0,0 +1,59 @@ +-- This script is an example of printing to a display via scripting +-- Connect a supported display to the autopilot, and configure the NTF_DISPLAY_TYPE parameter seen at https://ardupilot.org/copter/docs/common-display-onboard.html +-- The notify:send_text(text, row) method will override default on the display, disabling the default messages + +local switchTime +local displayWidth = 18 +local function update() + -- Just keep track of when we should switch to a smiley :) + if switchTime == nil then + switchTime = millis() + 10000 + end + + -- Example of overriding a line keeping some defaults, here we will replace the battery(1) and GPS(2) rows + if switchTime > millis() then + notify:send_text("Hello, World!", 1) + notify:send_text(tostring(millis()), 2) + + -- Example of overriding all lines, a smiley, try moving the autopilot around to see it change + else + -- Generate the smiley + local width = (displayWidth / 2) + local roll = math.floor(width + (ahrs:get_roll() * width)) - 4 + local pitch = math.floor(ahrs:get_pitch() * 6) + 2; + local sub = 5 - roll + if sub < 0 then + sub = 0 + end + local rows = {} + if pitch - 2 >= 0 and pitch - 2 <= 5 then + rows[pitch - 2] = (string.rep(" ", roll) .. " ##"):sub(sub); + end + if pitch - 1 >= 0 and pitch - 1 <= 5 then + rows[pitch - 1] = (string.rep(" ", roll) .. " # #"):sub(sub); + end + if pitch >= 0 and pitch <= 5 then + rows[pitch] = (string.rep(" ", roll) .. " #"):sub(sub); + end + if pitch + 1 >= 0 and pitch + 1 <= 5 then + rows[pitch + 1] = (string.rep(" ", roll) .. " # #"):sub(sub); + end + if pitch + 2 >= 0 and pitch + 2 <= 5 then + rows[pitch + 2] = (string.rep(" ", roll) .. " ##"):sub(sub); + end + if pitch + 3 >= 0 and pitch + 3 <= 5 then + rows[pitch + 3] = ""; + end + + -- Send it out to the display + for i = 0, 5 do + if rows[i] == nil then + rows[i] = "" + end + notify:send_text(rows[i], i) + end + end + + return update, 10 + end + return update, 1000 -- Wait a few seconds before starting \ No newline at end of file diff --git a/libraries/AP_Scripting/generator/description/bindings.desc b/libraries/AP_Scripting/generator/description/bindings.desc index 0f4194a9bc8ab6..9038c1f57151ad 100644 --- a/libraries/AP_Scripting/generator/description/bindings.desc +++ b/libraries/AP_Scripting/generator/description/bindings.desc @@ -191,6 +191,8 @@ singleton AP_Notify depends (!defined(HAL_BUILD_AP_PERIPH) || defined(HAL_PERIPH singleton AP_Notify method play_tune void string singleton AP_Notify method handle_rgb void uint8_t'skip_check uint8_t'skip_check uint8_t'skip_check uint8_t'skip_check singleton AP_Notify method handle_rgb_id void uint8_t'skip_check uint8_t'skip_check uint8_t'skip_check uint8_t'skip_check +singleton AP_Notify method send_text_scripting void string uint8_t'skip_check +singleton AP_Notify method send_text_scripting rename send_text include AP_Proximity/AP_Proximity.h include AP_Proximity/AP_Proximity_Backend.h