diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..e79e189 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,7 @@ +# Top-level CMake file for ESP-IDF project +cmake_minimum_required(VERSION 3.16) + +set(EXTRA_COMPONENT_DIRS "components") + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(loopy_midi_controller) diff --git a/components/controller/CMakeLists.txt b/components/controller/CMakeLists.txt new file mode 100644 index 0000000..c1f58b4 --- /dev/null +++ b/components/controller/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "app_task.cpp" + INCLUDE_DIRS "." + REQUIRES midi hal) diff --git a/components/controller/app_task.h b/components/controller/app_task.h index 4944447..0ba5fa2 100644 --- a/components/controller/app_task.h +++ b/components/controller/app_task.h @@ -3,6 +3,7 @@ #include #include +#include "midi/midi_transport.h" #include "hal/led_stub.h" #include "hal/switch_stub.h" @@ -17,6 +18,5 @@ struct AppTaskParams { BaseType_t app_task(void* parameters); // Application state management -void app_process_midi_event(const MidiEvent& event); +void app_process_midi_event(const MidiEvent& event, LedStub* led_driver); void app_process_switch_event(uint8_t switch_id, bool pressed); -void app_initialize_config(); \ No newline at end of file diff --git a/components/hal/CMakeLists.txt b/components/hal/CMakeLists.txt new file mode 100644 index 0000000..a08ca51 --- /dev/null +++ b/components/hal/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "led_stub.cpp" "switch_stub.cpp" + INCLUDE_DIRS "." + REQUIRES ) diff --git a/components/hal/led_stub.cpp b/components/hal/led_stub.cpp index 0ac9408..f08151a 100644 --- a/components/hal/led_stub.cpp +++ b/components/hal/led_stub.cpp @@ -1,62 +1,48 @@ -// hal/led_stub.cpp +// components/hal/led_stub.cpp #include "hal/led_stub.h" #include "esp_log.h" static const char* TAG = "led_stub"; -class DefaultLedStub : public LedStub { -private: - LedState led_states[10]; // Support up to 10 LEDs - bool initialized; - -public: - DefaultLedStub() : initialized(false) { - // Initialize all LEDs to off state - for (int i = 0; i < 10; i++) { - led_states[i].active = false; - led_states[i].velocity = 0; - } +DefaultLedStub::DefaultLedStub() : initialized(false) { + for (int i = 0; i < NUM_LEDS; i++) { + led_states[i].active = false; + led_states[i].velocity = 0; + led_states[i].note = 0; + led_states[i].channel = 0; + led_states[i].timestamp = 0; } - - void begin() override { - // GPIO initialization would go here - // For Phase 1, this is a stub - initialized = true; - ESP_LOGI(TAG, "LED stub initialized (GPIO pins not configured yet)"); - } - - void set_led_state(uint8_t note, uint8_t channel, uint8_t velocity) override { - if (!initialized) return; - - // For Phase 1, we assume note 0-9 maps directly to LED 0-9 - // This is configurable in the PadMapping - uint8_t led_index = note_to_index(note); - - if (led_index < 10) { - led_states[led_index].note = note; - led_states[led_index].channel = channel; - led_states[led_index].velocity = velocity; - led_states[led_index].active = (velocity > 0); - led_states[led_index].timestamp = 0; // TODO: Add proper timestamp - - ESP_LOGI(TAG, "LED STATE: Note %d -> LED %d Channel %d Velocity %d (%s)", - note, led_index, channel, velocity, - velocity > 0 ? "ON" : "OFF"); - } else { - ESP_LOGW(TAG, "LED index out of range: %d (Note: %d)", led_index, note); - } - } - - void clear_all() override { - for (int i = 0; i < 10; i++) { - led_states[i].active = false; - led_states[i].velocity = 0; - } - ESP_LOGI(TAG, "All LEDs cleared"); - } -}; +} -// Factory function to create the default LED stub -LedStub* create_led_stub() { - return new DefaultLedStub(); -} \ No newline at end of file +void DefaultLedStub::begin() { + initialized = true; + ESP_LOGI(TAG, "LED stub initialized (GPIO pins not configured yet)"); +} + +void DefaultLedStub::set_led_state(uint8_t note, uint8_t channel, uint8_t velocity) { + if (!initialized) return; + + uint8_t led_index = note_to_index(note); + + if (led_index < NUM_LEDS) { + led_states[led_index].note = note; + led_states[led_index].channel = channel; + led_states[led_index].velocity = velocity; + led_states[led_index].active = (velocity > 0); + led_states[led_index].timestamp = 0; + + ESP_LOGI(TAG, "LED STATE: Note %d -> LED %d Channel %d Velocity %d (%s)", + note, led_index, channel, velocity, + velocity > 0 ? "ON" : "OFF"); + } else { + ESP_LOGW(TAG, "LED index out of range: %d (Note: %d)", led_index, note); + } +} + +void DefaultLedStub::clear_all() { + for (int i = 0; i < NUM_LEDS; i++) { + led_states[i].active = false; + led_states[i].velocity = 0; + } + ESP_LOGI(TAG, "All LEDs cleared"); +} diff --git a/components/hal/led_stub.h b/components/hal/led_stub.h index d82dd70..260bc2c 100644 --- a/components/hal/led_stub.h +++ b/components/hal/led_stub.h @@ -1,6 +1,8 @@ -// hal/led_stub.h +// components/hal/led_stub.h #pragma once +#include + class LedStub { public: virtual ~LedStub() {} @@ -11,8 +13,6 @@ public: // Helper function to map MIDI note to LED index virtual uint8_t note_to_index(uint8_t note) { - // Default implementation - direct mapping - // Can be overridden by specific implementations return note; } }; @@ -24,4 +24,18 @@ struct LedState { uint8_t velocity; // Color/brightness (0-127) uint32_t timestamp; // When state was set bool active; // Current on/off state -}; \ No newline at end of file +}; + +// Default stub implementation +class DefaultLedStub : public LedStub { +private: + static const uint8_t NUM_LEDS = 10; + LedState led_states[NUM_LEDS]; + bool initialized; + +public: + DefaultLedStub(); + void begin() override; + void set_led_state(uint8_t note, uint8_t channel, uint8_t velocity) override; + void clear_all() override; +}; diff --git a/components/hal/switch_stub.cpp b/components/hal/switch_stub.cpp new file mode 100644 index 0000000..1cc1b97 --- /dev/null +++ b/components/hal/switch_stub.cpp @@ -0,0 +1,41 @@ +// components/hal/switch_stub.cpp +#include "hal/switch_stub.h" +#include "esp_log.h" + +static const char* TAG = "switch_stub"; + +DefaultSwitchStub::DefaultSwitchStub() : initialized(false) { + for (int i = 0; i < NUM_SWITCHES; i++) { + switch_states[i].id = i; + switch_states[i].gpio_pin = 0; + switch_states[i].current_state = false; + switch_states[i].previous_state = false; + switch_states[i].last_change_time = 0; + switch_states[i].debounce_time = 50; + } +} + +void DefaultSwitchStub::begin() { + initialized = true; + ESP_LOGI(TAG, "Switch stub initialized (GPIO pins not configured yet)"); +} + +bool DefaultSwitchStub::is_pressed(uint8_t switch_id) { + if (!initialized || switch_id >= NUM_SWITCHES) { + return false; + } + return switch_states[switch_id].current_state; +} + +void DefaultSwitchStub::configure_switch(uint8_t switch_id, uint8_t gpio_pin) { + if (switch_id >= NUM_SWITCHES) return; + switch_states[switch_id].gpio_pin = gpio_pin; + ESP_LOGI(TAG, "Switch %d configured to GPIO %d", switch_id, gpio_pin); +} + +void DefaultSwitchStub::set_debounce_time(uint32_t time_ms) { + for (int i = 0; i < NUM_SWITCHES; i++) { + switch_states[i].debounce_time = time_ms; + } + ESP_LOGI(TAG, "Debounce time set to %lu ms", time_ms); +} diff --git a/components/hal/switch_stub.h b/components/hal/switch_stub.h index 93daefd..513df2f 100644 --- a/components/hal/switch_stub.h +++ b/components/hal/switch_stub.h @@ -1,6 +1,8 @@ -// hal/switch_stub.h +// components/hal/switch_stub.h #pragma once +#include + class SwitchStub { public: virtual ~SwitchStub() {} @@ -21,4 +23,19 @@ struct SwitchState { bool previous_state; // Previous state (for debounce) uint32_t last_change_time; // Timestamp of last state change uint32_t debounce_time; // Debounce time in ms -}; \ No newline at end of file +}; + +// Default stub implementation +class DefaultSwitchStub : public SwitchStub { +private: + static const uint8_t NUM_SWITCHES = 10; + SwitchState switch_states[NUM_SWITCHES]; + bool initialized; + +public: + DefaultSwitchStub(); + void begin() override; + bool is_pressed(uint8_t switch_id) override; + void configure_switch(uint8_t switch_id, uint8_t gpio_pin) override; + void set_debounce_time(uint32_t time_ms) override; +}; diff --git a/components/midi/CMakeLists.txt b/components/midi/CMakeLists.txt new file mode 100644 index 0000000..2249454 --- /dev/null +++ b/components/midi/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "midi_transport.cpp" + INCLUDE_DIRS "." + REQUIRES driver) diff --git a/components/midi/midi_transport.cpp b/components/midi/midi_transport.cpp index 9ad7d8e..5099e56 100644 --- a/components/midi/midi_transport.cpp +++ b/components/midi/midi_transport.cpp @@ -1,8 +1,7 @@ -// midi/midi_transport.cpp +// components/midi/midi_transport.cpp #include "midi/midi_transport.h" #include "esp_log.h" #include "tusb.h" -#include "class/midi/midi.h" static const char* TAG = "midi_transport"; @@ -23,15 +22,9 @@ bool UsbMidiTransport::begin() { return false; } - // Initialize TinyUSB MIDI + // Initialize TinyUSB tusb_init(); - // Configure USB device descriptors - tusb_device_set_string(1, "Loopy Foot Controller"); - - // Register MIDI callback - tuh_midi_set_cb(usb_midi_callback); - initialized = true; ESP_LOGI(TAG, "USB MIDI transport initialized"); return true; @@ -40,82 +33,95 @@ bool UsbMidiTransport::begin() { void UsbMidiTransport::task() { if (!initialized) return; - // Process USB MIDI events - while (tuh_uart_read_available()) { - uint8_t buffer[128]; - uint32_t bytes_read = tuh_midi_read_packet(buffer, sizeof(buffer)); - - if (bytes_read > 0) { + // TinyUSB device task handling + tuh_task(); + + // Check for MIDI data on the USB host interface + uint8_t cable_num; + uint8_t midi_packet[4]; + + while (tud_midi_available()) { + if (tud_midi_packet_read(midi_packet)) { MidiEvent event; - parse_midi_packet(buffer, bytes_read, event); + parse_midi_packet(midi_packet, 4, event); - // Log incoming event log_incoming("USB", event); - // Send to event queue - if (xQueueSend(event_queue, &event, portMAX_DELAY) != pdPASS) { - ESP_LOGW(TAG, "Failed to queue MIDI event"); + if (xQueueSend(event_queue, &event, 0) != pdPASS) { + ESP_LOGW(TAG, "Failed to queue MIDI event (queue full)"); } } } } -void usb_midi_callback(const uint8_t* event, uint32_t size) { - // This callback is called by TinyUSB when MIDI data is received - // For now, we'll implement a simple version - // In a full implementation, this would parse the MIDI packet - - MidiEvent midi_event; - // TODO: Implement actual MIDI parsing based on event type - // For Phase 1, we'll handle basic Note On/Off messages -} - void UsbMidiTransport::log_incoming(const char* source, const MidiEvent& event) { const char* type_str; switch (event.type) { case MidiEvent::NOTE_ON: type_str = "NOTE_ON"; break; case MidiEvent::NOTE_OFF: type_str = "NOTE_OFF"; break; - case MidiEvent::CONTROL_CHANGE: type_str = "CONTROL_CHANGE"; break; + case MidiEvent::CONTROL_CHANGE: type_str = "CC"; break; + case MidiEvent::PROGRAM_CHANGE: type_str = "PC"; break; + case MidiEvent::PITCH_BEND: type_str = "PITCH_BEND"; break; default: type_str = "UNKNOWN"; break; } - ESP_LOGI(TAG, "MIDI IN: %s Channel: %d Type: %s Note: %d Velocity: %d", + ESP_LOGI(TAG, "MIDI IN: %s Ch:%d %s:%d:%d", source, event.channel, type_str, event.data1, event.data2); } void UsbMidiTransport::parse_midi_packet(const uint8_t* buffer, uint32_t size, MidiEvent& event) { - // Simple MIDI parser for basic messages - // This is a simplified version for Phase 1 + if (size < 4) return; - if (size < 2) return; + // USB MIDI packet format: [cable_num | CIN], [status], [data1], [data2] + uint8_t cin = buffer[0] & 0x0F; + uint8_t status = buffer[1]; + uint8_t type = status & 0xF0; + uint8_t channel = (status & 0x0F) + 1; // Convert to 1-16 range - uint8_t status = buffer[0]; - uint8_t type = status & 0xF0; // Message type - uint8_t channel = status & 0x0F; // Channel (0-15, but MIDI uses 1-16) + event.channel = channel; + event.data1 = buffer[2]; + event.data2 = buffer[3]; - event.channel = channel + 1; // Convert to 1-16 range - - switch (type) { - case 0x90: // Note On - event.type = MidiEvent::NOTE_ON; - event.data1 = buffer[1]; - event.data2 = buffer[2]; - break; - - case 0x80: // Note Off + switch (cin) { + case 0x8: // Note Off event.type = MidiEvent::NOTE_OFF; - event.data1 = buffer[1]; - event.data2 = buffer[2]; break; - - case 0xB0: // Control Change + case 0x9: // Note On + event.type = MidiEvent::NOTE_ON; + if (event.data2 == 0) { + event.type = MidiEvent::NOTE_OFF; + } + break; + case 0xB: // Control Change event.type = MidiEvent::CONTROL_CHANGE; - event.data1 = buffer[1]; - event.data2 = buffer[2]; break; - + case 0xC: // Program Change + event.type = MidiEvent::PROGRAM_CHANGE; + break; + case 0xE: // Pitch Bend + event.type = MidiEvent::PITCH_BEND; + break; default: - // Unknown message type - ignore for now - return; + // Try to infer from status byte + switch (type) { + case 0x80: event.type = MidiEvent::NOTE_OFF; break; + case 0x90: event.type = MidiEvent::NOTE_ON; break; + case 0xB0: event.type = MidiEvent::CONTROL_CHANGE; break; + case 0xC0: event.type = MidiEvent::PROGRAM_CHANGE; break; + case 0xE0: event.type = MidiEvent::PITCH_BEND; break; + default: event.type = MidiEvent::NOTE_ON; break; + } + break; } -} \ No newline at end of file +} + +void usb_midi_task(void* pvParameters) { + UsbMidiTransport* transport = static_cast(pvParameters); + + ESP_LOGI(TAG, "USB MIDI task started"); + + while (true) { + transport->task(); + vTaskDelay(pdMS_TO_TICKS(1)); + } +} diff --git a/components/midi/midi_transport.h b/components/midi/midi_transport.h index ab2df82..05ba4d1 100644 --- a/components/midi/midi_transport.h +++ b/components/midi/midi_transport.h @@ -1,4 +1,4 @@ -// midi/midi_transport.h +// components/midi/midi_transport.h #pragma once #include @@ -40,7 +40,10 @@ public: private: QueueHandle_t event_queue; bool initialized; + + // MIDI packet parsing + void parse_midi_packet(const uint8_t* buffer, uint32_t size, MidiEvent& event); }; -// Forward declaration for USB callback -void usb_midi_callback(const uint8_t* event, uint32_t size); \ No newline at end of file +// Task function for USB MIDI processing +void usb_midi_task(void* pvParameters); diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 62e91a1..0000000 --- a/main.cpp +++ /dev/null @@ -1,76 +0,0 @@ -// main.cpp - Entry point for ESP32-S3 FreeRTOS application -// Phase 1: USB MIDI + Basic Event Processing - -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "esp_system.h" -#include "esp_log.h" -#include "driver/gpio.h" - -// Component includes -#include "midi/midi_transport.h" -#include "controller/app_task.h" -#include "hal/led_stub.h" -#include "hal/switch_stub.h" - -// Logging tag -static const char *TAG = "loopy_midi_controller"; - -// FreeRTOS task handles -static TaskHandle_t usb_midi_task_handle = NULL; -static TaskHandle_t controller_task_handle = NULL; - -extern "C" void app_main(void) { - ESP_LOGI(TAG, "Starting Loopy MIDI Controller (Phase 1)"); - ESP_LOGI(TAG, "Device name: Loopy Foot Controller"); - - // Initialize hardware stubs (Phase 1 - no real hardware yet) - LedStub led_driver; - SwitchStub switch_driver; - - led_driver.begin(); - switch_driver.begin(); - - // Initialize MIDI transport (USB) - UsbMidiTransport midi_transport; - midi_transport.begin(); - - // Create USB MIDI task (High priority) - BaseType_t usb_midi_result = xTaskCreate( - usb_midi_task, - "usb_midi_task", - 4096, - (void*)&midi_transport, - tskIDLE_PRIORITY + 3, - &usb_midi_task_handle - ); - - if (usb_midi_result != pdPASS) { - ESP_LOGE(TAG, "Failed to create USB MIDI task"); - return; - } - - // Create Controller task (Lower priority) - AppTaskParams app_params; - app_params.led_driver = &led_driver; - app_params.switch_driver = &switch_driver; - app_params.midi_queue = midi_transport.get_event_queue(); - - BaseType_t controller_result = xTaskCreate( - app_task, - "controller_task", - 4096, - (void*)&app_params, - tskIDLE_PRIORITY + 1, - &controller_task_handle - ); - - if (controller_result != pdPASS) { - ESP_LOGE(TAG, "Failed to create Controller task"); - return; - } - - ESP_LOGI(TAG, "Loopy MIDI Controller initialized successfully"); - ESP_LOGI(TAG, "Phase 1 complete: USB MIDI device ready"); -} \ No newline at end of file diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt new file mode 100644 index 0000000..c4fbd1a --- /dev/null +++ b/main/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "../main.cpp" + INCLUDE_DIRS "." + REQUIRES controller midi hal) diff --git a/main/idf_component.yml b/main/idf_component.yml new file mode 100644 index 0000000..dd66ab6 --- /dev/null +++ b/main/idf_component.yml @@ -0,0 +1,8 @@ +## IDF Component Manager Manifest File +dependencies: + idf: + version: ">=5.0.0" + espressif/esp_tinyusb: + version: ">=1.0.0" + rules: + - if: "target == esp32s3" diff --git a/sdkconfig.defaults b/sdkconfig.defaults new file mode 100644 index 0000000..865fdf2 --- /dev/null +++ b/sdkconfig.defaults @@ -0,0 +1,16 @@ +# ESP32-S3 Configuration +CONFIG_IDF_TARGET="esp32s3" + +# USB Configuration +CONFIG_TINYUSB_DESC_MANUFACTURER_STRING="Ashley Strahle" +CONFIG_TINYUSB_DESC_PRODUCT_STRING="Loopy Foot Controller" +CONFIG_TINYUSB_DESC_CDC_STRING="Loopy Foot Controller" +CONFIG_TINYUSB_MIDI_ENABLED=y +CONFIG_TINYUSB_MIDI_RX_BUFSIZE=64 +CONFIG_TINYUSB_MIDI_TX_BUFSIZE=64 + +# FreeRTOS +CONFIG_FREERTOS_HZ=1000 + +# Log level +CONFIG_LOG_DEFAULT_LEVEL_INFO=y