From 63805a00a9b9362e2cf5e144dce8ff8e41f2f379 Mon Sep 17 00:00:00 2001 From: Andy Piper Date: Fri, 29 Sep 2023 17:12:14 +0100 Subject: [PATCH] AP_Scripting: add an applet to control LED brightness with a switch --- .../AP_Scripting/applets/leds_on_a_switch.lua | 30 +++++++++++++++++++ .../AP_Scripting/applets/leds_on_a_switch.md | 6 ++++ 2 files changed, 36 insertions(+) create mode 100644 libraries/AP_Scripting/applets/leds_on_a_switch.lua create mode 100644 libraries/AP_Scripting/applets/leds_on_a_switch.md diff --git a/libraries/AP_Scripting/applets/leds_on_a_switch.lua b/libraries/AP_Scripting/applets/leds_on_a_switch.lua new file mode 100644 index 0000000000..9126289f34 --- /dev/null +++ b/libraries/AP_Scripting/applets/leds_on_a_switch.lua @@ -0,0 +1,30 @@ +-- leds_on_a_switch.lua: control led brightness with a radio switch +-- + +-- constants +local AuxSwitchPos = {LOW=0, MIDDLE=1, HIGH=2} +local NTF_LED_BRIGHT = Parameter("NTF_LED_BRIGHT") +local MAV_SEVERITY = {EMERGENCY=0, ALERT=1, CRITICAL=2, ERROR=3, WARNING=4, NOTICE=5, INFO=6, DEBUG=7} + +-- state +local prev_pos = -1 + +function update() + local sw_pos = rc:get_aux_cached(300) + if sw_pos ~= prev_pos then + if sw_pos == AuxSwitchPos.LOW then + NTF_LED_BRIGHT:set(0) + gcs:send_text(MAV_SEVERITY.INFO, "LEDs turned OFF") + elseif sw_pos == AuxSwitchPos.MIDDLE then + NTF_LED_BRIGHT:set(1) + gcs:send_text(MAV_SEVERITY.INFO, "LEDs dimmed") + else + NTF_LED_BRIGHT:set(3) + gcs:send_text(MAV_SEVERITY.INFO, "LEDs turned ON") + end + prev_pos = sw_pos + end + return update, 1000 +end + +return update, 5000 diff --git a/libraries/AP_Scripting/applets/leds_on_a_switch.md b/libraries/AP_Scripting/applets/leds_on_a_switch.md new file mode 100644 index 0000000000..4a2adad995 --- /dev/null +++ b/libraries/AP_Scripting/applets/leds_on_a_switch.md @@ -0,0 +1,6 @@ +# `leds_on_a_switch.lua`: change LED brightness using an RC switch + +ArduPilot controls LED brightness via the parameter `NTF_LED_BRIGHT`. This script allows you to control the value of the parameter using an RC switch. + +Configure an `RCx_OPTION` as 300 for scripting and load this script. +The low position will turn off the LEDs, the high position with give maximum brightness and the middle position will dim the LEDs \ No newline at end of file