Skip to content

Commit

Permalink
fix #11 add repeat count to start() (#12)
Browse files Browse the repository at this point in the history
* fix #11 add repeat count to start()
* add PP_ONCE and PP_CONTINUOUS constants
  • Loading branch information
RobTillaart authored Mar 11, 2022
1 parent 9cc4884 commit 1c8c1de
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 14 deletions.
28 changes: 24 additions & 4 deletions PulsePattern.cpp
Original file line number Diff line number Diff line change
@@ -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
//
Expand All @@ -19,13 +19,16 @@
// 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"


// Predefined generator (singleton)
PulsePattern PPGenerator;


ISR(TIMER1_COMPA_vect)
{
PPGenerator.worker();
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
}
}
}


Expand Down
16 changes: 11 additions & 5 deletions PulsePattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
{
Expand All @@ -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; };
Expand All @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ...
51 changes: 51 additions & 0 deletions examples/SOS_demo2_once/SOS_demo2_once.ino
Original file line number Diff line number Diff line change
@@ -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 --
5 changes: 4 additions & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 1c8c1de

Please sign in to comment.