Advertisement
retrokits

Wii2MIDI

Aug 4th, 2019
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. *  Project     Nintendo Extension Controller Library
  3. *  @author     David Madison
  4. *  @link       github.com/dmadison/NintendoExtensionCtrl
  5. *  @license    LGPLv3 - Copyright (c) 2018 David Madison
  6. *
  7. *  This file is part of the Nintendo Extension Controller Library.
  8. *
  9. *  This program is free software: you can redistribute it and/or modify
  10. *  it under the terms of the GNU Lesser General Public License as published by
  11. *  the Free Software Foundation, either version 3 of the License, or
  12. *  (at your option) any later version.
  13. *
  14. *  This program is distributed in the hope that it will be useful,
  15. *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. *  GNU Lesser General Public License for more details.
  18. *
  19. *  You should have received a copy of the GNU Lesser General Public License
  20. *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. *  Example:      Classic_Demo
  23. *  Description:  Connect to a Classic Controller and demonstrate all of
  24. *                the avaiable control data functions.
  25. *                
  26. *  ADDED BY RETROKITS:
  27. *  MIDIUSB library to translate buttons/stick values to notes and CC's
  28. *  variables to hold and detect message changes so the MIDI bus
  29. *  won't be flooded if no real controller action is going on
  30. *  
  31. */
  32.  
  33. #include <MIDIUSB.h>
  34.  
  35. #include <NintendoExtensionCtrl.h>
  36.  
  37. byte pad[19]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  38. byte changedPad[19]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  39. bool changedFlag[19]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  40.  
  41. byte notes[13]={36,37,38,39,40,41,42,43,44,45,46,47,48};
  42. byte ccs[6]=  {46, 45, 48, 44, 47, 42};
  43.  
  44. ClassicController classic;
  45.  
  46. void setup() {
  47.     //Serial.begin(115200);
  48.     classic.begin();
  49.  
  50.     while (!classic.connect()) {
  51.         //Serial.println("Classic Controller not detected!");
  52.         delay(1000);
  53.     }
  54. }
  55.  
  56. void noteOn(byte channel, byte pitch, byte velocity) {
  57.   midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  58.   MidiUSB.sendMIDI(noteOn);
  59. }
  60.  
  61. void noteOff(byte channel, byte pitch, byte velocity) {
  62.   midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  63.   MidiUSB.sendMIDI(noteOff);
  64. }
  65.  
  66. // First parameter is the event type (0x0B = control change).
  67. // Second parameter is the event type, combined with the channel.
  68. // Third parameter is the control number number (0-119).
  69. // Fourth parameter is the control value (0-127).
  70.  
  71. void controlChange(byte channel, byte control, byte value) {
  72.   midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  73.   MidiUSB.sendMIDI(event);
  74. }
  75.  
  76. void readAllStates(){
  77.   memcpy(changedPad, pad, sizeof(pad[0])*19);
  78.   pad[0]=classic.dpadLeft();
  79.   pad[1]=classic.dpadUp();
  80.   pad[2]=classic.dpadDown();
  81.   pad[3]=classic.dpadRight();
  82.  
  83.   pad[4]=classic.buttonMinus();
  84.   pad[5]=classic.buttonHome();
  85.   pad[6]=classic.buttonPlus();
  86.  
  87.   pad[7]=classic.buttonA();
  88.   pad[8]=classic.buttonB();
  89.   pad[9]=classic.buttonX();
  90.   pad[10]=classic.buttonY();
  91.  
  92.   pad[11]=classic.buttonZL();
  93.   pad[12]=classic.buttonZR();
  94.  
  95.   pad[13]=classic.leftJoyX();
  96.   pad[14]=classic.leftJoyY();
  97.  
  98.   pad[15]=classic.rightJoyX();
  99.   pad[16]=classic.rightJoyY();
  100.  
  101.   pad[17]=classic.triggerL();
  102.   pad[18]=classic.triggerR();
  103.  
  104.   }
  105. void loop() {
  106.   readAllStates();
  107.     boolean success = classic.update();  // Get new data from the controller
  108.  
  109.     if (!success) {
  110.         // ...no controller... wating...
  111.         delay(1000);
  112.     }
  113.     else
  114.     {
  115.     for(byte i=0;i<19;i++)
  116.     {
  117.       if(pad[i]!=changedPad[i]) changedFlag[i]=true;
  118.     }
  119.     for(byte i=0;i<19;i++)
  120.     {
  121.       if(changedFlag[i]==true)
  122.       {
  123.         if(i<13)
  124.         {
  125.           if(changedPad[i]==0)
  126.           {
  127.             noteOn(0, notes[i], 100);   // Channel 0, middle C, normal velocity
  128.             MidiUSB.flush();
  129.           }
  130.           else
  131.           {
  132.             noteOff(0, notes[i], 100);   // Channel 0, middle C, normal velocity
  133.             MidiUSB.flush();
  134.           }
  135.         }
  136.         else
  137.         {
  138.           // stick analog values upscaler
  139.           byte mul=2;
  140.           //right and rear stick have lower range
  141.           if(i>14)mul=4;
  142.           controlChange(0, ccs[i-13],min(127,changedPad[i]*mul));
  143.           MidiUSB.flush();
  144.         }
  145.         changedFlag[i]=false;
  146.       }
  147.     }
  148.    
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement