Download the file
  1. #include "digital_input.h"
  2.  
  3. void DigitalInput::read_state() {
  4. old_state <<= 1;
  5. old_state |= !digitalRead(pin) & 0x01;
  6. }
  7. bool DigitalInput::canRun(uint32_t now) {
  8. if (!handler) return false;
  9. read_state();
  10. return states[old_state & 0x03] != 0;
  11. }
  12.  
  13. void DigitalInput::run(uint32_t now) {
  14. handler->on_change(states[old_state & 0x03]);
  15. }
  16.  
  17. void DigitalInput::set_handler(DigitalInputObserver * observer) {
  18. read_state();
  19. handler = observer;
  20. }
  21.  
  22. const int8_t DigitalInput::states[] = {0, 1, -1 ,0};
  23. // vim:ft=arduino
  24.