forked from daveruiz/doom-nano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.cpp
100 lines (82 loc) · 1.87 KB
/
input.cpp
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
#include <Arduino.h>
#include "input.h"
#include "constants.h"
#ifdef USE_INPUT_PULLUP
#define INPUT_MODE INPUT_PULLUP
#define INPUT_STATE LOW
#else
#define INPUT_MODE INPUT
#define INPUT_STATE HIGH
#endif
#ifdef SNES_CONTROLLER
uint16_t buttons = 0;
void input_setup() {
// Set DATA_CLOCK normally HIGH
pinMode(DATA_CLOCK, OUTPUT);
digitalWrite(DATA_CLOCK, HIGH);
// Set DATA_LATCH normally LOW
pinMode(DATA_LATCH, OUTPUT);
digitalWrite(DATA_LATCH, LOW);
// Set DATA_SERIAL normally HIGH
pinMode(DATA_SERIAL, OUTPUT);
digitalWrite(DATA_SERIAL, HIGH);
pinMode(DATA_SERIAL, INPUT);
}
void getControllerData(void){
// Latch for 12us
digitalWrite(DATA_LATCH, HIGH);
delayMicroseconds(12);
digitalWrite(DATA_LATCH, LOW);
delayMicroseconds(6);
buttons = 0;
// Retrieve button presses from shift register by pulling the clock high for 6us
for(uint8_t i = 0; i < 16; ++i){
digitalWrite(DATA_CLOCK, LOW);
delayMicroseconds(6);
buttons |= !digitalRead(DATA_SERIAL) << i;
digitalWrite(DATA_CLOCK, HIGH);
delayMicroseconds(6);
}
}
bool input_left() {
return buttons & LEFT;
};
bool input_right() {
return buttons & RIGHT;
};
bool input_up() {
return buttons & UP;
};
bool input_down() {
return buttons & DOWN;
};
bool input_fire() {
return buttons & Y;
};
bool input_start() {
return buttons & START;
}
#else
void input_setup() {
pinMode(K_LEFT, INPUT_MODE);
pinMode(K_RIGHT, INPUT_MODE);
pinMode(K_UP, INPUT_MODE);
pinMode(K_DOWN, INPUT_MODE);
pinMode(K_FIRE, INPUT_MODE);
}
bool input_left() {
return digitalRead(K_LEFT) == INPUT_STATE;
};
bool input_right() {
return digitalRead(K_RIGHT) == INPUT_STATE;
};
bool input_up() {
return digitalRead(K_UP) == INPUT_STATE;
};
bool input_down() {
return digitalRead(K_DOWN) == INPUT_STATE;
};
bool input_fire() {
return digitalRead(K_FIRE) == INPUT_STATE;
};
#endif