Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
delta time passed to the task wasn't task specific, fixed if delta time is large compared to task update time, it could end rolling over, fixed by checking and just skipping to the next frame
- Loading branch information
Showing
4 changed files
with
152 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// button should be attached to the pin, and when pressed, it should connect the pin to ground | ||
|
||
enum ButtonState | ||
{ | ||
ButtonState_Pressed, | ||
ButtonState_Released | ||
}; | ||
|
||
class ButtonTask : public Task | ||
{ | ||
public: | ||
typedef void(*action)(ButtonState state); | ||
|
||
ButtonTask(action function, uint8_t pin) : | ||
Task(3), // check every three millisecond, 1-10 ms should be ok | ||
_buttonPin(pin), | ||
_lastValue(HIGH), | ||
_state(ButtonState_Released), | ||
_callback(function) | ||
{ }; | ||
|
||
private: | ||
static const uint8_t _debouceMs = 50; // (30-100) are good values | ||
const uint8_t _buttonPin; | ||
const action _callback; | ||
uint8_t _lastValue; | ||
uint8_t _debouceTimer; | ||
ButtonState _state; | ||
|
||
virtual void OnStart() | ||
{ | ||
pinMode(_buttonPin, INPUT_PULLUP); | ||
} | ||
|
||
virtual void OnUpdate(uint32_t deltaTimeMs) | ||
{ | ||
uint8_t value = digitalRead(_buttonPin); | ||
|
||
if (value != _lastValue) | ||
{ | ||
_lastValue = value; | ||
if (value == LOW) | ||
{ | ||
// just read button down | ||
_debouceTimer = _debouceMs; | ||
} | ||
else if (_state == ButtonState_Pressed) | ||
{ | ||
// triggered released | ||
_state = ButtonState_Released; | ||
_callback(_state); | ||
} | ||
} | ||
else | ||
{ | ||
if (value == LOW && _state == ButtonState_Released) | ||
{ | ||
// still button down | ||
if (deltaTimeMs >= _debouceTimer) | ||
{ | ||
// triggered press | ||
_state = ButtonState_Pressed; | ||
_callback(_state); | ||
} | ||
else | ||
{ | ||
_debouceTimer -= deltaTimeMs; | ||
} | ||
} | ||
} | ||
} | ||
}; |
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,67 @@ | ||
// ButtonTask | ||
// This demonstrates the use of the custom Task object feature of Task library | ||
// It will instance two custom ButtonTasks to monitor two different pins and call back | ||
// when they change state; with debouce support | ||
// This requires two momentary buttons attached between pins 4 & 5 and ground | ||
// One button will turn on the on board led when it is pressed down, | ||
// the other button turn off the on board led when it is released | ||
|
||
// Pin 13 has an LED connected on most Arduino boards. | ||
#define LedPin 13 | ||
#define OnButtonPin 4 | ||
#define OffButtonPin 5 | ||
|
||
// include libraries | ||
#include <Task.h> | ||
|
||
// include sub files | ||
#include "ButtonTask.h" | ||
|
||
TaskManager taskManager; | ||
|
||
ButtonTask onButton(OnButtonChanged, OnButtonPin); | ||
ButtonTask offButton(OffButtonChanged, OffButtonPin); | ||
|
||
uint8_t onButtonCount = 0; | ||
uint8_t offButtonCount = 0; | ||
|
||
void setup() | ||
{ | ||
Serial.begin(57600); | ||
pinMode(LedPin, OUTPUT); | ||
taskManager.StartTask(&onButton); | ||
taskManager.StartTask(&offButton); | ||
|
||
Serial.println("Running..."); | ||
} | ||
|
||
void loop() | ||
{ | ||
taskManager.Loop(); | ||
} | ||
|
||
void OnButtonChanged(ButtonState state) | ||
{ | ||
// apply on press | ||
if (state == ButtonState_Pressed) | ||
{ | ||
digitalWrite(LedPin, HIGH); // turn the LED on (HIGH is the voltage level) | ||
|
||
onButtonCount++; | ||
Serial.print("OnCount = "); | ||
Serial.println(onButtonCount); | ||
} | ||
} | ||
|
||
void OffButtonChanged(ButtonState state) | ||
{ | ||
// apply on release | ||
if (state == ButtonState_Released) | ||
{ | ||
digitalWrite(LedPin, LOW); // turn the LED off (LOW is the voltage level) | ||
|
||
offButtonCount++; | ||
Serial.print("OffCount = "); | ||
Serial.println(offButtonCount); | ||
} | ||
} |