Download the file
  1. #include "PushButton.h"
  2.  
  3. int8_t PushButton::states[] = {0, 1, -1 ,0};
  4.  
  5. PushButton::PushButton(uint8_t pin_) {
  6. pin = pin_;
  7. pinMode(pin, INPUT);
  8. digitalWrite(pin, HIGH);
  9. }
  10.  
  11. uint8_t PushButton::getState(uint8_t debounce) {
  12. if (millis() > debounce * 10 + lastMillis) {
  13. lastMillis = millis();
  14. return !digitalRead(pin);
  15. }
  16. return 0;
  17. }
  18.  
  19. int8_t PushButton::getEdge() {
  20. old_state <<= 1;
  21. old_state |= !digitalRead(pin) & 0x01;
  22. return PushButton::states[old_state & 0x03];
  23. }
  24.  
  25. uint8_t PushButton::getPressed() {
  26. return getEdge() == 1;
  27. }
  28.  
  29. uint8_t PushButton::getReleased() {
  30. return getEdge() == -1;
  31. }
  32. // vim:ft=arduino
  33.