Compare commits

...

4 Commits

Author SHA1 Message Date
b9aa89f6d2 timerlib added 2020-10-13 01:59:55 +02:00
527ec7353d pointing to 1.8.3 now 2020-10-12 23:24:15 +02:00
01aab1559f readability update 2020-10-12 22:32:47 +02:00
12822f737d windows support added 2020-10-12 22:02:52 +02:00
4 changed files with 232 additions and 7 deletions

@ -1 +1 @@
Subproject commit 0e7fae85bdfbab43242374c0289cdd806d13b0bc
Subproject commit 3055c1efa3c6980c864f661e6c8cc5d5ac773af4

View File

@ -37,11 +37,16 @@ CORE_LIB = $(ARDDIR)/arduino_core.a
#
# Exemplia gratia:
#
# TARGET = foo # the name of your main file, with no extension
# SRC = foo.c bar.c # the C source files
# PSRC = baz.cpp qux.cpp # the C++ source files
# ARDLIBS = SoftwareSerial # extra Arduino libraries
# include /d/dev/avr/Makefile.base # this line includes the Makefile.base
# the name of your main file, with no extension
# TARGET = foo
# the C source files
# SRC = foo.c bar.c
# the C++ source files
# PSRC = baz.cpp qux.cpp
# extra Arduino libraries
# ARDLIBS = SoftwareSerial
# this line includes the Makefile.base
# include /d/dev/avr/Makefile.base
# A number of necessary variables are set to defaults below, but you can override them
# in your own Makefile:
@ -233,12 +238,27 @@ DIRLIB = $(DIRAVR)/avr/lib
# Define programs and commands.
SHELL = sh
ifdef WINDOWS
CC = avr-gcc.exe
OBJCOPY = avr-objcopy.exe
OBJDUMP = avr-objdump.exe
SIZE = avr-size.exe
AVR_NM = avr-nm.exe
else
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
AVR_NM = avr-nm
endif
# Programming support using avrdude.
AVRDUDE = avrdude
@ -376,7 +396,7 @@ program: $(TARGET).hex $(TARGET).eep
%.sym: %.elf
@echo
@echo $(MSG_SYMBOL_TABLE) $@
avr-nm -n $< > $@
$(AVR_NM) -n $< > $@
# Archive Arduino core library

202
src/arduino-timer.h Normal file
View File

@ -0,0 +1,202 @@
/**
arduino-timer - library for delaying function calls
Copyright (c) 2018, Michael Contreras
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _CM_ARDUINO_TIMER_H__
#define _CM_ARDUINO_TIMER_H__
#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#ifndef TIMER_MAX_TASKS
#define TIMER_MAX_TASKS 0x10
#endif
template <
size_t max_tasks = TIMER_MAX_TASKS, /* max allocated tasks */
unsigned long (*time_func)() = millis, /* time function for timer */
typename T = void * /* handler argument type */
>
class Timer {
public:
typedef uintptr_t Task; /* public task handle */
typedef bool (*handler_t)(T opaque); /* task handler func signature */
/* Calls handler with opaque as argument in delay units of time */
Task
in(unsigned long delay, handler_t h, T opaque = T())
{
return task_id(add_task(time_func(), delay, h, opaque));
}
/* Calls handler with opaque as argument at time */
Task
at(unsigned long time, handler_t h, T opaque = T())
{
const unsigned long now = time_func();
return task_id(add_task(now, time - now, h, opaque));
}
/* Calls handler with opaque as argument every interval units of time */
Task
every(unsigned long interval, handler_t h, T opaque = T())
{
return task_id(add_task(time_func(), interval, h, opaque, interval));
}
/* Cancel the timer task */
void
cancel(Task &task)
{
if (!task) return;
for (size_t i = 0; i < max_tasks; ++i) {
struct task * const t = &tasks[i];
if (t->handler && (t->id ^ task) == (uintptr_t)t) {
remove(t);
break;
}
}
task = (Task)NULL;
}
/* Ticks the timer forward - call this function in loop() */
unsigned long
tick()
{
unsigned long ticks = (unsigned long)-1;
for (size_t i = 0; i < max_tasks; ++i) {
struct task * const task = &tasks[i];
if (task->handler) {
const unsigned long t = time_func();
const unsigned long duration = t - task->start;
if (duration >= task->expires) {
task->repeat = task->handler(task->opaque) && task->repeat;
if (task->repeat) task->start = t;
else remove(task);
} else {
const unsigned long remaining = task->expires - duration;
ticks = remaining < ticks ? remaining : ticks;
}
}
}
return ticks == (unsigned long)-1 ? 0 : ticks;
}
private:
size_t ctr;
struct task {
handler_t handler; /* task handler callback func */
T opaque; /* argument given to the callback handler */
unsigned long start,
expires; /* when the task expires */
size_t repeat, /* repeat task */
id;
} tasks[max_tasks];
inline
void
remove(struct task *task)
{
task->handler = NULL;
task->opaque = T();
task->start = 0;
task->expires = 0;
task->repeat = 0;
task->id = 0;
}
inline
Task
task_id(const struct task * const t)
{
const Task id = (Task)t;
return id ? id ^ t->id : id;
}
inline
struct task *
next_task_slot()
{
for (size_t i = 0; i < max_tasks; ++i) {
struct task * const slot = &tasks[i];
if (slot->handler == NULL) return slot;
}
return NULL;
}
inline
struct task *
add_task(unsigned long start, unsigned long expires,
handler_t h, T opaque, bool repeat = 0)
{
struct task * const slot = next_task_slot();
if (!slot) return NULL;
if (++ctr == 0) ++ctr; // overflow
slot->id = ctr;
slot->handler = h;
slot->opaque = opaque;
slot->start = start;
slot->expires = expires;
slot->repeat = repeat;
return slot;
}
};
/* create a timer with the default settings */
inline Timer<>
timer_create_default()
{
return Timer<>();
}
#endif

3
src/timer.h Normal file
View File

@ -0,0 +1,3 @@
#warning "Including this file is deprecated. Please #include <arduino-timer.h> instead."
#include <arduino-timer.h>