Add Library
This commit is contained in:
parent
e365b9dbd9
commit
3c47103b39
318 changed files with 56465 additions and 0 deletions
31
libraries/ButtonDebounce/src/ButtonDebounce.cpp
Normal file
31
libraries/ButtonDebounce/src/ButtonDebounce.cpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
#include "Arduino.h"
|
||||
#include "ButtonDebounce.h"
|
||||
|
||||
ButtonDebounce::ButtonDebounce(int pin, unsigned long delay){
|
||||
pinMode(pin, INPUT_PULLUP);
|
||||
_pin = pin;
|
||||
_delay = delay;
|
||||
_lastDebounceTime = 0;
|
||||
_lastStateBtn = HIGH;
|
||||
}
|
||||
|
||||
bool ButtonDebounce::isTimeToUpdate(){
|
||||
return (millis() - _lastDebounceTime) > _delay;
|
||||
}
|
||||
|
||||
void ButtonDebounce::update(){
|
||||
if(!isTimeToUpdate()) return;
|
||||
_lastDebounceTime = millis();
|
||||
int btnState = digitalRead(_pin);
|
||||
if(btnState == _lastStateBtn) return;
|
||||
_lastStateBtn = btnState;
|
||||
if(this->callback) this->callback(_lastStateBtn);
|
||||
}
|
||||
|
||||
int ButtonDebounce::state(){
|
||||
return _lastStateBtn;
|
||||
}
|
||||
|
||||
void ButtonDebounce::setCallback(BTN_CALLBACK){
|
||||
this->callback = callback;
|
||||
}
|
28
libraries/ButtonDebounce/src/ButtonDebounce.h
Normal file
28
libraries/ButtonDebounce/src/ButtonDebounce.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
ButtonDebounce.h - Library for Button Debounce.
|
||||
Created by Maykon L. Capellari, September 30, 2017.
|
||||
Released into the public domain.
|
||||
*/
|
||||
#ifndef ButtonDebounce_h
|
||||
#define ButtonDebounce_h
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#define BTN_CALLBACK void (*callback)(int)
|
||||
|
||||
class ButtonDebounce{
|
||||
private:
|
||||
int _pin;
|
||||
unsigned long _delay;
|
||||
unsigned long _lastDebounceTime;
|
||||
int _lastStateBtn;
|
||||
BTN_CALLBACK;
|
||||
bool isTimeToUpdate();
|
||||
public:
|
||||
ButtonDebounce(int pin, unsigned long delay);
|
||||
void update();
|
||||
int state();
|
||||
void setCallback(BTN_CALLBACK);
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue