Control Surface  1.1.1
MIDI Control Surface library for Arduino
FastLED.hpp
Go to the documentation of this file.
1 #if defined(ARDUINO) && defined(FASTLED_VERSION)
2 #include <FastLED.h>
3 #endif
4 
5 #include <Settings/NamespaceSettings.hpp>
6 #include <stdint.h>
7 
9 
11 struct Color {
12  uint8_t r;
13  uint8_t g;
14  uint8_t b;
15 #ifdef FASTLED_VERSION
16  explicit operator CRGB() const { return CRGB{r, g, b}; }
17 #endif
18 };
19 
24  Color operator()(uint8_t value, uint8_t index) const;
25 };
26 
28 
29 #ifdef FASTLED_VERSION
30 
32 
34 
36 using index_permuter_f = uint8_t (*)(uint8_t);
37 
40 template <class ColorMapper>
42  public:
43  NoteCCFastLEDCallback(CRGB *ledcolors, const ColorMapper &colormapper)
45 
46  NoteCCFastLEDCallback(CRGB *ledcolors, const ColorMapper &colormapper,
47  index_permuter_f index_permuter)
49  ledIndexPermuter(index_permuter) {}
50 
56  void setBrightness(uint8_t brightness) { this->brightness = brightness; }
58  uint8_t getBrightness() const { return this->brightness; }
59 
70  this->ledIndexPermuter = permuter ? permuter : identityPermuter;
71  }
72 
73  // Called once upon initialization.
74  void begin(const INoteCCValue &input) override { updateAll(input); }
75 
76  // Called each time a MIDI message is received and an LED has to be updated.
77  // @param input
78  // The NoteCCRange or NoteCCValue object this callback belongs to.
79  // This is the object that actually receives and stores the MIDI
80  // values.
81  // @param index
82  // The index of the value that changed. (zero-based)
83  void update(const INoteCCValue &input, uint8_t index) override {
84  // Get the MIDI value that changed [0, 127]
85  uint8_t value = input.getValue(index);
86  // Apply the color mapper to convert the value to a color
87  CRGB newColor = CRGB(colormapper(value, index));
88  // Apply the brightness to the color
89  newColor = newColor.nscale8_video(brightness);
90  // Map the note index to the LED index
91  uint8_t ledIndex = ledIndexPermuter(index);
92  // Update the LED color
93  ledcolors[ledIndex] = newColor;
94  }
95 
96  private:
97  CRGB *ledcolors;
98  uint8_t brightness = 255;
100 
101  static uint8_t identityPermuter(uint8_t i) { return i; }
102 
103  public:
104  ColorMapper colormapper;
105 };
106 
107 // :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //
108 // Everything below is just definitions of type aliases to make the library
109 // easier to use.
110 //
111 // It defines MIDI elements that listen to (a single, a range of)
112 // (MIDI Note, MIDI Control Change) message(s) that display the values of
113 // these messages using FastLED LEDs or LED strips.
114 // An optional color mapper can be supplied that defines the mapping from a
115 // MIDI value [0, 127] to an RGB color.
116 // :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //
117 
120 
135 template <uint8_t RangeLen, class ColorMapper = DefaultColorMapper>
137  : public GenericNoteCCRange<MIDIInputElementNote, RangeLen,
138  NoteCCFastLEDCallback<ColorMapper>> {
139  public:
157  const ColorMapper &colormapper = {})
160  address,
161  {leds.data, colormapper},
162  } {}
163 
181  const ColorMapper &colormapper = {})
184  address,
185  {leds, colormapper},
186  } {}
187 
189  void setBrightness(uint8_t brightness) {
190  this->callback.setBrightness(brightness);
191  }
193  uint8_t getBrightness() const { return this->callback.getBrightness(); }
196  this->callback.setLEDIndexPermuter(permuter);
197  }
198 };
199 
200 template <class ColorMapper = DefaultColorMapper>
202  : public GenericNoteCCRange<MIDIInputElementNote, 1,
203  NoteCCFastLEDCallback<ColorMapper>> {
204  public:
206  const ColorMapper &colormapper = {})
209  address,
210  {&led, colormapper},
211  } {}
212 
214  void setBrightness(uint8_t brightness) {
215  this->callback.setBrightness(brightness);
216  }
218  uint8_t getBrightness() const { return this->callback.getBrightness(); }
219 };
220 
221 template <uint8_t RangeLen, class ColorMapper = DefaultColorMapper>
223  : public GenericNoteCCRange<MIDIInputElementCC, RangeLen,
224  NoteCCFastLEDCallback<ColorMapper>> {
225 
226  public:
228  const ColorMapper &colormapper = {})
231  address,
232  {leds.data, colormapper},
233  } {}
234 
236  const ColorMapper &colormapper = {})
239  address,
240  {leds, colormapper},
241  } {}
242 
244  void setBrightness(uint8_t brightness) {
245  this->callback.setBrightness(brightness);
246  }
248  uint8_t getBrightness() const { return this->callback.getBrightness(); }
251  this->callback.setLEDIndexPermuter(permuter);
252  }
253 };
254 
255 template <class ColorMapper = DefaultColorMapper>
257  : public GenericNoteCCRange<MIDIInputElementCC, 1,
258  NoteCCFastLEDCallback<ColorMapper>> {
259  public:
261  const ColorMapper &colormapper = {})
264  address,
265  {&led, colormapper},
266  } {}
267 
269  void setBrightness(uint8_t brightness) {
270  this->callback.setBrightness(brightness);
271  }
273  uint8_t getBrightness() const { return this->callback.getBrightness(); }
274 };
275 
277 
278 namespace Bankable {
279 
282 
283 template <uint8_t RangeLen, uint8_t BankSize,
284  class ColorMapper = DefaultColorMapper>
286  : public GenericNoteCCRange<MIDIInputElementNote, RangeLen, BankSize,
287  NoteCCFastLEDCallback<ColorMapper>> {
288  public:
291  const ColorMapper &colormapper = {})
292  : GenericNoteCCRange<MIDIInputElementNote, RangeLen, BankSize,
294  config,
295  address,
296  {leds.data, colormapper},
297  } {}
298 
301  const ColorMapper &colormapper = {})
302  : GenericNoteCCRange<MIDIInputElementNote, RangeLen, BankSize,
304  config,
305  address,
306  {leds, colormapper},
307  } {}
308 
310  void setBrightness(uint8_t brightness) {
311  this->callback.setBrightness(brightness);
312  }
314  uint8_t getBrightness() const { return this->callback.getBrightness(); }
317  this->callback.setLEDIndexPermuter(permuter);
318  }
319 };
320 
321 template <uint8_t BankSize, class ColorMapper = DefaultColorMapper>
323  : public GenericNoteCCRange<MIDIInputElementNote, 1, BankSize,
324  NoteCCFastLEDCallback<ColorMapper>> {
325  public:
328  const ColorMapper &colormapper = {})
331  config,
332  address,
333  {&led, colormapper},
334  } {}
335 
337  void setBrightness(uint8_t brightness) {
338  this->callback.setBrightness(brightness);
339  }
341  uint8_t getBrightness() const { return this->callback.getBrightness(); }
342 };
343 
344 template <uint8_t RangeLen, uint8_t BankSize,
345  class ColorMapper = DefaultColorMapper>
347  : public GenericNoteCCRange<MIDIInputElementCC, RangeLen, BankSize,
348  NoteCCFastLEDCallback<ColorMapper>> {
349  public:
352  const ColorMapper &colormapper = {})
353  : GenericNoteCCRange<MIDIInputElementCC, RangeLen, BankSize,
355  config,
356  address,
357  {leds.data, colormapper},
358  } {}
359 
362  const ColorMapper &colormapper = {})
363  : GenericNoteCCRange<MIDIInputElementCC, RangeLen, BankSize,
365  config,
366  address,
367  {leds, colormapper},
368  } {}
369 
371  void setBrightness(uint8_t brightness) {
372  this->callback.setBrightness(brightness);
373  }
375  uint8_t getBrightness() const { return this->callback.getBrightness(); }
378  this->callback.setLEDIndexPermuter(permuter);
379  }
380 };
381 
382 template <uint8_t BankSize, class ColorMapper = DefaultColorMapper>
384  : public GenericNoteCCRange<MIDIInputElementCC, 1, BankSize,
385  NoteCCFastLEDCallback<ColorMapper>> {
386  public:
389  const ColorMapper &colormapper = {})
390  : GenericNoteCCRange<MIDIInputElementCC, 1, BankSize,
392  config,
393  address,
394  {&led, colormapper},
395  } {}
396 
398  void setBrightness(uint8_t brightness) {
399  this->callback.setBrightness(brightness);
400  }
402  uint8_t getBrightness() const { return this->callback.getBrightness(); }
403 };
404 
406 
407 } // namespace Bankable
408 
410 
411 #endif
MIDIInputElement::address
const MIDICNChannelAddress address
Definition: MIDIInputElement.hpp:80
DefaultColorMapper
The default mapping from a 7-bit MIDI value to an RGB color.
Definition: FastLED.hpp:22
Bankable::NoteRangeFastLED::getBrightness
uint8_t getBrightness() const
Get the maximum brightness of the LEDs.
Definition: FastLED.hpp:314
NoteRangeFastLED::NoteRangeFastLED
NoteRangeFastLED(Array< CRGB, RangeLen > &leds, MIDICNChannelAddress address, const ColorMapper &colormapper={})
Construct a new NoteRangeFastLED object.
Definition: FastLED.hpp:156
MIDIInputElementNote
Class for objects that listen for incoming MIDI Note events.
Definition: MIDIInputElementNote.hpp:20
NoteCCFastLEDCallback::update
void update(const INoteCCValue &input, uint8_t index) override
Update the given index: called when a new message is received for this index.
Definition: FastLED.hpp:83
Color::r
uint8_t r
Definition: FastLED.hpp:12
Bankable
A namespace for MIDI elements that can be added to a Bank, to change their address or channel.
Definition: BankAddresses.hpp:7
Bankable::CCValueFastLED
Definition: FastLED.hpp:383
SimpleNoteCCValueCallback::updateAll
virtual void updateAll(const INoteCCValue &noteccval)
Update all values: called when a bank change causes all values to (possibly) change,...
Definition: NoteCCRange.hpp:58
CCRangeFastLED::setBrightness
void setBrightness(uint8_t brightness)
Set the maximum brightness of the LEDs.
Definition: FastLED.hpp:244
NoteRangeFastLED::getBrightness
uint8_t getBrightness() const
Get the maximum brightness of the LEDs.
Definition: FastLED.hpp:193
GenericNoteCCRange
Definition: NoteCCRange.hpp:155
INoteCCValue
Interface for NoteCCValue objects: provides getters for the velocity or controller values.
Definition: NoteCCRange.hpp:13
NoteRangeFastLED::setLEDIndexPermuter
void setLEDIndexPermuter(index_permuter_f permuter)
Change the mapping from the MIDI index to the LED index.
Definition: FastLED.hpp:195
Bankable::NoteValueFastLED::getBrightness
uint8_t getBrightness() const
Get the maximum brightness of the LEDs.
Definition: FastLED.hpp:341
NoteCCFastLEDCallback
Callback for Note or CC range or value input that displays the value to a FastLED strip.
Definition: FastLED.hpp:41
NoteRangeFastLED::setBrightness
void setBrightness(uint8_t brightness)
Set the maximum brightness of the LEDs.
Definition: FastLED.hpp:189
NoteRangeFastLED
MIDI Input Element that listens for MIDI Note messages in a given range, and displays their values us...
Definition: FastLED.hpp:136
SimpleNoteCCValueCallback
A callback for NoteCCRange with an action that can be implemented by the user.
Definition: NoteCCRange.hpp:46
Bankable::GenericNoteCCRange
Definition: NoteCCRange.hpp:247
CCValueFastLED::getBrightness
uint8_t getBrightness() const
Get the maximum brightness of the LEDs.
Definition: FastLED.hpp:273
index_permuter_f
uint8_t(*)(uint8_t) index_permuter_f
Function pointer type to permute indices.
Definition: FastLED.hpp:36
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
MIDIInputElementCC
Class for objects that listen for incoming MIDI Controller Change events.
Definition: MIDIInputElementCC.hpp:23
Bankable::CCValueFastLED::CCValueFastLED
CCValueFastLED(BankConfig< BankSize > config, CRGB &led, MIDICNChannelAddress address, const ColorMapper &colormapper={})
Definition: FastLED.hpp:387
Bankable::NoteRangeFastLED::setLEDIndexPermuter
void setLEDIndexPermuter(index_permuter_f permuter)
Change the mapping from the MIDI index to the LED index.
Definition: FastLED.hpp:316
NoteCCFastLEDCallback::setBrightness
void setBrightness(uint8_t brightness)
Set the maximum brightness of the LEDs.
Definition: FastLED.hpp:56
CCRangeFastLED::setLEDIndexPermuter
void setLEDIndexPermuter(index_permuter_f permuter)
Change the mapping from the MIDI index to the LED index.
Definition: FastLED.hpp:250
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
Bankable::NoteRangeFastLED::setBrightness
void setBrightness(uint8_t brightness)
Set the maximum brightness of the LEDs.
Definition: FastLED.hpp:310
Bankable::CCRangeFastLED::getBrightness
uint8_t getBrightness() const
Get the maximum brightness of the LEDs.
Definition: FastLED.hpp:375
NoteCCRange< MIDIInputElementNote, RangeLen, 1, NoteCCFastLEDCallback< ColorMapper > >::callback
NoteCCFastLEDCallback< ColorMapper > callback
Callback that is called when a value in the active bank changes.
Definition: NoteCCRange.hpp:146
NoteRangeFastLED::NoteRangeFastLED
NoteRangeFastLED(CRGB *leds, MIDICNChannelAddress address, const ColorMapper &colormapper={})
Construct a new NoteRangeFastLED object.
Definition: FastLED.hpp:180
Bankable::CCValueFastLED::setBrightness
void setBrightness(uint8_t brightness)
Set the maximum brightness of the LEDs.
Definition: FastLED.hpp:398
Bankable::NoteValueFastLED::setBrightness
void setBrightness(uint8_t brightness)
Set the maximum brightness of the LEDs.
Definition: FastLED.hpp:337
Bankable::NoteValueFastLED
Definition: FastLED.hpp:322
MIDICNChannelAddress
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...
Definition: MIDICNChannelAddress.hpp:82
AH::Array
An array wrapper for easy copying, comparing, and iterating.
Definition: Array.hpp:36
CCRangeFastLED::CCRangeFastLED
CCRangeFastLED(Array< CRGB, RangeLen > &leds, MIDICNChannelAddress address, const ColorMapper &colormapper={})
Definition: FastLED.hpp:227
NoteValueFastLED::NoteValueFastLED
NoteValueFastLED(CRGB &led, MIDICNChannelAddress address, const ColorMapper &colormapper={})
Definition: FastLED.hpp:205
Bankable::CCRangeFastLED::CCRangeFastLED
CCRangeFastLED(BankConfig< BankSize > config, Array< CRGB, RangeLen > &leds, MIDICNChannelAddress address, const ColorMapper &colormapper={})
Definition: FastLED.hpp:350
Bankable::NoteValueFastLED::NoteValueFastLED
NoteValueFastLED(BankConfig< BankSize > config, CRGB &led, MIDICNChannelAddress address, const ColorMapper &colormapper={})
Definition: FastLED.hpp:326
DefaultColorMapper::operator()
Color operator()(uint8_t value, uint8_t index) const
Map from a 7-bit MIDI value to an RGB color.
Definition: FastLED.cpp:8
NoteCCFastLEDCallback::ledIndexPermuter
index_permuter_f ledIndexPermuter
Definition: FastLED.hpp:99
Bankable::CCValueFastLED::getBrightness
uint8_t getBrightness() const
Get the maximum brightness of the LEDs.
Definition: FastLED.hpp:402
NoteValueFastLED::setBrightness
void setBrightness(uint8_t brightness)
Set the maximum brightness of the LEDs.
Definition: FastLED.hpp:214
CCValueFastLED::setBrightness
void setBrightness(uint8_t brightness)
Set the maximum brightness of the LEDs.
Definition: FastLED.hpp:269
CCValueFastLED
Definition: FastLED.hpp:256
NoteCCFastLEDCallback::brightness
uint8_t brightness
Definition: FastLED.hpp:98
NoteCCFastLEDCallback::identityPermuter
static uint8_t identityPermuter(uint8_t i)
Definition: FastLED.hpp:101
CCValueFastLED::CCValueFastLED
CCValueFastLED(CRGB &led, MIDICNChannelAddress address, const ColorMapper &colormapper={})
Definition: FastLED.hpp:260
INoteCCValue::getValue
virtual uint8_t getValue(uint8_t index) const =0
Get the velocity or controller value for the given index in the range.
NoteCCFastLEDCallback::setLEDIndexPermuter
void setLEDIndexPermuter(index_permuter_f permuter)
Change the mapping from the MIDI index to the LED index.
Definition: FastLED.hpp:69
Bankable::CCRangeFastLED::setBrightness
void setBrightness(uint8_t brightness)
Set the maximum brightness of the LEDs.
Definition: FastLED.hpp:371
NoteCCFastLEDCallback::colormapper
ColorMapper colormapper
Definition: FastLED.hpp:104
CCRangeFastLED::getBrightness
uint8_t getBrightness() const
Get the maximum brightness of the LEDs.
Definition: FastLED.hpp:248
NoteValueFastLED::getBrightness
uint8_t getBrightness() const
Get the maximum brightness of the LEDs.
Definition: FastLED.hpp:218
Bankable::NoteRangeFastLED
Definition: FastLED.hpp:285
Bankable::NoteRangeFastLED::NoteRangeFastLED
NoteRangeFastLED(BankConfig< BankSize > config, CRGB *leds, MIDICNChannelAddress address, const ColorMapper &colormapper={})
Definition: FastLED.hpp:299
Bankable::CCRangeFastLED::CCRangeFastLED
CCRangeFastLED(BankConfig< BankSize > config, CRGB *leds, MIDICNChannelAddress address, const ColorMapper &colormapper={})
Definition: FastLED.hpp:360
AH::Array::data
T data[N]
Definition: Array.hpp:37
NoteValueFastLED
Definition: FastLED.hpp:201
BankConfig
A struct for selecting the bank of BankableMIDIInputs and the bank type.
Definition: BankConfig.hpp:39
Color::b
uint8_t b
Definition: FastLED.hpp:14
NoteCCFastLEDCallback::ledcolors
CRGB * ledcolors
Definition: FastLED.hpp:97
Bankable::CCRangeFastLED::setLEDIndexPermuter
void setLEDIndexPermuter(index_permuter_f permuter)
Change the mapping from the MIDI index to the LED index.
Definition: FastLED.hpp:377
Color::g
uint8_t g
Definition: FastLED.hpp:13
Bankable::CCRangeFastLED
Definition: FastLED.hpp:346
Bankable::NoteRangeFastLED::NoteRangeFastLED
NoteRangeFastLED(BankConfig< BankSize > config, Array< CRGB, RangeLen > &leds, MIDICNChannelAddress address, const ColorMapper &colormapper={})
Definition: FastLED.hpp:289
NoteCCFastLEDCallback::NoteCCFastLEDCallback
NoteCCFastLEDCallback(CRGB *ledcolors, const ColorMapper &colormapper, index_permuter_f index_permuter)
Definition: FastLED.hpp:46
NoteCCRange.hpp
NoteCCFastLEDCallback::begin
void begin(const INoteCCValue &input) override
Initialize: called once.
Definition: FastLED.hpp:74
Color
A structure for RGB colors.
Definition: FastLED.hpp:11
CCRangeFastLED::CCRangeFastLED
CCRangeFastLED(CRGB *leds, MIDICNChannelAddress address, const ColorMapper &colormapper={})
Definition: FastLED.hpp:235
NoteCCFastLEDCallback::NoteCCFastLEDCallback
NoteCCFastLEDCallback(CRGB *ledcolors, const ColorMapper &colormapper)
Definition: FastLED.hpp:43
CCRangeFastLED
Definition: FastLED.hpp:222
NoteCCFastLEDCallback::getBrightness
uint8_t getBrightness() const
Get the maximum brightness of the LEDs.
Definition: FastLED.hpp:58