-
-
Save kindfulkirby/4d4d5cb8f9ea41df898b8cad14a39e71 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() { | |
| DigiJoystick.setValues(buf); | |
| DigiJoystick.delay(1); | |
| buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = 0x00; | |
| DigiJoystick.setValues(buf); | |
| DigiJoystick.delay(1); | |
| buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = 0x80; | |
| 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); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I added some code to send initial data for all axes, otherwise my Linux PC didn't read them correctly.