Skip to content

Commit

Permalink
Added enable/disable functions
Browse files Browse the repository at this point in the history
  • Loading branch information
eiannone committed Jul 24, 2016
1 parent 4c0a8ab commit 3180150
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ Simple scheduler for ESP8266 Arduino based on Ticker
| --- | --- |
| i | Task ID |

###Enable/Disable task###

```boolean enable(uint i)```

```boolean disable(uint i)```

**Returns**:

```true``` - task enabled/disabled sucessfully

```false``` - task was not enabled/disabled

| Param | Description |
| --- | --- |
| i | Task ID |

###Enable / disable all tasks###

``` void enableAll() ```

``` void disableAll() ```

###TODO###
* Task callback parameters support
* ...
56 changes: 45 additions & 11 deletions TickerScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,24 @@ void TickerScheduler::handleTicker(tscallback_t f, volatile bool * flag)
}
}

boolean TickerScheduler::add(uint i, uint32_t period, tscallback_t f, boolean shouldFireNow)
bool TickerScheduler::add(uint i, uint32_t period, tscallback_t f, boolean shouldFireNow)
{
if (i >= this->size)
return false;

if (this->items[i].is_used)
if (i >= this->size || this->items[i].is_used)
return false;

this->items[i].cb = f;
this->items[i].flag = shouldFireNow;
this->items[i].t.attach_ms(period, tickerFlagHandle, &this->items[i].flag);
this->items[i].period = period;
this->items[i].is_used = true;

enable(i);

return true;
}

boolean TickerScheduler::remove(uint i)
bool TickerScheduler::remove(uint i)
{
if (i >= this->size)
return false;

if (!this->items[i].is_used)
if (i >= this->size || !this->items[i].is_used)
return false;

this->items[i].is_used = false;
Expand All @@ -70,6 +66,44 @@ boolean TickerScheduler::remove(uint i)
return true;
}

bool TickerScheduler::disable(uint i)
{
if (i >= this->size || !this->items[i].is_used)
return false;

this->items[i].t.detach();

return true;
}

bool TickerScheduler::enable(uint i)
{
if (i >= this->size || !this->items[i].is_used)
return false;

this->items[i].t.attach_ms(this->items[i].period, tickerFlagHandle, &this->items[i].flag);

return true;
}

void TickerScheduler::disableAll()
{
for (uint i = 0; i < this->size; i++)
{
if (this->items[i].is_used)
this->items[i].t.detach();
}
}

void TickerScheduler::enableAll()
{
for (uint i = 0; i < this->size; i++)
{
if (this->items[i].is_used)
this->items[i].t.attach_ms(this->items[i].period, tickerFlagHandle, &this->items[i].flag);
}
}

void TickerScheduler::update()
{
for (uint i = 0; i < this->size; i++)
Expand Down
11 changes: 7 additions & 4 deletions TickerScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct TickerSchedulerItem
Ticker t;
volatile bool flag = false;
tscallback_t cb;
uint32_t period;
volatile bool is_used = false;
};

Expand All @@ -26,9 +27,11 @@ class TickerScheduler
TickerScheduler(uint size);
~TickerScheduler();

boolean add(uint i, uint32_t period, tscallback_t, boolean shouldFireNow = false);

boolean remove(uint i);

bool add(uint i, uint32_t period, tscallback_t, boolean shouldFireNow = false);
bool remove(uint i);
bool enable(uint i);
bool disable(uint i);
void enableAll();
void disableAll();
void update();
};

0 comments on commit 3180150

Please sign in to comment.