From 5c3f1e6e0500751e92e39bd60c8b116cde2072ab Mon Sep 17 00:00:00 2001 From: sprae Date: Thu, 20 Dec 2012 14:41:25 +0100 Subject: [PATCH 01/14] Nowa wersja testowa 1.99 --- Timers.cpp | 51 ----------------- Timers.h | 82 ++++++++++++++++++++++------ examples/timers_test/timers_test.ino | 47 ---------------- keywords.txt | 4 +- 4 files changed, 67 insertions(+), 117 deletions(-) delete mode 100644 Timers.cpp delete mode 100644 examples/timers_test/timers_test.ino diff --git a/Timers.cpp b/Timers.cpp deleted file mode 100644 index eb77172..0000000 --- a/Timers.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "Timers.h" - -void NOP(void) -{ - return; -} - -Timers::Timers(void) -{ - for (int i=0; i 0 && delta_time >= _elements[i].interval) - { - _elements[i].func(); - _elements[i].begin_time = actual_time; - } - } -} - diff --git a/Timers.h b/Timers.h index abc953f..9acafee 100644 --- a/Timers.h +++ b/Timers.h @@ -3,34 +3,82 @@ #ifndef timers_h #define timers_h -#ifndef TIMER_ITEMS -#define TIMER_ITEMS 8 -#endif - typedef void (*timerFunc)(void); +typedef void (*timerEventFunc)(byte); +void NOP(void) +{ + return; +} -void NOP(void); - +void NOP2(byte slot) +{ + return; +} struct TimerElement { - timerFunc func; - unsigned long interval; - unsigned long begin_time; + timerFunc func; + unsigned long interval; + unsigned long begin_time; }; +template class Timers { - private: - struct TimerElement _elements[TIMER_ITEMS]; - - public: - Timers(void); - void attach(byte slot, unsigned long interval, timerFunc func); - void setInterval(byte slot, unsigned long interval); - void process(void); + private: + struct TimerElement _elements[TIMER_ITEMS]; + + public: + timerEventFunc onTime; + + Timers(void) + { + for (int i=0; i 0 && delta_time >= _elements[i].interval) + { + onTime(i); + _elements[i].func(); + _elements[i].begin_time = actual_time; + } + } + } }; #endif diff --git a/examples/timers_test/timers_test.ino b/examples/timers_test/timers_test.ino deleted file mode 100644 index a5ece97..0000000 --- a/examples/timers_test/timers_test.ino +++ /dev/null @@ -1,47 +0,0 @@ -// Ilość elementow timera -// musi byc podane przed include -#define TIMER_ITEMS 2 - -#include -#include - -#define SERVO_PIN 2 -#define LED_PIN 13 - -Servo servo; - -Timers timer; // ** Deklaracja obiektu timera z 2 slotami - -// * Obróć serwo o 1 stopień -void servoMove() -{ - static byte angle = 0; - servo.write(angle); - angle++; -} - -// * Zmień stan diody na przeciwny -void ledChange() -{ - static byte ledState = LOW; - digitalWrite(LED_PIN, ledState); - ledState = ~ledState; -} - -void setup() -{ - servo.attach(SERVO_PIN); - pinMode(LED_PIN, OUTPUT); - - // Podłączenie funkcji wywoływanej co 0,02 s - timer.attach(0, 20, servoMove); - - // Podłączenie funkcji wywoływanej co 0,5 s - timer.attach(1, 500, ledChange); -} - -void loop() -{ - // Aktualizacja stanu timera - timer.process(); -} diff --git a/keywords.txt b/keywords.txt index 71fdb4a..e908c4c 100644 --- a/keywords.txt +++ b/keywords.txt @@ -14,11 +14,11 @@ Timers KEYWORD1 attach KEYWORD2 setInterval KEYWORD2 process KEYWORD2 +onTime KEYWORD2 ####################################### # Constants (LITERAL1) ####################################### NOP LITERAL1 -TIMER_ITEMS LITERAL1 - +NOP2 LITERAL1 From a872cf9e4061d4c00b3b5d860bd05fc7596e85aa Mon Sep 17 00:00:00 2001 From: sprae Date: Thu, 17 Jan 2013 07:03:55 +0100 Subject: [PATCH 02/14] =?UTF-8?q?Doda=C5=82em=20updateInterval=20i=20przyp?= =?UTF-8?q?udrowa=C5=82em=20kod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Timers.h | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/Timers.h b/Timers.h index 9acafee..e0bbe21 100644 --- a/Timers.h +++ b/Timers.h @@ -6,12 +6,12 @@ typedef void (*timerFunc)(void); typedef void (*timerEventFunc)(byte); -void NOP(void) +void nullTimerFunc(void) { return; } -void NOP2(byte slot) +void nullOnTimeFunc(byte element) { return; } @@ -37,25 +37,30 @@ class Timers { for (int i=0; i Date: Thu, 17 Jan 2013 07:04:32 +0100 Subject: [PATCH 03/14] +updateInterval --- keywords.txt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/keywords.txt b/keywords.txt index e908c4c..a8a7aaa 100644 --- a/keywords.txt +++ b/keywords.txt @@ -6,13 +6,14 @@ # Datatypes (KEYWORD1) ####################################### -Timers KEYWORD1 +Timers KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) ####################################### attach KEYWORD2 setInterval KEYWORD2 +updateInterval KEYWORD2 process KEYWORD2 onTime KEYWORD2 @@ -20,5 +21,5 @@ onTime KEYWORD2 # Constants (LITERAL1) ####################################### -NOP LITERAL1 -NOP2 LITERAL1 +nullTimerFunc LITERAL1 +nullOnTimeFunc LITERAL1 From a10f5731cdf9082e0741923c0d229a83693e40f2 Mon Sep 17 00:00:00 2001 From: sprae Date: Fri, 6 Jun 2014 19:17:56 +0200 Subject: [PATCH 04/14] Nowa wersja 3.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Łatwiejsza w obsłudze --- README.md | 124 ++++++++++++++ Timers.cpp | 151 ++++++++++++++++++ Timers.h | 110 ++++--------- .../timers_10s_blink/timers_10s_blink.ino | 38 +++++ examples/timers_blink/timers_blink.ino | 23 +++ keywords.txt | 19 +-- 6 files changed, 375 insertions(+), 90 deletions(-) create mode 100644 README.md create mode 100644 Timers.cpp create mode 100644 examples/timers_10s_blink/timers_10s_blink.ino create mode 100644 examples/timers_blink/timers_blink.ino diff --git a/README.md b/README.md new file mode 100644 index 0000000..bc7a0c7 --- /dev/null +++ b/README.md @@ -0,0 +1,124 @@ +# Timers - program sterowany czasem + +Timers jest to biblioteka dla Arduino do wywoływania funkcji programu wokreślonym czasie. + + +### Przykład 1: Program mrugający diodą podłączoną do pinu 13 + + // ** Program mrugający diodą podłączoną do pinu 13 ** + + // Ładowanie biblioteki Timers + #include + + void setup() + { + pinMode(13, OUTPUT); + // Wywoływanie funkcji "toggle" co 500 ms + Timers.every(500, toggle); + } + + void loop() + { + // Obliczanie czasu + Timers.process(); + } + + // Funkcja mrugająca diodą + void toggle() + { + digitalWrite(13, !digitalRead(13)); + } + + +### Przykład 2: Program mrugający diodą podłączoną do pinu 13 przez 10 sekund + + // ** Program mrugający diodą podłączoną do pinu 13 przez 10 sekund ** + + // Ładowanie biblioteki Timers + #include + + // Zmienna przechowująca numer timera mrugającego diodą + byte blinker; + + void setup() + { + pinMode(13, OUTPUT); + + // Wywoływanie funkcji "toggle" co 500 ms + blinker = Timers.every(500, toggle); + + // Wywołanie funkcji "blinkOff" za 10 s + Timers.after(10000, blinkOff); + } + + void loop() + { + // Obliczanie czasu + Timers.process(); + } + + // Funkcja mrugająca diodą + void toggle() + { + digitalWrite(13, !digitalRead(13)); + } + + // Funkcja wyłączająca mruganie + void blinkOff() + { + // Usunięcie timera o numerze przechowywanym w zmiennej "blinker" + Timers.clear(blinker); + } + + + +### Referencje + +`Timers.every(czas, nazwa_funkcji);` - ustawia wywoływanie fukcji cykliczne co określony czas. + +- czas - czas co jaki będzie wywoływana funkcja w milisekundach. +- nazwa_funkcji - nazwa funkcji, ktora ma być cyklicznie wywoływana + +Funkcja zwraca unikatowy numer identyfikacyjny potrzebny do zarządzania czasem dla tej funkcji przez metody `Timers.clear`, `Timers.reset` i `Timers.update`. +Jeśli zwrócowa wartość wynosi 0 to zadanie się nie powiodło (zabrakło pamięci RAM, albo przekroczyło 255 dodanych funkji) + + +`Timers.after(czas, nazwa_funkcji);` - wywołuje funkcje raz po ustalonym w argumencie czasie. + +- czas - czas po jakim zostanie wywołana funkcja w milisekundach. +- nazwa_funkcji - nazwa funkcji, która ma być wywołana po czasie + +Funkcja zwraca unikatory numer identyfikacyjny potrzebny do zarządzania czasem dla tej funkcji przez metody `Timers.clear`, `Timers.reset` i `Timers.update`. +Jeśli zwrócowa wartość wynosi 0 to zadanie się nie powiodło (zabrakło pamięci RAM, albo przekroczyło 255 dodanych funkji) + + +`Timers.clear(id);` - wyłącza wywoływanie funkcji rozpoczęte przez `Timers.every` i `Timers.after`. + +- id - numer identyfikkacyjny wywoływanej funkcji zwracany przez `Timers.every` i `Timers.after`. + +Funkcja zwraca wartość typu `bool`. Zwraca `true` jeśli operacja się powiodła. Wartość `false` zwraca jeśli nie ma timera o takim numerze `id`. + + +`Timers.reset(id, czas);` - funkcja zmienia czas wywoływania funkcji. Czas jest ofliczany od chwili jego przestawienia. + +- id - numer identyfikkacyjny wywoływanej funkcji zwracany przez `Timers.every` i `Timers.after`. +- czas - czas po jakim zostanie wywołana funkcja w milisekundach. + +Funkcja zwraca wartość typu `bool`. Zwraca `true` jeśli operacja się powiodła. Wartość `false` zwraca jeśli nie ma timera o takim numerze `id`. + + +`Timers.update(id, czas);` - funkcja zmienia czas wywoływania funkcji. Czas jest odliczany od ostatniego wywołania funkcji lub jej zarejestrowania przez `Timers.every` i `Timers.after`. + +- id - numer identyfikkacyjny wywoływanej funkcji zwracany przez `Timers.every` i `Timers.after`. +- czas - czas po jakim zostanie wywołana funkcja w milisekundach. + +Funkcja zwraca wartość typu `bool`. Zwraca `true` jeśli operacja się powiodła. Wartość `false` zwraca jeśli nie ma timera o takim numerze `id`. + + +`Timers.length();` - odczytuje ile jest zarejestrowanych funkcji w timerze. + +Funkcja zwraca wartośc typu byte z ilością zarejestrowanych funkcji do wywołania. + + +`Timers.process();` - funkcja zajmuje się obliczaniem czasu potrzebnego do wywołania zarejestrowanych funkcji. Powina być wywoływana możliwie często, najlepiej w funkcji `loop`. + diff --git a/Timers.cpp b/Timers.cpp new file mode 100644 index 0000000..c11987d --- /dev/null +++ b/Timers.cpp @@ -0,0 +1,151 @@ +#include "Timers.h" + +CTimers::CTimers() +{ + _elements = NULL; + _length = 0; +} + +CTimers::~CTimers() +{ + free(_elements); +} + +uint8_t CTimers::every(uint32_t interval, timersFunc func) +{ + _attach(func, interval, false); +} + +uint8_t CTimers::after(uint32_t interval, timersFunc func) +{ + _attach(func, interval, true); +} + +bool CTimers::clear(uint8_t id) +{ + byte index = _getIndex(id); + if (index == 255) + return false; + + if (index != (_length-1)) + { + memmove(_elements+index, _elements+index+1, (_length-1-index)*sizeof(struct TimersElement)); + } + + struct TimersElement *elements; + elements = (struct TimersElement *) realloc(_elements, (_length-1)*sizeof(struct TimersElement)); + if (elements == NULL) + return false; + + _elements = elements; + _length--; + return true; +} + +bool CTimers::reset(uint8_t id, uint32_t interval) +{ + uint8_t index = _getIndex(id); + if (index == 255) + return false; + + _elements[index].interval = interval; + _elements[index].last_time = millis(); + + return true; +} + +bool CTimers::update(uint8_t id, uint32_t interval) +{ + uint8_t index = _getIndex(id); + if (index == 255) + return false; + + _elements[index].interval = interval; + + return true; +} + +uint8_t CTimers::length() +{ + return _length; +} + +void CTimers::process() +{ + unsigned long actual_time = millis(); + + for (int i=0; i<_length; i++) + { + unsigned long delta_time = actual_time - _elements[i].last_time; + + if (_elements[i].interval > 0 && delta_time >= _elements[i].interval) + { + _elements[i].func(); + _elements[i].last_time = actual_time; + + if (_elements[i].once) + clear(_elements[i].id); + } + } +} + +uint8_t CTimers::_attach(timersFunc func, uint32_t interval, bool once) +{ + uint8_t id = _getFreeId(); + if (id == 0) + return 0; + + struct TimersElement *elements; + elements = (struct TimersElement *) realloc(_elements, (_length+1)*sizeof(struct TimersElement)); + if (elements == NULL) + return 0; + + _elements = elements; + struct TimersElement *element = &_elements[_length]; + element->func = func; + element->interval = interval; + element->last_time = millis(); + element->once = once; + element->id = id; + + _length++; + return id; +} + +bool CTimers::_anyId(uint8_t id) +{ + if (_length == 0) + return false; + + for (uint8_t i=0; i<_length; i++) + { + if (_elements[i].id == id) + return true; + } + + return false; +} + +uint8_t CTimers::_getFreeId() +{ + for (uint8_t i=1; i<255; i++) + { + if (!_anyId(i)) + return i; + } + + return 0; +} + +uint8_t CTimers::_getIndex(uint8_t id) +{ + for (uint8_t i=0; i<_length; i++) + { + if (_elements[i].id == id) + return i; + } + + return 255; +} + +CTimers Timers; \ No newline at end of file diff --git a/Timers.h b/Timers.h index e0bbe21..c2481b8 100644 --- a/Timers.h +++ b/Timers.h @@ -1,89 +1,43 @@ -#include "Arduino.h" - #ifndef timers_h #define timers_h -typedef void (*timerFunc)(void); -typedef void (*timerEventFunc)(byte); - -void nullTimerFunc(void) -{ - return; -} +#include "Arduino.h" +#include -void nullOnTimeFunc(byte element) -{ - return; -} +typedef void (*timersFunc)(void); -struct TimerElement +struct TimersElement { - timerFunc func; - unsigned long interval; - unsigned long begin_time; + timersFunc func; + uint32_t interval; + uint32_t last_time; + bool once; + uint8_t id; }; - -template -class Timers +class CTimers { - private: - struct TimerElement _elements[TIMER_ITEMS]; - - public: - timerEventFunc onTime; - - Timers(void) - { - for (int i=0; i 0 && delta_time >= _elements[i].interval) - { - onTime(i); - _elements[i].func(); - _elements[i].begin_time = actual_time; - } - } - } +private: + struct TimersElement *_elements; + uint8_t _length; + + bool _anyId(uint8_t id); + uint8_t _getFreeId(); + uint8_t _getIndex(uint8_t id); + uint8_t _attach(timersFunc func, uint32_t interval, bool once=false); + +public: + CTimers(); + ~CTimers(); + uint8_t every(uint32_t interval, timersFunc func); + uint8_t after(uint32_t interval, timersFunc func); + bool clear(uint8_t id); + bool reset(uint8_t id, uint32_t interval); + bool update(uint8_t id, uint32_t interval); + uint8_t length(); + void process(); }; -#endif +extern CTimers Timers; + +#endif \ No newline at end of file diff --git a/examples/timers_10s_blink/timers_10s_blink.ino b/examples/timers_10s_blink/timers_10s_blink.ino new file mode 100644 index 0000000..ed6e66a --- /dev/null +++ b/examples/timers_10s_blink/timers_10s_blink.ino @@ -0,0 +1,38 @@ +// ** Program mrugający diodą podłączoną do pinu 13 przez 10 sekund ** + +// Ładowanie biblioteki Timers +#include + +// Zmienna przechowująca numer timera mrugającego diodą +byte blinker; + +void setup() +{ + pinMode(13, OUTPUT); + + // Wywoływanie funkcji "toggle" co 500 ms + blinker = Timers.every(500, toggle); + + // Wywołanie funkcji "blinkOff" za 10 s + Timers.after(10000, blinkOff); +} + +void loop() +{ + // Obliczanie czasu + Timers.process(); +} + +// Funkcja mrugająca diodą +void toggle() +{ + digitalWrite(13, !digitalRead(13)); +} + +// Funkcja wyłączająca mruganie +void blinkOff() +{ + // Usunięcie timera o numerze przechowywanym w zmiennej "blinker" + Timers.clear(blinker); +} + diff --git a/examples/timers_blink/timers_blink.ino b/examples/timers_blink/timers_blink.ino new file mode 100644 index 0000000..edc1894 --- /dev/null +++ b/examples/timers_blink/timers_blink.ino @@ -0,0 +1,23 @@ +// ** Program mrugający diodą podłączoną do pinu 13 ** + +// Ładowanie biblioteki Timers +#include + +void setup() +{ + pinMode(13, OUTPUT); + // Wywoływanie funkcji "toggle" co 500 ms + Timers.every(500, toggle); +} + +void loop() +{ + // Obliczanie czasu + Timers.process(); +} + +// Funkcja mrugająca diodą +void toggle() +{ + digitalWrite(13, !digitalRead(13)); +} diff --git a/keywords.txt b/keywords.txt index a8a7aaa..6ec2aaf 100644 --- a/keywords.txt +++ b/keywords.txt @@ -6,20 +6,15 @@ # Datatypes (KEYWORD1) ####################################### -Timers KEYWORD1 +Timers KEYWORD3 ####################################### # Methods and Functions (KEYWORD2) ####################################### -attach KEYWORD2 -setInterval KEYWORD2 -updateInterval KEYWORD2 +every KEYWORD2 +after KEYWORD2 +clear KEYWORD2 +reset KEYWORD2 +update KEYWORD2 +length KEYWORD2 process KEYWORD2 -onTime KEYWORD2 - -####################################### -# Constants (LITERAL1) -####################################### - -nullTimerFunc LITERAL1 -nullOnTimeFunc LITERAL1 From 91e25bb2ce5f092e63ff2b33731da74e81ba364f Mon Sep 17 00:00:00 2001 From: sprae Date: Sun, 10 Apr 2016 23:53:40 +0200 Subject: [PATCH 05/14] Ver 16.4.0 --- README.md | 124 ------------- Timers.cpp | 151 ---------------- Timers.h | 43 ----- Timers/Timers.cpp | 48 +++++ Timers/Timers.h | 26 +++ .../examples/analog_beeper/analog_beeper.ino | 34 ++++ .../auto_light_timer/auto_light_timer.ino | 51 ++++++ Timers/examples/blink/blink.ino | 19 ++ Timers/examples/smooth_blink/smooth_blink.ino | 62 +++++++ Timers/keywords.txt | 11 ++ Timers/library.properties | 9 + Timers/readme.md | 164 ++++++++++++++++++ .../timers_10s_blink/timers_10s_blink.ino | 38 ---- examples/timers_blink/timers_blink.ino | 23 --- keywords.txt | 20 --- 15 files changed, 424 insertions(+), 399 deletions(-) delete mode 100644 README.md delete mode 100644 Timers.cpp delete mode 100644 Timers.h create mode 100644 Timers/Timers.cpp create mode 100644 Timers/Timers.h create mode 100644 Timers/examples/analog_beeper/analog_beeper.ino create mode 100644 Timers/examples/auto_light_timer/auto_light_timer.ino create mode 100644 Timers/examples/blink/blink.ino create mode 100644 Timers/examples/smooth_blink/smooth_blink.ino create mode 100644 Timers/keywords.txt create mode 100644 Timers/library.properties create mode 100644 Timers/readme.md delete mode 100644 examples/timers_10s_blink/timers_10s_blink.ino delete mode 100644 examples/timers_blink/timers_blink.ino delete mode 100644 keywords.txt diff --git a/README.md b/README.md deleted file mode 100644 index bc7a0c7..0000000 --- a/README.md +++ /dev/null @@ -1,124 +0,0 @@ -# Timers - program sterowany czasem - -Timers jest to biblioteka dla Arduino do wywoływania funkcji programu wokreślonym czasie. - - -### Przykład 1: Program mrugający diodą podłączoną do pinu 13 - - // ** Program mrugający diodą podłączoną do pinu 13 ** - - // Ładowanie biblioteki Timers - #include - - void setup() - { - pinMode(13, OUTPUT); - // Wywoływanie funkcji "toggle" co 500 ms - Timers.every(500, toggle); - } - - void loop() - { - // Obliczanie czasu - Timers.process(); - } - - // Funkcja mrugająca diodą - void toggle() - { - digitalWrite(13, !digitalRead(13)); - } - - -### Przykład 2: Program mrugający diodą podłączoną do pinu 13 przez 10 sekund - - // ** Program mrugający diodą podłączoną do pinu 13 przez 10 sekund ** - - // Ładowanie biblioteki Timers - #include - - // Zmienna przechowująca numer timera mrugającego diodą - byte blinker; - - void setup() - { - pinMode(13, OUTPUT); - - // Wywoływanie funkcji "toggle" co 500 ms - blinker = Timers.every(500, toggle); - - // Wywołanie funkcji "blinkOff" za 10 s - Timers.after(10000, blinkOff); - } - - void loop() - { - // Obliczanie czasu - Timers.process(); - } - - // Funkcja mrugająca diodą - void toggle() - { - digitalWrite(13, !digitalRead(13)); - } - - // Funkcja wyłączająca mruganie - void blinkOff() - { - // Usunięcie timera o numerze przechowywanym w zmiennej "blinker" - Timers.clear(blinker); - } - - - -### Referencje - -`Timers.every(czas, nazwa_funkcji);` - ustawia wywoływanie fukcji cykliczne co określony czas. - -- czas - czas co jaki będzie wywoływana funkcja w milisekundach. -- nazwa_funkcji - nazwa funkcji, ktora ma być cyklicznie wywoływana - -Funkcja zwraca unikatowy numer identyfikacyjny potrzebny do zarządzania czasem dla tej funkcji przez metody `Timers.clear`, `Timers.reset` i `Timers.update`. -Jeśli zwrócowa wartość wynosi 0 to zadanie się nie powiodło (zabrakło pamięci RAM, albo przekroczyło 255 dodanych funkji) - - -`Timers.after(czas, nazwa_funkcji);` - wywołuje funkcje raz po ustalonym w argumencie czasie. - -- czas - czas po jakim zostanie wywołana funkcja w milisekundach. -- nazwa_funkcji - nazwa funkcji, która ma być wywołana po czasie - -Funkcja zwraca unikatory numer identyfikacyjny potrzebny do zarządzania czasem dla tej funkcji przez metody `Timers.clear`, `Timers.reset` i `Timers.update`. -Jeśli zwrócowa wartość wynosi 0 to zadanie się nie powiodło (zabrakło pamięci RAM, albo przekroczyło 255 dodanych funkji) - - -`Timers.clear(id);` - wyłącza wywoływanie funkcji rozpoczęte przez `Timers.every` i `Timers.after`. - -- id - numer identyfikkacyjny wywoływanej funkcji zwracany przez `Timers.every` i `Timers.after`. - -Funkcja zwraca wartość typu `bool`. Zwraca `true` jeśli operacja się powiodła. Wartość `false` zwraca jeśli nie ma timera o takim numerze `id`. - - -`Timers.reset(id, czas);` - funkcja zmienia czas wywoływania funkcji. Czas jest ofliczany od chwili jego przestawienia. - -- id - numer identyfikkacyjny wywoływanej funkcji zwracany przez `Timers.every` i `Timers.after`. -- czas - czas po jakim zostanie wywołana funkcja w milisekundach. - -Funkcja zwraca wartość typu `bool`. Zwraca `true` jeśli operacja się powiodła. Wartość `false` zwraca jeśli nie ma timera o takim numerze `id`. - - -`Timers.update(id, czas);` - funkcja zmienia czas wywoływania funkcji. Czas jest odliczany od ostatniego wywołania funkcji lub jej zarejestrowania przez `Timers.every` i `Timers.after`. - -- id - numer identyfikkacyjny wywoływanej funkcji zwracany przez `Timers.every` i `Timers.after`. -- czas - czas po jakim zostanie wywołana funkcja w milisekundach. - -Funkcja zwraca wartość typu `bool`. Zwraca `true` jeśli operacja się powiodła. Wartość `false` zwraca jeśli nie ma timera o takim numerze `id`. - - -`Timers.length();` - odczytuje ile jest zarejestrowanych funkcji w timerze. - -Funkcja zwraca wartośc typu byte z ilością zarejestrowanych funkcji do wywołania. - - -`Timers.process();` - funkcja zajmuje się obliczaniem czasu potrzebnego do wywołania zarejestrowanych funkcji. Powina być wywoływana możliwie często, najlepiej w funkcji `loop`. - diff --git a/Timers.cpp b/Timers.cpp deleted file mode 100644 index c11987d..0000000 --- a/Timers.cpp +++ /dev/null @@ -1,151 +0,0 @@ -#include "Timers.h" - -CTimers::CTimers() -{ - _elements = NULL; - _length = 0; -} - -CTimers::~CTimers() -{ - free(_elements); -} - -uint8_t CTimers::every(uint32_t interval, timersFunc func) -{ - _attach(func, interval, false); -} - -uint8_t CTimers::after(uint32_t interval, timersFunc func) -{ - _attach(func, interval, true); -} - -bool CTimers::clear(uint8_t id) -{ - byte index = _getIndex(id); - if (index == 255) - return false; - - if (index != (_length-1)) - { - memmove(_elements+index, _elements+index+1, (_length-1-index)*sizeof(struct TimersElement)); - } - - struct TimersElement *elements; - elements = (struct TimersElement *) realloc(_elements, (_length-1)*sizeof(struct TimersElement)); - if (elements == NULL) - return false; - - _elements = elements; - _length--; - return true; -} - -bool CTimers::reset(uint8_t id, uint32_t interval) -{ - uint8_t index = _getIndex(id); - if (index == 255) - return false; - - _elements[index].interval = interval; - _elements[index].last_time = millis(); - - return true; -} - -bool CTimers::update(uint8_t id, uint32_t interval) -{ - uint8_t index = _getIndex(id); - if (index == 255) - return false; - - _elements[index].interval = interval; - - return true; -} - -uint8_t CTimers::length() -{ - return _length; -} - -void CTimers::process() -{ - unsigned long actual_time = millis(); - - for (int i=0; i<_length; i++) - { - unsigned long delta_time = actual_time - _elements[i].last_time; - - if (_elements[i].interval > 0 && delta_time >= _elements[i].interval) - { - _elements[i].func(); - _elements[i].last_time = actual_time; - - if (_elements[i].once) - clear(_elements[i].id); - } - } -} - -uint8_t CTimers::_attach(timersFunc func, uint32_t interval, bool once) -{ - uint8_t id = _getFreeId(); - if (id == 0) - return 0; - - struct TimersElement *elements; - elements = (struct TimersElement *) realloc(_elements, (_length+1)*sizeof(struct TimersElement)); - if (elements == NULL) - return 0; - - _elements = elements; - struct TimersElement *element = &_elements[_length]; - element->func = func; - element->interval = interval; - element->last_time = millis(); - element->once = once; - element->id = id; - - _length++; - return id; -} - -bool CTimers::_anyId(uint8_t id) -{ - if (_length == 0) - return false; - - for (uint8_t i=0; i<_length; i++) - { - if (_elements[i].id == id) - return true; - } - - return false; -} - -uint8_t CTimers::_getFreeId() -{ - for (uint8_t i=1; i<255; i++) - { - if (!_anyId(i)) - return i; - } - - return 0; -} - -uint8_t CTimers::_getIndex(uint8_t id) -{ - for (uint8_t i=0; i<_length; i++) - { - if (_elements[i].id == id) - return i; - } - - return 255; -} - -CTimers Timers; \ No newline at end of file diff --git a/Timers.h b/Timers.h deleted file mode 100644 index c2481b8..0000000 --- a/Timers.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef timers_h -#define timers_h - -#include "Arduino.h" -#include - -typedef void (*timersFunc)(void); - -struct TimersElement -{ - timersFunc func; - uint32_t interval; - uint32_t last_time; - bool once; - uint8_t id; -}; - -class CTimers -{ -private: - struct TimersElement *_elements; - uint8_t _length; - - bool _anyId(uint8_t id); - uint8_t _getFreeId(); - uint8_t _getIndex(uint8_t id); - uint8_t _attach(timersFunc func, uint32_t interval, bool once=false); - -public: - CTimers(); - ~CTimers(); - uint8_t every(uint32_t interval, timersFunc func); - uint8_t after(uint32_t interval, timersFunc func); - bool clear(uint8_t id); - bool reset(uint8_t id, uint32_t interval); - bool update(uint8_t id, uint32_t interval); - uint8_t length(); - void process(); -}; - -extern CTimers Timers; - -#endif \ No newline at end of file diff --git a/Timers/Timers.cpp b/Timers/Timers.cpp new file mode 100644 index 0000000..23351c4 --- /dev/null +++ b/Timers/Timers.cpp @@ -0,0 +1,48 @@ +#include "Timers.h" + + +void Timer::restart() +{ + _lastTime = millis(); +} + +void Timer::begin(const uint32_t interval) +{ + time(interval); + restart(); +} + +bool Timer::available() +{ + if (_time == 0) + { + return false; + } + + uint32_t actualTime = millis(); + uint32_t deltaTime = actualTime - _lastTime; + if (deltaTime >= _time) + { + return true; + } + + return false; +} + +uint32_t Timer::time() +{ + if (_time == 0) + { + return 0; + } + + uint32_t actualTime = millis(); + uint32_t deltaTime = actualTime - _lastTime; + + return _time - deltaTime; +} + +void Timer::time(const uint32_t interval) +{ + _time = interval; +} diff --git a/Timers/Timers.h b/Timers/Timers.h new file mode 100644 index 0000000..0fc0965 --- /dev/null +++ b/Timers/Timers.h @@ -0,0 +1,26 @@ +#ifndef _Timers_h +#define _Timers_h + +#include +#include + +#define SECS(t) (unsigned long) (t * 1000) +#define MINS(t) SECS(t) * 60 +#define HOURS(t) MINS(t) * 60 +#define STOP 0 + +class Timer +{ +private: + uint32_t _time; + uint32_t _lastTime; + +public: + void begin(const uint32_t); + void restart(); + bool available(); + uint32_t time(); + void time(const uint32_t); +}; + +#endif diff --git a/Timers/examples/analog_beeper/analog_beeper.ino b/Timers/examples/analog_beeper/analog_beeper.ino new file mode 100644 index 0000000..0e8b978 --- /dev/null +++ b/Timers/examples/analog_beeper/analog_beeper.ino @@ -0,0 +1,34 @@ +// Głośnik podłączony do pinu 3 genetuje przerywane sygnały dźwiękowe +// Sygnały są tym częstsze, im wyższe jest napięcie na wejściu analogowym A0 +// Autor: Łukasz Tretyn - http://nettigo.pl + +#include + +// config +const byte SPEAKER_PIN = 3; +const byte ANALOG_IN_PIN = 0; +const unsigned long MAX_BEEP_CYCLE = SECS(1); + +Timer beeperTimer; + +void beeperSetup() { + beeperTimer.begin(STOP); +} + +void beeperUpdate() { + word analogData = analogRead(ANALOG_IN_PIN); + unsigned long beepTime = map(analogData, 0, 1023, 0, MAX_BEEP_CYCLE); + beeperTimer.time(beepTime); + if (beeperTimer.available()) { + beeperTimer.restart(); + tone(SPEAKER_PIN, 500, beepTime / 2); + } +} + +void setup() { + beeperSetup(); +} + +void loop() { + beeperUpdate(); +} diff --git a/Timers/examples/auto_light_timer/auto_light_timer.ino b/Timers/examples/auto_light_timer/auto_light_timer.ino new file mode 100644 index 0000000..bf2e8d2 --- /dev/null +++ b/Timers/examples/auto_light_timer/auto_light_timer.ino @@ -0,0 +1,51 @@ +// Automatyczny wyłącznik światła +// 5 niezależnych kanałów +// Świeci przez 1 minutę od wciśniecia przycisku +// Wejścia z rezystorem podciągającym dla przycisków/czujników podczerwieni +// na pinach cyfrowych 2, 3, 4, 5, 6 +// Cyfrowe wyjścia dla tranzystorów MOSFET/Przekaźników +// na pinach cyfrowych 7, 8, 9, 10, 11 +// Autor: Łukasz Tretyn - http://nettigo.pl + +#include + +// Config +const byte LIGHTS_NUM = 5; +const byte switchPins[LIGHTS_NUM] = {2, 3, 4, 5, 6}; +const byte lightPins[LIGHTS_NUM] = {7, 8, 9, 10, 11}; +const unsigned long LIGHTS_ON_TIME = MINS(1); +const byte LIGHT_ON_STATE = HIGH; +const byte SWITCH_ON_STATE = LOW; + +Timer lightTimers[LIGHTS_NUM]; + +void lightSetup() { + for (byte i=0; i + +Timer ledBlinkTimer; + +void setup() { + pinMode(LED_BUILTIN, OUTPUT); + ledBlinkTimer.begin(SECS(2)); +} + +void loop() { + if (ledBlinkTimer.available()) + { + digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); + ledBlinkTimer.restart(); + } +} diff --git a/Timers/examples/smooth_blink/smooth_blink.ino b/Timers/examples/smooth_blink/smooth_blink.ino new file mode 100644 index 0000000..2d7e6c9 --- /dev/null +++ b/Timers/examples/smooth_blink/smooth_blink.ino @@ -0,0 +1,62 @@ +// Płynnie zapala i gasi diodę świecącą podłączoną do pinu PWM 3 +// Autor: Łukasz Tretyn - http://nettigo.pl + +#include + +const byte PWM_LED = 3; +const unsigned long FADE_IN_TIME = SECS(2.25); +const unsigned long FADE_OUT_TIME = SECS(1.8); +const unsigned long CYCLE_DELAY_TIME = SECS(0.75); + +Timer timer; + +enum class BlinkState : byte { + DELAY, + FADE_IN, + FADE_OUT +} state; + +void blinkSetup() { + pinMode(PWM_LED, OUTPUT); + timer.begin(CYCLE_DELAY_TIME); +} + +void blinkUpdate() { + byte brightness; + + switch (state) { + case BlinkState::DELAY: + if (timer.available()) { + state = BlinkState::FADE_IN; + timer.begin(FADE_IN_TIME); + } + break; + + case BlinkState::FADE_IN: + if (timer.available()) { + state = BlinkState::FADE_OUT; + timer.begin(FADE_OUT_TIME); + } + brightness = map(timer.time(), FADE_IN_TIME, 0, 0, 255); + break; + + case BlinkState::FADE_OUT: + if (timer.available()) { + state = BlinkState::DELAY; + timer.begin(CYCLE_DELAY_TIME); + } + brightness = map(timer.time(), FADE_OUT_TIME, 0, 255, 0); + break; + } + + analogWrite(PWM_LED, brightness); +} + +void setup() { + blinkSetup(); +} + +void loop() { + blinkUpdate(); +} + diff --git a/Timers/keywords.txt b/Timers/keywords.txt new file mode 100644 index 0000000..4a1a901 --- /dev/null +++ b/Timers/keywords.txt @@ -0,0 +1,11 @@ +Timer KEYWORD1 + +begin KEYWORD2 +restart KEYWORD2 +available KEYWORD2 +time KEYWORD2 + +SECS LITERAL1 +MINS LITERAL1 +HOURS LITERAL1 +STOP LITERAL1 diff --git a/Timers/library.properties b/Timers/library.properties new file mode 100644 index 0000000..a3f14c4 --- /dev/null +++ b/Timers/library.properties @@ -0,0 +1,9 @@ +name=Timers +version=16.4.0 +author=Łukasz Tretyn, Nettigo +maintainer=Łukasz Tretyn +sentence=Program controlled by time +paragraph=Your program will be able to run multiple tasks at a specific time +category=time,timeout +url=https://github.com/nettigo/Timers +architectures=avr,sam,samd diff --git a/Timers/readme.md b/Timers/readme.md new file mode 100644 index 0000000..ad1d1d0 --- /dev/null +++ b/Timers/readme.md @@ -0,0 +1,164 @@ +#Timers - program Arduino sterowany czasem + +Wersja: 16.4.0 + +Autor: Łukasz Tretyn, [Nettigo.pl](http://nettigo.pl) + +Biblioteka umożliwia: + +- Budowanie programów robiących wiele rzeczy na raz +- Uruchamiane różnych części programu o różnym czasie +- Timeout - określanie czasu na wykonanie zadania +- Timestamp - sterowanie algorytmem przez upływający czas +- Miałczenie jak kot + +Biblioteka nie blokuje programu, tak jak funkcje `delay()` i `delayMicrosoconds()`. + +## Instalacja biblioteki + +Bibliotekę należy pobrać i umieścić w katalogu libraries, który znajduje się w folderze ze szicami Arduino. + +[Strona projektu biblioteki Timers na Github](https://github.com/nettigo/Timers) + +## Referencja biblioteki + +### Dodawanie biblioteki do programu + +```Arduino +#include +``` + +Tę linię należy dodać na początku programu by skorzystać z bilioteki Timers. + +### Klasa i tworzenie obiektu + +```Arduino +Timer timer; +``` + +Główna klasa biblioteki nazwya się `Timer`. +W przykładzie utworzyłem z niej obiekt o nazwie `timer`. +Jeśli potrzebujesz więcej timerów, możesz stworzyć kilka obiektów o różnych nazwach albo ich tablicę. + +### Metody + +#### begin + +```Arduino +timer.begin(300); +``` + +`begin` rozpoczyna odliczanie timera od nowa, z nowym czasem do odliczenia. + +Argument przyjmuje wartości w milisekundach. + +W przykładzie obiekt `timer` będzie odliczał czas 300 milisekund od chwili wywołania metody `begin`. + +#### available + +```Arduino +bool isTimeEnd = timer.available(); +``` + +`available` zwraca wartość `true` jeśli upłynął już czas, lub `false` jeśli nie upłynął. + +Wartość jest typu `bool`. + +W przykładzie metoda `available` obiektu `timer` zapisuje czy upłynął już czas, do zmiennej `isTimeEnd`. + +#### restart + +```Arduino +timer.restart(); +``` + +`restart` rozpoczyna odliczanie czasu od nowa. + +#### time + +```Arduino +timer.time(600); +``` + +`time` ustawia nowy czas do odliczania, bez rozpoczynania odliczania od nowa. + +Ardument przyjmuje wartości w milisekundach. + +W przykładzie czas do odliczenia został zmieniony na 600 milisekund. + +#### time + +```Arduino +unsigned long timeToEnd = timer.time(); +``` + +`time` (bez argumentów) podaje ile jeszcze zostało czasu do końca odliczania. + +Czas jest zwracany w milisekundach. + +W przykładzie pozostały czas pracy `timer` został zapisany w zmiennej `timeToEnd`. + +### Elementy pomocnicze + +#### SECS + +```Arduino +unsigned long timeoutTime = SECS(40); +``` + +`SECS` zamienia czas w sekundach na milisekundy. + +#### MINS + +```Arduino +unsigned long timeoutTime = MINS(20); +``` + +`MINS` zamienia czas w minutach na milisekundy. + +#### HOURS + +```Arduino +unsigned long timeoutTime = HOURS(2); +``` + +`HOURS` zamienia czas w godzinach na milisekundy. + +#### STOP + +```Arduino +timer.time(STOP); +``` + +`STOP` zatrzymuje działanie timera. + +## Przykład: + +Nieodzowny przykład migania diodą: + +```Arduino +#include + +Timer ledBlinkTimer; + +void setup() { + pinMode(LED_BUILTIN, OUTPUT); + ledBlinkTimer.begin(SECS(2)); +} + +void loop() { + if (ledBlinkTimer.available()) + { + digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); + ledBlinkTimer.restart(); + } +} +``` + +Program miga wbudowaną w arduino diodą świecącą "L". + +- Na początku za pomocą `include` ładowana jest biblioteka Timers +- Potem tworzony jest obiekt `ledBlinkTimer` +- W funkcji `setup` ustawiane jest odliczanie 2 sekund za pomocą `begin` i `SECS` +- W funkcji `loop` warunek `if` sprawdza czy upłynął już czas. Obwieści to metoda `available` +- Gdy warunek się spełni dioda zmieni swój stan na przeciwny, a timer zostanie zresetowany za pomocą metody `restart`, żeby odliczał czas od nowa. diff --git a/examples/timers_10s_blink/timers_10s_blink.ino b/examples/timers_10s_blink/timers_10s_blink.ino deleted file mode 100644 index ed6e66a..0000000 --- a/examples/timers_10s_blink/timers_10s_blink.ino +++ /dev/null @@ -1,38 +0,0 @@ -// ** Program mrugający diodą podłączoną do pinu 13 przez 10 sekund ** - -// Ładowanie biblioteki Timers -#include - -// Zmienna przechowująca numer timera mrugającego diodą -byte blinker; - -void setup() -{ - pinMode(13, OUTPUT); - - // Wywoływanie funkcji "toggle" co 500 ms - blinker = Timers.every(500, toggle); - - // Wywołanie funkcji "blinkOff" za 10 s - Timers.after(10000, blinkOff); -} - -void loop() -{ - // Obliczanie czasu - Timers.process(); -} - -// Funkcja mrugająca diodą -void toggle() -{ - digitalWrite(13, !digitalRead(13)); -} - -// Funkcja wyłączająca mruganie -void blinkOff() -{ - // Usunięcie timera o numerze przechowywanym w zmiennej "blinker" - Timers.clear(blinker); -} - diff --git a/examples/timers_blink/timers_blink.ino b/examples/timers_blink/timers_blink.ino deleted file mode 100644 index edc1894..0000000 --- a/examples/timers_blink/timers_blink.ino +++ /dev/null @@ -1,23 +0,0 @@ -// ** Program mrugający diodą podłączoną do pinu 13 ** - -// Ładowanie biblioteki Timers -#include - -void setup() -{ - pinMode(13, OUTPUT); - // Wywoływanie funkcji "toggle" co 500 ms - Timers.every(500, toggle); -} - -void loop() -{ - // Obliczanie czasu - Timers.process(); -} - -// Funkcja mrugająca diodą -void toggle() -{ - digitalWrite(13, !digitalRead(13)); -} diff --git a/keywords.txt b/keywords.txt deleted file mode 100644 index 6ec2aaf..0000000 --- a/keywords.txt +++ /dev/null @@ -1,20 +0,0 @@ -####################################### -# Syntax Coloring Map Timers -####################################### - -####################################### -# Datatypes (KEYWORD1) -####################################### - -Timers KEYWORD3 - -####################################### -# Methods and Functions (KEYWORD2) -####################################### -every KEYWORD2 -after KEYWORD2 -clear KEYWORD2 -reset KEYWORD2 -update KEYWORD2 -length KEYWORD2 -process KEYWORD2 From 2dc423fbaecee3150375b23a4c83b48867b9e88d Mon Sep 17 00:00:00 2001 From: Witold Rugowski Date: Thu, 27 Oct 2016 15:17:02 +0200 Subject: [PATCH 06/14] =?UTF-8?q?zmiana=20struktury,=20by=20Arduino=20IDE?= =?UTF-8?q?=20importowa=C5=82o=20.zip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Timers/Timers.cpp => Timers.cpp | 0 Timers/Timers.h => Timers.h | 0 {Timers/examples => examples}/analog_beeper/analog_beeper.ino | 0 .../examples => examples}/auto_light_timer/auto_light_timer.ino | 0 {Timers/examples => examples}/blink/blink.ino | 0 {Timers/examples => examples}/smooth_blink/smooth_blink.ino | 0 Timers/keywords.txt => keywords.txt | 0 Timers/library.properties => library.properties | 0 Timers/readme.md => readme.md | 0 9 files changed, 0 insertions(+), 0 deletions(-) rename Timers/Timers.cpp => Timers.cpp (100%) rename Timers/Timers.h => Timers.h (100%) rename {Timers/examples => examples}/analog_beeper/analog_beeper.ino (100%) rename {Timers/examples => examples}/auto_light_timer/auto_light_timer.ino (100%) rename {Timers/examples => examples}/blink/blink.ino (100%) rename {Timers/examples => examples}/smooth_blink/smooth_blink.ino (100%) rename Timers/keywords.txt => keywords.txt (100%) rename Timers/library.properties => library.properties (100%) rename Timers/readme.md => readme.md (100%) diff --git a/Timers/Timers.cpp b/Timers.cpp similarity index 100% rename from Timers/Timers.cpp rename to Timers.cpp diff --git a/Timers/Timers.h b/Timers.h similarity index 100% rename from Timers/Timers.h rename to Timers.h diff --git a/Timers/examples/analog_beeper/analog_beeper.ino b/examples/analog_beeper/analog_beeper.ino similarity index 100% rename from Timers/examples/analog_beeper/analog_beeper.ino rename to examples/analog_beeper/analog_beeper.ino diff --git a/Timers/examples/auto_light_timer/auto_light_timer.ino b/examples/auto_light_timer/auto_light_timer.ino similarity index 100% rename from Timers/examples/auto_light_timer/auto_light_timer.ino rename to examples/auto_light_timer/auto_light_timer.ino diff --git a/Timers/examples/blink/blink.ino b/examples/blink/blink.ino similarity index 100% rename from Timers/examples/blink/blink.ino rename to examples/blink/blink.ino diff --git a/Timers/examples/smooth_blink/smooth_blink.ino b/examples/smooth_blink/smooth_blink.ino similarity index 100% rename from Timers/examples/smooth_blink/smooth_blink.ino rename to examples/smooth_blink/smooth_blink.ino diff --git a/Timers/keywords.txt b/keywords.txt similarity index 100% rename from Timers/keywords.txt rename to keywords.txt diff --git a/Timers/library.properties b/library.properties similarity index 100% rename from Timers/library.properties rename to library.properties diff --git a/Timers/readme.md b/readme.md similarity index 100% rename from Timers/readme.md rename to readme.md From 21d0656ca23821ec1fc27d4e931ff17318ea7dde Mon Sep 17 00:00:00 2001 From: Witold Rugowski Date: Thu, 27 Oct 2016 15:23:50 +0200 Subject: [PATCH 07/14] Dodana licencja MIT --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1260d10 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 Łukasz Tretyn + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From d55e0c31c9f943cae8dc17ff0460d7969e86b592 Mon Sep 17 00:00:00 2001 From: Witold Rugowski Date: Fri, 14 Apr 2017 13:06:03 +0200 Subject: [PATCH 08/14] Library category set, to prevent warnings from Arduino IDE --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index a3f14c4..3a62c61 100644 --- a/library.properties +++ b/library.properties @@ -4,6 +4,6 @@ author=Łukasz Tretyn, Nettigo maintainer=Łukasz Tretyn sentence=Program controlled by time paragraph=Your program will be able to run multiple tasks at a specific time -category=time,timeout +category=Timing url=https://github.com/nettigo/Timers architectures=avr,sam,samd From 7d737db9070c09f3f8d52c0cf1cb8b3cba067bcb Mon Sep 17 00:00:00 2001 From: Witold Rugowski Date: Fri, 14 Apr 2017 13:07:22 +0200 Subject: [PATCH 09/14] Fixed markdown --- readme.md | 2 +- readme.md~ | 164 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 readme.md~ diff --git a/readme.md b/readme.md index ad1d1d0..fb56dd9 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -#Timers - program Arduino sterowany czasem +# Timers - program Arduino sterowany czasem Wersja: 16.4.0 diff --git a/readme.md~ b/readme.md~ new file mode 100644 index 0000000..ad1d1d0 --- /dev/null +++ b/readme.md~ @@ -0,0 +1,164 @@ +#Timers - program Arduino sterowany czasem + +Wersja: 16.4.0 + +Autor: Łukasz Tretyn, [Nettigo.pl](http://nettigo.pl) + +Biblioteka umożliwia: + +- Budowanie programów robiących wiele rzeczy na raz +- Uruchamiane różnych części programu o różnym czasie +- Timeout - określanie czasu na wykonanie zadania +- Timestamp - sterowanie algorytmem przez upływający czas +- Miałczenie jak kot + +Biblioteka nie blokuje programu, tak jak funkcje `delay()` i `delayMicrosoconds()`. + +## Instalacja biblioteki + +Bibliotekę należy pobrać i umieścić w katalogu libraries, który znajduje się w folderze ze szicami Arduino. + +[Strona projektu biblioteki Timers na Github](https://github.com/nettigo/Timers) + +## Referencja biblioteki + +### Dodawanie biblioteki do programu + +```Arduino +#include +``` + +Tę linię należy dodać na początku programu by skorzystać z bilioteki Timers. + +### Klasa i tworzenie obiektu + +```Arduino +Timer timer; +``` + +Główna klasa biblioteki nazwya się `Timer`. +W przykładzie utworzyłem z niej obiekt o nazwie `timer`. +Jeśli potrzebujesz więcej timerów, możesz stworzyć kilka obiektów o różnych nazwach albo ich tablicę. + +### Metody + +#### begin + +```Arduino +timer.begin(300); +``` + +`begin` rozpoczyna odliczanie timera od nowa, z nowym czasem do odliczenia. + +Argument przyjmuje wartości w milisekundach. + +W przykładzie obiekt `timer` będzie odliczał czas 300 milisekund od chwili wywołania metody `begin`. + +#### available + +```Arduino +bool isTimeEnd = timer.available(); +``` + +`available` zwraca wartość `true` jeśli upłynął już czas, lub `false` jeśli nie upłynął. + +Wartość jest typu `bool`. + +W przykładzie metoda `available` obiektu `timer` zapisuje czy upłynął już czas, do zmiennej `isTimeEnd`. + +#### restart + +```Arduino +timer.restart(); +``` + +`restart` rozpoczyna odliczanie czasu od nowa. + +#### time + +```Arduino +timer.time(600); +``` + +`time` ustawia nowy czas do odliczania, bez rozpoczynania odliczania od nowa. + +Ardument przyjmuje wartości w milisekundach. + +W przykładzie czas do odliczenia został zmieniony na 600 milisekund. + +#### time + +```Arduino +unsigned long timeToEnd = timer.time(); +``` + +`time` (bez argumentów) podaje ile jeszcze zostało czasu do końca odliczania. + +Czas jest zwracany w milisekundach. + +W przykładzie pozostały czas pracy `timer` został zapisany w zmiennej `timeToEnd`. + +### Elementy pomocnicze + +#### SECS + +```Arduino +unsigned long timeoutTime = SECS(40); +``` + +`SECS` zamienia czas w sekundach na milisekundy. + +#### MINS + +```Arduino +unsigned long timeoutTime = MINS(20); +``` + +`MINS` zamienia czas w minutach na milisekundy. + +#### HOURS + +```Arduino +unsigned long timeoutTime = HOURS(2); +``` + +`HOURS` zamienia czas w godzinach na milisekundy. + +#### STOP + +```Arduino +timer.time(STOP); +``` + +`STOP` zatrzymuje działanie timera. + +## Przykład: + +Nieodzowny przykład migania diodą: + +```Arduino +#include + +Timer ledBlinkTimer; + +void setup() { + pinMode(LED_BUILTIN, OUTPUT); + ledBlinkTimer.begin(SECS(2)); +} + +void loop() { + if (ledBlinkTimer.available()) + { + digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); + ledBlinkTimer.restart(); + } +} +``` + +Program miga wbudowaną w arduino diodą świecącą "L". + +- Na początku za pomocą `include` ładowana jest biblioteka Timers +- Potem tworzony jest obiekt `ledBlinkTimer` +- W funkcji `setup` ustawiane jest odliczanie 2 sekund za pomocą `begin` i `SECS` +- W funkcji `loop` warunek `if` sprawdza czy upłynął już czas. Obwieści to metoda `available` +- Gdy warunek się spełni dioda zmieni swój stan na przeciwny, a timer zostanie zresetowany za pomocą metody `restart`, żeby odliczał czas od nowa. From a2924eec47216defc97f8c80ee05a78b189f3408 Mon Sep 17 00:00:00 2001 From: Witold Rugowski Date: Fri, 14 Apr 2017 13:12:07 +0200 Subject: [PATCH 10/14] Bumped library version --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 3a62c61..9beee64 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Timers -version=16.4.0 +version=16.4.1 author=Łukasz Tretyn, Nettigo maintainer=Łukasz Tretyn sentence=Program controlled by time From 77a17eb18bf1060e0755bee1407fa764fd8f6192 Mon Sep 17 00:00:00 2001 From: Witold Rugowski Date: Fri, 14 Apr 2017 13:14:16 +0200 Subject: [PATCH 11/14] clean up --- readme.md~ | 164 ----------------------------------------------------- 1 file changed, 164 deletions(-) delete mode 100644 readme.md~ diff --git a/readme.md~ b/readme.md~ deleted file mode 100644 index ad1d1d0..0000000 --- a/readme.md~ +++ /dev/null @@ -1,164 +0,0 @@ -#Timers - program Arduino sterowany czasem - -Wersja: 16.4.0 - -Autor: Łukasz Tretyn, [Nettigo.pl](http://nettigo.pl) - -Biblioteka umożliwia: - -- Budowanie programów robiących wiele rzeczy na raz -- Uruchamiane różnych części programu o różnym czasie -- Timeout - określanie czasu na wykonanie zadania -- Timestamp - sterowanie algorytmem przez upływający czas -- Miałczenie jak kot - -Biblioteka nie blokuje programu, tak jak funkcje `delay()` i `delayMicrosoconds()`. - -## Instalacja biblioteki - -Bibliotekę należy pobrać i umieścić w katalogu libraries, który znajduje się w folderze ze szicami Arduino. - -[Strona projektu biblioteki Timers na Github](https://github.com/nettigo/Timers) - -## Referencja biblioteki - -### Dodawanie biblioteki do programu - -```Arduino -#include -``` - -Tę linię należy dodać na początku programu by skorzystać z bilioteki Timers. - -### Klasa i tworzenie obiektu - -```Arduino -Timer timer; -``` - -Główna klasa biblioteki nazwya się `Timer`. -W przykładzie utworzyłem z niej obiekt o nazwie `timer`. -Jeśli potrzebujesz więcej timerów, możesz stworzyć kilka obiektów o różnych nazwach albo ich tablicę. - -### Metody - -#### begin - -```Arduino -timer.begin(300); -``` - -`begin` rozpoczyna odliczanie timera od nowa, z nowym czasem do odliczenia. - -Argument przyjmuje wartości w milisekundach. - -W przykładzie obiekt `timer` będzie odliczał czas 300 milisekund od chwili wywołania metody `begin`. - -#### available - -```Arduino -bool isTimeEnd = timer.available(); -``` - -`available` zwraca wartość `true` jeśli upłynął już czas, lub `false` jeśli nie upłynął. - -Wartość jest typu `bool`. - -W przykładzie metoda `available` obiektu `timer` zapisuje czy upłynął już czas, do zmiennej `isTimeEnd`. - -#### restart - -```Arduino -timer.restart(); -``` - -`restart` rozpoczyna odliczanie czasu od nowa. - -#### time - -```Arduino -timer.time(600); -``` - -`time` ustawia nowy czas do odliczania, bez rozpoczynania odliczania od nowa. - -Ardument przyjmuje wartości w milisekundach. - -W przykładzie czas do odliczenia został zmieniony na 600 milisekund. - -#### time - -```Arduino -unsigned long timeToEnd = timer.time(); -``` - -`time` (bez argumentów) podaje ile jeszcze zostało czasu do końca odliczania. - -Czas jest zwracany w milisekundach. - -W przykładzie pozostały czas pracy `timer` został zapisany w zmiennej `timeToEnd`. - -### Elementy pomocnicze - -#### SECS - -```Arduino -unsigned long timeoutTime = SECS(40); -``` - -`SECS` zamienia czas w sekundach na milisekundy. - -#### MINS - -```Arduino -unsigned long timeoutTime = MINS(20); -``` - -`MINS` zamienia czas w minutach na milisekundy. - -#### HOURS - -```Arduino -unsigned long timeoutTime = HOURS(2); -``` - -`HOURS` zamienia czas w godzinach na milisekundy. - -#### STOP - -```Arduino -timer.time(STOP); -``` - -`STOP` zatrzymuje działanie timera. - -## Przykład: - -Nieodzowny przykład migania diodą: - -```Arduino -#include - -Timer ledBlinkTimer; - -void setup() { - pinMode(LED_BUILTIN, OUTPUT); - ledBlinkTimer.begin(SECS(2)); -} - -void loop() { - if (ledBlinkTimer.available()) - { - digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); - ledBlinkTimer.restart(); - } -} -``` - -Program miga wbudowaną w arduino diodą świecącą "L". - -- Na początku za pomocą `include` ładowana jest biblioteka Timers -- Potem tworzony jest obiekt `ledBlinkTimer` -- W funkcji `setup` ustawiane jest odliczanie 2 sekund za pomocą `begin` i `SECS` -- W funkcji `loop` warunek `if` sprawdza czy upłynął już czas. Obwieści to metoda `available` -- Gdy warunek się spełni dioda zmieni swój stan na przeciwny, a timer zostanie zresetowany za pomocą metody `restart`, żeby odliczał czas od nowa. From 7020b54249cb204d6bd80615f44c44ab5184e744 Mon Sep 17 00:00:00 2001 From: Witold Rugowski Date: Fri, 14 Apr 2017 13:14:41 +0200 Subject: [PATCH 12/14] no more ~ files --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ From ca35a72b39afcbb3b1c0304a6af5a3b92f43096c Mon Sep 17 00:00:00 2001 From: Witold Rugowski Date: Fri, 14 Apr 2017 13:18:30 +0200 Subject: [PATCH 13/14] Lib version in desc fixed --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index fb56dd9..fa06f87 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # Timers - program Arduino sterowany czasem -Wersja: 16.4.0 +Wersja: 16.4.1 Autor: Łukasz Tretyn, [Nettigo.pl](http://nettigo.pl) From 651b205ab6b6ac64ab08983f2ecf158459c19d16 Mon Sep 17 00:00:00 2001 From: Witold Rugowski Date: Thu, 28 Apr 2022 13:39:46 +0200 Subject: [PATCH 14/14] Update readme.md Typo fixed --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index fa06f87..55f2c73 100644 --- a/readme.md +++ b/readme.md @@ -16,7 +16,7 @@ Biblioteka nie blokuje programu, tak jak funkcje `delay()` i `delayMicrosoconds( ## Instalacja biblioteki -Bibliotekę należy pobrać i umieścić w katalogu libraries, który znajduje się w folderze ze szicami Arduino. +Bibliotekę należy pobrać i umieścić w katalogu libraries, który znajduje się w folderze ze szkicami Arduino. [Strona projektu biblioteki Timers na Github](https://github.com/nettigo/Timers)