Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: PinDetect USBDevice_STM32F103 mbed-STM32F103C8T6
midiswitch.cpp@8:75c5ec68765e, 2017-08-04 (annotated)
- Committer:
- SpotlightKid
- Date:
- Fri Aug 04 05:22:22 2017 +0200
- Revision:
- 8:75c5ec68765e
- Parent:
- 7:553836a26221
- Child:
- 9:d5fa853818dd
Make MIDI message type configurable
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| SpotlightKid |
7:553836a26221 | 1 | #include "mbed.h" |
| SpotlightKid |
3:8b8cb5392fa0 | 2 | #include "midiswitch.h" |
| SpotlightKid |
3:8b8cb5392fa0 | 3 | |
| SpotlightKid |
8:75c5ec68765e | 4 | SwitchHandler::SwitchHandler(EventQueue * queue, MIDI_CB cb, SwitchConfig sw) : |
| SpotlightKid |
8:75c5ec68765e | 5 | queue(queue), write_cb(cb), btn(sw.pin), cfg(sw) { |
| SpotlightKid |
8:75c5ec68765e | 6 | btn.mode(PullUp); |
| SpotlightKid |
7:553836a26221 | 7 | btn.setAssertValue(0); |
| SpotlightKid |
3:8b8cb5392fa0 | 8 | btn.attach_asserted(this, &SwitchHandler::handle_pressed); |
| SpotlightKid |
3:8b8cb5392fa0 | 9 | btn.attach_deasserted(this, &SwitchHandler::handle_released); |
| SpotlightKid |
3:8b8cb5392fa0 | 10 | btn.setSampleFrequency(); |
| SpotlightKid |
3:8b8cb5392fa0 | 11 | }; |
| SpotlightKid |
3:8b8cb5392fa0 | 12 | |
| SpotlightKid |
3:8b8cb5392fa0 | 13 | void SwitchHandler::handle_pressed(void) { |
| SpotlightKid |
8:75c5ec68765e | 14 | if (cfg.on_value >= 0) { |
| SpotlightKid |
8:75c5ec68765e | 15 | if (make_message(true)) { |
| SpotlightKid |
8:75c5ec68765e | 16 | queue->call(write_cb, msg); |
| SpotlightKid |
8:75c5ec68765e | 17 | } |
| SpotlightKid |
8:75c5ec68765e | 18 | } |
| SpotlightKid |
3:8b8cb5392fa0 | 19 | }; |
| SpotlightKid |
3:8b8cb5392fa0 | 20 | |
| SpotlightKid |
3:8b8cb5392fa0 | 21 | void SwitchHandler::handle_released(void) { |
| SpotlightKid |
8:75c5ec68765e | 22 | if (cfg.off_value >= 0) { |
| SpotlightKid |
8:75c5ec68765e | 23 | if (make_message(false)) { |
| SpotlightKid |
8:75c5ec68765e | 24 | queue->call(write_cb, msg); |
| SpotlightKid |
8:75c5ec68765e | 25 | } |
| SpotlightKid |
8:75c5ec68765e | 26 | } |
| SpotlightKid |
3:8b8cb5392fa0 | 27 | }; |
| SpotlightKid |
8:75c5ec68765e | 28 | |
| SpotlightKid |
8:75c5ec68765e | 29 | bool SwitchHandler::make_message(bool onoff) { |
| SpotlightKid |
8:75c5ec68765e | 30 | uint8_t value = (onoff ? cfg.on_value : cfg.off_value) & 0x7F; |
| SpotlightKid |
8:75c5ec68765e | 31 | msg.data[0] = CABLE_NUM | cfg.type; |
| SpotlightKid |
8:75c5ec68765e | 32 | msg.data[1] = cfg.type; |
| SpotlightKid |
8:75c5ec68765e | 33 | |
| SpotlightKid |
8:75c5ec68765e | 34 | switch(cfg.type) { |
| SpotlightKid |
8:75c5ec68765e | 35 | case 0xFA: // START |
| SpotlightKid |
8:75c5ec68765e | 36 | case 0xFB: // CONTINUE |
| SpotlightKid |
8:75c5ec68765e | 37 | case 0xFC: // STOP |
| SpotlightKid |
8:75c5ec68765e | 38 | msg.data[2] = 0x00; |
| SpotlightKid |
8:75c5ec68765e | 39 | msg.data[3] = 0x00; |
| SpotlightKid |
8:75c5ec68765e | 40 | break; |
| SpotlightKid |
8:75c5ec68765e | 41 | case 0xC0: // Program change |
| SpotlightKid |
8:75c5ec68765e | 42 | case 0xD0: // Channel Aftertouch |
| SpotlightKid |
8:75c5ec68765e | 43 | msg.data[1] = msg.data[1] | (cfg.channel & 0x0F); |
| SpotlightKid |
8:75c5ec68765e | 44 | msg.data[2] = value; |
| SpotlightKid |
8:75c5ec68765e | 45 | msg.data[3] = 0x00; |
| SpotlightKid |
8:75c5ec68765e | 46 | break; |
| SpotlightKid |
8:75c5ec68765e | 47 | case 0x80: // Note off |
| SpotlightKid |
8:75c5ec68765e | 48 | case 0x90: // Note on |
| SpotlightKid |
8:75c5ec68765e | 49 | msg.data[1] = msg.data[1] | (cfg.channel & 0x0F); |
| SpotlightKid |
8:75c5ec68765e | 50 | msg.data[3] = value; |
| SpotlightKid |
8:75c5ec68765e | 51 | msg.data[2] = cfg.data1; |
| SpotlightKid |
8:75c5ec68765e | 52 | break; |
| SpotlightKid |
8:75c5ec68765e | 53 | case 0xA0: // Poly pressure |
| SpotlightKid |
8:75c5ec68765e | 54 | case 0xB0: // Control change |
| SpotlightKid |
8:75c5ec68765e | 55 | case 0xE0: // PitchBend |
| SpotlightKid |
8:75c5ec68765e | 56 | msg.data[1] = msg.data[1] | (cfg.channel & 0x0F); |
| SpotlightKid |
8:75c5ec68765e | 57 | msg.data[2] = cfg.data1; |
| SpotlightKid |
8:75c5ec68765e | 58 | msg.data[3] = value; |
| SpotlightKid |
8:75c5ec68765e | 59 | break; |
| SpotlightKid |
8:75c5ec68765e | 60 | default: |
| SpotlightKid |
8:75c5ec68765e | 61 | return false; |
| SpotlightKid |
8:75c5ec68765e | 62 | } |
| SpotlightKid |
8:75c5ec68765e | 63 | return true; |
| SpotlightKid |
8:75c5ec68765e | 64 | }; |