From 1c8c1de2e9cd94a80c33feca2c8f28f360f90c5d Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Fri, 11 Mar 2022 10:32:02 +0100 Subject: [PATCH] fix #11 add repeat count to start() (#12) * fix #11 add repeat count to start() * add PP_ONCE and PP_CONTINUOUS constants --- PulsePattern.cpp | 28 ++++++++++-- PulsePattern.h | 16 ++++--- README.md | 7 ++- examples/SOS_demo2_once/SOS_demo2_once.ino | 51 ++++++++++++++++++++++ keywords.txt | 5 ++- library.json | 2 +- library.properties | 2 +- 7 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 examples/SOS_demo2_once/SOS_demo2_once.ino diff --git a/PulsePattern.cpp b/PulsePattern.cpp index 593e57c..8c04e3d 100644 --- a/PulsePattern.cpp +++ b/PulsePattern.cpp @@ -1,7 +1,7 @@ // // FILE: PulsePattern.cpp // AUTHOR: Rob dot Tillaart at gmail dot com -// VERSION: 0.1.4 +// VERSION: 0.1.5 // DATE: 2012-11-23 // PURPOSE: Arduino Library to generate repeating pulse patterns // @@ -19,6 +19,8 @@ // 0.1.2 2020-08-07 speed up toggle pin + get/setFactor() // 0.1.3 2021-01-06 Arduino-CI (no unit test) // 0.1.4 2021-12-24 update library.json, license, minor edits +// 0.1.5 2022-03-10 add times to start(), 0 = continuous mode + #include "PulsePattern.h" @@ -26,6 +28,7 @@ // Predefined generator (singleton) PulsePattern PPGenerator; + ISR(TIMER1_COMPA_vect) { PPGenerator.worker(); @@ -67,10 +70,11 @@ const uint8_t level, const uint8_t prescaler) } -void PulsePattern::start() +void PulsePattern::start(uint32_t times) { if (_state == RUNNING) return; // no restart - _cnt = 0; // start from begin + _cnt = 0; // start from begin + _times = times; cont(); } @@ -112,7 +116,23 @@ void PulsePattern::worker() OCR1A = _ar[_cnt] * (F_CPU/1000000UL); } _cnt++; - if (_cnt >= _size) _cnt = 0; // repeat pattern + if (_cnt >= _size) + { + _cnt = 0; // reset pattern + switch(_times) + { + case PP_CONTINUOUS: + break; + case 1: + _times--; + case 0: + stop(); + break; + default: + _times--; + break; + } + } } diff --git a/PulsePattern.h b/PulsePattern.h index 4fca43e..ea9b279 100644 --- a/PulsePattern.h +++ b/PulsePattern.h @@ -2,7 +2,7 @@ // // FILE: PulsePattern.h // AUTHOR: Rob dot Tillaart at gmail dot com -// VERSION: 0.1.4 +// VERSION: 0.1.5 // DATE: 2012-11-23 // PURPOSE: Arduino Library to generate repeating pulse patterns // sends a pulse pattern to a digital pin (continuously) @@ -17,14 +17,14 @@ #include "Arduino.h" -#define PULSEPATTERN_LIB_VERSION (F("0.1.4")) - +#define PULSEPATTERN_LIB_VERSION (F("0.1.5")) +// RUNNING STATES #define NOTINIT -1 #define STOPPED 0 #define RUNNING 1 - +// PRESCALER CONSTANTS #define NO_CLOCK 0 // timer off #define PRESCALE_1 1 #define PRESCALE_8 2 @@ -34,6 +34,10 @@ #define EXT_T1_FALLING 6 // external clock #define EXT_T2_RISING 7 // external clock +// RUNNING MODE +#define PP_ONCE 1 +#define PP_CONTINUOUS 0xFFFFFFFF + class PulsePattern { @@ -46,7 +50,7 @@ class PulsePattern void setFactor(float perc) { _factor = round(4096 * (1 + perc)); }; float getFactor() { return _factor / 4096.0 - 1; }; - void start(); + void start(uint32_t times = PP_CONTINUOUS); void cont(); void stop(); bool isRunning() const { return _state == RUNNING; }; @@ -67,6 +71,8 @@ class PulsePattern volatile uint8_t _level; volatile int8_t _state; volatile uint8_t _cnt; + + uint32_t _times = PP_CONTINUOUS; }; extern PulsePattern PPGenerator; diff --git a/README.md b/README.md index 3f17a76..b87d09a 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,9 @@ Use with care. - **void setFactor(float perc)** percentage = factor to correct timing (relative). - **float getFactor()** get the internal used factor. Due to rounding it can be slightly different. - **void stop()** stop the pattern generator -- **void start()** start the pattern generator +- **void start(uint32_t times = PP_CONTINUOUS)** start the pattern generator. +Default in the continuous mode to be backwards compatible. +**PP_CONTINUOUS** == 0xFFFFFFFF, so times should be less than 4294967295. For convenience there is a **PP_ONCE** == 1 defined. - **void cont()** continue the pattern generator from the last stopped place (approx). - **bool isRunning()** status indicator - **void worker()** must be public otherwise the ISR cannot call it. @@ -74,5 +76,6 @@ See examples. - ESP32 variant of this class (base class -> AVR -> ESP class) - pulse recorder class to record / generate patterns - +- add interval between patterns? + - or is this just LOW/HIGH for a certain time. if time permits ... diff --git a/examples/SOS_demo2_once/SOS_demo2_once.ino b/examples/SOS_demo2_once/SOS_demo2_once.ino new file mode 100644 index 0000000..b8145a3 --- /dev/null +++ b/examples/SOS_demo2_once/SOS_demo2_once.ino @@ -0,0 +1,51 @@ +// +// FILE: SOS_demo2_once.ino +// AUTHOR: Rob Tillaart +// DATE: 2022-03-10 +// PURPOSE: demo of the PulsePattern Library +// uses timer1 + + +#include "PulsePattern.h" + + +// a pattern consists of durations of LOW and HIGH periods +// so the first line of the SOSpattern is +// 500 units LOW, 500 units HIGH etc +// for a duty cycle of 50% LOW and HIGH should have equal periods +// +// NOTE max period = 4095. +// min period = about 12 +uint16_t SOSpattern[] = +{ + 500,500,500,500,500,1500, // SOS in morse + 1500,500,1500,500,1500,1500, + 500,500,500,500,500,1500 +}; + + +uint8_t patternSize = 18; +uint8_t startLevel = LOW; + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + + // as the prescaler = 1024 the periods of the pattern are a + // few percent less than a millisecond + PPGenerator.init(13, SOSpattern, patternSize, startLevel, PRESCALE_1024); + PPGenerator.start(1); +} + + +void loop() +{ + // dummy code + Serial.println(millis()); + delay(1000); +} + + +// -- END OF FILE -- diff --git a/keywords.txt b/keywords.txt index e2ee48b..4f355bd 100644 --- a/keywords.txt +++ b/keywords.txt @@ -26,4 +26,7 @@ PRESCALE_256 LITERAL1 PRESCALE_1024 LITERAL1 EXT_T1_FALLING LITERAL1 -EXT_T2_RISING LITERAL1 \ No newline at end of file +EXT_T2_RISING LITERAL1 + +PP_ONCE LITERAL1 +PP_CONTINUOUS LITERAL1 \ No newline at end of file diff --git a/library.json b/library.json index 42ea8bb..f87c9a0 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/PulsePattern.git" }, - "version": "0.1.4", + "version": "0.1.5", "license": "MIT", "frameworks": "arduino", "platforms": "avr", diff --git a/library.properties b/library.properties index 9058702..bd3009a 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=PulsePattern -version=0.1.4 +version=0.1.5 author=Rob Tillaart maintainer=Rob Tillaart sentence=Library to generate repeating pulse patterns. (AVR only)