vmuller

Arduino Leonardo USB Midi

May 15th, 2022 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <midi_serialization.h>
  2. #include <usbmidi.h>
  3.  
  4. void sendCC(uint8_t channel, uint8_t control, uint8_t value) {
  5.   USBMIDI.write(0xB0 | (channel & 0xf));
  6.   USBMIDI.write(control & 0x7f);
  7.   USBMIDI.write(value & 0x7f);
  8. }
  9.  
  10. void sendNote(uint8_t channel, uint8_t note, uint8_t velocity) {
  11.   USBMIDI.write((velocity != 0 ? 0x90 : 0x80) | (channel & 0xf));
  12.   USBMIDI.write(note & 0x7f);
  13.   USBMIDI.write(velocity & 0x7f);
  14. }
  15.  
  16.  
  17. #include <Keypad.h>
  18.  
  19. const byte ROWS = 4; // four rows
  20. const byte COLS = 4; // four columns
  21.  
  22. char keys[ROWS][COLS] = {
  23.   {(char)36, (char)37, (char)38, (char)39},
  24.   {(char)40, (char)41, (char)42, (char)43},
  25.   {(char)44, (char)45, (char)46, (char)47},
  26.   {(char)48, (char)49, (char)50, (char)51}
  27. };
  28.  
  29. byte rowPins[ROWS] = {2, 3, 4, 5}; // connect to the row pinouts of the keypad
  30. byte colPins[COLS] = {6, 7, 8, 9};    // connect to the column pinouts of the keypad
  31.  
  32. Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  33.  
  34.  
  35. #include <LedControl.h>
  36. LedControl lc = LedControl(11,13,10,1); //DIN, SCK, LOAD,1xMAX7219
  37.  
  38. byte state = 0;
  39. byte channel = 0;
  40. byte note;
  41.  
  42. boolean noteDown = LOW;
  43.  
  44.  
  45. void onLED () {
  46.   if ((note >= 36) && (note <= 99)) {
  47.     int col = (note - 36) / 8;
  48.     int row = (note - 36) % 8;
  49.     lc.setLed(0, col, row, true);
  50.   }
  51. }
  52.  
  53. void offLED ()
  54. {
  55.   if ((note >= 36) && (note <= 99)) {
  56.     int col = (note - 36) / 8;
  57.     int row = (note - 36) % 8;
  58.     lc.setLed(0, col, row, false);
  59.   }
  60. }
  61.  
  62.  
  63. void setup() {
  64.   pinMode(13, OUTPUT);
  65.   lc.shutdown(0, false);
  66.   lc.setIntensity(0, 8);
  67.   lc.clearDisplay(0);
  68.   digitalWrite(13, LOW);
  69. }
  70.  
  71.  
  72. void loop() {
  73.   //Handle USB communication
  74.   USBMIDI.poll();
  75.  
  76.   while (USBMIDI.available()) {
  77.     // We must read entire available data, so in case we receive incoming
  78.     // MIDI data, the host wouldn't get stuck.
  79.  
  80.     byte incomingByte = USBMIDI.read();
  81.  
  82.     switch (state) {
  83.       case 0:
  84.         // look for as status-byte, our channel, note on
  85.         if (incomingByte == ( 0x90 | channel)) {  // read only one channel
  86.           noteDown = HIGH;
  87.           state = 1;
  88.         }
  89.         // look for as status-byte, our channel, note off
  90.         if (incomingByte == (0x80 | channel)) {   // read only one channel
  91.           noteDown = LOW;
  92.           state = 1;
  93.         }
  94.         // look for any after touch, or program message
  95.         if ((incomingByte & 0xE0) == 0xC0) {
  96.           state = 4; // just wait for the data
  97.         }
  98.         // look for any control or polyphonic after touch
  99.         if ((incomingByte & 0xE0) == 0xA0) {
  100.           state = 3; // just wait for two bytes of data
  101.         }
  102.         // look for any pitch wheel or Channel Mode data
  103.         if ((incomingByte & 0xF0) == 0xA0 || (incomingByte & 0xF0) == 0xB0) {
  104.           state = 3; // just wait for two bytes of data
  105.         }
  106.         break;
  107.       case 1:
  108.         // get the note to play or stop
  109.         if (incomingByte < 128) {
  110.           note = incomingByte;
  111.           state = 2;
  112.         }
  113.         else {
  114.           state = 0;   // reset state machine as this should be a note number
  115.         }
  116.         break;
  117.       case 2:
  118.         // get the velocity
  119.         if (incomingByte < 128) {
  120.           doNote(note, incomingByte, noteDown);
  121.         }
  122.         state = 0;   // reset state machine to start
  123.         break;
  124.       case 3: // first of two bytes to discard
  125.         state = 4; // next byte to discard
  126.         break;
  127.       case 4: // data to discard
  128.         state = 0; // reset state machine
  129.     }
  130.  
  131.   }
  132.  
  133.   if (kpd.getKeys())
  134.   {
  135.     for (int i = 0; i < LIST_MAX; i++) // Scan the whole key list.
  136.     {
  137.       if ( kpd.key[i].stateChanged )   // Only find keys that have changed state.
  138.       {
  139.         switch (kpd.key[i].kstate) {  // Report active key state : IDLE, PRESSED, HOLD, or RELEASED
  140.           case PRESSED:
  141.             sendNote(0, kpd.key[i].kchar, 127);
  142.             break;
  143.           case RELEASED:
  144.             sendNote(0, kpd.key[i].kchar, 0);
  145.         }
  146.       }
  147.     }
  148.   }
  149.   // Flush the output.
  150.   USBMIDI.flush();
  151. }
  152.  
  153.  
  154. void doNote(byte note, byte velocity, int down) {
  155.   // if velocity = 0 on a 'Note ON1 command, treat it as a note off
  156.   if ((down == HIGH) && (velocity == 0)) {
  157.     down = LOW;
  158.   }
  159.   // send out this note message
  160.   if (down == LOW) offLED();
  161.   else onLED();
  162. }
  163.  
Add Comment
Please, Sign In to add comment