diff --git a/libraries/PulsePattern/PulsePattern.cpp b/libraries/PulsePattern/PulsePattern.cpp index 9e04731f..8fa6e601 100644 --- a/libraries/PulsePattern/PulsePattern.cpp +++ b/libraries/PulsePattern/PulsePattern.cpp @@ -2,21 +2,26 @@ // FILE: PulsePattern.cpp // AUTHOR: Rob dot Tillaart at gmail dot com // VERSION: see PULSEPATTERN_LIB_VERSION in .h -// PURPOSE: PulsePatternOut library for Arduino +// PURPOSE: PulsePattern library for Arduino // // HISTORY: // 0.0.1 - 2012-11-23 initial version // 0.0.2 - 2012-11-23 adapted a static PPO // 0.0.3 - 2012-12-27 renamed to PulsePattern -// 0.0.4 - 2012-12-27 code stable enough to publish(?) +// 0.0.4 - 2012-12-27 code stable(?) enough to publish +// 0.0.5 - 2012-12-27 code cleanup+comment // // Released to the public domain // -// TODO -// - PRE 1.0 backwards compatibility +// TODO +// - fast function iso array to return the next period? +// more adaptive to e.g. sensor values. (investigate) +// - test PRE 1.0 backwards compatibility // - move code to .h file so compiler can inline? // - optimize timer code // - adjust timing to more accurate values -> setTimer() +// - worker should be private - how??? +// - test invalid array periods // #include "PulsePattern.h" @@ -27,11 +32,12 @@ #include "WProgram.h" #endif -PulsePattern PPO; +// Predefined generator (singleton) +PulsePattern PPGenerator; ISR(TIMER1_COMPA_vect) { - PPO.worker(); + PPGenerator.worker(); } PulsePattern::PulsePattern() @@ -47,6 +53,8 @@ void PulsePattern::init(uint8_t pin, uint16_t * ar, uint8_t size, _pin = pin; _ar = ar; _size = size; + // TODO: run over the array to test invalid values? + // constrain them 10-4095? _level = constrain(level, LOW, HIGH); _prescaler = constrain(prescaler, PRESCALE_1, PRESCALE_1024); _cnt = 0; @@ -82,12 +90,10 @@ void PulsePattern::worker() // set next period & flip signal _level = !_level; digitalWrite(_pin, _level); - // TODO small adjustment needed for code overhead?; inline iso call? - // for now + // TODO: adjustment needed for code overhead when micros?; // + 5.2 usec for digitalWrite // + 3 usec for settimer call - setTimer(_ar[_cnt]-8); - + OCR1A = (_ar[_cnt]) * (F_CPU/1000000L); _cnt++; if (_cnt >= _size) _cnt = 0; // repeat } @@ -104,8 +110,10 @@ void PulsePattern::setTimer(uint16_t cc) { TCCR1A = 0; TCCR1B = 0; - TCNT1 = 0; // reset counter - OCR1A = cc*16; // compare A register value; + TCNT1 = 0; // reset counter + OCR1A = cc*16; // compare A register value; + // *16 makes max period 4095 + // min period 12? // 4: CTC mode, top = OCR1A TCCR1A = _BV (COM1A1); // clear on compare @@ -114,5 +122,4 @@ void PulsePattern::setTimer(uint16_t cc) TIMSK1 = _BV (OCIE1A); // interrupt on Compare A Match } - // END OF FILE \ No newline at end of file diff --git a/libraries/PulsePattern/PulsePattern.h b/libraries/PulsePattern/PulsePattern.h index 93d3a22a..83ab53ae 100644 --- a/libraries/PulsePattern/PulsePattern.h +++ b/libraries/PulsePattern/PulsePattern.h @@ -1,18 +1,18 @@ -#ifndef Histogram_h -#define PulsePatternOut_h +#ifndef PulsePattern_h +#define PulsePattern_h // -// FILE: PulsePatternOut.h +// FILE: PulsePattern.h // AUTHOR: Rob dot Tillaart at gmail dot com -// PURPOSE: PulsePatternOut library for Arduino +// PURPOSE: PulsePattern library for Arduino // sends a pulse pattern to a digital pin (continuously) -// HISTORY: See PulsePatternOut.cpp +// HISTORY: See PulsePattern.cpp // // Released to the public domain // #include -#define PULSEPATTERN_LIB_VERSION "0.0.4" +#define PULSEPATTERN_LIB_VERSION "0.0.5" #define NOTINIT -1 #define STOPPED 0 @@ -50,7 +50,7 @@ private: volatile uint8_t _cnt; }; -extern PulsePattern PPO; +extern PulsePattern PPGenerator; #endif // END OF FILE \ No newline at end of file diff --git a/libraries/PulsePattern/examples/SOS_demo2/SOS_demo2.ino b/libraries/PulsePattern/examples/SOS_demo2/SOS_demo2.ino new file mode 100644 index 00000000..87649762 --- /dev/null +++ b/libraries/PulsePattern/examples/SOS_demo2/SOS_demo2.ino @@ -0,0 +1,48 @@ +// +// FILE: SOS_demo2.pde +// AUTHOR: Rob Tillaart +// DATE: 2012-11-23 +// +// PUPROSE: 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 dutycycle 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 }; + +uint16_t XYZpattern[] = { + 100,100,100,100,100,100, // SOS in morse + 500,500,500,500,500,500, + 1000,1000,1000,1000,1000,1000 }; + +uint8_t patternSize = 18; +uint8_t startLevel = LOW; + +void setup() +{ + Serial.begin(9600); + Serial.println("Start PulsePattern"); + + // 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(); +} + +void loop() +{ + // dummy code + Serial.println(millis()); + delay(1000); +} +