diff --git a/Joystick.cpp b/Joystick.cpp index e53b715..c518999 100644 --- a/Joystick.cpp +++ b/Joystick.cpp @@ -3,11 +3,8 @@ Joystick::Joystick(bool debug) { _debug = debug; -} - -void Joystick::Init() { - Serial.begin(115200); - delay(100); + _num_buttons = 0; + _num_axes = 0; for (uint8_t i=0; i < JOYSTICK_NUM_AXES; i++) { _joyReport.axis[i] = 0; @@ -15,20 +12,44 @@ void Joystick::Init() { for (uint8_t i=0; i < JOYSTICK_NUM_BYTES; i++) { _joyReport.button[i] = 0; } - +} + +void Joystick::Init() { + Serial.begin(115200); + delay(100); + if (_debug) Serial.println("DEBUG: Joystick library initialized."); } void Joystick::AddButton(uint8_t pin, ButtonType type) { - // stub + _buttons[_num_buttons].pin = pin; + _buttons[_num_buttons].type = type; + _buttons[_num_buttons].last_state = digitalRead(pin); + _num_buttons++; } void Joystick::AddAxis(uint8_t pin) { - // stub + _axes[_num_axes] = pin; + _num_axes++; } void Joystick::Update() { - // stub + JoyReport oldReport = _joyReport; + + for (int i = 0; i < _num_buttons; i++) { + uint8_t value = digitalRead(_buttons[i].pin); + // TODO: handle button according to ButtonType + } + + for (int i = 0; i < _num_axes; i++) { + uint16_t value = analogRead(_axes[i]); + // TODO: convert raw analog value to something sensible for + // HID Analog data. + } + + if (_joyReport != oldReport) { + Serial.write(_joyReport); + } } void Joystick::SetAxis(uint8_t axis, int16_t value) { diff --git a/Joystick.h b/Joystick.h index 85d1c9b..7f7ab07 100644 --- a/Joystick.h +++ b/Joystick.h @@ -44,9 +44,15 @@ class Joystick { private: struct { - + uint8_t pin; + ButtonType type; + uint8_t last_state; } _buttons[JOYSTICK_NUM_BUTTONS]; uint8_t _num_buttons; + + uint8_t _axes[JOYSTICK_NUM_AXES]; + uint8_t _num_axes; + JoyReport _joyReport; bool _debug; };