From 24448d6adf8fe86f4f6e2b56aab6efdf2796e636 Mon Sep 17 00:00:00 2001 From: Siddharth Purohit Date: Wed, 23 Sep 2020 01:51:04 +0530 Subject: [PATCH] AP_Scripting: add rgb control for independent LEDs --- .../examples/rgb_notify_patterns.lua | 58 +++++++++++++++++++ .../generator/description/bindings.desc | 1 + 2 files changed, 59 insertions(+) create mode 100644 libraries/AP_Scripting/examples/rgb_notify_patterns.lua diff --git a/libraries/AP_Scripting/examples/rgb_notify_patterns.lua b/libraries/AP_Scripting/examples/rgb_notify_patterns.lua new file mode 100644 index 0000000000..02864fa378 --- /dev/null +++ b/libraries/AP_Scripting/examples/rgb_notify_patterns.lua @@ -0,0 +1,58 @@ +-- This script is a test of led override + +local count = 0 +local num_leds = 16 + +-- constrain a value between limits +function constrain(v, vmin, vmax) + if v < vmin then + v = vmin + end + if v > vmax then + v = vmax + end + return v +end + +--[[ +Table of colors on a rainbow, red first +--]] +local rainbow = { + { 255, 0, 0 }, + { 255, 127, 0 }, + { 255, 255, 0 }, + { 0, 255, 0 }, + { 0, 0, 255 }, + { 75, 0, 130 }, + { 143, 0, 255 }, +} + +--[[ +Function to set a LED to a color on a classic rainbow spectrum, with v=0 giving red +--]] +function set_Rainbow(led, v) + local num_rows = #rainbow + local row = math.floor(constrain(v * (num_rows-1)+1, 1, num_rows-1)) + local v0 = (row-1) / (num_rows-1) + local v1 = row / (num_rows-1) + local p = (v - v0) / (v1 - v0) + r = math.floor(rainbow[row][1] + p * (rainbow[row+1][1] - rainbow[row][1])) + g = math.floor(rainbow[row][2] + p * (rainbow[row+1][2] - rainbow[row][2])) + b = math.floor(rainbow[row][3] + p * (rainbow[row+1][3] - rainbow[row][3])) + notify:handle_rgb_id(r, g, b, led) +end + +function update() -- this is the loop which periodically runs + count = count + 1 + if count > 16 then + count = 0 + end + for led = 0, num_leds-1 do + local v = ((count+led)%16)/16 + set_Rainbow(led, v) + end + return update, 20 -- reschedules the loop in 15 seconds + +end + +return update() -- run immediately before starting to reschedule diff --git a/libraries/AP_Scripting/generator/description/bindings.desc b/libraries/AP_Scripting/generator/description/bindings.desc index dddbd2a4bd..b72dc365d6 100644 --- a/libraries/AP_Scripting/generator/description/bindings.desc +++ b/libraries/AP_Scripting/generator/description/bindings.desc @@ -135,6 +135,7 @@ include AP_Notify/AP_Notify.h singleton AP_Notify alias notify singleton AP_Notify method play_tune void string singleton AP_Notify method handle_rgb void uint8_t 0 UINT8_MAX uint8_t 0 UINT8_MAX uint8_t 0 UINT8_MAX uint8_t 0 UINT8_MAX +singleton AP_Notify method handle_rgb_id void uint8_t 0 UINT8_MAX uint8_t 0 UINT8_MAX uint8_t 0 UINT8_MAX uint8_t 0 UINT8_MAX include AP_Proximity/AP_Proximity.h