FastLED 3.9.15
Loading...
Searching...
No Matches
ui_impl.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4
5#include "fl/audio.h"
6#include "fl/math_macros.h"
7#include "fl/namespace.h"
8#include "fl/str.h"
9#include "fl/template_magic.h"
10#include "fl/unused.h"
11#include "fl/warn.h"
12#include "platforms/ui_defs.h"
13
14#ifndef FASTLED_HAS_UI_SLIDER
15#define FASTLED_HAS_UI_SLIDER 0
16#endif
17
18#ifndef FASTLED_HAS_UI_BUTTON
19#define FASTLED_HAS_UI_BUTTON 0
20#endif
21
22#ifndef FASTLED_HAS_UI_CHECKBOX
23#define FASTLED_HAS_UI_CHECKBOX 0
24#endif
25
26#ifndef FASTLED_HAS_UI_NUMBER_FIELD
27#define FASTLED_HAS_UI_NUMBER_FIELD 0
28#endif
29
30#ifndef FASTLED_HAS_UI_TITLE
31#define FASTLED_HAS_UI_TITLE 0
32#endif
33
34#ifndef FASTLED_HAS_UI_DESCRIPTION
35#define FASTLED_HAS_UI_DESCRIPTION 0
36#endif
37
38#ifndef FASTLED_HAS_UI_AUDIO
39#define FASTLED_HAS_UI_AUDIO 0
40#endif
41
42namespace fl {
43
44// If the platform is missing ui components, provide stubs.
45
46#if !FASTLED_HAS_UI_SLIDER
47
49 public:
50 // If step is -1, it will be calculated as (max - min) / 100
51 UISliderImpl(const char *name, float value = 128.0f, float min = 1,
52 float max = 255, float step = -1.f)
53 : mValue(value), mMin(MIN(min, max)), mMax(MAX(min, max)) {
54 FASTLED_UNUSED(name);
55 FASTLED_UNUSED(step);
56 if (value < min) {
57 mValue = min;
58 }
59 if (value > max) {
60 mValue = max;
61 }
62 }
64 float value() const { return mValue; }
65 float getMax() const { return mMax; }
66 float getMin() const { return mMin; }
67 void setValue(float value) { mValue = MAX(mMin, MIN(mMax, value)); }
68 operator float() const { return mValue; }
69 operator uint8_t() const { return static_cast<uint8_t>(mValue); }
70 operator uint16_t() const { return static_cast<uint16_t>(mValue); }
71 operator int() const { return static_cast<int>(mValue); }
72 template <typename T> T as() const { return static_cast<T>(mValue); }
73
74 int as_int() const { return static_cast<int>(mValue); }
75
78 return *this;
79 }
81 setValue(static_cast<float>(value));
82 return *this;
83 }
84
85 private:
86 float mValue;
87 float mMin;
88 float mMax;
89};
90
91// template operator for >= against a jsSliderImpl
92
93#endif
94
95#if !FASTLED_HAS_UI_BUTTON
96
98 public:
101 bool isPressed() const { return false; }
102 bool clicked() const { return false; }
103 int clickedCount() const { return 0; }
104 operator bool() const { return false; }
105 void click() {}
106 fl::Str name() const { return mName; }
107
108 private:
110};
111
112#endif
113
114#if !FASTLED_HAS_UI_CHECKBOX
115
117 public:
118 UICheckboxImpl(const char *name, bool value = false) : mValue(value) {
119 FASTLED_UNUSED(name);
120 }
122 operator bool() const { return mValue; }
123 explicit operator int() const { return mValue ? 1 : 0; }
126 return *this;
127 }
129 setValue(value != 0);
130 return *this;
131 }
132 bool value() const { return mValue; }
133
134 private:
135 void setValue(bool value) { mValue = value; }
136 bool mValue;
137};
138
139#endif
140
141#if !FASTLED_HAS_UI_NUMBER_FIELD
142
144 public:
145 UINumberFieldImpl(const char *name, double value, double min = 0,
146 double max = 100)
147 : mValue(value), mMin(MIN(min, max)), mMax(MAX(min, max)) {
148 FASTLED_UNUSED(name);
149 }
151 double value() const { return mValue; }
152 void setValue(double value) { mValue = MAX(mMin, MIN(mMax, value)); }
153 operator double() const { return mValue; }
154 operator int() const { return static_cast<int>(mValue); }
157 return *this;
158 }
160 setValue(static_cast<double>(value));
161 return *this;
162 }
163
164 private:
165 double mValue;
166 double mMin;
167 double mMax;
168};
169
170#endif
171
172#if !FASTLED_HAS_UI_TITLE
173
175 public:
176 UITitleImpl(const char *name) { FASTLED_UNUSED(name); }
178};
179
180#endif
181
182#if !FASTLED_HAS_UI_DESCRIPTION
183
185 public:
186 UIDescriptionImpl(const char *name) { FASTLED_UNUSED(name); }
188};
189
190#endif
191
192#if !FASTLED_HAS_UI_AUDIO
194 public:
195 UIAudioImpl(const char *name) { FASTLED_UNUSED(name); }
197
199 FASTLED_WARN("Audio sample not implemented");
200 return AudioSample();
201 }
202
203 bool hasNext() {
204 FASTLED_WARN("Audio sample not implemented");
205 return false;
206 }
207};
208#endif
209
210} // end namespace fl
Definition str.h:388
bool hasNext()
Definition ui_impl.h:203
AudioSample next()
Definition ui_impl.h:198
UIAudioImpl(const char *name)
Definition ui_impl.h:195
bool isPressed() const
Definition ui_impl.h:101
fl::Str name() const
Definition ui_impl.h:106
bool clicked() const
Definition ui_impl.h:102
UIButtonImpl(const char *name)
Definition ui_impl.h:99
int clickedCount() const
Definition ui_impl.h:103
fl::Str mName
Definition ui_impl.h:109
void setValue(bool value)
Definition ui_impl.h:135
UICheckboxImpl & operator=(int value)
Definition ui_impl.h:128
bool value() const
Definition ui_impl.h:132
UICheckboxImpl(const char *name, bool value=false)
Definition ui_impl.h:118
UICheckboxImpl & operator=(bool value)
Definition ui_impl.h:124
UIDescriptionImpl(const char *name)
Definition ui_impl.h:186
UINumberFieldImpl & operator=(int value)
Definition ui_impl.h:159
UINumberFieldImpl & operator=(double value)
Definition ui_impl.h:155
UINumberFieldImpl(const char *name, double value, double min=0, double max=100)
Definition ui_impl.h:145
double value() const
Definition ui_impl.h:151
void setValue(double value)
Definition ui_impl.h:152
float getMax() const
Definition ui_impl.h:65
UISliderImpl & operator=(int value)
Definition ui_impl.h:80
float value() const
Definition ui_impl.h:64
int as_int() const
Definition ui_impl.h:74
void setValue(float value)
Definition ui_impl.h:67
float getMin() const
Definition ui_impl.h:66
T as() const
Definition ui_impl.h:72
UISliderImpl & operator=(float value)
Definition ui_impl.h:76
UISliderImpl(const char *name, float value=128.0f, float min=1, float max=255, float step=-1.f)
Definition ui_impl.h:51
UITitleImpl(const char *name)
Definition ui_impl.h:176
#define MIN(a, b)
Definition math_macros.h:15
#define MAX(a, b)
Definition math_macros.h:11
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
#define FASTLED_UNUSED(x)
Definition unused.h:3
#define FASTLED_WARN
Definition warn.h:7