Skip to content
Snippets Groups Projects
Button.cpp 2.65 KiB
Newer Older
Button::Button(int pin, int id = 99) : _pin(pin), _id(id) {
  pinMode(_pin, INPUT_PULLUP);
  _state = _read();
  _previousTimer = millis();
}

Matthew's avatar
Matthew committed
void Button::check() {
        unsigned long timer = millis();
        unsigned long deltaTime = timer - _previousTimer;
        _state = _read();
        if (_inputFlag == true) {
                _inputFlag = false;
                // Button pressed
                if (_state == LOW
                    && _pressedFlag == false) {
                        _pressedFlag = true;
                        _previousTimer = timer;
                        _cb(this, kEventPressed, _state);
                        return;
                        // Button clicked
                } else if (_state == HIGH
                           && deltaTime < _clickInterval
                           && _holdFlag == false) {
                        _pressedFlag = false;
                        _previousTimer = timer;
                        _cb(this, kEventClicked, _state);
                        return;
                        // Button released
                } else if (_state == HIGH) {
                        _pressedFlag = false;
                        _holdFlag = false;
                        _previousTimer = timer;
                        _cb(this, kEventReleased, _state);
                        return;
                }
        // Button held
        if (_state == LOW
            && deltaTime > _holdInterval
            && _holdFlag == false) {
                _holdFlag = true;
                _previousTimer = timer;
                _cb(this, kEventHeld, _state);
                return;
                // Button tick
        } else if (_state == LOW
                   && deltaTime > _repeatInterval
                   && _holdFlag == true) {
                _previousTimer = timer;
                _cb(this, kEventTick, _state);
                return;
        }
};
Matthew's avatar
Matthew committed
int Button::getHoldInterval() {
        return _holdInterval;
};
Matthew's avatar
Matthew committed
void Button::_setHoldInterval(int x) {
        _holdInterval = x;
};
Matthew's avatar
Matthew committed
int Button::getId() {
        return _id;
};
Matthew's avatar
Matthew committed
int Button::getRepeatInterval() {
        return _repeatInterval;
};
void Button::setEventHandler(void (*function)(Button*, uint8_t, bool)) {
        _cb = function;
};
Matthew's avatar
Matthew committed
void Button::_setRepeatInterval(int x) {
        _repeatInterval = x;
};
Matthew's avatar
Matthew committed
bool Button::getState() {
        return _state;
};
Matthew's avatar
Matthew committed
bool Button::_read() {
        return digitalRead(_pin);
};
Matthew's avatar
Matthew committed
int Button::getClickInterval() {
        return _clickInterval;
};
Matthew's avatar
Matthew committed
void Button::_setClickInterval(int x) {
        _clickInterval = x;
};
void Button::initInterrupts(void (*function)()) {
        attachInterrupt(_pin, function, CHANGE);
};