FastLED 3.9.15
Loading...
Searching...
No Matches
ui.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4
5#include "fl/math_macros.h"
6#include "fl/namespace.h"
7#include "fl/template_magic.h"
8#include "fl/unused.h"
9#include "platforms/ui_defs.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// If the platform is missing ui components, provide stubs.
38
39#if !FASTLED_HAS_UI_SLIDER
40
41class UISlider {
42 public:
43 // If step is -1, it will be calculated as (max - min) / 100
44 UISlider(const char *name, float value = 128.0f, float min = 1,
45 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) {
50 mValue = min;
51 }
52 if (value > max) {
53 mValue = max;
54 }
55 }
57 float value() const { return mValue; }
58 float value_normalized() const {
59 if (ALMOST_EQUAL(mMax, mMin, 0.0001f)) {
60 return 0;
61 }
62 return (mValue - mMin) / (mMax - mMin);
63 }
64 float max_value() const { return mMax; }
65 void setValue(float value) { mValue = MAX(mMin, MIN(mMax, value)); }
66 operator float() const { return mValue; }
67 operator uint8_t() const { return static_cast<uint8_t>(mValue); }
68 operator uint16_t() const { return static_cast<uint16_t>(mValue); }
69 operator int() const { return static_cast<int>(mValue); }
70 template <typename T> T as() const { return static_cast<T>(mValue); }
71
74 return *this;
75 }
77 setValue(static_cast<float>(value));
78 return *this;
79 }
80
81
82 private:
83 float mValue;
84 float mMin;
85 float mMax;
86};
87
88// template operator for >= against a jsSlider
89
90#endif
91
92#if !FASTLED_HAS_UI_BUTTON
93
94class UIButton {
95 public:
96 UIButton(const char *name) { FASTLED_UNUSED(name); }
98 bool isPressed() const { return false; }
99 bool clicked() const { return false; }
100 int clickedCount() const { return 0; }
101 operator bool() const { return false; }
102};
103
104#endif
105
106#if !FASTLED_HAS_UI_CHECKBOX
107
109 public:
110 UICheckbox(const char *name, bool value = false) : mValue(value) {
111 FASTLED_UNUSED(name);
112 }
114 operator bool() const { return mValue; }
115 operator int() const { return mValue ? 1 : 0; }
116 UICheckbox &operator=(bool value) {
117 setValue(value);
118 return *this;
119 }
120 UICheckbox &operator=(int value) {
121 setValue(value != 0);
122 return *this;
123 }
124
125 private:
126 void setValue(bool value) { mValue = value; }
127 bool mValue;
128};
129
130#endif
131
132#if !FASTLED_HAS_UI_NUMBER_FIELD
133
135 public:
136 UINumberField(const char *name, double value, double min = 0,
137 double max = 100)
138 : mValue(value), mMin(MIN(min, max)), mMax(MAX(min, max)) {
139 FASTLED_UNUSED(name);
140 }
142 double value() const { return mValue; }
143 void setValue(double value) { mValue = MAX(mMin, MIN(mMax, value)); }
144 operator double() const { return mValue; }
145 operator int() const { return static_cast<int>(mValue); }
148 return *this;
149 }
151 setValue(static_cast<double>(value));
152 return *this;
153 }
154
155 private:
156 double mValue;
157 double mMin;
158 double mMax;
159};
160
161#endif
162
163#if !FASTLED_HAS_UI_TITLE
164
165class UITitle {
166 public:
167 UITitle(const char *name) { FASTLED_UNUSED(name); }
169};
170
171#endif
172
173#if !FASTLED_HAS_UI_DESCRIPTION
174
176 public:
177 UIDescription(const char *name) { FASTLED_UNUSED(name); }
179};
180
181#endif
182
183#define FASTLED_UI_DEFINE_OPERATORS(UI_CLASS) \
184 FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, >=) \
185 FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, <=) \
186 FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, >) \
187 FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, <) \
188 FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, ==) \
189 FASTLED_DEFINE_POD_COMPARISON_OPERATOR(UI_CLASS, !=)
190
195
196} // end namespace fl
bool clicked() const
Definition ui.h:99
bool isPressed() const
Definition ui.h:98
UIButton(const char *name)
Definition ui.h:96
int clickedCount() const
Definition ui.h:100
~UIButton()
Definition ui.h:97
void setValue(bool value)
Definition ui.h:126
UICheckbox & operator=(bool value)
Definition ui.h:116
UICheckbox & operator=(int value)
Definition ui.h:120
bool mValue
Definition ui.h:127
~UICheckbox()
Definition ui.h:113
UICheckbox(const char *name, bool value=false)
Definition ui.h:110
UIDescription(const char *name)
Definition ui.h:177
double value() const
Definition ui.h:142
UINumberField & operator=(double value)
Definition ui.h:146
double mMin
Definition ui.h:157
UINumberField & operator=(int value)
Definition ui.h:150
double mValue
Definition ui.h:156
UINumberField(const char *name, double value, double min=0, double max=100)
Definition ui.h:136
double mMax
Definition ui.h:158
void setValue(double value)
Definition ui.h:143
UISlider & operator=(int value)
Definition ui.h:76
UISlider(const char *name, float value=128.0f, float min=1, float max=255, float step=-1.f)
Definition ui.h:44
T as() const
Definition ui.h:70
float mMin
Definition ui.h:84
~UISlider()
Definition ui.h:56
void setValue(float value)
Definition ui.h:65
float mMax
Definition ui.h:85
float mValue
Definition ui.h:83
UISlider & operator=(float value)
Definition ui.h:72
float value_normalized() const
Definition ui.h:58
float max_value() const
Definition ui.h:64
float value() const
Definition ui.h:57
~UITitle()
Definition ui.h:168
UITitle(const char *name)
Definition ui.h:167
#define ALMOST_EQUAL(a, b, small)
Definition math_macros.h:16
#define MIN(a, b)
Definition math_macros.h:8
#define MAX(a, b)
Definition math_macros.h:4
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
#define FASTLED_UI_DEFINE_OPERATORS(UI_CLASS)
Definition ui.h:183
#define FASTLED_UNUSED(x)
Definition unused.h:3