void PushButton::check() { uint8_t newState; // Get the new state from the pin newState = digitalRead(_pin); // Button state changed? if(newState != _lastState) _lastDebounceTime = millis(); // Is the debounce delay reached and is it a new state at all? if ((millis() - _lastDebounceTime) > _debounceDelay && _state != newState) { // The new state exceeded the debounce delay, take it as the actual statue _state = newState; // 0 == active, generate a press event if(_state == 0 && _pressed != NULL) _pressed(); if(_state == 1) { if((millis() - _pressTime) < _longPressDelay && _released != NULL) _released(); else if (_longreleased != NULL) _longreleased(); } _pressTime = millis(); } // Save the state... _lastState = newState; }
// show the screen bool Display::show () { if (_quit) { if (_died) _dump(); return false; } if (!_window) return true; // poll events SDL_Event e; while (SDL_PollEvent(&e)) switch (e.type) { case SDL_QUIT: quit(); return false; case SDL_KEYDOWN: _pressed(e.key.keysym.sym); break; case SDL_KEYUP: _released(e.key.keysym.sym); break; default: break; } // get delta time tick_t now = _now(); tick_t diff = now - _time; _time = now; // eliminate unusual framerate errors (dragging, etc) if (diff > (tick_s / MinFPS)) diff = tick_s / MinFPS; else if (diff == 0) diff = tick_s / MaxFPS; if (_view != nullptr) { auto dt = diff / double(tick_s); _view->update(this, dt); _view->draw(this); } else color(.5f, .8f, 1.f).glApplyClear(); SDL_GL_SwapWindow(_window); /* const double sleep_s = 1.0 / 100.0; const time_t sleep_t = time_t(tick_s * sleep_s); auto n = _now(); while ((_now() - n) < sleep_t) ; */ SDL_Delay(3); return true; }