Add Ticker lib (Hardware Timer)

This commit is contained in:
Bigbits
2019-04-07 14:48:39 +08:00
parent e13913f802
commit 170fb03039
7 changed files with 231 additions and 0 deletions

View File

@@ -107,6 +107,7 @@ UARTClass::read(void)
while(this->_buff->available()){
return this->_buff->read_char();
}
return 0;
}
void

View File

@@ -0,0 +1,26 @@
#include <Arduino.h>
#include <Ticker.h>
// attach a LED to GPIO 21
#define LED_PIN 13
Ticker tickerSetHigh(TIMER1);
Ticker tickerSetLow(TIMER2);
void setPin(int state) {
digitalWrite(LED_PIN, state);
}
void setup() {
pinMode(LED_PIN, OUTPUT);
// every 25 ms, call setPin(0)
tickerSetLow.attach_ms(25, setPin, 0);
// every 26 ms, call setPin(1)
tickerSetHigh.attach_ms(26, setPin, 1);
}
void loop() {
}

View File

@@ -0,0 +1,42 @@
#include <Arduino.h>
#include <Ticker.h>
// attach a LED to pPIO 13
#define LED_PIN 13
Ticker blinker(TIMER0);
Ticker toggler(TIMER1);
Ticker changer(TIMER2);
float blinkerPace = 0.1; //seconds
const float togglePeriod = 5; //seconds
void change() {
blinkerPace = 0.5;
}
void blink() {
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}
void toggle() {
static bool isBlinking = false;
if (isBlinking) {
blinker.detach();
isBlinking = false;
}
else {
blinker.attach(blinkerPace, blink);
isBlinking = true;
}
digitalWrite(LED_PIN, LOW); //make sure LED on on after toggling (pin LOW = led ON)
}
void setup() {
pinMode(LED_PIN, OUTPUT);
toggler.attach(togglePeriod, toggle);
changer.once(30, change);
}
void loop() {
}

View File

@@ -0,0 +1,22 @@
#######################################
# Datatypes (KEYWORD1)
#######################################
Ticker KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
attach KEYWORD2
attach_ms KEYWORD2
once KEYWORD2
once_ms KEYWORD2
detach KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
TIMER0 LITERAL1
TIMER1 LITERAL1
TIMER2 LITERAL1

View File

@@ -0,0 +1,9 @@
name=Ticker
version=1.0
author=BigBits
maintainer=BigBits <bigbits@bigbits.cc>
sentence=Allows to call functions with a given interval.
paragraph=Allows to call functions with a given interval.
category=Timing
url=
architectures=k210

View File

@@ -0,0 +1,38 @@
#include "Ticker.h"
#include "sysctl.h"
Ticker::Ticker(timer_id_t id)
:timer_id(id)
{
timer_init(timer_device_number_t(timer_id));
}
Ticker::~Ticker()
{
detach();
}
void
Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, size_t arg)
{
user_callback = callback;
_arg = arg;
timer_set_interval(timer_device_number_t(timer_id), TIMER_CHANNEL_0, milliseconds * 1000000);
timer_irq_register(timer_device_number_t(timer_id), TIMER_CHANNEL_0, !repeat, 4, timer_callback, this);
timer_set_enable(timer_device_number_t(timer_id), TIMER_CHANNEL_0, 1);
sysctl_enable_irq();
}
void
Ticker::detach()
{
timer_irq_unregister(timer_device_number_t(timer_id), TIMER_CHANNEL_0);
timer_set_enable(timer_device_number_t(timer_id), TIMER_CHANNEL_0, 0);
}
static int timer_callback(void* ctx)
{
auto &driver = *reinterpret_cast<Ticker *>(ctx);
driver.user_callback((void *)driver._arg);
return 0;
}

View File

@@ -0,0 +1,93 @@
#ifndef TICKER_H
#define TICKER_H
#include "utils.h"
#include "timer.h"
#include "stdio.h"
#include "stdint.h"
typedef enum{
TIMER0 = 0,
TIMER1,
TIMER2,
TIMER_MAX
}timer_id_t;
class Ticker
{
public:
Ticker(timer_id_t id = TIMER2);
~Ticker();
typedef void (*callback_t)(void);
typedef void (*callback_with_arg_t)(void*);
void attach(float seconds, callback_t callback)
{
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
}
void attach_ms(uint32_t milliseconds, callback_t callback)
{
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
}
template<typename TArg>
void attach(float seconds, void (*callback)(TArg), TArg arg)
{
configASSERT(sizeof(TArg) <= sizeof(size_t));
size_t arg32 = (size_t)arg;
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}
template<typename TArg>
void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
{
configASSERT(sizeof(TArg) <= sizeof(size_t));
size_t arg32 = (size_t)arg;
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}
void once(float seconds, callback_t callback)
{
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
}
void once_ms(uint32_t milliseconds, callback_t callback)
{
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
}
template<typename TArg>
void once(float seconds, void (*callback)(TArg), TArg arg)
{
configASSERT(sizeof(TArg) <= sizeof(size_t));
size_t arg32 = (size_t)(arg);
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}
template<typename TArg>
void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
{
configASSERT(sizeof(TArg) <= sizeof(size_t));
size_t arg32 = (size_t)(arg);
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}
void detach();
bool active();
callback_with_arg_t user_callback = NULL;
size_t _arg;
protected:
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, size_t arg);
private:
timer_id_t timer_id;
}__attribute__((packed));
static int timer_callback(void* ctx);
#endif