Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
71 changes: 71 additions & 0 deletions releases/00_Simple_MIDI/v2_prototype/firmware/AnalogueToMIDI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#ifndef ANALOGUE_TO_MIDI_H
#define ANALOGUE_TO_MIDI_H

// Class that converts a signal in the range min_ to max_ into 7-bit values 0-127
// No filtering, but uses hysteresis to avoid jitter

class AnalogueToMIDI
{
public:
AnalogueToMIDI(int32_t minval_, int32_t maxval_)
{
minval = minval_;
maxval = maxval_;
currentMIDIValue = 0;
previousMIDIValue = 0;
}

void operator()(int32_t in)
{
// Rescale input from 0 to 65535
if (maxval == minval)
{
in = maxval;
}
else
{
in = ((in - minval) << 16) / (maxval - minval);
}

if (in < 0)
{
in = 0;
}
if (in > 65535)
{
in = 65535;
}

// Naive mapping of in (0-65535) to MIDI (0-127) is (in >> 9)
// 0-511 -> 0
// 512-1023 -> 1
// 1024-1535 -> 2 etc.
// Extend these 'windows' by half a window (256) in either direction,
// for hysteresis, and update MIDI output value if outside this window.
int32_t windowMax = ((currentMIDIValue + 1) << 9) + 255;
int32_t windowMin = (currentMIDIValue << 9) - 256;

if (in > windowMax || in < windowMin)
{
currentMIDIValue = in >> 9;
}
}
int8_t GetMIDIValueIfNew()
{
if (currentMIDIValue != previousMIDIValue)
{
previousMIDIValue = currentMIDIValue;
return currentMIDIValue;
}
else
{
return -1;
}
}
private:
int32_t minval, maxval;
volatile int8_t currentMIDIValue;
int8_t previousMIDIValue;
};

#endif
55 changes: 55 additions & 0 deletions releases/00_Simple_MIDI/v2_prototype/firmware/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
cmake_minimum_required (VERSION 3.13)
include(pico_sdk_import.cmake)
project(simple_midi_2 C CXX ASM)
set(CMAKE_CXX_STANDARD 17)
pico_sdk_init()

option(OPTIMIZE_SIZE "Optimize for size (-Os) instead of default -O2" OFF)

macro (add_example _name)
add_executable(${ARGV})
if (TARGET ${_name})

# Add warnings, in particular about float/double conversion, as it's very easy to end up
# with slower double-precision calculations when we only mean to use single-precision float.
target_compile_options(${_name} PRIVATE -Wdouble-promotion -Wfloat-conversion -Wall -Wextra -mtune=cortex-m0plus)
if (OPTIMIZE_SIZE)
target_compile_options(${_name} PRIVATE -Os)
endif()
target_link_options(${_name} PRIVATE -Wl,--print-memory-usage)

# Give oscillator more time to start - some boards won't run if this isn't included
target_compile_definitions(${_name} PRIVATE PICO_XOSC_STARTUP_DELAY_MULTIPLIER=64)

target_include_directories(${_name} PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(${_name} pico_unique_id pico_stdlib hardware_dma hardware_interp hardware_i2c hardware_pwm hardware_adc hardware_spi)
pico_add_extra_outputs(${_name})
target_sources(${_name} PUBLIC ${CMAKE_CURRENT_LIST_DIR}/main.cpp)


pico_set_binary_type(${_name} copy_to_ram)

pico_enable_stdio_usb(${_name} 0)
endif()
endmacro()



add_example(simple_midi_2)

target_link_libraries(simple_midi_2 pico_multicore hardware_sync tinyusb_device tinyusb_host tinyusb_board)
target_sources(simple_midi_2 PUBLIC
${CMAKE_CURRENT_LIST_DIR}/usb_descriptors.c
${CMAKE_CURRENT_LIST_DIR}/usb_midi_host.c
${CMAKE_CURRENT_LIST_DIR}/usb_midi_host_app_driver.c)

# Calibration passthrough test: AudioIn millivolts -> CVOut millivolts
add_executable(test_passthrough ${CMAKE_CURRENT_LIST_DIR}/test_passthrough.cpp)
target_compile_options(test_passthrough PRIVATE -Wdouble-promotion -Wfloat-conversion -Wall -Wextra -mtune=cortex-m0plus)
target_compile_definitions(test_passthrough PRIVATE PICO_XOSC_STARTUP_DELAY_MULTIPLIER=64)
target_link_options(test_passthrough PRIVATE -Wl,--print-memory-usage)
target_include_directories(test_passthrough PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(test_passthrough pico_unique_id pico_stdlib pico_multicore hardware_sync hardware_dma hardware_interp hardware_i2c hardware_pwm hardware_adc hardware_spi)
pico_add_extra_outputs(test_passthrough)
pico_set_binary_type(test_passthrough copy_to_ram)
pico_enable_stdio_usb(test_passthrough 0)
Loading