Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////////////////////////////////////////////
- // Firmware for the Arduino UNO
- // v1.0
- // Written by EvilAsh25
- //
- // This is firmware that uses SPI (borrowed from Omnigamer's InputMania software) that is modified
- // with the option to display with either the InputMania or NintendoSpy software.
- // It will work with both NES and SNES inputs, but InputMania only supports SNES. It doesn't
- // support the Logging feature on InputMania, and sometimes you have to Log/Abort on InputMania
- // to get it to work, but I had to do that in the original software as well.
- //
- // InputMania: https://sourceforge.net/projects/inputcontrol/
- // NintendoSpy: https://github.com/jaburns/NintendoSpy
- ///////////////////////////////////////////////////////////////////////////////////////////////
- #include <SPI.h>
- // SET the mode for SNES or NES here (pick only one)
- #define MODE_SNES
- //#define MODE_NES
- // SET the type of Display Software to support (pick only one)
- //#define MODE_INPUTMANIA
- #define MODE_NINTENDOSPY
- //can't modify these, just for pin reference
- #define SNES_LATCH 2 //INT0 Pin
- #define SNES_DATA 11 //SPI MOSI Pin
- #define SNES_CLOCK 13 //SPI SCK Pin
- #define ZERO '\0' // Use a byte value of 0x00 to represent a bit with value 0.
- #define ONE '1' // Use an ASCII one to represent a bit with value 1. This makes Arduino debugging easier.
- #define SPLIT 255 // Use 255 byte to split up the controller state packets for InputMania.
- #define SPLIT2 '\n' // Use a new-line character to split up the controller state packets for NintendoSpy.
- // Declare some space to store the bits we read from a controller.
- volatile unsigned char rawData[ 4 ];
- volatile byte pos = 0;
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // General initialization, just sets all pins to input and starts serial communication.
- void setup()
- {
- //Set up MOSI Pin
- pinMode(MOSI, INPUT);
- digitalWrite(MOSI, HIGH);
- //Set up SPI
- SPCR =(0<<SPE)|(0<<MSTR)|(0<<SPR1)|(0<<SPR0)|(1<<CPOL)|(0<<CPHA)|(1<<SPIE)|(1<<DORD);
- SPI.setBitOrder(LSBFIRST);
- SPI.setDataMode(SPI_MODE2);
- SPI.setClockDivider(SPI_CLOCK_DIV2);
- SPI.attachInterrupt();
- //Set up INT0 interrupt
- EICRA &= ~3;
- EICRA |= 3;
- EIMSK |= 1;
- // Open the Serial Comms
- Serial.begin( 115200 );
- }
- ISR(INT0_vect)
- {
- //Turn on SPI Interrupts
- SPCR =(1<<SPE)|(0<<MSTR)|(0<<SPR1)|(0<<SPR0)|(1<<CPOL)|(0<<CPHA)|(1<<SPIE)|(1<<DORD);
- }
- ISR(SPI_STC_vect)
- {
- // grab byte from SPI Data Register (controller data)
- rawData[pos++] = SPDR;
- }
- // Sends a packet of controller data over the Arduino serial interface.
- inline void sendRawDataInputMania()
- {
- //Reset to state 0
- pos = 0;
- // Write controller data to the Serial output
- Serial.write( rawData[0] );
- Serial.write( rawData[1] );
- Serial.write( SPLIT );
- }
- inline void sendRawDataNintendoSpy( unsigned char count )
- {
- //Reset to state 0
- pos = 0;
- // Write controller data to the Serial output
- for(int i = 0; i < (count/8); i++)
- {
- Serial.write( (rawData[i] & 0x01) ? ZERO : ONE );
- Serial.write( (rawData[i] & 0x02) ? ZERO : ONE );
- Serial.write( (rawData[i] & 0x04) ? ZERO : ONE );
- Serial.write( (rawData[i] & 0x08) ? ZERO : ONE );
- Serial.write( (rawData[i] & 0x10) ? ZERO : ONE );
- Serial.write( (rawData[i] & 0x20) ? ZERO : ONE );
- Serial.write( (rawData[i] & 0x40) ? ZERO : ONE );
- Serial.write( (rawData[i] & 0x80) ? ZERO : ONE );
- }
- Serial.write( SPLIT2 );
- }
- inline void loop_SNES()
- {
- if(pos >= 2){
- //If 2 packets collected...
- //Turn off SPI Interrupts
- SPCR =(0<<SPE)|(0<<MSTR)|(0<<SPR1)|(0<<SPR0)|(1<<CPOL)|(0<<CPHA)|(1<<SPIE)|(1<<DORD);
- #ifdef MODE_INPUTMANIA
- sendRawDataInputMania(); //Send collected data for InputMania
- #elif defined MODE_NINTENDOSPY
- sendRawDataNintendoSpy(16); //Send collected data for NintendoSpy
- #endif
- }
- }
- inline void loop_NES()
- {
- if(pos >= 1){
- //If 1 packet collected...
- //Turn off SPI Interrupts
- SPCR =(0<<SPE)|(0<<MSTR)|(0<<SPR1)|(0<<SPR0)|(1<<CPOL)|(0<<CPHA)|(1<<SPIE)|(1<<DORD);
- #ifdef MODE_INPUTMANIA
- sendRawDataInputMania(); //Send collected data for InputMania (not actually supported)
- #elif defined MODE_NINTENDOSPY
- sendRawDataNintendoSpy(8); //Send collected data for NintendoSpy
- #endif
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Arduino sketch main loop definition.
- void loop()
- {
- #ifdef MODE_SNES
- loop_SNES();
- #elif defined MODE_NES
- loop_NES();
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement