Moved some variables to constants

pull/194/head
ZodiusInfuser 2021-08-23 14:58:38 +01:00
rodzic b72e0a1bb9
commit bfcb688264
1 zmienionych plików z 25 dodań i 8 usunięć

Wyświetl plik

@ -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;
}
}