fork/GramFirmware

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

Jonathan HaylettNintendo Switch USB support (#20)38b67d3

v0.1.2
1.4 KiB53 linesraw
1#include "input/NunchukInput.hpp"
2
3#include "gpio.hpp"
4
5#include <Wire.h>
6
7NunchukInput::NunchukInput(TwoWire &wire, int detect_pin, int sda_pin, int scl_pin) {
8    delay(50);
9
10    if (sda_pin < 0 || scl_pin < 0) {
11        _nunchuk = nullptr;
12        return;
13    }
14
15    if (detect_pin > -1) {
16        gpio::init_pin(detect_pin, gpio::GpioMode::GPIO_INPUT_PULLUP);
17        if (gpio::read_digital(detect_pin)) {
18            _nunchuk = nullptr;
19            return;
20        }
21    }
22
23    wire.setSDA(sda_pin);
24    wire.setSCL(scl_pin);
25
26    _nunchuk = new ArduinoNunchuk(wire);
27
28    if (!_nunchuk->init() || !_nunchuk->update()) {
29        // If a Nunchuk isn't connected we don't want i2c to stay enabled because it might interfere
30        // with inputs if i2c pins are also used for buttons. The destructor of ArduinoNunchuk
31        // ends the i2c communication.
32        delete _nunchuk;
33        _nunchuk = nullptr;
34    }
35}
36
37NunchukInput::~NunchukInput() {
38    delete _nunchuk;
39}
40
41InputScanSpeed NunchukInput::ScanSpeed() {
42    return InputScanSpeed::SLOW;
43}
44
45void NunchukInput::UpdateInputs(InputState &inputs) {
46    if (_nunchuk != nullptr && _nunchuk->update()) {
47        inputs.nunchuk_connected = true;
48        inputs.nunchuk_x = _nunchuk->stickX();
49        inputs.nunchuk_y = _nunchuk->stickY();
50        inputs.nunchuk_c = _nunchuk->buttonC();
51        inputs.nunchuk_z = _nunchuk->buttonZ();
52    }
53}