GY-63_MS5611/libraries/PulsePattern/PulsePattern.cpp

151 lines
2.8 KiB
C++
Raw Normal View History

2013-09-30 12:29:52 -04:00
//
2013-09-30 12:32:34 -04:00
// FILE: PulsePattern.cpp
2017-07-16 10:01:09 -04:00
// AUTHOR: Rob dot Tillaart at gmail dot com
2022-11-22 11:20:26 -05:00
// VERSION: 0.1.6
2020-11-27 05:28:57 -05:00
// DATE: 2012-11-23
// PURPOSE: Arduino Library to generate repeating pulse patterns
2013-09-30 12:29:52 -04:00
//
2022-03-11 04:36:49 -05:00
2013-09-30 12:29:52 -04:00
2013-09-30 12:32:34 -04:00
#include "PulsePattern.h"
2013-09-30 12:29:52 -04:00
2022-11-22 11:20:26 -05:00
// Predefined generator (singleton)
2013-09-30 12:38:54 -04:00
PulsePattern PPGenerator;
2022-03-11 04:36:49 -05:00
ISR(TIMER1_COMPA_vect)
2013-09-30 12:29:52 -04:00
{
2013-09-30 12:38:54 -04:00
PPGenerator.worker();
2013-09-30 12:29:52 -04:00
}
2021-12-24 09:10:06 -05:00
2013-09-30 12:32:34 -04:00
PulsePattern::PulsePattern()
2013-09-30 12:29:52 -04:00
{
2017-07-16 10:01:09 -04:00
_size = 0;
_state = NOTINIT;
_ar = NULL;
_cnt = 0;
_level = LOW;
_prescaler = 0;
_pin = 0;
2013-09-30 12:29:52 -04:00
}
2021-12-24 09:10:06 -05:00
2020-11-27 05:28:57 -05:00
void PulsePattern::init(const uint8_t pin, const uint16_t * ar, const uint8_t size,
const uint8_t level, const uint8_t prescaler)
2017-07-16 10:01:09 -04:00
{
_pin = pin;
pinMode(_pin, OUTPUT);
stop();
2020-11-27 05:28:57 -05:00
_ar = (uint16_t *) ar;
2017-07-16 10:01:09 -04:00
_size = size;
2020-11-27 05:28:57 -05:00
_level = level ? LOW : HIGH;
2017-07-16 10:01:09 -04:00
_prescaler = constrain(prescaler, PRESCALE_1, PRESCALE_1024);
_cnt = 0;
2017-07-16 10:01:09 -04:00
digitalWrite(_pin, _level);
_state = STOPPED;
2020-11-27 05:28:57 -05:00
// fast low level AVR ports
uint8_t _pinport = digitalPinToPort(_pin);
_pinout = portOutputRegister(_pinport);
_pinbit = digitalPinToBitMask(_pin);
2013-09-30 12:29:52 -04:00
}
2021-12-24 09:10:06 -05:00
2022-03-11 04:36:49 -05:00
void PulsePattern::start(uint32_t times)
2013-09-30 12:29:52 -04:00
{
2022-11-22 11:20:26 -05:00
if (_state == RUNNING) return; // no restart
_cnt = 0; // start from begin
2022-03-11 04:36:49 -05:00
_times = times;
2020-11-27 05:28:57 -05:00
cont();
}
2021-12-24 09:10:06 -05:00
2020-11-27 05:28:57 -05:00
void PulsePattern::cont()
{
2022-11-22 11:20:26 -05:00
if (_state == RUNNING) return; // no continue
if (_size == 0) return; // no pattern
setTimer(1); // start asap
2017-07-16 10:01:09 -04:00
_state = RUNNING;
2013-09-30 12:29:52 -04:00
}
2021-12-24 09:10:06 -05:00
2013-09-30 12:32:34 -04:00
void PulsePattern::stop()
2013-09-30 12:29:52 -04:00
{
2017-07-16 10:01:09 -04:00
stopTimer();
_state = STOPPED;
_level = LOW;
digitalWrite(_pin, _level);
2013-09-30 12:29:52 -04:00
}
2021-12-24 09:10:06 -05:00
2013-09-30 12:32:34 -04:00
void PulsePattern::worker()
2013-09-30 12:29:52 -04:00
{
2017-07-16 10:01:09 -04:00
if (_state != RUNNING) return;
2022-11-22 11:20:26 -05:00
// set next period & flip signal
2017-07-16 10:01:09 -04:00
_level = !_level;
2022-11-22 11:20:26 -05:00
// digitalWrite(_pin, _level);
// direct port much faster
2020-11-27 05:28:57 -05:00
if (_level == 0) *_pinout &= ~_pinbit;
else *_pinout |= _pinbit;
if (_factor != 4096)
{
OCR1A = _ar[_cnt] * _factor * (F_CPU/1000000UL) / 4096;
}
else
{
OCR1A = _ar[_cnt] * (F_CPU/1000000UL);
}
2017-07-16 10:01:09 -04:00
_cnt++;
2022-03-11 04:36:49 -05:00
if (_cnt >= _size)
{
_cnt = 0; // reset pattern
switch(_times)
{
case PP_CONTINUOUS:
break;
case 1:
_times--;
case 0:
stop();
break;
default:
_times--;
break;
}
}
2013-09-30 12:29:52 -04:00
}
2021-12-24 09:10:06 -05:00
2022-11-22 11:20:26 -05:00
// TIMER code based upon - http://www.gammon.com.au/forum/?id=11504
2013-09-30 12:32:34 -04:00
void PulsePattern::stopTimer()
2013-09-30 12:29:52 -04:00
{
2017-07-16 10:01:09 -04:00
TCCR1A = 0; // reset timer 1
TCCR1B = 0;
2013-09-30 12:29:52 -04:00
}
2021-12-24 09:10:06 -05:00
2017-07-16 10:01:09 -04:00
void PulsePattern::setTimer(const uint16_t cc) const
2013-09-30 12:29:52 -04:00
{
2022-11-22 11:20:26 -05:00
TCCR1A = 0; // stop timer first
2017-07-16 10:01:09 -04:00
TCCR1B = 0;
2022-11-22 11:20:26 -05:00
TCNT1 = 0; // reset counter
OCR1A = cc * 16; // compare A register value;
// * 16 makes max period 4095
// min period 12?
2017-07-16 10:01:09 -04:00
2022-11-22 11:20:26 -05:00
// 4: CTC mode, top = OCR1A
TCCR1A = _BV (COM1A1); // clear on compare
2017-07-16 10:01:09 -04:00
TCCR1B = _BV (WGM12) | _prescaler;
2022-11-22 11:20:26 -05:00
TIFR1 |= _BV (OCF1A); // clear interrupt flag
TIMSK1 = _BV (OCIE1A); // interrupt on Compare A Match
2013-09-30 12:29:52 -04:00
}
2021-12-24 09:10:06 -05:00
2022-11-22 11:20:26 -05:00
// -- END OF FILE --
2021-12-24 09:10:06 -05:00