-
Notifications
You must be signed in to change notification settings - Fork 0
/
snes.ino
103 lines (86 loc) · 2.9 KB
/
snes.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "Keyboard.h"
// From https://gamefaqs.gamespot.com/snes/916396-super-nintendo/faqs/5395
// ----------------------------- ---------------------
// | | \
// | (1) (2) (3) (4) | (X) (X) (7) |
// | | /
// ----------------------------- ---------------------
//
//
// Pin Description Color of wire in cable
// === =========== ======================
// 1 +5v White
// 2 Data clock Yellow
// 3 Data latch Orange
// 4 Serial data Red
// 7 Ground Brown
// These are the arduino pins, not the snes pins from above
#define CLOCK_PIN 7
#define DATA_PIN 6
#define LATCH_PIN 5
// Store the buttons in a single 16-bit number
uint16_t previousButtons = 0;
/*
+----------+---------------+
| Button | Bit Position |
+----------+---------------+
| B | 0 |
| Y | 1 |
| Select | 2 |
| Start | 3 |
| Up | 4 |
| Down | 5 |
| Left | 6 |
| Right | 7 |
| A | 8 |
| X | 9 |
| L | 10 |
| R | 11 |
+----------+---------------+
*/
// This is the default button map for the openEmu emulator
int buttonMap[] = {'s', 'a', KEY_RIGHT_SHIFT, KEY_RETURN,
KEY_UP_ARROW, KEY_DOWN_ARROW, KEY_LEFT_ARROW,
KEY_RIGHT_ARROW, 'd', 'w', 'e', 'q' };
void setup() {
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
digitalWrite(CLOCK_PIN, HIGH);
digitalWrite(LATCH_PIN, LOW);
pinMode(DATA_PIN, OUTPUT);
digitalWrite(DATA_PIN, HIGH);
pinMode(DATA_PIN, INPUT);
Keyboard.begin();
}
void loop() {
// Tell the controller to latch the inputs
digitalWrite(LATCH_PIN, HIGH);
delayMicroseconds(12);
digitalWrite(LATCH_PIN, LOW);
delayMicroseconds(6);
uint16_t currentButtons = 0;
for (int i = 0; i < 16; i++) {
digitalWrite(CLOCK_PIN, LOW);
delayMicroseconds(6);
currentButtons |= digitalRead(DATA_PIN) << i;
delayMicroseconds(6);
digitalWrite(CLOCK_PIN, HIGH);
}
// Iterate the keys and send press/release events for changes.
// As per the key diagram above, we only care about the first 12 bits
for (int i = 0; i <= 11; i++) {
auto now = ((currentButtons & (1 << i)) >> i);
auto prev = ((previousButtons & (1 << i)) >> i);
// Press
if (now == 1 && prev == 0) {
Keyboard.press(buttonMap[i]);
} else {
// Release
if (now == 0 && prev == 1) {
Keyboard.release(buttonMap[i]);
}
}
}
previousButtons = currentButtons;
delay(16);
}