Simple USB-MIDI foot controller
Dependencies: PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6
main.cpp@0:2f530d7169a6, 2017-07-30 (annotated)
- Committer:
- SpotlightKid
- Date:
- Sun Jul 30 20:26:29 2017 +0000
- Revision:
- 0:2f530d7169a6
- Child:
- 1:61415f07477d
First working version with single switch
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:2f530d7169a6 | 17 | SwitchHandler(int ctrl, int ch = 1, int on = 0, int off = 127) : |
SpotlightKid | 0:2f530d7169a6 | 18 | control(ctrl), channel(ch), on_value(on), off_value(off), b_pressed(false), b_released(false) {}; |
SpotlightKid | 0:2f530d7169a6 | 19 | |
SpotlightKid | 0:2f530d7169a6 | 20 | void handle_pressed(void) { |
SpotlightKid | 0:2f530d7169a6 | 21 | b_pressed = true; |
SpotlightKid | 0:2f530d7169a6 | 22 | } |
SpotlightKid | 0:2f530d7169a6 | 23 | |
SpotlightKid | 0:2f530d7169a6 | 24 | void handle_released(void) { |
SpotlightKid | 0:2f530d7169a6 | 25 | b_released = true; |
SpotlightKid | 0:2f530d7169a6 | 26 | } |
SpotlightKid | 0:2f530d7169a6 | 27 | |
SpotlightKid | 0:2f530d7169a6 | 28 | void handle_events() { |
SpotlightKid | 0:2f530d7169a6 | 29 | if (b_pressed) { |
SpotlightKid | 0:2f530d7169a6 | 30 | led1->write(0); |
SpotlightKid | 0:2f530d7169a6 | 31 | led_ontime = timer->read_us(); |
SpotlightKid | 0:2f530d7169a6 | 32 | serial->printf("Switch pressed: controller=%d channel=%d value=%d\r\n", control, channel, on_value); |
SpotlightKid | 0:2f530d7169a6 | 33 | midi->write(MIDIMessage::ControlChange(control, on_value, channel)); |
SpotlightKid | 0:2f530d7169a6 | 34 | b_pressed = false; |
SpotlightKid | 0:2f530d7169a6 | 35 | } else if (b_released) { |
SpotlightKid | 0:2f530d7169a6 | 36 | led1->write(0); |
SpotlightKid | 0:2f530d7169a6 | 37 | led_ontime = timer->read_us(); |
SpotlightKid | 0:2f530d7169a6 | 38 | serial->printf("Switch released: control=%d channel=%d value=%d\r\n", control, channel, off_value); |
SpotlightKid | 0:2f530d7169a6 | 39 | midi->write(MIDIMessage::ControlChange(control, off_value, channel)); |
SpotlightKid | 0:2f530d7169a6 | 40 | b_released = false; |
SpotlightKid | 0:2f530d7169a6 | 41 | } |
SpotlightKid | 0:2f530d7169a6 | 42 | } |
SpotlightKid | 0:2f530d7169a6 | 43 | private: |
SpotlightKid | 0:2f530d7169a6 | 44 | bool b_pressed, b_released; |
SpotlightKid | 0:2f530d7169a6 | 45 | int control, channel, on_value, off_value; |
SpotlightKid | 0:2f530d7169a6 | 46 | }; |
SpotlightKid | 0:2f530d7169a6 | 47 | |
SpotlightKid | 0:2f530d7169a6 | 48 | int main() { |
SpotlightKid | 0:2f530d7169a6 | 49 | // Configure system clock (72MHz HSE clock, 48MHz USB clock) |
SpotlightKid | 0:2f530d7169a6 | 50 | confSysClock(); |
SpotlightKid | 0:2f530d7169a6 | 51 | |
SpotlightKid | 0:2f530d7169a6 | 52 | timer = new Timer; |
SpotlightKid | 0:2f530d7169a6 | 53 | led1 = new DigitalOut(LED1); |
SpotlightKid | 0:2f530d7169a6 | 54 | led1->write(1); |
SpotlightKid | 0:2f530d7169a6 | 55 | serial = new Serial(PA_9, PA_10); |
SpotlightKid | 0:2f530d7169a6 | 56 | midi = new USBMIDI(0x1f00, 0x2012, 0x0001); |
SpotlightKid | 0:2f530d7169a6 | 57 | SwitchHandler handler1(1, 10); |
SpotlightKid | 0:2f530d7169a6 | 58 | |
SpotlightKid | 0:2f530d7169a6 | 59 | PinDetect btn(SWITCH1_PIN); |
SpotlightKid | 0:2f530d7169a6 | 60 | btn.attach_asserted(&handler1, &SwitchHandler::handle_pressed); |
SpotlightKid | 0:2f530d7169a6 | 61 | btn.attach_deasserted(&handler1, &SwitchHandler::handle_released); |
SpotlightKid | 0:2f530d7169a6 | 62 | btn.setSampleFrequency(); |
SpotlightKid | 0:2f530d7169a6 | 63 | timer->start(); |
SpotlightKid | 0:2f530d7169a6 | 64 | |
SpotlightKid | 0:2f530d7169a6 | 65 | while (true) { |
SpotlightKid | 0:2f530d7169a6 | 66 | handler1.handle_events(); |
SpotlightKid | 0:2f530d7169a6 | 67 | wait_ms(10); |
SpotlightKid | 0:2f530d7169a6 | 68 | if (timer->read_us() - led_ontime >= LED_DURATION) { |
SpotlightKid | 0:2f530d7169a6 | 69 | led_ontime = 0; |
SpotlightKid | 0:2f530d7169a6 | 70 | led1->write(1); |
SpotlightKid | 0:2f530d7169a6 | 71 | } |
SpotlightKid | 0:2f530d7169a6 | 72 | } |
SpotlightKid | 0:2f530d7169a6 | 73 | } |