Advertisement
EvilAsh25

Arduino Firmware

Feb 11th, 2017
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.81 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////////////////////
  2. // Firmware for the Arduino UNO
  3. // v1.0
  4. // Written by EvilAsh25
  5. //
  6. // This is firmware that uses SPI (borrowed from Omnigamer's InputMania software) that is modified
  7. // with the option to display with either the InputMania or NintendoSpy software.
  8. // It will work with both NES and SNES inputs, but InputMania only supports SNES. It doesn't
  9. // support the Logging feature on InputMania, and sometimes you have to Log/Abort on InputMania
  10. // to get it to work, but I had to do that in the original software as well.
  11. //
  12. // InputMania: https://sourceforge.net/projects/inputcontrol/
  13. // NintendoSpy: https://github.com/jaburns/NintendoSpy
  14. ///////////////////////////////////////////////////////////////////////////////////////////////
  15.  
  16. #include <SPI.h>
  17.  
  18. // SET the mode for SNES or NES here (pick only one)
  19. #define MODE_SNES
  20. //#define MODE_NES
  21.  
  22. // SET the type of Display Software to support (pick only one)
  23. //#define MODE_INPUTMANIA
  24. #define MODE_NINTENDOSPY
  25.  
  26. //can't modify these, just for pin reference
  27. #define SNES_LATCH      2    //INT0 Pin
  28. #define SNES_DATA       11   //SPI MOSI Pin
  29. #define SNES_CLOCK      13   //SPI SCK Pin
  30.  
  31. #define ZERO  '\0'  // Use a byte value of 0x00 to represent a bit with value 0.
  32. #define ONE    '1'  // Use an ASCII one to represent a bit with value 1.  This makes Arduino debugging easier.
  33. #define SPLIT 255   // Use 255 byte to split up the controller state packets for InputMania.
  34. #define SPLIT2 '\n' // Use a new-line character to split up the controller state packets for NintendoSpy.
  35.  
  36. // Declare some space to store the bits we read from a controller.
  37. volatile unsigned char rawData[ 4 ];
  38. volatile byte pos = 0;
  39.  
  40. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  41. // General initialization, just sets all pins to input and starts serial communication.
  42. void setup()
  43. {
  44.     //Set up MOSI Pin
  45.     pinMode(MOSI, INPUT);
  46.     digitalWrite(MOSI, HIGH);
  47.    
  48.     //Set up SPI
  49.     SPCR =(0<<SPE)|(0<<MSTR)|(0<<SPR1)|(0<<SPR0)|(1<<CPOL)|(0<<CPHA)|(1<<SPIE)|(1<<DORD);
  50.    
  51.     SPI.setBitOrder(LSBFIRST);
  52.     SPI.setDataMode(SPI_MODE2);
  53.     SPI.setClockDivider(SPI_CLOCK_DIV2);
  54.     SPI.attachInterrupt();
  55.    
  56.     //Set up INT0 interrupt
  57.     EICRA &= ~3;
  58.     EICRA |= 3;
  59.     EIMSK |= 1;
  60.  
  61.     // Open the Serial Comms
  62.     Serial.begin( 115200 );
  63. }
  64.  
  65. ISR(INT0_vect)
  66. {
  67.     //Turn on SPI Interrupts
  68.     SPCR =(1<<SPE)|(0<<MSTR)|(0<<SPR1)|(0<<SPR0)|(1<<CPOL)|(0<<CPHA)|(1<<SPIE)|(1<<DORD);
  69. }
  70.  
  71. ISR(SPI_STC_vect)
  72. {
  73.     // grab byte from SPI Data Register (controller data)
  74.     rawData[pos++] = SPDR;  
  75. }
  76.  
  77. // Sends a packet of controller data over the Arduino serial interface.
  78. inline void sendRawDataInputMania()
  79. {
  80.      //Reset to state 0
  81.      pos = 0;
  82.  
  83.      // Write controller data to the Serial output
  84.      Serial.write( rawData[0] );
  85.      Serial.write( rawData[1] );
  86.      Serial.write( SPLIT );
  87. }
  88.  
  89. inline void sendRawDataNintendoSpy( unsigned char count )
  90. {
  91.   //Reset to state 0
  92.   pos = 0;
  93.  
  94.   // Write controller data to the Serial output
  95.   for(int i = 0; i < (count/8); i++)
  96.   {
  97.     Serial.write( (rawData[i] & 0x01) ? ZERO : ONE );
  98.     Serial.write( (rawData[i] & 0x02) ? ZERO : ONE );
  99.     Serial.write( (rawData[i] & 0x04) ? ZERO : ONE );
  100.     Serial.write( (rawData[i] & 0x08) ? ZERO : ONE );
  101.     Serial.write( (rawData[i] & 0x10) ? ZERO : ONE );
  102.     Serial.write( (rawData[i] & 0x20) ? ZERO : ONE );
  103.     Serial.write( (rawData[i] & 0x40) ? ZERO : ONE );
  104.     Serial.write( (rawData[i] & 0x80) ? ZERO : ONE );
  105.   }  
  106.   Serial.write( SPLIT2 );
  107. }
  108.  
  109. inline void loop_SNES()
  110. {
  111.      if(pos >= 2){
  112.         //If 2 packets collected...
  113.         //Turn off SPI Interrupts
  114.         SPCR =(0<<SPE)|(0<<MSTR)|(0<<SPR1)|(0<<SPR0)|(1<<CPOL)|(0<<CPHA)|(1<<SPIE)|(1<<DORD);
  115.        
  116. #ifdef MODE_INPUTMANIA
  117.         sendRawDataInputMania(); //Send collected data for InputMania
  118. #elif defined MODE_NINTENDOSPY
  119.         sendRawDataNintendoSpy(16); //Send collected data for NintendoSpy
  120. #endif
  121.      }
  122. }
  123.  
  124. inline void loop_NES()
  125. {
  126.      if(pos >= 1){
  127.         //If 1 packet collected...
  128.         //Turn off SPI Interrupts
  129.         SPCR =(0<<SPE)|(0<<MSTR)|(0<<SPR1)|(0<<SPR0)|(1<<CPOL)|(0<<CPHA)|(1<<SPIE)|(1<<DORD);
  130.  
  131. #ifdef MODE_INPUTMANIA
  132.         sendRawDataInputMania(); //Send collected data for InputMania (not actually supported)
  133. #elif defined MODE_NINTENDOSPY
  134.         sendRawDataNintendoSpy(8); //Send collected data for NintendoSpy
  135. #endif
  136.      }
  137. }
  138.  
  139.  
  140. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  141. // Arduino sketch main loop definition.
  142. void loop()
  143. {
  144. #ifdef MODE_SNES
  145.     loop_SNES();
  146. #elif defined MODE_NES
  147.     loop_NES();
  148. #endif
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement