Skip to content

Minimize header use, move Ticker function definitions into cpp file #6496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 14, 2019
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