Skip to content

Commit 9f3c003

Browse files
authored
Merge pull request #658 from adafruit/TheKitty-patch-20
Create teensySNES_test1.ino
2 parents aeceba5 + 2f0c96e commit 9f3c003

1 file changed

Lines changed: 141 additions & 0 deletions

File tree

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#define REPEATRATE 100 // milliseconds
2+
3+
const int pinBtnUp = 0;
4+
const int pinBtnRight = 1;
5+
const int pinBtnDown = 2;
6+
const int pinBtnLeft = 3;
7+
8+
const int pinBtnSelect = 4;
9+
const int pinBtnStart = 5;
10+
11+
const int pinBtnB = 7;
12+
const int pinBtnA = 8;
13+
const int pinBtnY = 10;
14+
const int pinBtnX = 9;
15+
16+
const int pinBtnTrigLeft = 6;
17+
const int pinBtnTrigRight = 23;
18+
19+
const int pinLEDOutput = 11;
20+
21+
//Variables for the states of the SNES buttons
22+
byte buttons[] = { pinBtnUp, pinBtnRight, pinBtnDown, pinBtnLeft, pinBtnSelect, pinBtnStart,
23+
pinBtnB, pinBtnA, pinBtnY, pinBtnX, pinBtnTrigLeft, pinBtnTrigRight
24+
};
25+
short keys[] = {KEY_U, KEY_R, KEY_D, KEY_L, KEY_ENTER, KEY_TAB, KEY_B, KEY_A, KEY_Y, KEY_X, KEY_P, KEY_Q};
26+
27+
#define NUMBUTTONS sizeof(buttons)
28+
29+
typedef void KeyFunction_t(uint8_t c);
30+
31+
KeyFunction_t* buttonActive[NUMBUTTONS];
32+
KeyFunction_t* keyList[] = {myset_key6, myset_key5, myset_key4, myset_key3, myset_key2, myset_key1};
33+
int keySlot = sizeof(keyList) / sizeof(KeyFunction_t*);
34+
35+
void setup()
36+
{
37+
//Setup the pin modes.
38+
pinMode( pinLEDOutput, OUTPUT );
39+
40+
//Special for the Teensy is the INPUT_PULLUP
41+
//It enables a pullup resitor on the pin.
42+
for (byte i=0; i< NUMBUTTONS; i++) {
43+
pinMode(buttons[i], INPUT_PULLUP);
44+
}
45+
46+
//Uncomment this line to debug the acceleromter values:
47+
// Serial.begin();
48+
49+
for (int i=0; i < NUMBUTTONS; i++) {
50+
buttonActive[i] = 0;
51+
}
52+
53+
}
54+
55+
void loop()
56+
{
57+
// //debugging the start button...
58+
digitalWrite ( pinLEDOutput, digitalRead(pinBtnStart));
59+
60+
//Progess the SNES controller buttons to send keystrokes.
61+
fcnProcessButtons();
62+
63+
}
64+
65+
//Function to process the buttons from the SNES controller
66+
void fcnProcessButtons()
67+
{
68+
bool keysPressed = false;
69+
bool keysReleased = false;
70+
71+
// run through all the buttons
72+
for (byte i = 0; i < NUMBUTTONS; i++) {
73+
74+
// are any of them pressed?
75+
if (! digitalRead(buttons[i]))
76+
{ //this button is pressed
77+
keysPressed = true;
78+
if (!buttonActive[i]) //was it pressed before?
79+
activateButton(i); //no - activate the keypress
80+
}
81+
else
82+
{ //this button is not pressed
83+
if (buttonActive[i]) { //was it pressed before?
84+
releaseButton(i); //yes - release the keypress
85+
keysReleased = true;
86+
}
87+
}
88+
}
89+
90+
if (keysPressed || keysReleased)
91+
Keyboard.send_now(); //update all the keypresses
92+
93+
}
94+
95+
void activateButton(byte index)
96+
{
97+
if (keySlot) //any key slots left?
98+
{
99+
keySlot--; //Push the keySlot stack
100+
buttonActive[index] = keyList[keySlot]; //Associate the keySlot function pointer with the button
101+
(*keyList[keySlot])(keys[index]); //Call the key slot function to set the key value
102+
}
103+
}
104+
105+
void releaseButton(byte index)
106+
{
107+
keyList[keySlot] = buttonActive[index]; //retrieve the keySlot function pointer
108+
buttonActive[index] = 0; //mark the button as no longer pressed
109+
(*keyList[keySlot])(0); //release the key slot
110+
keySlot++; //pop the keySlot stack
111+
}
112+
113+
void myset_key1(uint8_t c)
114+
{
115+
Keyboard.set_key1(c);
116+
}
117+
118+
void myset_key2(uint8_t c)
119+
{
120+
Keyboard.set_key2(c);
121+
}
122+
123+
void myset_key3(uint8_t c)
124+
{
125+
Keyboard.set_key3(c);
126+
}
127+
128+
void myset_key4(uint8_t c)
129+
{
130+
Keyboard.set_key4(c);
131+
}
132+
133+
void myset_key5(uint8_t c)
134+
{
135+
Keyboard.set_key5(c);
136+
}
137+
138+
void myset_key6(uint8_t c)
139+
{
140+
Keyboard.set_key6(c);
141+
}

0 commit comments

Comments
 (0)