Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworked code for more universal nintendo-input, added functionality #44

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
218 changes: 218 additions & 0 deletions galagino/Nintendo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
#ifndef _NINTENDO_H_
#define _NINTENDO_H_

// ----------------------------
// Standard Libraries
// ----------------------------

#include <Wire.h>

// ----------------------------
// Additional Libraries - each one of these will need to be installed.
// ----------------------------

#include <NintendoExtensionCtrl.h>
// This library is for interfacing with the Nunchuck

// Can be installed from the library manager
// https://github.com/dmadison/NintendoExtensionCtrl


bool nun=false;
bool classic=false;



ExtensionPort check; //Instance to check Controllertype
ClassicController inputC; //Classic Instance
Nunchuk inputN; //Nunchuk instance

void inputSetup() {

Wire.begin(NINTENDO_SDA, NINTENDO_SCL); //use our pins
check.begin();
check.connect();
while (!check.connect()) {
Serial.println("Controller not detected!");
delay(1000);};

ExtensionType conType = check.getControllerType(); //check our controllertype

switch (conType) {
case(ExtensionType::NoController):
Serial.println("No controller detected");
break;
case(ExtensionType::UnknownController):
Serial.println("Unknown controller connected");
break;
case(ExtensionType::Nunchuk):
Serial.println("Nunchuk connected!");
inputN.begin(); //instantiate Nunchuk-Inputfunction
inputN.connect();
while (!inputN.connect()) {
Serial.println("Controller not detected!");
delay(1000);};
nun=true; // we have a Nunchuk connected!
break;

case(ExtensionType::ClassicController):
Serial.println("Classic Controller connected!");
inputC.begin(); //instantiate ClassicController-Inputfunction
inputC.connect();
while (!inputC.connect()) {
Serial.println("Controller not detected!");
delay(1000);};
classic=true; // we have a ClassicController connected!
break;

case(ExtensionType::GuitarController):
Serial.println("Guitar controller connected! WTF?"); //Not yet supported
break;

case(ExtensionType::DrumController):
Serial.println("Drum set controller connected! WTF?"); //Not yet supported
break;

case(ExtensionType::DJTurntableController):
Serial.println("DJ turntable connected! WTF?"); //Not yet supported
break;

case(ExtensionType::uDrawTablet):
Serial.println("uDraw Tablet connected! WTF?"); //Not yet supported
break;

case(ExtensionType::DrawsomeTablet):
Serial.println("Drawsome Tablet connected! WTF?"); //Not yet supported
break;

default: break;
}
};









unsigned char getNintendoInput() {
boolean success = false;
if (classic) success = inputC.update(); // Get new data from the Classic-Controller
else if (nun) success = inputN.update(); // Get new data from the Nunchuk


if (!success) { // Ruh roh
Serial.println("Nintendo Controller device disconnected!");
return 0;
}
else


if (classic) //input definitions from Classic Controller
{
#ifdef CLASSIC_DPAD
#ifndef CLASSIC_LJOY
//int joyY = inputC.leftJoyY(); //uncomment if Joystick should be used
//int joyX = inputC.leftJoyX(); //uncomment if Joystick should be used


return (inputC.dpadLeft() ? BUTTON_LEFT : 0) | //Move Left, comment out if Joystick should be used
(inputC.dpadRight() ? BUTTON_RIGHT : 0) | //Move Right, comment out if Joystick should be used
(inputC.dpadUp() ? BUTTON_UP : 0) | //Move Up,comment out if Joystick should be used
(inputC.dpadDown() ? BUTTON_DOWN : 0) | //Move Down,comment out if Joystick should be used
(inputC.buttonMinus() ? BUTTON_COIN : 0) | //Coin
(inputC.buttonPlus() ? BUTTON_START : 0) | //start
(inputC.buttonHome() ? BUTTON_HOME : 0) |//home

// ((joyX < 127 - JOYSTICK_MOVE_THRESHOLD) ? BUTTON_LEFT : 0) | //Move Left //uncomment if Joystick should be used
// ((joyX > 127 + JOYSTICK_MOVE_THRESHOLD) ? BUTTON_RIGHT : 0) | //Move Right //uncomment if Joystick should be used
// ((joyY > 127 + JOYSTICK_MOVE_THRESHOLD) ? BUTTON_UP : 0) | //Move Up //uncomment if Joystick should be used
// ((joyY < 127 - JOYSTICK_MOVE_THRESHOLD) ? BUTTON_DOWN : 0) | //Move Down //uncomment if Joystick should be used


(inputC.buttonA() ? BUTTON_FIRE : 0) ;
#endif
#endif

#ifdef CLASSIC_LJOY
#ifndef CLASSIC_DPAD
int joyY = inputC.leftJoyY(); //uncomment if Joystick should be used
int joyX = inputC.leftJoyX(); //uncomment if Joystick should be used


return //(inputC.dpadLeft() ? BUTTON_LEFT : 0) | //Move Left, comment out if Joystick should be used
//(inputC.dpadRight() ? BUTTON_RIGHT : 0) | //Move Right, comment out if Joystick should be used
//(inputC.dpadUp() ? BUTTON_UP : 0) | //Move Up,comment out if Joystick should be used
//(inputC.dpadDown() ? BUTTON_DOWN : 0) | //Move Down,comment out if Joystick should be used
(inputC.buttonMinus() ? BUTTON_COIN : 0) | //Coin
(inputC.buttonPlus() ? BUTTON_START : 0) | //start
(inputC.buttonHome() ? BUTTON_HOME : 0) |//home

((joyX < 127 - JOYSTICK_MOVE_THRESHOLD) ? BUTTON_LEFT : 0) | //Move Left //uncomment if Joystick should be used
((joyX > 127 + JOYSTICK_MOVE_THRESHOLD) ? BUTTON_RIGHT : 0) | //Move Right //uncomment if Joystick should be used
((joyY > 127 + JOYSTICK_MOVE_THRESHOLD) ? BUTTON_UP : 0) | //Move Up //uncomment if Joystick should be used
((joyY < 127 - JOYSTICK_MOVE_THRESHOLD) ? BUTTON_DOWN : 0) | //Move Down //uncomment if Joystick should be used


(inputC.buttonA() ? BUTTON_FIRE : 0) ;

#endif
#endif

#ifdef CLASSIC_LJOY
#ifdef CLASSIC_DPAD
int joyY = inputC.leftJoyY(); //uncomment if Joystick should be used
int joyX = inputC.leftJoyX(); //uncomment if Joystick should be used


return (inputC.dpadLeft() ? BUTTON_LEFT : 0) | //Move Left, comment out if Joystick should be used
(inputC.dpadRight() ? BUTTON_RIGHT : 0) | //Move Right, comment out if Joystick should be used
(inputC.dpadUp() ? BUTTON_UP : 0) | //Move Up,comment out if Joystick should be used
(inputC.dpadDown() ? BUTTON_DOWN : 0) | //Move Down,comment out if Joystick should be used
(inputC.buttonMinus() ? BUTTON_COIN : 0) | //Coin
(inputC.buttonPlus() ? BUTTON_START : 0) | //start
(inputC.buttonHome() ? BUTTON_HOME : 0) |//home

((joyX < 127 - JOYSTICK_MOVE_THRESHOLD) ? BUTTON_LEFT : 0) | //Move Left //uncomment if Joystick should be used
((joyX > 127 + JOYSTICK_MOVE_THRESHOLD) ? BUTTON_RIGHT : 0) | //Move Right //uncomment if Joystick should be used
((joyY > 127 + JOYSTICK_MOVE_THRESHOLD) ? BUTTON_UP : 0) | //Move Up //uncomment if Joystick should be used
((joyY < 127 - JOYSTICK_MOVE_THRESHOLD) ? BUTTON_DOWN : 0) | //Move Down //uncomment if Joystick should be used


(inputC.buttonA() ? BUTTON_FIRE : 0) ;

#endif
#endif

#ifndef CLASSIC_LJOY
#ifndef CLASSIC_DPAD
#error "Define at least one method of input for a Classic Controller in config.h!"
#endif
#endif
}


if (nun)
{
// Read a joystick axis (0-255, X and Y)
// Roughly 127 will be the axis centered
int joyY = inputN.joyY();
int joyX = inputN.joyX();

return ((joyX < 127 - JOYSTICK_MOVE_THRESHOLD) ? BUTTON_LEFT : 0) | //Move Left
((joyX > 127 + JOYSTICK_MOVE_THRESHOLD) ? BUTTON_RIGHT : 0) | //Move Right
((joyY > 127 + JOYSTICK_MOVE_THRESHOLD) ? BUTTON_UP : 0) | //Move Up
((joyY < 127 - JOYSTICK_MOVE_THRESHOLD) ? BUTTON_DOWN : 0) | //Move Down
(inputN.buttonZ() ? BUTTON_FIRE : 0)|
(inputN.buttonC() ? BUTTON_COIN
: 0) ;

};



}

#endif //_NINTENDO_H_
56 changes: 0 additions & 56 deletions galagino/Nunchuck.h

This file was deleted.

12 changes: 7 additions & 5 deletions galagino/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#include "dip_switches.h"

// #define CHEAP_YELLOW_DISPLAY_CONF
#define CHEAP_YELLOW_DISPLAY_CONF

#ifndef CHEAP_YELLOW_DISPLAY_CONF // Config as it was before

Expand Down Expand Up @@ -90,13 +90,15 @@
// audio config (leave both commented out for GPIO 25 for Audio)
// #define SND_DIFF // set to output differential audio on GPIO25 _and_ inverted on GPIO26
#define SND_LEFT_CHANNEL // Use GPIO 26 for audio
#define NINTENDO_INPUT // currently supported: Nunchuk and Classic Controller
#define CLASSIC_DPAD // Use DPAD or use left Joystick or both, if so desired
#define CLASSIC_LJOY // Use left joystick

#define NUNCHUCK_INPUT

#define NUNCHUCK_SDA 22
#define NUNCHUCK_SCL 27
#define NINTENDO_SDA 22
#define NINTENDO_SCL 27

#define NUNCHUCK_MOVE_THRESHOLD 30 // This is the dead-zone for where minor movements on the stick will not be considered valid movements
#define JOYSTICK_MOVE_THRESHOLD 30 // This is the dead-zone for where minor movements on the stick will not be considered valid movements

// Pins used for buttons
#define BTN_START_PIN 0 //This is the "boot" button
Expand Down
5 changes: 3 additions & 2 deletions galagino/emulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern unsigned char dkong_audio_transfer_buffer[DKONG_AUDIO_QUEUE_LEN][64];
extern unsigned char dkong_audio_rptr, dkong_audio_wptr;
#endif

// a total of 7 button is needed for
// a total of 8 button is needed for
// most games
#define BUTTON_LEFT 0x01
#define BUTTON_RIGHT 0x02
Expand All @@ -18,7 +18,8 @@ extern unsigned char dkong_audio_rptr, dkong_audio_wptr;
#define BUTTON_FIRE 0x10
#define BUTTON_START 0x20
#define BUTTON_COIN 0x40
#define BUTTON_EXTRA 0x80
#define BUTTON_HOME 0x80


#ifndef SINGLE_MACHINE
enum {
Expand Down
Loading