Simple USB-MIDI foot controller

Dependencies:   PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6

Committer:
SpotlightKid
Date:
Mon Jul 31 04:40:06 2017 +0000
Revision:
1:61415f07477d
Parent:
0:2f530d7169a6
Child:
3:8b8cb5392fa0
Use mbed-os 5.x instead of mbed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SpotlightKid 0:2f530d7169a6 1 #include "stm32f103c8t6.h"
SpotlightKid 0:2f530d7169a6 2 #include "mbed.h"
SpotlightKid 0:2f530d7169a6 3 #include "USBMIDI.h"
SpotlightKid 0:2f530d7169a6 4 #include "PinDetect.h"
SpotlightKid 0:2f530d7169a6 5
SpotlightKid 0:2f530d7169a6 6 #define SWITCH1_PIN PB_5
SpotlightKid 0:2f530d7169a6 7 #define LED_DURATION 20000
SpotlightKid 0:2f530d7169a6 8
SpotlightKid 0:2f530d7169a6 9 USBMIDI *midi;
SpotlightKid 0:2f530d7169a6 10 Serial *serial;
SpotlightKid 0:2f530d7169a6 11 DigitalOut *led1;
SpotlightKid 0:2f530d7169a6 12 int led_ontime;
SpotlightKid 0:2f530d7169a6 13 Timer *timer;
SpotlightKid 0:2f530d7169a6 14
SpotlightKid 0:2f530d7169a6 15 class SwitchHandler {
SpotlightKid 0:2f530d7169a6 16 public:
SpotlightKid 1:61415f07477d 17 explicit SwitchHandler(uint8_t ctrl, uint8_t ch = 1, uint8_t on = 0, uint8_t off = 127) :
SpotlightKid 1:61415f07477d 18 control(ctrl), channel(ch), on_value(on), off_value(off), b_pressed(false), b_released(false) {
SpotlightKid 1:61415f07477d 19 };
SpotlightKid 0:2f530d7169a6 20
SpotlightKid 0:2f530d7169a6 21 void handle_pressed(void) {
SpotlightKid 0:2f530d7169a6 22 b_pressed = true;
SpotlightKid 0:2f530d7169a6 23 }
SpotlightKid 0:2f530d7169a6 24
SpotlightKid 0:2f530d7169a6 25 void handle_released(void) {
SpotlightKid 0:2f530d7169a6 26 b_released = true;
SpotlightKid 0:2f530d7169a6 27 }
SpotlightKid 0:2f530d7169a6 28
SpotlightKid 0:2f530d7169a6 29 void handle_events() {
SpotlightKid 0:2f530d7169a6 30 if (b_pressed) {
SpotlightKid 0:2f530d7169a6 31 led1->write(0);
SpotlightKid 0:2f530d7169a6 32 led_ontime = timer->read_us();
SpotlightKid 0:2f530d7169a6 33 serial->printf("Switch pressed: controller=%d channel=%d value=%d\r\n", control, channel, on_value);
SpotlightKid 1:61415f07477d 34 midi->write(MIDIMessage::ControlChange((int) control, (int) on_value, (int) (channel - 1)));
SpotlightKid 0:2f530d7169a6 35 b_pressed = false;
SpotlightKid 0:2f530d7169a6 36 } else if (b_released) {
SpotlightKid 0:2f530d7169a6 37 led1->write(0);
SpotlightKid 0:2f530d7169a6 38 led_ontime = timer->read_us();
SpotlightKid 0:2f530d7169a6 39 serial->printf("Switch released: control=%d channel=%d value=%d\r\n", control, channel, off_value);
SpotlightKid 1:61415f07477d 40 midi->write(MIDIMessage::ControlChange((int) control, (int) off_value, (int) (channel - 1)));
SpotlightKid 0:2f530d7169a6 41 b_released = false;
SpotlightKid 0:2f530d7169a6 42 }
SpotlightKid 0:2f530d7169a6 43 }
SpotlightKid 0:2f530d7169a6 44 private:
SpotlightKid 1:61415f07477d 45 uint8_t control, channel, on_value, off_value;
SpotlightKid 0:2f530d7169a6 46 bool b_pressed, b_released;
SpotlightKid 0:2f530d7169a6 47 };
SpotlightKid 0:2f530d7169a6 48
SpotlightKid 0:2f530d7169a6 49 int main() {
SpotlightKid 0:2f530d7169a6 50 // Configure system clock (72MHz HSE clock, 48MHz USB clock)
SpotlightKid 0:2f530d7169a6 51 confSysClock();
SpotlightKid 0:2f530d7169a6 52
SpotlightKid 0:2f530d7169a6 53 timer = new Timer;
SpotlightKid 0:2f530d7169a6 54 led1 = new DigitalOut(LED1);
SpotlightKid 0:2f530d7169a6 55 led1->write(1);
SpotlightKid 0:2f530d7169a6 56 serial = new Serial(PA_9, PA_10);
SpotlightKid 0:2f530d7169a6 57 midi = new USBMIDI(0x1f00, 0x2012, 0x0001);
SpotlightKid 0:2f530d7169a6 58 SwitchHandler handler1(1, 10);
SpotlightKid 0:2f530d7169a6 59
SpotlightKid 0:2f530d7169a6 60 PinDetect btn(SWITCH1_PIN);
SpotlightKid 0:2f530d7169a6 61 btn.attach_asserted(&handler1, &SwitchHandler::handle_pressed);
SpotlightKid 0:2f530d7169a6 62 btn.attach_deasserted(&handler1, &SwitchHandler::handle_released);
SpotlightKid 0:2f530d7169a6 63 btn.setSampleFrequency();
SpotlightKid 0:2f530d7169a6 64 timer->start();
SpotlightKid 0:2f530d7169a6 65
SpotlightKid 0:2f530d7169a6 66 while (true) {
SpotlightKid 0:2f530d7169a6 67 handler1.handle_events();
SpotlightKid 0:2f530d7169a6 68 wait_ms(10);
SpotlightKid 0:2f530d7169a6 69 if (timer->read_us() - led_ontime >= LED_DURATION) {
SpotlightKid 0:2f530d7169a6 70 led_ontime = 0;
SpotlightKid 0:2f530d7169a6 71 led1->write(1);
SpotlightKid 0:2f530d7169a6 72 }
SpotlightKid 0:2f530d7169a6 73 }
SpotlightKid 0:2f530d7169a6 74 }