FastLED 3.9.15
Loading...
Searching...
No Matches
LuminescentGrand.ino
Go to the documentation of this file.
1// @filter: (memory is large) and not (platform is stm32)
2
9
10//#define DEBUG_PAINTER
11//#define DEBUG_KEYBOARD 1
12
13// Repeated keyboard presses in the main loop
14#define DEBUG_FORCED_KEYBOARD
15// #define DEBUG_MIDI_KEY 72
16
17#define MIDI_SERIAL_PORT Serial1
18
19#define FASTLED_UI // Brings in the UI components.
20#include "FastLED.h"
21
22// H
23#include <Arduino.h>
24#include "shared/Keyboard.h"
25#include "shared/color.h"
27#include "shared/Keyboard.h"
28#include "shared/Painter.h"
29#include "shared/settings.h"
30#include "arduino/LedRopeTCL.h"
31#include "arduino/ui_state.h"
32#include "shared/dprint.h"
33#include "fl/log/log.h"
34#include "fl/ui/ui.h"
36
37// Spoof the midi library so it thinks it's running on an arduino.
38//#ifndef ARDUINO
39//#define ARDUINO 1
40//#endif
41
42#ifdef MIDI_AUTO_INSTANCIATE
43#undef MIDI_AUTO_INSTANCIATE
44#define MIDI_AUTO_INSTANCIATE 0
45#endif
46
47#include "arduino/MIDI.h"
48
49
50MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MY_MIDI);
51
52
53FASTLED_TITLE("Luminescent Grand");
54FASTLED_DESCRIPTION("A midi keyboard visualizer.");
55
56
58// Light rope and keyboard.
61
62
64// Called when the note is pressed.
65// Input:
66// channel - Ignored.
67// midi_note - Value between 21-108 which maps to the keyboard keys.
68// velocity - Value between 0-127
69void HandleNoteOn(byte channel, byte midi_note, byte velocity) {
70 FL_UNUSED(channel);
71 FL_DBG("HandleNoteOn: midi_note = " << int(midi_note) << ", velocity = " << int(velocity));
72 keyboard.HandleNoteOn(midi_note, velocity, color_selector.curr_val(), fl::millis());
73}
74
76// Called when the note is released.
77// Input:
78// channel - Ignored.
79// midi_note - Value between 21-108 which maps to the keyboard keys.
80// velocity - Value between 0-127
81void HandleNoteOff(byte channel, byte midi_note, byte velocity) {
82 FL_UNUSED(channel);
83 FL_DBG("HandleNoteOn: midi_note = " << int(midi_note) << ", velocity = " << int(velocity));
84 keyboard.HandleNoteOff(midi_note, velocity, fl::millis());
85}
86
88// This is uninmplemented because the test keyboard didn't
89// have this functionality. Right now the only thing it does is
90// print out that the key was pressed.
91void HandleAfterTouchPoly(byte channel, byte note, byte pressure) {
92 FL_UNUSED(channel);
93 keyboard.HandleAfterTouchPoly(note, pressure);
94}
95
96
98// Detects whether the foot pedal has been touched.
99void HandleControlChange(byte channel, byte d1, byte d2) {
100 FL_UNUSED(channel);
101 keyboard.HandleControlChange(d1, d2);
102}
103
104void HandleAfterTouchChannel(byte channel, byte pressure) {
105 FL_UNUSED(channel);
106 FL_UNUSED(pressure);
107 #if 0 // Disabled for now.
108 if (0 == pressure) {
109 for (int i = 0; i < kNumKeys; ++i) {
110 Key& key = keyboard.keys_[i];
111 key.SetOff();
112 }
113 }
114 #endif
115}
116
117
118
120// Called once when the app starts.
121void setup() {
122 FL_DBG("setup");
123 // Serial port for logging.
124 Serial.begin(57600);
125 //start serial with midi baudrate 31250
126 // Initiate MIDI communications, listen to all channels
127 MY_MIDI.begin(MIDI_CHANNEL_OMNI);
128
129 // Connect the HandleNoteOn function to the library, so it is called upon reception of a NoteOn.
130 MY_MIDI.setHandleNoteOn(HandleNoteOn);
131 MY_MIDI.setHandleNoteOff(HandleNoteOff);
132 MY_MIDI.setHandleAfterTouchPoly(HandleAfterTouchPoly);
133 MY_MIDI.setHandleAfterTouchChannel(HandleAfterTouchChannel);
134 MY_MIDI.setHandleControlChange(HandleControlChange);
135
136 ui_init();
137}
138
140#ifdef DEBUG_FORCED_KEYBOARD
141 static uint32_t start_time = 0;
142 static bool toggle = 0;
143
144 const uint32_t time_on = 25;
145 const uint32_t time_off = 500;
146
147 // Just force it on whenever this function is called.
148 is_debugging = true;
149
150 uint32_t now = fl::millis();
151 uint32_t delta_time = now - start_time;
152
153
154 uint32_t threshold_time = toggle ? time_off : time_on;
155 if (delta_time < threshold_time) {
156 return;
157 }
158
159 int random_key = random(0, 88);
160
161 start_time = now;
162 if (toggle) {
163 HandleNoteOn(0, random_key, 64);
164 } else {
165 HandleNoteOff(0, random_key, 82);
166 }
167 toggle = !toggle;
168#endif
169}
170
172// Repeatedly called by the app.
173void loop() {
174 //FL_DBG("loop");
175
176
177 // Calculate dt.
178 static uint32_t s_prev_time = 0;
179 uint32_t prev_time = 0;
180 FASTLED_UNUSED(prev_time); // actually used in perf tests.
181 uint32_t now_ms = fl::millis();
182 uint32_t delta_ms = now_ms - s_prev_time;
183 s_prev_time = now_ms;
184
185 if (!is_debugging) {
186 if (Serial.available() > 0) {
187 int v = Serial.read();
188 if (v == 'd') {
189 is_debugging = true;
190 }
191 }
192 }
193
195
196 const unsigned long start_time = fl::millis();
197 // Each frame we call the midi processor 100 times to make sure that all notes
198 // are processed.
199 for (int i = 0; i < 100; ++i) {
200 MY_MIDI.read();
201 }
202
203 const unsigned long midi_time = fl::millis() - start_time;
204
205 // Updates keyboard: releases sustained keys that.
206
207 const uint32_t keyboard_time_start = fl::millis();
208
209 // This is kind of a hack... but give the keyboard a future time
210 // so that all keys just pressed get a value > 0 for their time
211 // durations.
212 keyboard.Update(now_ms + delta_ms, delta_ms);
213 const uint32_t keyboard_delta_time = fl::millis() - keyboard_time_start;
214
215 ui_state ui_st = ui_update(now_ms, delta_ms);
216
217 //dprint("vis selector = ");
218 //dprintln(vis_state);
219
220
221 // These int values are for desting the performance of the
222 // app. If the app ever runs slow then set kShowFps to 1
223 // in the settings.h file.
224 const unsigned long start_painting = fl::millis();
225 FASTLED_UNUSED(start_painting);
226
227
228 // Paints the keyboard using the led_rope.
230 Painter::Paint(now_ms, delta_ms, which_vis, &keyboard, &led_rope);
231
232 const unsigned long paint_time = fl::millis() - start_time;
233 const unsigned long total_time = midi_time + paint_time + keyboard_delta_time;
234
235 if (kShowFps) {
236 float fps = 1.0f/(float(total_time) / 1000.f);
237 Serial.print("fps - "); Serial.println(fps);
238 Serial.print("midi time - "); Serial.println(midi_time);
239 Serial.print("keyboard update time - "); Serial.println(keyboard_delta_time);
240 Serial.print("draw & paint time - "); Serial.println(paint_time);
241 }
242
243 EVERY_N_SECONDS(1) {
244 FL_DBG("is_debugging = " << is_debugging);
245 }
246
247
248 FastLED.show();
249}
250
bool toggle
Definition Blur.ino:12
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
void HandleAfterTouchPoly(byte channel, byte note, byte pressure)
void setup()
FASTLED_DESCRIPTION("A midi keyboard visualizer.")
void HandleAfterTouchChannel(byte channel, byte pressure)
LedRopeTCL led_rope(kNumKeys)
void HandleNoteOff(byte channel, byte midi_note, byte velocity)
void DbgDoSimulatedKeyboardPress()
void HandleNoteOn(byte channel, byte midi_note, byte velocity)
KeyboardState keyboard
FASTLED_TITLE("Luminescent Grand")
void HandleControlChange(byte channel, byte d1, byte d2)
void loop()
MIDI Library for the Arduino.
bool is_debugging
Definition dprint.cpp:5
#define EVERY_N_SECONDS(N)
Checks whether to execute a block of code every N seconds.
Definition lib8tion.h:1010
#define FL_DBG
Definition log.h:388
Centralized logging categories for FastLED hardware interfaces and subsystems.
#define MIDI_CHANNEL_OMNI
Definition midi_Defs.h:43
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.
#define FL_UNUSED(x)
#define FASTLED_UNUSED(x)
#define MIDI_CREATE_INSTANCE(Type, SerialPort, Name)
Create an instance of the library attached to a serial port. You can use HardwareSerial or SoftwareSe...
Definition serialMIDI.h:99
@ kShowFps
Definition settings.h:17
@ kNumKeys
Definition settings.h:5
void SetOff(uint32_t now_ms)
Definition Keyboard.cpp:24
Definition Keyboard.h:22
static void Paint(uint32_t now_ms, uint32_t delta_ms, VisState vis_state, KeyboardState *keyboard, LedRopeInterface *light_rope)
Definition Painter.cpp:99
int which_visualizer
Definition ui_state.h:13
#define Serial
Definition serial.h:304
Aggregator header for the fl/ui/ family of per-element UI types.
ui_state ui_update(uint32_t now_ms, uint32_t delta_ms)
Definition ui_state.cpp:58
ColorSelector color_selector(PIN_COLOR_SELECT)
void ui_init()
Definition ui_state.cpp:54