#include "Button.h" #include "Joystick.h" #include #include using namespace admux; Button::Button(uint8_t vbutton) { this->vbutton = vbutton; } void Button::ReleaseButtons(Joystick* js) { js->ReleaseButton(vbutton); } SwitchButton::SwitchButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux) : Button(vbutton) { this->mux = mux; uint8_t mode = INPUT; if (pullup) { this->inverted = true; mode = INPUT_PULLUP; } if (mux == NULL) this->bouncer.attach(pin, mode); else { this->bouncer.attach(mux->signalPin().pin, mode); this->channel_id = pin; } } bool SwitchButton::BouncerUpdate() { if (mux != NULL) { mux->channel(channel_id); } return bouncer.update(); } bool SwitchButton::On() { bool state = bouncer.rose(); if (inverted) state = bouncer.fell(); return state; } PassthruButton::PassthruButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux) : SwitchButton(pin, vbutton, pullup, mux) { this->type = BUTTON_PASSTHRU; } bool PassthruButton::Update(Joystick* js) { if (!BouncerUpdate()) return false; if (On()) js->PressButton(vbutton); else js->ReleaseButton(vbutton); return true; } LatchedButton::LatchedButton(uint8_t pin, uint8_t vbutton, bool pullup, Mux* mux) : SwitchButton(pin, vbutton, pullup, mux) { this->type = BUTTON_LATCHED_MOMENTARY; this->pressed = false; } bool LatchedButton::Update(Joystick* js) { if (!BouncerUpdate()) return false; if (On()) { if (!pressed) { js->PressButton(vbutton); pressed = true; } else { js->ReleaseButton(vbutton); pressed = false; } } return true; } PulsedButton::PulsedButton(uint8_t pin, uint8_t vbutton, bool double_action, bool split, bool pullup, Mux* mux) : SwitchButton(pin, vbutton, pullup, mux) { if (double_action) { if (split) { this->type = BUTTON_PULSED_DOUBLE_ACTION_SPLIT; this->vbutton2 = vbutton + 1; } else { this->type = BUTTON_PULSED_DOUBLE_ACTION; } } else { this->type = BUTTON_PULSED; } } bool PulsedButton::Update(Joystick* js) { if (!BouncerUpdate()) return false; switch(type) { case BUTTON_PULSED: if (On()) js->PressButton(vbutton); break; case BUTTON_PULSED_DOUBLE_ACTION: js->PressButton(vbutton); break; case BUTTON_PULSED_DOUBLE_ACTION_SPLIT: if (On()) js->PressButton(vbutton); else js->PressButton(vbutton2); break; } return true; } void PulsedButton::ReleaseButtons(Joystick* js) { Button::ReleaseButtons(js); if (type == BUTTON_PULSED_DOUBLE_ACTION_SPLIT) js->ReleaseButton(vbutton2); } EncoderButton::EncoderButton(uint8_t pin1, uint8_t pin2, uint8_t vbutton) : Button(vbutton) { this->type = ENCODER_PULSED_SPLIT; this->vbutton2 = vbutton + 1; this->encoder = new Encoder(pin1, pin2); this->last_value = encoder->read(); } bool EncoderButton::Update(Joystick* js) { bool changed = false; long new_value = encoder->read(); if (new_value > last_value) { js->PressButton(vbutton); changed = true; } else if (new_value < last_value) { js->PressButton(vbutton2); changed = true; } last_value = new_value; return changed; } void EncoderButton::ReleaseButtons(Joystick* js) { Button::ReleaseButtons(js); js->ReleaseButton(vbutton2); }