diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ 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. diff --git a/Timers.cpp b/Timers.cpp index eb77172..23351c4 100644 --- a/Timers.cpp +++ b/Timers.cpp @@ -1,51 +1,48 @@ #include "Timers.h" -void NOP(void) + +void Timer::restart() { - return; + _lastTime = millis(); } -Timers::Timers(void) +void Timer::begin(const uint32_t interval) { - for (int i=0; i= _time) + { + return true; + } + + return false; } -void Timers::setInterval(byte slot, unsigned long interval) +uint32_t Timer::time() { - _elements[slot].interval = interval; - _elements[slot].begin_time = millis(); + if (_time == 0) + { + return 0; + } + + uint32_t actualTime = millis(); + uint32_t deltaTime = actualTime - _lastTime; + + return _time - deltaTime; } -void Timers::process(void) +void Timer::time(const uint32_t interval) { - unsigned long actual_time = millis(); - - for (int i=0; i 0 && delta_time >= _elements[i].interval) - { - _elements[i].func(); - _elements[i].begin_time = actual_time; - } - } + _time = interval; } - diff --git a/Timers.h b/Timers.h index abc953f..0fc0965 100644 --- a/Timers.h +++ b/Timers.h @@ -1,36 +1,26 @@ -#include "Arduino.h" +#ifndef _Timers_h +#define _Timers_h -#ifndef timers_h -#define timers_h - -#ifndef TIMER_ITEMS -#define TIMER_ITEMS 8 -#endif - -typedef void (*timerFunc)(void); - - -void NOP(void); - - -struct TimerElement -{ - timerFunc func; - unsigned long interval; - unsigned long begin_time; -}; +#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 Timers +class Timer { - 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: + 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/examples/analog_beeper/analog_beeper.ino b/examples/analog_beeper/analog_beeper.ino new file mode 100644 index 0000000..0e8b978 --- /dev/null +++ b/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/examples/auto_light_timer/auto_light_timer.ino b/examples/auto_light_timer/auto_light_timer.ino new file mode 100644 index 0000000..bf2e8d2 --- /dev/null +++ b/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/examples/smooth_blink/smooth_blink.ino b/examples/smooth_blink/smooth_blink.ino new file mode 100644 index 0000000..2d7e6c9 --- /dev/null +++ b/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/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..4a1a901 100644 --- a/keywords.txt +++ b/keywords.txt @@ -1,24 +1,11 @@ -####################################### -# Syntax Coloring Map Timers -####################################### +Timer KEYWORD1 -####################################### -# Datatypes (KEYWORD1) -####################################### - -Timers KEYWORD1 - -####################################### -# Methods and Functions (KEYWORD2) -####################################### -attach KEYWORD2 -setInterval KEYWORD2 -process KEYWORD2 - -####################################### -# Constants (LITERAL1) -####################################### - -NOP LITERAL1 -TIMER_ITEMS LITERAL1 +begin KEYWORD2 +restart KEYWORD2 +available KEYWORD2 +time KEYWORD2 +SECS LITERAL1 +MINS LITERAL1 +HOURS LITERAL1 +STOP LITERAL1 diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..9beee64 --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=Timers +version=16.4.1 +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=Timing +url=https://github.com/nettigo/Timers +architectures=avr,sam,samd diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..55f2c73 --- /dev/null +++ b/readme.md @@ -0,0 +1,164 @@ +# Timers - program Arduino sterowany czasem + +Wersja: 16.4.1 + +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 szkicami 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.