-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.cpp
62 lines (53 loc) · 1.85 KB
/
controller.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
#include "arduino.h"
#include "controller.h"
#include "multi_console.h"
void enterMultiConsoleModeIfDetected() {
pinMode(CONTROLLER_CLK_PIN, INPUT_PULLUP);
for (int i = 0; i < 8; i++) {
if (!digitalRead(CONTROLLER_CLK_PIN)) {
MultiConsole multiConsole;
multiConsole.run_peripheral_mode();
}
}
pinMode(CONTROLLER_CLK_PIN, OUTPUT);
digitalWrite(CONTROLLER_CLK_PIN, LOW);
}
Controller::Controller(int ser_pin, int connected_pin, Controller *aux_controller) :
SER_PIN(ser_pin), CONNECTED_PIN(connected_pin), aux(aux_controller), button_state(0)
{}
bool Controller::operator[](Button button) const {
return (button_state >> button) & 1;
}
bool Controller::is_connected() {
if (RGB_CONSOLE && CONNECTED_PIN == CONTROLLER_1_CONN_PIN) {
return true;
}
if (CONTROLLER_AUX_ENABLED && CONNECTED_PIN == CONTROLLER_AUX_CONN_PIN) {
return !digitalRead(CONNECTED_PIN); // it uses INPUT_PULLUP
}
return digitalRead(CONNECTED_PIN);
}
void Controller::update_state(Controller controllers[], int count) {
enterMultiConsoleModeIfDetected();
digitalWrite(CONTROLLER_SHLD_PIN, LOW);
digitalWrite(CONTROLLER_CLK_PIN, HIGH);
digitalWrite(CONTROLLER_CLK_PIN, LOW);
digitalWrite(CONTROLLER_SHLD_PIN, HIGH);
if (CONTROLLER_AUX_ENABLED) {
digitalWrite(CONTROLLER_AUX_SHLD_PIN, LOW);
digitalWrite(CONTROLLER_AUX_CLK_PIN, HIGH);
digitalWrite(CONTROLLER_AUX_CLK_PIN, LOW);
digitalWrite(CONTROLLER_AUX_SHLD_PIN, HIGH);
}
for (int i = 0; i < 8; i++) {
for (int c = 0; c < count; c++) {
Controller &con = controllers[c];
con.button_state = (con.button_state << 1) | (digitalRead(con.SER_PIN) & 1);
if (con.aux && con.aux->is_connected()) {
con.button_state |= digitalRead(con.aux->SER_PIN) & 1;
}
}
digitalWrite(CONTROLLER_CLK_PIN, HIGH);
digitalWrite(CONTROLLER_CLK_PIN, LOW);
}
}