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@11:ff22140b5119, 2017-08-04 (annotated)
- Committer:
- SpotlightKid
- Date:
- Fri Aug 04 07:23:38 2017 +0200
- Revision:
- 11:ff22140b5119
- Parent:
- 9:d5fa853818dd
- Child:
- 12:d9ccbfd2cc8c
Change encoding of message type
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 |
11:ff22140b5119 | 32 | msg.data[1] = (cfg.type << 4) | (cfg.channel & 0x0F); |
| SpotlightKid |
11:ff22140b5119 | 33 | msg.data[2] = 0x00; |
| SpotlightKid |
11:ff22140b5119 | 34 | msg.data[3] = 0x00; |
| SpotlightKid |
8:75c5ec68765e | 35 | |
| SpotlightKid |
8:75c5ec68765e | 36 | switch(cfg.type) { |
| SpotlightKid |
11:ff22140b5119 | 37 | case 2: // START |
| SpotlightKid |
11:ff22140b5119 | 38 | case 3: // CONTINUE |
| SpotlightKid |
11:ff22140b5119 | 39 | case 4: // STOP |
| SpotlightKid |
11:ff22140b5119 | 40 | msg.data[0] = 0xF; |
| SpotlightKid |
11:ff22140b5119 | 41 | msg.data[1] = 0xF8 + cfg.type; |
| SpotlightKid |
8:75c5ec68765e | 42 | break; |
| SpotlightKid |
11:ff22140b5119 | 43 | case 0xC: // Program change |
| SpotlightKid |
11:ff22140b5119 | 44 | case 0xD: // Channel Aftertouch |
| SpotlightKid |
8:75c5ec68765e | 45 | msg.data[2] = value; |
| SpotlightKid |
8:75c5ec68765e | 46 | break; |
| SpotlightKid |
11:ff22140b5119 | 47 | case 0x8: // Note off |
| SpotlightKid |
11:ff22140b5119 | 48 | case 0x9: // Note on |
| SpotlightKid |
8:75c5ec68765e | 49 | msg.data[3] = value; |
| SpotlightKid |
9:d5fa853818dd | 50 | msg.data[2] = cfg.data1 & 0x0F; |
| SpotlightKid |
8:75c5ec68765e | 51 | break; |
| SpotlightKid |
11:ff22140b5119 | 52 | case 0xA: // Poly pressure |
| SpotlightKid |
11:ff22140b5119 | 53 | case 0xB: // Control change |
| SpotlightKid |
11:ff22140b5119 | 54 | case 0xE: // PitchBend |
| SpotlightKid |
9:d5fa853818dd | 55 | msg.data[2] = cfg.data1 & 0x0F; |
| SpotlightKid |
8:75c5ec68765e | 56 | msg.data[3] = value; |
| SpotlightKid |
8:75c5ec68765e | 57 | break; |
| SpotlightKid |
8:75c5ec68765e | 58 | default: |
| SpotlightKid |
8:75c5ec68765e | 59 | return false; |
| SpotlightKid |
8:75c5ec68765e | 60 | } |
| SpotlightKid |
8:75c5ec68765e | 61 | return true; |
| SpotlightKid |
8:75c5ec68765e | 62 | }; |