fork/GramFirmware
git clone https://git.julia.blue/fork/GramFirmware
38b67d3
codespace-scaling-space-guide-56rpxqvxpg2v57v
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 ) { 8delay (50 ); 9 10if (sda_pin < 0 || scl_pin < 0 ) { 11_nunchuk = nullptr ; 12return ; 13 } 14 15if (detect_pin > -1 ) { 16 gpio::init_pin (detect_pin , gpio::GpioMode ::GPIO_INPUT_PULLUP ); 17if (gpio::read_digital (detect_pin )) { 18_nunchuk = nullptr ; 19return ; 20 } 21 } 22 23wire .setSDA (sda_pin ); 24wire .setSCL (scl_pin ); 25 26_nunchuk = new ArduinoNunchuk (wire ); 27 28if (!_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. 32delete _nunchuk ; 33_nunchuk = nullptr ; 34 } 35} 36 37NunchukInput ::~NunchukInput () { 38delete _nunchuk ; 39} 40 41InputScanSpeed NunchukInput ::ScanSpeed () { 42return InputScanSpeed ::SLOW ; 43} 44 45void NunchukInput ::UpdateInputs (InputState & inputs ) { 46if (_nunchuk != nullptr && _nunchuk -> update ()) { 47inputs .nunchuk_connected = true; 48inputs .nunchuk_x = _nunchuk -> stickX (); 49inputs .nunchuk_y = _nunchuk -> stickY (); 50inputs .nunchuk_c = _nunchuk -> buttonC (); 51inputs .nunchuk_z = _nunchuk -> buttonZ (); 52 } 53}