Skip to content

Commit

Permalink
New Button Sample
Browse files Browse the repository at this point in the history
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
Makuna committed Jan 3, 2015
1 parent 6fe0784 commit 5e400e9
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 4 deletions.
2 changes: 0 additions & 2 deletions Task.h
Expand Up @@ -15,8 +15,6 @@ See GNU Lesser General Public License at <http://www.gnu.org/licenses/>.
#ifndef TASK_H
#define TASK_H



#if (ARDUINO >= 100)
#include <Arduino.h>
#else
Expand Down
15 changes: 13 additions & 2 deletions TaskManager.cpp
Expand Up @@ -118,11 +118,22 @@ uint32_t TaskManager::ProcessTasks(uint32_t deltaTimeMs)
{
if (pIterate->remainingTimeMs <= deltaTimeMs)
{
// calc per task delta time
uint32_t taskDeltaTime = max(1, ((pIterate->initialTimeMs - pIterate->remainingTimeMs) + deltaTimeMs) - 1);

pIterate->OnUpdate(taskDeltaTime);

// add the initial time so we don't loose any remainders
pIterate->remainingTimeMs += pIterate->initialTimeMs;
pIterate->OnUpdate(deltaTimeMs);

// if we are still less than delta time, things are running slow
// so push to the next update frame
if (pIterate->remainingTimeMs <= deltaTimeMs)
{
pIterate->remainingTimeMs = deltaTimeMs + 1;
}
}

pIterate->remainingTimeMs -= deltaTimeMs;

if (pIterate->remainingTimeMs < nextWakeTimeMs)
Expand Down
72 changes: 72 additions & 0 deletions examples/ButtonTask/ButtonTask.h
@@ -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;
}
}
}
}
};
67 changes: 67 additions & 0 deletions examples/ButtonTask/ButtonTask.ino
@@ -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);
}
}

0 comments on commit 5e400e9

Please sign in to comment.