Last active
May 10, 2023 05:01
-
-
Save joshuaboniface/660ab942198909e4f136f66a4065a691 to your computer and use it in GitHub Desktop.
STM32 Blackpill code for MIDI Rewriter Module (RB3 Pro Drums w/Alesis Strike Pro)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Written with the Arduino IDE; to compile outside of the Arduino IDE, more includes would be required | |
#include <MIDI.h> | |
// Define GPIO pins | |
#define BUTTON PA5 | |
#define LED_A PA6 | |
#define LED_B PA7 | |
#define LED_I PC13 | |
// Define CC#4 HiHat open threshold (~50 = fully open, 127 = fully closed on Strike head unit) | |
// You may need to adjust this slightly, I'm still working on finding the perfect number | |
#define HH_OPEN 100 | |
// Global: Represents whether the HiHat pedal mode is on (true) or off (false) | |
bool PedalMode = true; | |
// Global: Represents the last CC#4 value from the HiHat pedal, updated at every CC#4 event | |
byte HiHatLCC = 0; | |
// Create MIDI instance on Blackpill USART2 | |
HardwareSerial SerialM(USART2); | |
MIDI_CREATE_INSTANCE(HardwareSerial, SerialM, _midi); | |
// Set the HiHat Pedal mode LEDs | |
void setPedalModeLEDs() { | |
if (PedalMode) { | |
digitalWrite(LED_B, HIGH); | |
digitalWrite(LED_A, LOW); | |
} else { | |
digitalWrite(LED_A, HIGH); | |
digitalWrite(LED_B, LOW); | |
} | |
} | |
// Handle control channel events | |
void HandleControlChange(byte channel, byte number, byte value) { | |
// If CC#4 set HiHatLCC to value | |
if (number == 4) HiHatLCC = value; | |
} | |
// Handle note on events | |
void HandleNoteOn(byte channel, byte pitch, byte velocity) { | |
// If pitch is 22 (Yellow Cymbal) and we're in HiHat pedal mode and HiHatLCC <= HH_OPEN... | |
if (pitch == 22 && PedalMode && HiHatLCC <= HH_OPEN) { | |
// Rewrite note on to 59 (Blue Cymbal) | |
_midi.sendNoteOn(59, velocity, channel); | |
// Otherwise... | |
} else { | |
// Pass on input note on | |
_midi.sendNoteOn(pitch, velocity, channel); | |
} | |
} | |
// Handle note off events | |
void HandleNoteOff(byte channel, byte pitch, byte velocity) { | |
// If pitch is 22 (Yellow Cymbal) and we're in HiHatpedal mode and HiHatLCC <= HH_OPEN... | |
if (pitch == 22 && PedalMode && HiHatLCC <= HH_OPEN) { | |
// Rewrite note off to 59 (Blue Cymbal) | |
_midi.sendNoteOff(59, velocity, channel); | |
} else { | |
// Pass on input note off | |
_midi.sendNoteOff(pitch, velocity, channel); | |
} | |
} | |
// Startup sequence | |
void setup() { | |
// Turn status LED on so we know the Blackpill has booted | |
pinMode(LED_I, OUTPUT); | |
digitalWrite(LED_I, LOW); | |
// Turn HiHat pedal mode LEDs off | |
pinMode(LED_A, OUTPUT); | |
digitalWrite(LED_A, HIGH); | |
pinMode(LED_B, OUTPUT); | |
digitalWrite(LED_B, HIGH); | |
// Set HiHat pedal mode button to pullup | |
pinMode(BUTTON, INPUT_PULLUP); | |
// Wait 1 second for power to stabilize | |
delay(1000); | |
// Set the LEDs for the default (true) HiHat pedal mode | |
setPedalModeLEDs(); | |
// Start an omni-channel MIDI instance with Thru off | |
_midi.begin(MIDI_CHANNEL_OMNI); | |
_midi.turnThruOff(); | |
// Set handlers for control change, note on, and note off events | |
_midi.setHandleControlChange(HandleControlChange); | |
_midi.setHandleNoteOn(HandleNoteOn); | |
_midi.setHandleNoteOff(HandleNoteOff); | |
} | |
// Main loop | |
void loop() { | |
// Check for button press and switch HiHat pedal mode | |
// (this is "not" because BUTTON GPIO is active low) | |
// TODO: Debounce the actual trigger to <10ms and reduce the delay for real-time changes (with a pedal perhaps?) | |
if (!digitalRead(BUTTON)) { | |
PedalMode = (PedalMode) ? false : true; | |
setPedalModeLEDs(); | |
// Delay 1/2 of a second to allow release of the switch | |
delay(500); | |
} | |
// Read MIDI events | |
_midi.read(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment