fork/GramFirmware

git clone https://git.julia.blue/fork/GramFirmware

CarVacrebase to configurator818d962

gram
1.4 KiB50 linesraw
1#include "comms/console_detection.hpp"
2
3#include "core/pinout.hpp"
4
5#include <GamecubeConsole.hpp>
6#include <N64Console.hpp>
7#include <config.pb.h>
8#include <hardware/structs/usb.h>
9
10uint latch_pulses = 0;
11uint clock_pulses = 0;
12
13void latch_irq_handler() {
14    latch_pulses++;
15}
16
17void clock_irq_handler() {
18    clock_pulses++;
19}
20
21CommunicationBackendId detect_console(const Pinout &pinout) {
22    if (pinout.nes_latch > -1 && pinout.nes_clock > -1) {
23        attachInterrupt(pinout.nes_latch, &latch_irq_handler, PinStatus::RISING);
24        attachInterrupt(pinout.nes_clock, &clock_irq_handler, PinStatus::FALLING);
25    }
26
27    delay(500);
28    bool usb_connected = usb_hw->sie_status & USB_SIE_STATUS_CONNECTED_BITS;
29
30    if (pinout.nes_latch > -1 && pinout.nes_clock > -1) {
31        detachInterrupt(pinout.nes_latch);
32        detachInterrupt(pinout.nes_clock);
33    }
34
35    CommunicationBackendId result = COMMS_BACKEND_UNSPECIFIED;
36
37    if (usb_connected) {
38        result = COMMS_BACKEND_XINPUT;
39    } else if (latch_pulses && (clock_pulses / latch_pulses) > 8) {
40        result = COMMS_BACKEND_SNES;
41    } else if (latch_pulses && (clock_pulses / latch_pulses) > 1) {
42        result = COMMS_BACKEND_NES;
43    } else if (GamecubeConsole(pinout.joybus_data).Detect()) {
44        result = COMMS_BACKEND_GAMECUBE;
45    } else if (N64Console(pinout.joybus_data).Detect()) {
46        result = COMMS_BACKEND_N64;
47    }
48
49    return result;
50}