Last active
October 1, 2017 19:27
-
-
Save netalkGB/792351e70b29f10ff4a5c0e91503c775 to your computer and use it in GitHub Desktop.
Digispark SNES controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "DigiJoystick.h" | |
| #define DAT 0 | |
| #define PS 1 | |
| #define CLK 2 | |
| #define PULSE_TIME 3 | |
| char buf[8] = { | |
| 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, | |
| 0x00, 0x00 | |
| }; | |
| void pulseClock(byte pin) { | |
| digitalWrite(pin, HIGH); | |
| delayMicroseconds(PULSE_TIME); | |
| digitalWrite(pin, LOW); | |
| delayMicroseconds(PULSE_TIME); | |
| } | |
| void setup() { | |
| pinMode(DAT, INPUT); | |
| pinMode(PS, OUTPUT); | |
| pinMode(CLK, OUTPUT); | |
| digitalWrite(PS, LOW); | |
| digitalWrite(CLK, LOW); | |
| } | |
| void loop() { | |
| buf[0] = buf[1] = 0x80; | |
| buf[6] = 0x00; | |
| pulseClock(CLK); | |
| pulseClock(PS); | |
| if (!digitalRead(DAT)) buf[6] |= 0x04; | |
| pulseClock(CLK); | |
| if (!digitalRead(DAT)) buf[6] |= 0x08; | |
| pulseClock(CLK); | |
| if (!digitalRead(DAT)) buf[6] |= 0x80; | |
| pulseClock(CLK); | |
| if (!digitalRead(DAT)) buf[6] |= 0x40; | |
| pulseClock(CLK); | |
| if (!digitalRead(DAT)) buf[1] -= 128; | |
| pulseClock(CLK); | |
| if (!digitalRead(DAT)) buf[1] += 127; | |
| pulseClock(CLK); | |
| if (!digitalRead(DAT)) buf[0] -= 128; | |
| pulseClock(CLK); | |
| if (!digitalRead(DAT)) buf[0] += 127; | |
| pulseClock(CLK); | |
| if (!digitalRead(DAT)) buf[6] |= 0x02; | |
| pulseClock(CLK); | |
| if (!digitalRead(DAT)) buf[6] |= 0x01; | |
| pulseClock(CLK); | |
| if (!digitalRead(DAT)) buf[6] |= 0x10; | |
| pulseClock(CLK); | |
| if (!digitalRead(DAT)) buf[6] |= 0x20; | |
| DigiJoystick.setValues(buf); | |
| DigiJoystick.delay(1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment