Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usb_midi.c 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Extended from TinyUSB example code.
  3. *
  4. * Copyright (c) 2022 - 2023 Thomas Buck (thomas@xythobuz.de)
  5. *
  6. * The MIT License (MIT)
  7. *
  8. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. *
  28. */
  29. #include "bsp/board.h"
  30. #include "tusb.h"
  31. #include "log.h"
  32. #include "ui.h"
  33. #include "usb_midi.h"
  34. #define MIDI_CABLE_NUM 0 // MIDI jack associated with USB endpoint
  35. #define MIDI_NOTE_OFF 0x80
  36. #define MIDI_NOTE_ON 0x90
  37. void usb_midi_tx(uint8_t channel, uint8_t note, uint8_t velocity) {
  38. uint8_t packet[3] = {
  39. ((velocity == 0) ? MIDI_NOTE_OFF : MIDI_NOTE_ON) | channel,
  40. note, velocity,
  41. };
  42. tud_midi_stream_write(MIDI_CABLE_NUM, packet, sizeof(packet));
  43. }
  44. void usb_midi_run(void) {
  45. while (tud_midi_available()) {
  46. uint8_t packet[4];
  47. tud_midi_packet_read(packet);
  48. //debug("rx: %02X %02X %02X %02X", packet[0], packet[1], packet[2], packet[3]);
  49. uint8_t type = packet[1] & 0xF0;
  50. uint8_t channel = packet[1] & 0x0F;
  51. uint8_t note = packet[2];
  52. uint8_t velocity = packet[3];
  53. //debug("rx: type=%02X channel=%d note=%d velocity=%d", type, channel, note, velocity);
  54. if (type == MIDI_NOTE_OFF) {
  55. ui_midi_set(channel, note, 0);
  56. } else if (type == MIDI_NOTE_ON) {
  57. ui_midi_set(channel, note, (velocity == 0) ? 127 : velocity);
  58. }
  59. }
  60. }