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