Skip to content

Commit

Permalink
1.0.3
Browse files Browse the repository at this point in the history
Add _yield()_, _sleep()_, _rearm_async(); Remove _void rearm(timestamp_t
time, timestamp_t ival);_
  • Loading branch information
jmparatte committed May 1, 2017
1 parent c018bd6 commit 0774198
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<!--
2017-05-05: 1.0.3
2017-04-26: 1.0.2
2017-03-29: 1.0.1
2016-07-14: In progress...
Expand All @@ -17,6 +18,8 @@ Email: jean-marc@paratte.ch

# jm_Scheduler - A Scheduler Library for Arduino

2017-05-05: 1.0.3 - Add _yield()_, _sleep()_, _rearm_async(); Remove _void rearm(timestamp_t time, timestamp_t ival);_

2017-04-26: 1.0.2 - Add _void rearm(timestamp_t time, timestamp_t ival);_

2017-03-29: 1.0.1 - Minor adjustment
Expand Down
104 changes: 99 additions & 5 deletions jm_Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,15 @@ void jm_Scheduler::wakeup_chain_remove()
this->wakeup_next = 0;
}

//------------------------------------------------------------------------------

jm_Scheduler::jm_Scheduler()
{
// jm_Scheduler(false);
//}
//
//jm_Scheduler::jm_Scheduler(bool async)
//{
this->func = 0;
this->time = 0;
this->ival = 0;
Expand All @@ -128,8 +135,10 @@ jm_Scheduler::jm_Scheduler()
this->wakeup_next = 0; // next in wakeup routine chain
this->wakeup_count = 0; // count of repeated interrupt routine

// this->async = async;
this->started = false;
this->stopping = false;
this->yielded = false;
}

jm_Scheduler::~jm_Scheduler()
Expand All @@ -142,6 +151,8 @@ jm_Scheduler::operator bool()
return (this->started);
}

//------------------------------------------------------------------------------

void jm_Scheduler::display(int line)
{
Serial.print(line);
Expand Down Expand Up @@ -188,6 +199,8 @@ void jm_Scheduler::display(int line)
Serial.flush();
}

//------------------------------------------------------------------------------

void jm_Scheduler::time_cycle()
{
jm_Scheduler::tref = timestamp_read();
Expand Down Expand Up @@ -256,6 +269,41 @@ void jm_Scheduler::cycle()
}
}

void jm_Scheduler::yield()
{
if (jm_Scheduler::crnt) // called from a running routine ?
{
// backup routine states
timestamp_t tref_ = jm_Scheduler::tref;
jm_Scheduler *crnt_ = jm_Scheduler::crnt;

// set routine yielded state
crnt_->yielded = true;

jm_Scheduler::crnt = 0; // free scheduler from current routine
jm_Scheduler::cycle(); // yield current routine

// clr routine yielded state
crnt_->yielded = false;

// restore routine states
jm_Scheduler::crnt = crnt_;
jm_Scheduler::tref = tref_;
}
else // called from setup() or loop().
{
jm_Scheduler::cycle();
}
}

void jm_Scheduler::sleep(timestamp_t ival)
{
timestamp_t time1 = jm_Scheduler_time_read() + ival;
while (!jm_Scheduler_time_ge_time(jm_Scheduler_time_read(), time1)) jm_Scheduler::yield();
}

//------------------------------------------------------------------------------

// start routine immediately
void jm_Scheduler::start(voidfuncptr_t func)
{
Expand All @@ -271,6 +319,7 @@ void jm_Scheduler::start(voidfuncptr_t func)

this->started = true;
this->stopping = false;
this->yielded = false;
}

// start routine immediately and repeat it at fixed intervals
Expand All @@ -288,6 +337,7 @@ void jm_Scheduler::start(voidfuncptr_t func, timestamp_t ival)

this->started = true;
this->stopping = false;
this->yielded = false;
}

// start routine on time and repeat it at fixed intervals
Expand All @@ -305,6 +355,7 @@ void jm_Scheduler::start(voidfuncptr_t func, timestamp_t time, timestamp_t ival)

this->started = true;
this->stopping = false;
this->yielded = false;
}

// stop routine, current or scheduled, remove from chain
Expand All @@ -321,31 +372,70 @@ void jm_Scheduler::stop()
{
this->started = false;
this->stopping = false;
this->yielded = false;

this->chain_remove();
}
}

// rearm current routine and set or reset interval
//------------------------------------------------------------------------------

// rearm routine
void jm_Scheduler::rearm()
{
}

// rearm routine asynchronously
void jm_Scheduler::rearm_async()
{
this->time = jm_Scheduler_time_read();
}

// rearm routine and set next interval
void jm_Scheduler::rearm(timestamp_t ival)
{
this->ival = ival;
}

// rearm current routine, set time and set or reset interval
void jm_Scheduler::rearm(timestamp_t time, timestamp_t ival)
// rearm routine asynchronously and set next interval
void jm_Scheduler::rearm_async(timestamp_t ival)
{
this->time = time;
this->time = jm_Scheduler_time_read();
this->ival = ival;
}

// rearm current routine, change routine function and set or reset interval
//// rearm routine, set time and set next interval
//void jm_Scheduler::rearm(timestamp_t time, timestamp_t ival)
//{
// this->time = time;
// this->ival = ival;
//}

// rearm routine, change routine function and set next interval
void jm_Scheduler::rearm(voidfuncptr_t func, timestamp_t ival)
{
this->func = func;
this->ival = ival;
}

// rearm routine asynchronously, change routine function and set next interval
void jm_Scheduler::rearm_async(voidfuncptr_t func, timestamp_t ival)
{
this->time = jm_Scheduler_time_read();
this->func = func;
this->ival = ival;
}

//// rearm routine, change routine function, set time and set next interval
//void jm_Scheduler::rearm(voidfuncptr_t func, timestamp_t time, timestamp_t ival)
//{
// this->func = func;
// this->time = time;
// this->ival = ival;
//}

//------------------------------------------------------------------------------

// wakeup a scheduled routine (maybe repeated)
void jm_Scheduler::wakeup()
{
Expand Down Expand Up @@ -382,3 +472,7 @@ int jm_Scheduler::wakeup_read()

return count;
}

//------------------------------------------------------------------------------

// END.
31 changes: 26 additions & 5 deletions jm_Scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//------------------------------------------------------------------------------

#ifndef assert
#define assert(v) while(!v){} // default assert function
#define assert(v) while(!(v)){} // default assert function
#endif

#ifndef voidfuncptr_t
Expand Down Expand Up @@ -104,10 +104,14 @@ class jm_Scheduler
void wakeup_chain_append();
void wakeup_chain_remove();

// bool async;
bool started;
bool stopping;
bool yielded;

jm_Scheduler();
// jm_Scheduler(bool async);

~jm_Scheduler();

operator bool();
Expand All @@ -116,6 +120,8 @@ class jm_Scheduler

static void time_cycle();
static void cycle();
static void yield();
static void sleep(timestamp_t ival);

// start routine immediately
void start(voidfuncptr_t func);
Expand All @@ -129,15 +135,30 @@ class jm_Scheduler
// stop routine, current or scheduled, remove it from chain
void stop();

// rearm current routine and set or reset interval
// rearm routine
void rearm();

// rearm routine asynchronously
void rearm_async();

// rearm routine and set interval
void rearm(timestamp_t ival);

// rearm current routine, set time and set or reset interval
void rearm(timestamp_t time, timestamp_t ival);
// rearm routine asynchronously and set interval
void rearm_async(timestamp_t ival);

// rearm current routine, change routine function and set or reset interval
// // rearm routine, set time and set next interval
// void rearm(timestamp_t time, timestamp_t ival);

// rearm routine, change routine function and set interval
void rearm(voidfuncptr_t func, timestamp_t ival);

// rearm routine asynchronously, change routine function and set interval
void rearm_async(voidfuncptr_t func, timestamp_t ival);

// // rearm routine, change routine function, set time and set interval
// void rearm(voidfuncptr_t func, timestamp_t time, timestamp_t ival);

// wakeup a scheduled routine (maybe repeated)
void wakeup();

Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=jm_Scheduler
version=1.0.2
version=1.0.3
author=Jean-Marc Paratte <jean-marc@paratte.ch>
maintainer=Jean-Marc Paratte <jean-marc@paratte.ch>
sentence=A Scheduler Library for Arduino.
Expand Down

0 comments on commit 0774198

Please sign in to comment.