Compare commits
24
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
88fee54f96 | ||
|
|
868c5eda82 | ||
|
|
d7f9bb16fd | ||
|
|
b3f4eb1a8b | ||
|
|
3226986709 | ||
|
|
e1507d5d7c | ||
|
|
3c5e0b07ae | ||
|
|
9d1a2fdc87 | ||
|
|
7c93839f94 | ||
|
|
907279e1ea | ||
|
|
df1f678845 | ||
|
|
b859527169 | ||
|
|
ad977d3b09 | ||
|
|
acd3cc51fe | ||
|
|
eb5d66d29b | ||
|
|
354f18efe7 | ||
|
|
eda2a959dc | ||
|
|
53752365b8 | ||
|
|
ea45c927e7 | ||
|
|
72496d2daf | ||
|
|
1a70cbe870 | ||
|
|
ec0cdc9438 | ||
|
|
9d2872a513 | ||
|
|
c83a500b3c |
@@ -3,6 +3,10 @@
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
|
||||
extern volatile uint32_t midi_tick_count;
|
||||
extern volatile uint16_t last_spp_position;
|
||||
extern volatile bool spp_valid;
|
||||
|
||||
struct MidiEvent {
|
||||
enum Type {
|
||||
NOTE_ON,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "app_task.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
extern volatile uint8_t beats_per_bar;
|
||||
|
||||
AppTask::AppTask(LedStub* led, SwitchStub* sw, UsbMidiTransport* midi)
|
||||
: led_driver(led), switch_driver(sw), midi_transport(midi) {
|
||||
|
||||
@@ -124,6 +126,18 @@ void AppTask::process_midi_event(const MidiEvent& event) {
|
||||
}
|
||||
|
||||
void AppTask::process_switch_event(uint8_t switch_id, bool pressed) {
|
||||
// Time signature combo: hold pad 10 (switch 9) + press pad 1/2/3
|
||||
if (switch_id <= 2 && switch_driver->is_pressed(9)) {
|
||||
if (pressed) {
|
||||
switch (switch_id) {
|
||||
case 0: beats_per_bar = 4; Serial.println("[APP] Time sig: 4/4"); break;
|
||||
case 1: beats_per_bar = 3; Serial.println("[APP] Time sig: 3/4"); break;
|
||||
case 2: beats_per_bar = 6; Serial.println("[APP] Time sig: 6/4"); break;
|
||||
}
|
||||
}
|
||||
return; // suppress CC in combo mode
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; i < NUM_PADS; i++) {
|
||||
if (pad_mapping[i].physical_switch == switch_id) {
|
||||
uint8_t channel = pad_mapping[i].midi_channel;
|
||||
|
||||
@@ -21,14 +21,44 @@ UsbMidiTransport midi_transport;
|
||||
AppTask controller(&led_driver, &switch_driver, &midi_transport);
|
||||
ExpressionPedal exp_pedal(4);
|
||||
|
||||
volatile uint32_t flash_latency = 55; // ms delay to compensate for Loopy Pro rendering
|
||||
volatile uint8_t beats_per_bar = 4; // change to match your project's time signature
|
||||
|
||||
TaskHandle_t midi_task_handle = NULL;
|
||||
|
||||
void midi_task(void* parameter) {
|
||||
Serial.println("[TASK] MIDI task started on core 0");
|
||||
|
||||
// Beat timing fixes for precise beat alignment - sync to real MIDI time, not hardcoded tempo
|
||||
// UINT32_MAX ensures tick 0 (first 0xF8 after START) triggers a flash
|
||||
uint32_t last_beat = UINT32_MAX;
|
||||
uint32_t flash_start = 0;
|
||||
|
||||
while (true) {
|
||||
midi_transport.update();
|
||||
exp_pedal.update(midi_transport);
|
||||
|
||||
uint32_t tick = midi_tick_count;
|
||||
uint32_t now = millis();
|
||||
|
||||
uint32_t current_beat = tick / 24;
|
||||
|
||||
if (current_beat != last_beat) {
|
||||
last_beat = current_beat;
|
||||
flash_start = now + flash_latency;
|
||||
// All beats flash white
|
||||
mux.set_led_color(6, 255, 255, 255);
|
||||
}
|
||||
|
||||
if (flash_start > 0 && now >= flash_start) {
|
||||
mux.show();
|
||||
if (now - flash_start >= 50) {
|
||||
mux.set_led_color(6, 20, 20, 20);
|
||||
mux.show();
|
||||
flash_start = 0;
|
||||
}
|
||||
}
|
||||
|
||||
vTaskDelay(1);
|
||||
}
|
||||
}
|
||||
@@ -89,6 +119,16 @@ void handle_serial_command(const String& cmd) {
|
||||
} else if (cmd == "exp") {
|
||||
Serial.printf("[CMD] EXP ADC=%d MIDI=%d\n",
|
||||
exp_pedal.get_raw(), exp_pedal.get_value());
|
||||
} else if (cmd == "latency") {
|
||||
Serial.printf("[CMD] Current flash latency: %d ms\n", flash_latency);
|
||||
} else if (cmd.startsWith("latency ")) {
|
||||
int val = atoi(cmd.c_str() + 8);
|
||||
if (val >= 0 && val <= 500) {
|
||||
flash_latency = val;
|
||||
Serial.printf("[CMD] Flash latency set to %d ms\n", flash_latency);
|
||||
} else {
|
||||
Serial.println("[CMD] Latency must be 0-500 ms");
|
||||
}
|
||||
} else if (cmd == "usb") {
|
||||
Serial.printf("[CMD] USB mounted: %s\n", TinyUSBDevice.mounted() ? "YES" : "NO");
|
||||
Serial.printf("[CMD] USB ready: %s\n", TinyUSBDevice.ready() ? "YES" : "NO");
|
||||
@@ -133,6 +173,8 @@ void handle_serial_command(const String& cmd) {
|
||||
Serial.println(" red/green/blue - solid colour");
|
||||
Serial.println(" pixel0/pixel1 - single pixel test");
|
||||
Serial.println(" exp - expression pedal ADC/MIDI value");
|
||||
Serial.println(" latency - show current flash latency");
|
||||
Serial.println(" latency N - set flash latency to N ms (0-500)");
|
||||
Serial.println(" usb - USB connection status and descriptor info");
|
||||
Serial.println(" gpiotest - raw GPIO pin diagnostic");
|
||||
Serial.println(" rawled - bit-bang WS2812 (no library)");
|
||||
|
||||
+49
-1
@@ -2,6 +2,11 @@
|
||||
#include <Arduino.h>
|
||||
#include "Adafruit_TinyUSB.h"
|
||||
|
||||
volatile uint32_t midi_tick_count = 0;
|
||||
volatile uint16_t last_spp_position = 0;
|
||||
volatile bool spp_valid = false;
|
||||
extern volatile uint8_t beats_per_bar;
|
||||
|
||||
static Adafruit_USBD_MIDI usb_midi;
|
||||
|
||||
UsbMidiTransport::UsbMidiTransport() : initialized(false) {
|
||||
@@ -48,9 +53,51 @@ void UsbMidiTransport::update() {
|
||||
TinyUSBDevice.mounted() ? "YES" : "NO");
|
||||
}
|
||||
|
||||
while (usb_midi.available()) {
|
||||
if (usb_midi.available()) {
|
||||
uint8_t packet[4];
|
||||
if (usb_midi.readPacket(packet)) {
|
||||
uint8_t cin = packet[0] & 0x0F;
|
||||
if (cin == 0x0F) {
|
||||
if (packet[1] == 0xF8) {
|
||||
midi_tick_count++;
|
||||
} else if (packet[1] == 0xFA) {
|
||||
if (spp_valid) {
|
||||
midi_tick_count = last_spp_position * 6;
|
||||
spp_valid = false;
|
||||
Serial.printf("[CLK] START at SPP=%d -> tick %d\n", last_spp_position, midi_tick_count);
|
||||
} else {
|
||||
midi_tick_count = 0xFFFFFFFF;
|
||||
Serial.println("[CLK] START (no SPP) - next F8 = tick 0");
|
||||
}
|
||||
} else if (packet[1] == 0xFB) {
|
||||
// CONTINUE - same as START for our purposes
|
||||
if (spp_valid) {
|
||||
midi_tick_count = last_spp_position * 6;
|
||||
spp_valid = false;
|
||||
Serial.printf("[CLK] CONTINUE at SPP=%d -> tick %d\n", last_spp_position, midi_tick_count);
|
||||
} else {
|
||||
Serial.println("[CLK] CONTINUE (no SPP)");
|
||||
}
|
||||
}
|
||||
} else if (cin == 0x03 && packet[1] == 0xF2) {
|
||||
// Song Position Pointer
|
||||
uint16_t prev_spp = last_spp_position;
|
||||
last_spp_position = (packet[3] << 7) | packet[2];
|
||||
spp_valid = true;
|
||||
Serial.printf("[CLK] SPP=%d\n", last_spp_position);
|
||||
|
||||
// Auto-detect time signature from SPP delta (sent at bar boundaries)
|
||||
if (prev_spp > 0 && last_spp_position > prev_spp) {
|
||||
uint16_t delta = last_spp_position - prev_spp;
|
||||
if (delta >= 8 && delta <= 64 && delta % 4 == 0) {
|
||||
uint8_t detected = delta / 4;
|
||||
if (detected >= 2 && detected <= 16 && detected != beats_per_bar) {
|
||||
beats_per_bar = detected;
|
||||
Serial.printf("[CLK] Auto-detected %d/4 time from SPP delta=%d\n", detected, delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
MidiEvent event;
|
||||
parse_midi_packet(packet, 4, event);
|
||||
|
||||
@@ -71,6 +118,7 @@ void UsbMidiTransport::update() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UsbMidiTransport::on_midi_receive(std::function<void(const MidiEvent&)> callback) {
|
||||
receive_callback = callback;
|
||||
|
||||
Reference in New Issue
Block a user