Download the file
  1. #ifndef DIGITAL_INPUT_H
  2. #define DIGITAL_INPUT_H
  3.  
  4. #include "tasks.h"
  5. #include "hsphoto.h"
  6.  
  7. class DigitalInputObserver {
  8. public:
  9. virtual void on_rise() {}
  10. virtual void on_fall() {}
  11. virtual void on_change(int8_t state) {
  12. if (state == 1) on_fall();
  13. else if (state == -1) on_rise();
  14. }
  15. };
  16.  
  17. class DigitalInput: public Task {
  18. public:
  19. DigitalInput(uint8_t pin, bool pullup = false):
  20. handler(NULL),
  21. pin(pin) {
  22. pinMode(pin, INPUT);
  23. if (pullup)
  24. digitalWrite(pin, HIGH);
  25. else
  26. digitalWrite(pin, LOW);
  27. }
  28. bool canRun(uint32_t now);
  29. void run(uint32_t now);
  30. void set_handler(DigitalInputObserver * observer);
  31. inline uint8_t get_state() const { return digitalRead(pin); }
  32.  
  33. protected:
  34. void read_state();
  35. DigitalInputObserver * handler;
  36. const uint8_t pin;
  37. uint8_t old_state;
  38.  
  39. static const int8_t states[];
  40. };
  41. #endif
  42. // vim:ft=arduino
  43.