Skip to content

Commit

Permalink
Minimize header use, move Ticker function definitions into cpp file (e…
Browse files Browse the repository at this point in the history
…sp8266#6496)

* Backport from ESP32

* Use new library layout (.../src)

* Cleanup test case.

* C   style cast required.

* Whitespace

* Inlining via header has better baseline ROM footprint.

* Reordered functions for better code-compare to master

* Reduces ROM footprint some more.

* Avoid unnecessary parameter passing - refactoring, same generated footprint.

* Reformat example sources
  • Loading branch information
dok-net authored and devyte committed Nov 14, 2019
1 parent af85bd2 commit 482516e
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 186 deletions.
129 changes: 0 additions & 129 deletions libraries/Ticker/Ticker.h

This file was deleted.

23 changes: 16 additions & 7 deletions libraries/Ticker/examples/TickerParameter/TickerParameter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 13,17 @@

#include <Ticker.h>

Ticker tickerSetHigh;
Ticker tickerSetAnalog;
Ticker tickerSetLow;
Ticker tickerSetHigh;
Ticker tickerSetChar;

void setPinLow() {
digitalWrite(LED_BUILTIN, 0);
}

void setPinHigh() {
digitalWrite(LED_BUILTIN, 1);
}

void setPin(int state) {
digitalWrite(LED_BUILTIN, state);
Expand All @@ -27,14 35,15 @@ void setPinChar(char state) {

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(1, LOW);

// every 25 ms, call setPin(0)
tickerSetLow.attach_ms(25, setPin, 0);
// every 25 ms, call setPinLow()
tickerSetLow.attach_ms(25, setPinLow);

// every 26 ms, call setPinChar(1)
tickerSetHigh.attach_ms(26, setPinChar, (char)1);
// every 26 ms, call setPinHigh()
tickerSetHigh.attach_ms(26, setPinHigh);

// every 54 ms, call setPinChar(1)
tickerSetChar.attach_ms(26, setPinChar, (char)1);
}

void loop() {
Expand Down
17 changes: 2 additions & 15 deletions libraries/Ticker/keywords.txt
Original file line number Diff line number Diff line change
@@ -1,11 1,9 @@
#######################################
# Syntax Coloring Map For Wire
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

Ticker KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################
Expand All @@ -16,14 14,3 @@ once KEYWORD2
once_ms KEYWORD2
detach KEYWORD2
active KEYWORD2

#######################################
# Instances (KEYWORD2)
#######################################

Ticker KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################

57 changes: 22 additions & 35 deletions libraries/Ticker/Ticker.cpp → libraries/Ticker/src/Ticker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,60 25,47 @@

#include "Ticker.h"

namespace
{
constexpr int ONCE = 0;
constexpr int REPEAT = 1;
}

Ticker::Ticker()
: _timer(nullptr)
{
}
: _timer(nullptr) {}

Ticker::~Ticker()
{
detach();
}

void Ticker::_attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg)
{
_attach_ms(1000 * seconds, repeat, callback, arg);
detach();
}

void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg)
{
if (_timer)
{
os_timer_disarm(_timer);
}
else
{
_timer = &_etsTimer;
}

os_timer_setfn(_timer, callback, arg);
os_timer_arm(_timer, milliseconds, (repeat) ? REPEAT : ONCE);
if (_timer)
{
os_timer_disarm(_timer);
}
else
{
_timer = &_etsTimer;
}

os_timer_setfn(_timer, callback, arg);
os_timer_arm(_timer, milliseconds, repeat);
}

void Ticker::detach()
{
if (!_timer)
return;
if (!_timer)
return;

os_timer_disarm(_timer);
_timer = nullptr;
_callback_function = nullptr;
os_timer_disarm(_timer);
_timer = nullptr;
_callback_function = nullptr;
}

bool Ticker::active() const
{
return _timer;
return _timer;
}

void Ticker::_static_callback(void* arg)
{
Ticker* _this = reinterpret_cast<Ticker*>(arg);
if (_this && _this->_callback_function)
_this->_callback_function();
Ticker* _this = reinterpret_cast<Ticker*>(arg);
if (_this && _this->_callback_function)
_this->_callback_function();
}
Loading

0 comments on commit 482516e

Please sign in to comment.