-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update code to support extra buttons
- Loading branch information
Showing
7 changed files
with
179 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#ifndef __SHBUTTON_H__ | ||
#define __SHBUTTON_H__ | ||
|
||
#include <Arduino.h> | ||
#include "SHFastIO.h" | ||
|
||
typedef void(*SHButtonChanged) (int, byte); | ||
|
||
class SHButton { | ||
private: | ||
|
||
FastDigitalPin button; | ||
uint8_t buttonState; | ||
int buttonLastState = -1; | ||
bool vccToPinWiring; | ||
unsigned long buttonLastStateChanged; | ||
byte id; | ||
SHButtonChanged shButtonChangedCallback; | ||
int logicMode; | ||
|
||
int getState(int value) { | ||
int res = 0; | ||
if (!vccToPinWiring) { | ||
res = value == HIGH ? 0 : 1; | ||
} | ||
else { | ||
res = value == HIGH ? 1 : 0; | ||
} | ||
if (logicMode == 1) { | ||
res = res ? 0 : 1; | ||
} | ||
return res; | ||
} | ||
|
||
public: | ||
|
||
void begin(byte buttonid, uint8_t buttonPin, SHButtonChanged changedcallback, bool vccToPinWiring, int logicMode) { | ||
button.begin(buttonPin); | ||
this->vccToPinWiring = vccToPinWiring; | ||
this->logicMode = logicMode; | ||
|
||
if (buttonPin > 0) { | ||
if (!vccToPinWiring) { | ||
pinMode(buttonPin, INPUT_PULLUP); | ||
} | ||
else { | ||
pinMode(buttonPin, INPUT); | ||
} | ||
} | ||
id = buttonid; | ||
buttonLastState = getState(button.digitalReadPin()); | ||
shButtonChangedCallback = changedcallback; | ||
|
||
if (buttonLastState) | ||
shButtonChangedCallback(id, buttonLastState); | ||
} | ||
|
||
uint8_t getPressed() { | ||
return !buttonLastState; | ||
} | ||
|
||
void read() { | ||
buttonState = getState(button.digitalReadPin()); | ||
if (buttonState != buttonLastState && buttonLastStateChanged - millis() > 50) { | ||
shButtonChangedCallback(id, buttonState); | ||
buttonLastState = buttonState; | ||
buttonLastStateChanged = millis(); | ||
} | ||
} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef __SHDEBOUNCER_H__ | ||
#define __SHDEBOUNCER_H__ | ||
|
||
#include <Arduino.h> | ||
|
||
class SHDebouncer { | ||
private: | ||
unsigned long lastPoll = 0; | ||
unsigned long debounceDelay; | ||
public: | ||
SHDebouncer(byte delay) { | ||
debounceDelay = (unsigned long)delay; | ||
} | ||
|
||
SHDebouncer() { | ||
} | ||
|
||
void begin(byte delay) { | ||
debounceDelay = delay; | ||
} | ||
|
||
bool Debounce() { | ||
if (millis() - lastPoll > debounceDelay) { | ||
lastPoll = millis(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef __SHFASTIO_H__ | ||
#define __SHFASTIO_H__ | ||
#include <Arduino.h> | ||
|
||
class FastDigitalPin { | ||
private: | ||
uint8_t pin = -1; | ||
bool pinIsValid = false; | ||
public: | ||
|
||
bool isValid() { | ||
return true; | ||
} | ||
|
||
void begin(int pin) { | ||
if (pin < 0) { | ||
pinIsValid = false; | ||
} | ||
else { | ||
pinIsValid = true; | ||
this->pin = pin; | ||
} | ||
} | ||
|
||
int digitalReadPin() | ||
{ | ||
return digitalRead(this->pin); | ||
} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters