parent
9a496bab21
commit
5ddd49c42a
@ -0,0 +1,56 @@ |
|||||||
|
/**
|
||||||
|
* AutoConnectTicker class implementation. |
||||||
|
* Provides a service that shows the flicker signal according to WiFi |
||||||
|
* connection status.
|
||||||
|
* @file AutoConnect.cpp |
||||||
|
* @author hieromon@gmail.com |
||||||
|
* @version 0.9.11 |
||||||
|
* @date 2019-07-09 |
||||||
|
* @copyright MIT license. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "AutoConnectTicker.h" |
||||||
|
|
||||||
|
/**
|
||||||
|
* Start ticker cycle |
||||||
|
* @param cycle Cycle time in [ms] |
||||||
|
* @param duty Duty cycle in [ms] |
||||||
|
*/ |
||||||
|
void AutoConnectTicker::start(const uint32_t cycle, const uint32_t duty) { |
||||||
|
_cycle = cycle; |
||||||
|
if (duty <= _cycle) |
||||||
|
_duty = duty; |
||||||
|
start(); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* Start ticker cycle |
||||||
|
*/ |
||||||
|
void AutoConnectTicker::start(void) { |
||||||
|
pinMode(_port, OUTPUT); |
||||||
|
_pulse.detach(); |
||||||
|
_period.attach_ms<AutoConnectTicker*>(_cycle, AutoConnectTicker::_onPeriod, this); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn on the flicker signal and reserves a ticker to turn off the |
||||||
|
* signal. This behavior will perform every cycle to generate the |
||||||
|
* pseudo-PWM signal. |
||||||
|
* If the function is registered, call the callback function at the |
||||||
|
* end of one cycle. |
||||||
|
* @param t Its own address |
||||||
|
*/ |
||||||
|
void AutoConnectTicker::_onPeriod(AutoConnectTicker* t) { |
||||||
|
digitalWrite(t->_port, t->_turnOn); |
||||||
|
t->_pulse.once_ms<AutoConnectTicker*>(t->_duty, AutoConnectTicker::_onPulse, t); |
||||||
|
if (t->_callback) |
||||||
|
t->_callback(); |
||||||
|
} |
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn off the flicker signal |
||||||
|
* @param t Its own address |
||||||
|
*/ |
||||||
|
void AutoConnectTicker::_onPulse(AutoConnectTicker* t) { |
||||||
|
digitalWrite(t->_port, !(t->_turnOn)); |
||||||
|
} |
@ -0,0 +1,53 @@ |
|||||||
|
/**
|
||||||
|
* Declaration of AutoConnectTicker class. |
||||||
|
* @file AutoConnectTicker.h |
||||||
|
* @author hieromon@gmail.com |
||||||
|
* @version 0.9.11 |
||||||
|
* @date 2019-07-09 |
||||||
|
* @copyright MIT license. |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef _AUTOCONNECTTICKER_H_ |
||||||
|
#define _AUTOCONNECTTICKER_H_ |
||||||
|
|
||||||
|
#include <functional> |
||||||
|
#if defined(ARDUINO_ARCH_ESP8266) |
||||||
|
#include <ESP8266WiFi.h> |
||||||
|
#elif defined(ARDUINO_ARCH_ESP32) |
||||||
|
#include <WiFi.h> |
||||||
|
#endif |
||||||
|
#include <Ticker.h> |
||||||
|
#include "AutoConnectDefs.h" |
||||||
|
|
||||||
|
class AutoConnectTicker { |
||||||
|
public: |
||||||
|
explicit AutoConnectTicker(const uint8_t port = AUTOCONNECT_TICKER_PORT, const uint8_t active = LOW, const uint32_t cycle = 0, uint32_t duty = 0) : _cycle(cycle), _duty(duty), _port(port), _turnOn(active), _callback(nullptr) { |
||||||
|
if (_duty > _cycle) |
||||||
|
_duty = _cycle; |
||||||
|
} |
||||||
|
~AutoConnectTicker() { stop(); } |
||||||
|
|
||||||
|
typedef std::function<void(void)> Callback_ft; |
||||||
|
void setCycle(const uint32_t cycle) { _cycle = cycle; } |
||||||
|
void setDuty(const uint32_t duty) { _duty = duty <= _cycle ? duty : _duty; } |
||||||
|
void start(const uint32_t cycle, const uint32_t duty); |
||||||
|
void start(const uint32_t cycle, const uint8_t width) { start(cycle, (uint32_t)((cycle * width) >> 8)); } |
||||||
|
void start(void); |
||||||
|
void stop(void) { _period.detach(); _pulse.detach(); digitalWrite(_port, !_turnOn); } |
||||||
|
void onPeriod(Callback_ft cb) { _callback = cb ;} |
||||||
|
|
||||||
|
protected: |
||||||
|
Ticker _period; /**< Ticker for flicking cycle */ |
||||||
|
Ticker _pulse; /**< Ticker for pulse width generating */ |
||||||
|
uint32_t _cycle; /**< Cycle time in [ms] */ |
||||||
|
uint32_t _duty; /**< Pulse width in [ms] */ |
||||||
|
|
||||||
|
private: |
||||||
|
static void _onPeriod(AutoConnectTicker* t); |
||||||
|
static void _onPulse(AutoConnectTicker* t); |
||||||
|
uint8_t _port; /**< Port to output signal */ |
||||||
|
uint8_t _turnOn; /**< Signal to turn on */ |
||||||
|
Callback_ft _callback; /**< An exit by every cycle */ |
||||||
|
}; |
||||||
|
|
||||||
|
#endif // !_AUTOCONNECTTICKER_H_
|
Loading…
Reference in new issue