+ added demo sketch

+ clean up
This commit is contained in:
Rob Tillaart 2013-09-30 18:38:54 +02:00
parent 6c386805e9
commit 5af2868450
3 changed files with 75 additions and 20 deletions

View File

@ -2,21 +2,26 @@
// FILE: PulsePattern.cpp // FILE: PulsePattern.cpp
// AUTHOR: Rob dot Tillaart at gmail dot com // AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: see PULSEPATTERN_LIB_VERSION in .h // VERSION: see PULSEPATTERN_LIB_VERSION in .h
// PURPOSE: PulsePatternOut library for Arduino // PURPOSE: PulsePattern library for Arduino
// //
// HISTORY: // HISTORY:
// 0.0.1 - 2012-11-23 initial version // 0.0.1 - 2012-11-23 initial version
// 0.0.2 - 2012-11-23 adapted a static PPO // 0.0.2 - 2012-11-23 adapted a static PPO
// 0.0.3 - 2012-12-27 renamed to PulsePattern // 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 // Released to the public domain
// //
// TODO // TODO
// - PRE 1.0 backwards compatibility // - 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? // - move code to .h file so compiler can inline?
// - optimize timer code // - optimize timer code
// - adjust timing to more accurate values -> setTimer() // - adjust timing to more accurate values -> setTimer()
// - worker should be private - how???
// - test invalid array periods
// //
#include "PulsePattern.h" #include "PulsePattern.h"
@ -27,11 +32,12 @@
#include "WProgram.h" #include "WProgram.h"
#endif #endif
PulsePattern PPO; // Predefined generator (singleton)
PulsePattern PPGenerator;
ISR(TIMER1_COMPA_vect) ISR(TIMER1_COMPA_vect)
{ {
PPO.worker(); PPGenerator.worker();
} }
PulsePattern::PulsePattern() PulsePattern::PulsePattern()
@ -47,6 +53,8 @@ void PulsePattern::init(uint8_t pin, uint16_t * ar, uint8_t size,
_pin = pin; _pin = pin;
_ar = ar; _ar = ar;
_size = size; _size = size;
// TODO: run over the array to test invalid values?
// constrain them 10-4095?
_level = constrain(level, LOW, HIGH); _level = constrain(level, LOW, HIGH);
_prescaler = constrain(prescaler, PRESCALE_1, PRESCALE_1024); _prescaler = constrain(prescaler, PRESCALE_1, PRESCALE_1024);
_cnt = 0; _cnt = 0;
@ -82,12 +90,10 @@ void PulsePattern::worker()
// set next period & flip signal // set next period & flip signal
_level = !_level; _level = !_level;
digitalWrite(_pin, _level); digitalWrite(_pin, _level);
// TODO small adjustment needed for code overhead?; inline iso call? // TODO: adjustment needed for code overhead when micros?;
// for now
// + 5.2 usec for digitalWrite // + 5.2 usec for digitalWrite
// + 3 usec for settimer call // + 3 usec for settimer call
setTimer(_ar[_cnt]-8); OCR1A = (_ar[_cnt]) * (F_CPU/1000000L);
_cnt++; _cnt++;
if (_cnt >= _size) _cnt = 0; // repeat if (_cnt >= _size) _cnt = 0; // repeat
} }
@ -104,8 +110,10 @@ void PulsePattern::setTimer(uint16_t cc)
{ {
TCCR1A = 0; TCCR1A = 0;
TCCR1B = 0; TCCR1B = 0;
TCNT1 = 0; // reset counter TCNT1 = 0; // reset counter
OCR1A = cc*16; // compare A register value; OCR1A = cc*16; // compare A register value;
// *16 makes max period 4095
// min period 12?
// 4: CTC mode, top = OCR1A // 4: CTC mode, top = OCR1A
TCCR1A = _BV (COM1A1); // clear on compare TCCR1A = _BV (COM1A1); // clear on compare
@ -114,5 +122,4 @@ void PulsePattern::setTimer(uint16_t cc)
TIMSK1 = _BV (OCIE1A); // interrupt on Compare A Match TIMSK1 = _BV (OCIE1A); // interrupt on Compare A Match
} }
// END OF FILE // END OF FILE

View File

@ -1,18 +1,18 @@
#ifndef Histogram_h #ifndef PulsePattern_h
#define PulsePatternOut_h #define PulsePattern_h
// //
// FILE: PulsePatternOut.h // FILE: PulsePattern.h
// AUTHOR: Rob dot Tillaart at gmail dot com // 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) // sends a pulse pattern to a digital pin (continuously)
// HISTORY: See PulsePatternOut.cpp // HISTORY: See PulsePattern.cpp
// //
// Released to the public domain // Released to the public domain
// //
#include <inttypes.h> #include <inttypes.h>
#define PULSEPATTERN_LIB_VERSION "0.0.4" #define PULSEPATTERN_LIB_VERSION "0.0.5"
#define NOTINIT -1 #define NOTINIT -1
#define STOPPED 0 #define STOPPED 0
@ -50,7 +50,7 @@ private:
volatile uint8_t _cnt; volatile uint8_t _cnt;
}; };
extern PulsePattern PPO; extern PulsePattern PPGenerator;
#endif #endif
// END OF FILE // END OF FILE

View File

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