From bfcb688264d302ba69cc4586ebefe8a9bc7da807 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Mon, 23 Aug 2021 14:58:38 +0100 Subject: [PATCH] Moved some variables to constants --- examples/plasma2040/plasma2040_bme68x.cpp | 33 +++++++++++++++++------ 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/examples/plasma2040/plasma2040_bme68x.cpp b/examples/plasma2040/plasma2040_bme68x.cpp index e0ac2727..27aad76e 100644 --- a/examples/plasma2040/plasma2040_bme68x.cpp +++ b/examples/plasma2040/plasma2040_bme68x.cpp @@ -20,16 +20,32 @@ const uint N_LEDS = 30; // How many times the LEDs will be updated per second const uint UPDATES = 60; - +// The temperature range to show (in degrees celsius) constexpr float TEMPERATURE_C_MIN = 20.0f; constexpr float TEMPERATURE_C_MAX = 35.0f; +// The pressure range to show (in pascals) constexpr float PRESSURE_PA_MIN = 87000.0f; constexpr float PRESSURE_PA_MAX = 108500.0f; +// The humidity range to show (in percent) constexpr float HUMIDITY_MIN = 0.0f; constexpr float HUMIDITY_MAX = 100.0f; + +// The start and end hues for the temperature range +constexpr float TEMPERATURE_HUE_START = 0.667f; +constexpr float TEMPERATURE_HUE_END = 1.0f; + +// The start and end hues for the pressure range +constexpr float PRESSURE_HUE_START = 0.333f; +constexpr float PRESSURE_HUE_END = 0.0f; + +// The start and end hues for the humidity range +constexpr float HUMIDITY_HUE_START = 0.333f; +constexpr float HUMIDITY_HUE_END = 0.667f; + + // Pick *one* LED type by uncommenting the relevant line below: // APA102-style LEDs with Data/Clock lines. AKA DotStar @@ -55,11 +71,12 @@ enum DisplayMode { HUMIDITY }; - +// Maps a value from one range to another float map(float x, float in_min, float in_max, float out_min, float out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } +// Sets a section of the led strip to show a hue gradient based on the provided percent void colour_gauge(float percent, uint start_led, uint end_led, float start_hue, float end_hue) { if(end_led > start_led) { uint range = end_led - start_led; @@ -108,28 +125,28 @@ int main() { switch(mode) { case DisplayMode::ALL: t = map(data.temperature, TEMPERATURE_C_MIN, TEMPERATURE_C_MAX, 0.0f, 1.0f); - colour_gauge(t, 0, first_third, 0.667f, 1.0f); + colour_gauge(t, 0, first_third, TEMPERATURE_HUE_START, TEMPERATURE_HUE_END); t = map(data.pressure, PRESSURE_PA_MIN, PRESSURE_PA_MAX, 0.0f, 1.0f); - colour_gauge(t, first_third, second_third, 0.333f, 0.0f); + colour_gauge(t, first_third, second_third, PRESSURE_HUE_START, PRESSURE_HUE_END); t = map(data.humidity, HUMIDITY_MIN, HUMIDITY_MAX, 0.0f, 1.0f); - colour_gauge(t, second_third, led_strip.num_leds, 0.333f, 0.667f); + colour_gauge(t, second_third, led_strip.num_leds, HUMIDITY_HUE_START, HUMIDITY_HUE_END); break; case DisplayMode::TEMPERATURE: t = map(data.temperature, TEMPERATURE_C_MIN, TEMPERATURE_C_MAX, 0.0f, 1.0f); - colour_gauge(t, 0, led_strip.num_leds, 0.667f, 1.0f); + colour_gauge(t, 0, led_strip.num_leds, TEMPERATURE_HUE_START, TEMPERATURE_HUE_END); break; case DisplayMode::PRESSURE: t = map(data.pressure, PRESSURE_PA_MIN, PRESSURE_PA_MAX, 0.0f, 1.0f); - colour_gauge(t, 0, led_strip.num_leds, 0.333f, 0.0f); + colour_gauge(t, 0, led_strip.num_leds, PRESSURE_HUE_START, PRESSURE_HUE_END); break; case DisplayMode::HUMIDITY: t = map(data.humidity, HUMIDITY_MIN, HUMIDITY_MAX, 0.0f, 1.0f); - colour_gauge(t, 0, led_strip.num_leds, 0.333f, 0.667f); + colour_gauge(t, 0, led_strip.num_leds, HUMIDITY_HUE_START, HUMIDITY_HUE_END); break; } }