-
Notifications
You must be signed in to change notification settings - Fork 0
/
smsepad.c
133 lines (111 loc) · 2.95 KB
/
smsepad.c
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* This code is intended to run on a Raspberry Pi 3 Model B,
* with the WiringPi Library.
*/
#include <assert.h>
#include <stdio.h>
#include <wiringPi.h>
/* Macros */
/*
* _____________________
* | 1 | 3 | 5 | 7 | 9 |
* --------------------- (SMS DB9 to 10-pin cable connector)
* | 2 | 4 | 6 | 8 | X |
* ---------------------
*
* 1- up
* 2- down
* 3- left
* 4- right
* 5- x+y+z
* 6- button 1
* 7- start
* 8- gnd
* 9- button 2
*/
#define PAD_UP 0
#define PAD_DOWN 1
#define PAD_LEFT 2
#define PAD_RIGHT 3
#define PAD_XYZ 4
#define PAD_BUTTON1 5
#define PAD_START 6
#define PAD_GND 7
#define PAD_BUTTON2 8
/* NOTE: wiringPi pin numbering */
#define PAD_UP_PIN 21 /* GPIO.5 */
#define PAD_DOWN_PIN 26 /* GPIO.12 */
#define PAD_LEFT_PIN 22 /* GPIO.6 */
#define PAD_RIGHT_PIN 27 /* GPIO.16 */
#define PAD_XYZ_PIN 23 /* GPIO.13 */
#define PAD_BUTTON1_PIN 28 /* GPIO.20 */
#define PAD_START_PIN 24 /* GPIO.19 */
#define PAD_BUTTON2_PIN 25 /* GPIO.26 */
/* Data */
static char g_keyval[9];
static char g_keymap[10] = "wsadqke0l";
/* Functions */
static inline int pad_pressed(int key)
{
assert(key < 9);
return !g_keyval[key];
}
static void pad_setup(void)
{
pinMode(PAD_UP_PIN, INPUT);
pinMode(PAD_DOWN_PIN, INPUT);
pinMode(PAD_LEFT_PIN, INPUT);
pinMode(PAD_RIGHT_PIN, INPUT);
pinMode(PAD_XYZ_PIN, INPUT);
pinMode(PAD_BUTTON1_PIN, INPUT);
pinMode(PAD_START_PIN, INPUT);
pinMode(PAD_BUTTON2_PIN, INPUT);
pullUpDnControl(PAD_UP_PIN, PUD_UP);
pullUpDnControl(PAD_DOWN_PIN, PUD_UP);
pullUpDnControl(PAD_LEFT_PIN, PUD_UP);
pullUpDnControl(PAD_RIGHT_PIN, PUD_UP);
pullUpDnControl(PAD_XYZ_PIN, PUD_UP);
pullUpDnControl(PAD_BUTTON1_PIN, PUD_UP);
pullUpDnControl(PAD_START_PIN, PUD_UP);
pullUpDnControl(PAD_BUTTON2_PIN, PUD_UP);
/* Wait for stabilization of signals */
delayMicroseconds(10);
}
static void pad_read(void)
{
g_keyval[PAD_UP] = digitalRead(PAD_UP_PIN);
g_keyval[PAD_DOWN] = digitalRead(PAD_DOWN_PIN);
g_keyval[PAD_LEFT] = digitalRead(PAD_LEFT_PIN);
g_keyval[PAD_RIGHT] = digitalRead(PAD_RIGHT_PIN);
g_keyval[PAD_XYZ] = digitalRead(PAD_XYZ_PIN);
g_keyval[PAD_BUTTON1] = digitalRead(PAD_BUTTON1_PIN);
g_keyval[PAD_START] = digitalRead(PAD_START_PIN);
g_keyval[PAD_BUTTON2] = digitalRead(PAD_BUTTON2_PIN);
}
static void pad_print(void)
{
int i;
printf("pad:");
for (i = 0; i < 9; i++)
if (pad_pressed(i))
printf(" %c", g_keymap[i]);
printf("\n");
}
int main()
{
/* Init wiringPi */
if (wiringPiSetup() == -1) {
printf("failed to initialize wiringPi\n");
return 1;
}
pad_setup();
/* Specify gamepad's GND pin (8) as never pressed */
g_keyval[PAD_GND] = 1;
printf("SMSE Pad on Pi started\n");
while (1) {
pad_read();
pad_print();
delay(500); /* ms */
}
return 0;
}