Mil alteraçoes

This commit is contained in:
Pedro de Oliveira 2014-09-15 00:19:13 +01:00
parent 15c0dadd11
commit 4196f9ba40
1 changed files with 58 additions and 58 deletions

View File

@ -1,14 +1,14 @@
/* /*
Based on https://github.com/njguibert/NesJoystickArduino/ Based on https://github.com/njguibert/NesJoystickArduino/
Adapted for SNES with info from http://www.gamefaqs.com/snes/916396-super-nintendo/faqs/5395 Adapted for SNES with info from http://www.gamefaqs.com/snes/916396-super-nintendo/faqs/5395
Snes Controller - Arduino Snes Controller - Arduino
White 5V - 5V White 5V - 5V
Brown GND - GND Brown GND - GND
Yellow (Clock) - Pin 13 Yellow (Clock) - Pin 13
Orange (Latch) - Pin 12 Orange (Latch) - Pin 12
Red (Data) - Pin 11 Red (Data) - Pin 11
*/ */
static int clock = 13; // set the clock pin static int clock = 13; // set the clock pin
static int latch = 12; // set the latch pin static int latch = 12; // set the latch pin
@ -17,7 +17,9 @@ static int datin = 11; // set the data in pin
JoyState_t joySt; JoyState_t joySt;
void setup() { void setup() {
//Serial.begin(9600); Serial.begin(9600);
// while the serial stream is not open, do nothing:
while (!Serial) ;
pinMode(latch,OUTPUT); pinMode(latch,OUTPUT);
pinMode(clock,OUTPUT); pinMode(clock,OUTPUT);
@ -29,27 +31,23 @@ void setup() {
joySt.zAxis = 127; joySt.zAxis = 127;
} }
word controllerRead() { word controllerRead()
{
word data = 0; word data = 0;
digitalWrite(latch,LOW);
digitalWrite(clock,LOW);
digitalWrite(latch,HIGH); digitalWrite(latch,HIGH);
delayMicroseconds(2); delayMicroseconds(2);
digitalWrite(latch,LOW); digitalWrite(latch,LOW);
delayMicroseconds(1);
data = digitalRead(datin); for (int i = 0; i < 16; i++)
{
for (int i = 1; i <= 15; i ++) { data >>= 1;
data |= (digitalRead(datin) ? 0x8000 : 0);
digitalWrite(clock,HIGH); digitalWrite(clock,HIGH);
delayMicroseconds(2); //delayMicroseconds(1);
data = data << 1;
data = data | digitalRead(datin) ;
delayMicroseconds(2);
digitalWrite(clock,LOW); digitalWrite(clock,LOW);
} }
return data; return data;
} }
@ -76,33 +74,35 @@ void loop() {
*/ */
word controller_data = controllerRead(); // Read 16 bit of data word controller_data = controllerRead(); // Read 16 bit of data
Serial.println(controller_data, BIN);
joySt.buttons = 0; // Reset buttons joySt.buttons = 0; // Reset buttons
joySt.buttons |= (!bitRead(controller_data, 15) << 0); // B - bit 16 - bit 1 on the Joystick joySt.buttons |= (!bitRead(controller_data, 0) << 0); // B - bit 1 - bit 1 on the Joystick
joySt.buttons |= (!bitRead(controller_data, 14) << 1); // Y - bit 15 - bit 2 on the Joystick joySt.buttons |= (!bitRead(controller_data, 1) << 1); // Y - bit 2 - bit 2 on the Joystick
joySt.buttons |= (!bitRead(controller_data, 13) << 6); // Select - bit 14 - bit 7 on the Joystick joySt.buttons |= (!bitRead(controller_data, 2) << 6); // Select - bit 3 - bit 7 on the Joystick
joySt.buttons |= (!bitRead(controller_data, 12) << 7); // Start - bit 13 - bit 8 on the Joystick joySt.buttons |= (!bitRead(controller_data, 3) << 7); // Start - bit 4 - bit 8 on the Joystick
joySt.buttons |= (!bitRead(controller_data, 7) << 2); // A - bit 8 - bit 6 on the Joystick joySt.buttons |= (!bitRead(controller_data, 8) << 2); // A - bit 9 - bit 6 on the Joystick
joySt.buttons |= (!bitRead(controller_data, 6) << 3); // X - bit 7 - bit 5 on the Joystick joySt.buttons |= (!bitRead(controller_data, 9) << 3); // X - bit 10 - bit 5 on the Joystick
joySt.buttons |= (!bitRead(controller_data, 5) << 4); // L - bit 6 - bit 4 on the Joystick joySt.buttons |= (!bitRead(controller_data, 10) << 4); // L - bit 11 - bit 4 on the Joystick
joySt.buttons |= (!bitRead(controller_data, 4) << 5); // R - bit 5 - bit 3 on the Joystick joySt.buttons |= (!bitRead(controller_data, 11) << 5); // R - bit 12 - bit 3 on the Joystick
joySt.xAxis = 127; // Reset x axis joySt.xAxis = 127; // Reset x axis
joySt.yAxis = 127; // Rest y axis joySt.yAxis = 127; // Rest y axis
if (!bitRead(controller_data, 11)) // Up - bit 12 if (!bitRead(controller_data, 4)) // Up - bit 5
joySt.yAxis = 0; joySt.yAxis = 0;
if (!bitRead(controller_data, 10)) // Down - bit 11 if (!bitRead(controller_data, 5)) // Down - bit 6
joySt.yAxis = 255; joySt.yAxis = 255;
if (!bitRead(controller_data, 9)) // Left - bit 10 if (!bitRead(controller_data, 6)) // Left - bit 7
joySt.xAxis = 0; joySt.xAxis = 0;
if (!bitRead(controller_data, 8)) // Right - bit 9 if (!bitRead(controller_data, 7)) // Right - bit 8
joySt.xAxis = 255; joySt.xAxis = 255;
Joystick.setState(&joySt); // Update joystick state Joystick.setState(&joySt); // Update joystick state
} }