I think this is pretty much completed for the rp2xxx with the radio 2 module. I'll add support for a few others next.

This commit is contained in:
pszsh 2026-03-07 22:22:52 -08:00
parent 316a138ee0
commit 56d99df3af
3 changed files with 62 additions and 6 deletions

View File

@ -15,7 +15,7 @@ add_compile_definitions(PICO_RP2350A=0)
pico_sdk_init()
set(CS_MIDI_HID_MOUSE ON CACHE BOOL "" FORCE)
set(CS_MIDI_HID_KEYBOARD ON CACHE BOOL "" FORCE)
add_subdirectory(lib/cs-midi)
# cs_midi's BTstack sources need project-level btstack_config.h and lwipopts.h
@ -33,8 +33,8 @@ target_include_directories(fractional_looper PRIVATE
target_compile_definitions(fractional_looper PRIVATE
PICO_STDIO_USB_STDOUT_TIMEOUT_US=0
PICO_DEFAULT_UART_TX_PIN=12
PICO_DEFAULT_UART_RX_PIN=13
# PICO_DEFAULT_UART_TX_PIN=12
# PICO_DEFAULT_UART_RX_PIN=13
)
target_link_libraries(fractional_looper
@ -48,7 +48,7 @@ target_link_libraries(fractional_looper
cs_midi
)
pico_enable_stdio_usb(fractional_looper 0)
pico_enable_stdio_uart(fractional_looper 1)
pico_enable_stdio_usb(fractional_looper 1)
pico_enable_stdio_uart(fractional_looper 0)
pico_add_extra_outputs(fractional_looper)

@ -1 +1 @@
Subproject commit 517cbbb3180bc446ac919f7e787f8df10d90cbc0
Subproject commit 2187943124823e896b67084dbe8b2e87be2165ec

View File

@ -1,6 +1,7 @@
#include <cstdio>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "hardware/adc.h"
#include <cs_midi.h>
#include "spp_midi.h"
@ -45,6 +46,37 @@ void apply_map(const CCMap &m) {
btn4.setAddressUnsafe({m.btn[2], Channel_1});
}
constexpr float BATT_V_MIN = 3.0f;
constexpr float BATT_V_MAX = 4.2f;
constexpr uint32_t BATT_INTERVAL_MS = 60000;
constexpr uint32_t BATT_SETTLE_MS = 200;
constexpr uint BATT_SINK_PIN = 41;
constexpr uint BATT_ADC_INPUT = 2; // GP42 = ADC2
enum BattState { BATT_IDLE, BATT_SETTLING };
BattState batt_state = BATT_IDLE;
uint32_t batt_timer = 0;
void batt_sink_enable() {
gpio_put(BATT_SINK_PIN, 0);
gpio_set_dir(BATT_SINK_PIN, GPIO_OUT);
}
void batt_sink_float() {
gpio_set_dir(BATT_SINK_PIN, GPIO_IN);
gpio_disable_pulls(BATT_SINK_PIN);
}
uint8_t read_battery_percent() {
adc_select_input(BATT_ADC_INPUT);
uint16_t raw = adc_read();
float voltage = raw * (3.3f / 4096.0f) * 2.0f;
float pct = (voltage - BATT_V_MIN) / (BATT_V_MAX - BATT_V_MIN) * 100.0f;
if (pct < 0.0f) pct = 0.0f;
if (pct > 100.0f) pct = 100.0f;
return static_cast<uint8_t>(pct);
}
int main() {
stdio_init_all();
if (cyw43_arch_init()) {
@ -52,6 +84,11 @@ int main() {
return 1;
}
adc_init();
adc_gpio_init(42);
gpio_init(BATT_SINK_PIN);
batt_sink_float();
RelativeCCSender::setMode(relativeCCmode::MACKIE_CONTROL_RELATIVE);
ble_midi.setName("FractionalLooper");
Control_Surface.begin();
@ -67,6 +104,25 @@ int main() {
apply_map(maps[state]);
}
uint32_t now = to_ms_since_boot(get_absolute_time());
switch (batt_state) {
case BATT_IDLE:
if (now - batt_timer >= BATT_INTERVAL_MS) {
batt_sink_enable();
batt_timer = now;
batt_state = BATT_SETTLING;
}
break;
case BATT_SETTLING:
if (now - batt_timer >= BATT_SETTLE_MS) {
setBLEBatteryLevel(read_battery_percent());
batt_sink_float();
batt_timer = now;
batt_state = BATT_IDLE;
}
break;
}
sleep_ms(1);
}
}