0.1.5 PulsePattern

This commit is contained in:
rob tillaart 2022-03-11 10:36:49 +01:00
parent 4ad989246c
commit 0022bf07bc
7 changed files with 97 additions and 14 deletions

View File

@ -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;
}
}
}

View File

@ -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 @@ public:
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 @@ private:
volatile uint8_t _level;
volatile int8_t _state;
volatile uint8_t _cnt;
uint32_t _times = PP_CONTINUOUS;
};
extern PulsePattern PPGenerator;

View File

@ -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 ...

View File

@ -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 --

View File

@ -26,4 +26,7 @@ PRESCALE_256 LITERAL1
PRESCALE_1024 LITERAL1
EXT_T1_FALLING LITERAL1
EXT_T2_RISING LITERAL1
EXT_T2_RISING LITERAL1
PP_ONCE LITERAL1
PP_CONTINUOUS LITERAL1

View File

@ -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",

View File

@ -1,5 +1,5 @@
name=PulsePattern
version=0.1.4
version=0.1.5
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library to generate repeating pulse patterns. (AVR only)