Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Project Nintendo Extension Controller Library
- * @author David Madison
- * @link github.com/dmadison/NintendoExtensionCtrl
- * @license LGPLv3 - Copyright (c) 2018 David Madison
- *
- * This file is part of the Nintendo Extension Controller Library.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * Example: Classic_Demo
- * Description: Connect to a Classic Controller and demonstrate all of
- * the avaiable control data functions.
- *
- * ADDED BY RETROKITS:
- * MIDIUSB library to translate buttons/stick values to notes and CC's
- * variables to hold and detect message changes so the MIDI bus
- * won't be flooded if no real controller action is going on
- *
- */
- #include <MIDIUSB.h>
- #include <NintendoExtensionCtrl.h>
- byte pad[19]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
- byte changedPad[19]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
- bool changedFlag[19]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
- byte notes[13]={36,37,38,39,40,41,42,43,44,45,46,47,48};
- byte ccs[6]= {46, 45, 48, 44, 47, 42};
- ClassicController classic;
- void setup() {
- //Serial.begin(115200);
- classic.begin();
- while (!classic.connect()) {
- //Serial.println("Classic Controller not detected!");
- delay(1000);
- }
- }
- void noteOn(byte channel, byte pitch, byte velocity) {
- midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
- MidiUSB.sendMIDI(noteOn);
- }
- void noteOff(byte channel, byte pitch, byte velocity) {
- midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
- MidiUSB.sendMIDI(noteOff);
- }
- // First parameter is the event type (0x0B = control change).
- // Second parameter is the event type, combined with the channel.
- // Third parameter is the control number number (0-119).
- // Fourth parameter is the control value (0-127).
- void controlChange(byte channel, byte control, byte value) {
- midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
- MidiUSB.sendMIDI(event);
- }
- void readAllStates(){
- memcpy(changedPad, pad, sizeof(pad[0])*19);
- pad[0]=classic.dpadLeft();
- pad[1]=classic.dpadUp();
- pad[2]=classic.dpadDown();
- pad[3]=classic.dpadRight();
- pad[4]=classic.buttonMinus();
- pad[5]=classic.buttonHome();
- pad[6]=classic.buttonPlus();
- pad[7]=classic.buttonA();
- pad[8]=classic.buttonB();
- pad[9]=classic.buttonX();
- pad[10]=classic.buttonY();
- pad[11]=classic.buttonZL();
- pad[12]=classic.buttonZR();
- pad[13]=classic.leftJoyX();
- pad[14]=classic.leftJoyY();
- pad[15]=classic.rightJoyX();
- pad[16]=classic.rightJoyY();
- pad[17]=classic.triggerL();
- pad[18]=classic.triggerR();
- }
- void loop() {
- readAllStates();
- boolean success = classic.update(); // Get new data from the controller
- if (!success) {
- // ...no controller... wating...
- delay(1000);
- }
- else
- {
- for(byte i=0;i<19;i++)
- {
- if(pad[i]!=changedPad[i]) changedFlag[i]=true;
- }
- for(byte i=0;i<19;i++)
- {
- if(changedFlag[i]==true)
- {
- if(i<13)
- {
- if(changedPad[i]==0)
- {
- noteOn(0, notes[i], 100); // Channel 0, middle C, normal velocity
- MidiUSB.flush();
- }
- else
- {
- noteOff(0, notes[i], 100); // Channel 0, middle C, normal velocity
- MidiUSB.flush();
- }
- }
- else
- {
- // stick analog values upscaler
- byte mul=2;
- //right and rear stick have lower range
- if(i>14)mul=4;
- controlChange(0, ccs[i-13],min(127,changedPad[i]*mul));
- MidiUSB.flush();
- }
- changedFlag[i]=false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement