Control Surface main
MIDI Control Surface library for Arduino
NoteCCKPRangeFastLED.hpp
Go to the documentation of this file.
1#if defined(ARDUINO) && defined(FASTLED_VERSION)
2#include <FastLED.h>
3#endif
4
5#include <AH/STL/type_traits> // std::enable_if, std::is_default_constructible
6#include <Settings/NamespaceSettings.hpp>
7#include <stdint.h>
8
10
12struct Color {
13 uint8_t r;
14 uint8_t g;
15 uint8_t b;
16};
17
20Color velocityToNovationColor(uint8_t value);
21
23
24#ifdef FASTLED_VERSION
25
27
29
35 CRGB operator()(uint8_t value, uint8_t index) const {
36 (void)index;
38 return CRGB {c.r, c.g, c.b};
39 }
40};
41
43using index_permuter_f = uint8_t (*)(uint8_t);
44
60template <MIDIMessageType Type, uint8_t RangeLen, class ColorMapper>
62 : public MatchingMIDIInputElement<Type, TwoByteRangeMIDIMatcher> {
63 public:
65
67 const ColorMapper &colormapper)
69 {address, RangeLen}),
71
73 MIDIAddress address, const ColorMapper &colormapper)
74 : NoteCCKPRangeFastLED(ledcolors.data, address, colormapper) {}
75
77 const ColorMapper &colormapper,
78 index_permuter_f index_permuter)
80 {address, RangeLen}),
82 ledIndexPermuter(index_permuter) {}
83
85 MIDIAddress address, const ColorMapper &colormapper,
86 index_permuter_f index_permuter)
88 index_permuter) {}
89
90 template <class ColorMapper_ = ColorMapper>
92 typename std::enable_if<std::is_default_constructible<
93 ColorMapper_>::value>::type * = nullptr)
95 {address, RangeLen}),
97
98 template <class ColorMapper_ = ColorMapper>
100 MIDIAddress address,
101 typename std::enable_if<std::is_default_constructible<
102 ColorMapper_>::value>::type * = nullptr)
103 : NoteCCKPRangeFastLED(ledcolors.data, address) {}
104
110 void setBrightness(uint8_t brightness) { this->brightness = brightness; }
112 uint8_t getBrightness() const { return this->brightness; }
113
124 this->ledIndexPermuter = permuter ? permuter : identityPermuter;
125 }
126
127 void begin() override { resetLEDs(); }
128
129 void handleUpdate(typename Matcher::Result match) override {
130 updateLED(match.index, match.value);
131 }
132
133 void reset() override { resetLEDs(); }
134
135 void updateLED(uint8_t index, uint8_t value) {
136 // Apply the color mapper to convert the value and index to a color
137 CRGB newColor = CRGB(colormapper(value, index));
138 // Apply the brightness to the color
139 newColor = newColor.nscale8_video(brightness);
140 // Map the note index to the LED index
141 uint8_t ledIndex = ledIndexPermuter(index);
142 // Check if the color changed
143 dirty |= ledcolors[ledIndex] != newColor;
144 // Update the LED color
145 ledcolors[ledIndex] = newColor;
146 }
147
148 void resetLEDs() {
149 for (uint8_t index = 0; index < RangeLen; ++index)
150 updateLED(index, 0);
151 }
152
156 bool getDirty() const { return dirty; }
158 void clearDirty() { dirty = false; }
159
160 private:
162 bool dirty = true;
163 uint8_t brightness = 255;
165
166 static uint8_t identityPermuter(uint8_t i) { return i; }
167
168 public:
169 ColorMapper colormapper;
170};
171
172// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //
173// Everything below is just definitions of type aliases to make the library
174// easier to use.
175//
176// It defines MIDI elements that listen to (a single, a range of)
177// (MIDI Note, MIDI Control Change, MIDI Key Pressure) message(s) that display
178// the values of these messages using FastLED LEDs or LED strips.
179// An optional color mapper can be supplied that defines the mapping from a
180// MIDI value [0, 127] to an RGB color.
181// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: //
182
185
202template <uint8_t RangeLen, class ColorMapper = DefaultColorMapper>
205
220template <class ColorMapper = DefaultColorMapper>
223
240template <uint8_t RangeLen, class ColorMapper = DefaultColorMapper>
242 RangeLen, ColorMapper>;
243
259template <class ColorMapper = DefaultColorMapper>
262
279template <uint8_t RangeLen, class ColorMapper = DefaultColorMapper>
282
297template <class ColorMapper = DefaultColorMapper>
300
302
303namespace Bankable {
304
310 CRGB operator()(uint8_t value, uint8_t bankIndex, uint8_t index) const {
311 (void)bankIndex;
312 (void)index;
314 return CRGB {c.r, c.g, c.b};
315 }
316};
317
336template <MIDIMessageType Type, uint8_t BankSize, uint8_t RangeLen,
337 class ColorMapper>
338class NoteCCKPRangeFastLED : public NoteCCKPRange<Type, BankSize, RangeLen> {
339 public:
341 using Matcher = typename Parent::Matcher;
342
344 MIDIAddress address, const ColorMapper &colormapper)
345 : Parent(config, address), ledcolors(ledcolors),
347
350 MIDIAddress address, const ColorMapper &colormapper)
351 : NoteCCKPRangeFastLED(config, ledcolors.data, address, colormapper) {}
352
354 MIDIAddress address, const ColorMapper &colormapper,
355 index_permuter_f index_permuter)
356 : Parent(config, address), ledcolors(ledcolors),
357 colormapper(colormapper), ledIndexPermuter(index_permuter) {}
358
361 MIDIAddress address, const ColorMapper &colormapper,
362 index_permuter_f index_permuter)
363 : NoteCCKPRangeFastLED(config, ledcolors.data, address, colormapper,
364 index_permuter) {}
365
366 template <class ColorMapper_ = ColorMapper>
368 MIDIAddress address,
369 typename std::enable_if<std::is_default_constructible<
370 ColorMapper_>::value>::type * = nullptr)
371 : Parent(config, address), ledcolors(ledcolors) {}
372
373 template <class ColorMapper_ = ColorMapper>
376 MIDIAddress address,
377 typename std::enable_if<std::is_default_constructible<
378 ColorMapper_>::value>::type * = nullptr)
379 : NoteCCKPRangeFastLED(config, ledcolors.data, address) {}
380
386 void setBrightness(uint8_t brightness) { this->brightness = brightness; }
388 uint8_t getBrightness() const { return this->brightness; }
389
400 this->ledIndexPermuter = permuter ? permuter : identityPermuter;
401 }
402
403 void begin() override { updateLEDs(); }
404
405 void handleUpdate(typename Matcher::Result match) override {
406 bool newdirty = Parent::handleUpdateImpl(match);
407 if (newdirty)
408 updateLED(match.bankIndex, match.index, match.value);
409 this->dirty |= newdirty;
410 }
411
412 void reset() override {
414 updateLEDs();
415 }
416
417 void updateLED(uint8_t bankIndex, uint8_t index, uint8_t value) {
418 // Apply the color mapper to convert the value and index to a color
419 CRGB newColor = CRGB(colormapper(value, bankIndex, index));
420 // Apply the brightness to the color
421 newColor = newColor.nscale8_video(brightness);
422 // Map the note index to the LED index
423 uint8_t ledIndex = ledIndexPermuter(index);
424 // Update the LED color
425 ledcolors[ledIndex] = newColor;
426 }
427
428 void updateLEDs() {
429 const auto bankIndex = this->getActiveBank();
430 for (uint8_t index = 0; index < RangeLen; ++index)
431 updateLED(bankIndex, index, this->getValue(bankIndex, index));
432 }
433
434 protected:
435 void onBankSettingChange() override {
437 updateLEDs();
438 }
439
440 private:
442 uint8_t brightness = 255;
444
445 static uint8_t identityPermuter(uint8_t i) { return i; }
446
447 public:
448 ColorMapper colormapper;
449};
450
453
473template <uint8_t BankSize, uint8_t RangeLen,
474 class ColorMapper = DefaultColorMapper>
476 BankSize, RangeLen, ColorMapper>;
477
479template <uint8_t BankSize, class ColorMapper = DefaultColorMapper>
481
483template <uint8_t BankSize, uint8_t RangeLen,
484 class ColorMapper = DefaultColorMapper>
486 BankSize, RangeLen, ColorMapper>;
487
489template <uint8_t BankSize, class ColorMapper = DefaultColorMapper>
491
493template <uint8_t BankSize, uint8_t RangeLen,
494 class ColorMapper = DefaultColorMapper>
496 BankSize, RangeLen, ColorMapper>;
497
499template <uint8_t BankSize, class ColorMapper = DefaultColorMapper>
501
503
504} // namespace Bankable
505
507
508#endif
MIDIMessageType
All possible MIDI status byte values (without channel).
Color velocityToNovationColor(uint8_t value)
The default mapping from a 7-bit MIDI value to an RGB color, using the Novation Launchpad mapping.
uint8_t(*)(uint8_t) index_permuter_f
Function pointer type to permute indices.
A structure for RGB colors.
#define END_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Generic base class for classes that listen for MIDI Note, Control Change and Key Pressure events on a...
void setBrightness(uint8_t brightness)
Set the maximum brightness of the LEDs.
NoteCCKPRangeFastLED(BankConfig< BankSize > config, AH::Array< CRGB, RangeLen > &ledcolors, MIDIAddress address, const ColorMapper &colormapper)
void setLEDIndexPermuter(index_permuter_f permuter)
Change the mapping from the MIDI index to the LED index.
NoteCCKPRangeFastLED(BankConfig< BankSize > config, AH::Array< CRGB, RangeLen > &ledcolors, MIDIAddress address, const ColorMapper &colormapper, index_permuter_f index_permuter)
uint8_t getBrightness() const
Get the maximum brightness of the LEDs.
void updateLED(uint8_t bankIndex, uint8_t index, uint8_t value)
void onBankSettingChange() override
A function to be executed each time the bank setting changes.
void begin() override
Initialize the input element.
NoteCCKPRangeFastLED(BankConfig< BankSize > config, AH::Array< CRGB, RangeLen > &ledcolors, MIDIAddress address, typename std::enable_if< std::is_default_constructible< ColorMapper_ >::value >::type *=nullptr)
NoteCCKPRangeFastLED(BankConfig< BankSize > config, CRGB *ledcolors, MIDIAddress address, typename std::enable_if< std::is_default_constructible< ColorMapper_ >::value >::type *=nullptr)
static uint8_t identityPermuter(uint8_t i)
NoteCCKPRangeFastLED(BankConfig< BankSize > config, CRGB *ledcolors, MIDIAddress address, const ColorMapper &colormapper)
void handleUpdate(typename Matcher::Result match) override
NoteCCKPRangeFastLED(BankConfig< BankSize > config, CRGB *ledcolors, MIDIAddress address, const ColorMapper &colormapper, index_permuter_f index_permuter)
void reset() override
Reset the input element to its initial state.
Generic base class for classes that listen for MIDI Note, Control Change and Key Pressure events on a...
BankableTwoByteRangeMIDIMatcher< BankSize > Matcher
uint8_t getValue(uint8_t index) const
Get the most recent MIDI value that was received for the active bank.
void onBankSettingChange() override
A function to be executed each time the bank setting changes.
bool handleUpdateImpl(typename Matcher::Result match)
void reset() override
Reset all values to zero.
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...
The MIDIInputElement base class is very general: you give it a MIDI message, and it calls the updateW...
Generic base class for classes that listen for MIDI Note, Control Change and Key Pressure events on a...
void setBrightness(uint8_t brightness)
Set the maximum brightness of the LEDs.
void setLEDIndexPermuter(index_permuter_f permuter)
Change the mapping from the MIDI index to the LED index.
NoteCCKPRangeFastLED(CRGB *ledcolors, MIDIAddress address, const ColorMapper &colormapper, index_permuter_f index_permuter)
NoteCCKPRangeFastLED(AH::Array< CRGB, RangeLen > &ledcolors, MIDIAddress address, const ColorMapper &colormapper)
NoteCCKPRangeFastLED(AH::Array< CRGB, RangeLen > &ledcolors, MIDIAddress address, typename std::enable_if< std::is_default_constructible< ColorMapper_ >::value >::type *=nullptr)
uint8_t getBrightness() const
Get the maximum brightness of the LEDs.
bool getDirty() const
Check if the colors changed since the last time the dirty flag was cleared.
NoteCCKPRangeFastLED(CRGB *ledcolors, MIDIAddress address, const ColorMapper &colormapper)
void begin() override
Initialize the input element.
NoteCCKPRangeFastLED(CRGB *ledcolors, MIDIAddress address, typename std::enable_if< std::is_default_constructible< ColorMapper_ >::value >::type *=nullptr)
index_permuter_f ledIndexPermuter
NoteCCKPRangeFastLED(AH::Array< CRGB, RangeLen > &ledcolors, MIDIAddress address, const ColorMapper &colormapper, index_permuter_f index_permuter)
static uint8_t identityPermuter(uint8_t i)
void handleUpdate(typename Matcher::Result match) override
void reset() override
Reset the input element to its initial state.
void clearDirty()
Clear the dirty flag.
void updateLED(uint8_t index, uint8_t value)
A namespace for MIDI elements that can be added to a Bank, to change their address or channel.
An array wrapper for easy copying, comparing, and iterating.
Definition: Array.hpp:36
The default mapping from a 7-bit MIDI value to an RGB color, using the Novation Launchpad mapping.
CRGB operator()(uint8_t value, uint8_t bankIndex, uint8_t index) const
Map from a 7-bit MIDI value to an RGB color, using the Novation Launchpad mapping.
The default mapping from a 7-bit MIDI value to an RGB color, using the Novation Launchpad mapping.
CRGB operator()(uint8_t value, uint8_t index) const
Map from a 7-bit MIDI value to an RGB color, using the Novation Launchpad mapping.
Matcher for MIDI messages with 2 data bytes, such as Note On/Off, Control Change, Key Pressure (but n...