FastLED 3.9.7
Loading...
Searching...
No Matches
ui.h
1#pragma once
2
3#include <stdint.h>
4
5#include "platforms/ui_defs.h"
6#include "fl/namespace.h"
7#include "fl/math_macros.h"
8#include "fl/template_magic.h"
9#include "fl/unused.h"
10
11#ifndef FASTLED_HAS_UI_SLIDER
12#define FASTLED_HAS_UI_SLIDER 0
13#endif
14
15#ifndef FASTLED_HAS_UI_BUTTON
16#define FASTLED_HAS_UI_BUTTON 0
17#endif
18
19#ifndef FASTLED_HAS_UI_CHECKBOX
20#define FASTLED_HAS_UI_CHECKBOX 0
21#endif
22
23#ifndef FASTLED_HAS_UI_NUMBER_FIELD
24#define FASTLED_HAS_UI_NUMBER_FIELD 0
25#endif
26
27#ifndef FASTLED_HAS_UI_TITLE
28#define FASTLED_HAS_UI_TITLE 0
29#endif
30
31#ifndef FASTLED_HAS_UI_DESCRIPTION
32#define FASTLED_HAS_UI_DESCRIPTION 0
33#endif
34
35namespace fl {
36
37
38// If the platform is missing ui components, provide stubs.
39
40#if !FASTLED_HAS_UI_SLIDER
41
42class Slider {
43 public:
44 // If step is -1, it will be calculated as (max - min) / 100
45 Slider(const char *name, float value = 128.0f, float min = 1, float max = 255, float step = -1.f)
46 : mValue(value), mMin(MIN(min, max)), mMax(MAX(min, max)) {
47 FASTLED_UNUSED(name);
48 FASTLED_UNUSED(step);
49 if (value < min) { mValue = min; }
50 if (value > max) { mValue = max; }
51 }
52 ~Slider() {}
53 float value() const { return mValue; }
54 void setValue(float value) { mValue = MAX(mMin, MIN(mMax, value)); }
55 operator float() const { return mValue; }
56 operator uint8_t() const { return static_cast<uint8_t>(mValue); }
57 operator uint16_t() const { return static_cast<uint16_t>(mValue); }
58 operator int() const { return static_cast<int>(mValue); }
59 template <typename T> T as() const { return static_cast<T>(mValue); }
60
61 Slider& operator=(float value) { setValue(value); return *this; }
62 Slider& operator=(int value) { setValue(static_cast<float>(value)); return *this; }
63 private:
64 float mValue;
65 float mMin;
66 float mMax;
67};
68
69
70// template operator for >= against a jsSlider
71
72
73
74#endif
75
76#if !FASTLED_HAS_UI_BUTTON
77
78class Button {
79 public:
80 Button(const char *name) {
81 FASTLED_UNUSED(name);
82 }
83 ~Button() {}
84 bool isPressed() const { return false; }
85 bool clicked() const { return false; }
86 int clickedCount() const { return 0; }
87 operator bool() const { return false; }
88};
89
90#endif
91
92#if !FASTLED_HAS_UI_CHECKBOX
93
94class Checkbox {
95 public:
96 Checkbox(const char *name, bool value = false) : mValue(value) {
97 FASTLED_UNUSED(name);
98 }
99 ~Checkbox() {}
100 operator bool() const { return mValue; }
101 operator int() const { return mValue ? 1 : 0; }
102 Checkbox& operator=(bool value) { setValue(value); return *this; }
103 Checkbox& operator=(int value) { setValue(value != 0); return *this; }
104 private:
105 void setValue(bool value) { mValue = value; }
106 bool mValue;
107};
108
109#endif
110
111#if !FASTLED_HAS_UI_NUMBER_FIELD
112
114 public:
115 NumberField(const char *name, double value, double min = 0, double max = 100)
116 : mValue(value), mMin(MIN(min, max)), mMax(MAX(min, max)) {
117 FASTLED_UNUSED(name);
118 }
119 ~NumberField() {}
120 double value() const { return mValue; }
121 void setValue(double value) { mValue = MAX(mMin, MIN(mMax, value)); }
122 operator double() const { return mValue; }
123 operator int() const { return static_cast<int>(mValue); }
124 NumberField& operator=(double value) { setValue(value); return *this; }
125 NumberField& operator=(int value) { setValue(static_cast<double>(value)); return *this; }
126 private:
127 double mValue;
128 double mMin;
129 double mMax;
130};
131
132#endif
133
134#if !FASTLED_HAS_UI_TITLE
135
136
137class Title {
138 public:
139 Title(const char *name) {
140 FASTLED_UNUSED(name);
141 }
142 ~Title() {}
143};
144
145#endif
146
147
148
149
150#if !FASTLED_HAS_UI_DESCRIPTION
151
153 public:
154 Description(const char *name) {
155 FASTLED_UNUSED(name);
156 }
157 ~Description() {}
158};
159
160#endif
161
162
163#define FASTLED_UI_DEFINE_OPERATORS(UI_CLASS) \
164FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, >=) \
165FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, <=) \
166FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, >) \
167FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, <) \
168FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, ==) \
169FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, !=)
170
171FASTLED_UI_DEFINE_OPERATORS(Slider);
172FASTLED_UI_DEFINE_OPERATORS(NumberField);
173FASTLED_UI_DEFINE_OPERATORS(Checkbox);
174FASTLED_UI_DEFINE_OPERATORS(Button);
175
176} // end namespace fl
177
178
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16