Skip to content

Commit

Permalink
make the stop checker yield to other tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
rambo committed Nov 1, 2012
1 parent f3d7ce3 commit 2435e10
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ arduino pin 4 = OC1B = PORTB <- _BV(4) = SOIC pin 3 (Analog 2)
#ifndef TWI_RX_BUFFER_SIZE
#define TWI_RX_BUFFER_SIZE ( 16 )
#endif
// Get this library from http://bleaklow.com/files/2010/Task.tar.gz
// Get this library from http://bleaklow.com/files/2010/Task.tar.gz
// and read http://bleaklow.com/2010/07/20/a_very_simple_arduino_task_manager.html for instructions
#include <Task.h>
#include <TaskScheduler.h>

Expand Down Expand Up @@ -90,13 +91,16 @@ void Blinker::run(uint32_t now)
/**
* BEGIN: I2C Stop flag checker
*/
#define I2CStopCheck_YIELD_TICKS 4
// Task to echo serial input.
class I2CStopCheck : public Task
{
public:
I2CStopCheck();
virtual void run(uint32_t now);
virtual bool canRun(uint32_t now);
private:
uint8_t yield_counter; // Incremented on each canRun call, used to yield to other tasks.
};

I2CStopCheck::I2CStopCheck()
Expand All @@ -107,7 +111,8 @@ I2CStopCheck::I2CStopCheck()
// We're always ready to run this task-
bool I2CStopCheck::canRun(uint32_t now)
{
return true;
yield_counter++;
return ((yield_counter % I2CStopCheck_YIELD_TICKS) == 1);
}

void I2CStopCheck::run(uint32_t now)
Expand All @@ -122,8 +127,8 @@ void I2CStopCheck::run(uint32_t now)
Blinker blinker(3, 100);
I2CStopCheck checker;

// The checker needs to be the last task (todo: add some sort of yielding logic)
Task *tasks[] = { &blinker, &checker, };
// Tasks are in priority order, only one task is run per tick
Task *tasks[] = { &checker, &blinker, };
TaskScheduler sched(tasks, NUM_TASKS(tasks));

// The I2C registers
Expand Down

0 comments on commit 2435e10

Please sign in to comment.