pushbutton.c 819 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * button.c
  3. *
  4. * Created on: 28.04.2016
  5. * Author: terfloth
  6. */
  7. #include "Arduino.h"
  8. #include "pushbutton.h"
  9. void read_pushbutton(pushbutton_t *button){
  10. int pin_value = digitalRead(button->pin);
  11. if (pin_value != button->debounce_state) {
  12. button->last_debounce_time = millis();
  13. }
  14. if ((millis() - button->last_debounce_time) > button->debounce_delay) {
  15. if (pin_value != button->state) {
  16. button->state = pin_value;
  17. button->callback(button);
  18. }
  19. }
  20. button->debounce_state = pin_value;
  21. }
  22. void setup_pushbutton(pushbutton_t *button, int pin, void (*callback)(pushbutton_t*)) {
  23. button->pin = pin;
  24. button->debounce_delay = 50;
  25. button->state = digitalRead(button->pin);
  26. button->debounce_state = LOW;
  27. button->last_debounce_time = 0;
  28. button->callback = callback;
  29. pinMode(pin, INPUT);
  30. }