24#if (defined(ARDUINO) && ARDUINO >= 100) || defined(EPOXY_DUINO)
37#include "HybridArrayList.h"
39#include "pq_globals.h"
40#include "pq_constrain.h"
42#include "pq_phase_utils.h"
68 void preBegin(
unsigned long baudrate=PLAQUETTE_SERIAL_BAUD_RATE);
83 inline void begin(
unsigned long baudrate=PLAQUETTE_SERIAL_BAUD_RATE);
95 size_t nUnits() {
return _unitsEndIndex - _unitsBeginIndex; }
104 float seconds(
bool referenceTime=
true)
const;
125 unsigned long nSteps()
const {
return _nSteps; }
143 float samplePeriod()
const {
return (_samplePeriod ? _samplePeriod : (_samplePeriod = frequencyToPeriod(_sampleRate))); }
171 void add(
Unit* component);
174 void remove(
Unit* component);
185 static HybridArrayList<Unit*, PLAQUETTE_MAX_UNITS>& units();
188 size_t _unitsBeginIndex;
189 size_t _unitsEndIndex;
195 mutable float _samplePeriod;
198 float _targetSampleRate;
215 StepState _stepState;
218 uint32_t _deltaTimeMicroSeconds;
221 float _deltaTimeSecondsTimesFixed32Max;
224 uint32_t _targetDeltaTimeMicroSeconds;
227 unsigned long _nSteps;
230 bool _autoSampleRate;
233 bool _beginCompleted;
239 EventManager _eventManager;
242 static micro_seconds_t _totalGlobalMicroSeconds;
246 Engine(
const Engine&);
247 Engine& operator=(
const Engine&);
251extern Engine& Plaquette;
254unsigned long nSteps();
257bool hasAutoSampleRate();
260void autoSampleRate();
263void sampleRate(
float sampleRate);
266void samplePeriod(
float samplePeriod);
285bool randomTrigger(
float timeWindow);
288void beginSerial(
unsigned long baudRate);
300 virtual float get() {
return 0.0f; }
303 operator float() {
return get(); }
307 virtual float mapTo(
float toLow,
float toHigh) {
return constrain(
get(), toLow, toHigh); }
314 virtual float put(
float value) {
return get(); }
340 virtual void begin() {}
341 virtual void step() {}
345 virtual void clearEvents();
374 virtual void onEvent(EventCallback callback, EventType eventType);
393 virtual bool isOn() {
return false; }
415 virtual float put(
float value) {
427 virtual float mapTo(
float toLow,
float toHigh) {
return isOn() ? toHigh : toLow; }
430 operator bool() {
return isOn(); }
436 explicit operator float() {
return Unit::operator float(); }
449 virtual ~AnalogSource() {}
452 virtual float get() {
return _value; }
455 virtual float mapTo(
float toLow,
float toHigh) {
return mapFrom01(
get(), toLow, toHigh); }
469 virtual bool isOn() {
return _onValue; }
476 virtual bool putOn(
bool value) {
return (_onValue = value); }
494 virtual void onRise(EventCallback callback) {
onEvent(callback, EVENT_RISE); }
497 virtual void onFall(EventCallback callback) {
onEvent(callback, EVENT_FALL); }
503 void _updateChangeState() {
504 _changeState = (int8_t)_onValue - (int8_t)_prevOnValue;
505 _prevOnValue = _onValue;
511 case EVENT_CHANGE:
return changed();
512 case EVENT_RISE:
return rose();
513 case EVENT_FALL:
return fell();
522 bool _prevOnValue : 1;
525 int8_t _changeState : 2;
534inline Chainable& operator>>(
float value, Chainable& unit) {
540inline Chainable& operator>>(Chainable& source, Chainable& sink) {
541 return pq::operator>>(source.get(), sink);
544inline Chainable& operator>>(
double value, Chainable& unit) {
545 return pq::operator>>((
float)value, unit);
548inline Chainable& operator>>(
bool value, Chainable& unit) {
554#if defined(__arc__) || defined(__arm__)
555inline Chainable& operator>>(
int value, Chainable& unit) {
556 return pq::operator>>((
float)value, unit);
560inline Chainable& operator>>(int8_t value, Chainable& unit) {
561 return pq::operator>>((
float)value, unit);
564inline Chainable& operator>>(uint8_t value, Chainable& unit) {
565 return pq::operator>>((
float)value, unit);
568inline Chainable& operator>>(int16_t value, Chainable& unit) {
569 return pq::operator>>((
float)value, unit);
572inline Chainable& operator>>(uint16_t value, Chainable& unit) {
573 return pq::operator>>((
float)value, unit);
576inline Chainable& operator>>(int32_t value, Chainable& unit) {
577 return pq::operator>>((
float)value, unit);
580inline Chainable& operator>>(uint32_t value, Chainable& unit) {
581 return pq::operator>>((
float)value, unit);
584inline Chainable& operator>>(int64_t value, Chainable& unit) {
585 return pq::operator>>((
float)value, unit);
588inline Chainable& operator>>(uint64_t value, Chainable& unit) {
589 return pq::operator>>((
float)value, unit);
653 uint8_t
pin()
const {
return _pin; }
656 uint8_t
mode()
const {
return _mode; }
673 for (
size_t i=_unitsBeginIndex; i != _unitsEndIndex; i++) {
678 _eventManager.
step();
683 _updateGlobalMicroSeconds();
686 _deltaTimeMicroSeconds = _totalGlobalMicroSeconds.micros32.base - _microSeconds.micros32.base;
687 float trueSampleRate = (_deltaTimeMicroSeconds ? SECONDS_TO_MICROS / _deltaTimeMicroSeconds : PLAQUETTE_MAX_SAMPLE_RATE);
690 if (!_autoSampleRate) {
693 if (_stepState == STEP_INIT) {
695 _targetTime = _microSeconds;
696 _targetTime.micros32.base += _targetDeltaTimeMicroSeconds;
699 if (_targetTime.micros32.base >= _microSeconds.micros32.base) {
700 _stepState = STEP_WAIT;
703 _targetTime.micros32.overflows++;
704 _stepState = STEP_WAIT_OVERFLOW;
709 if (_stepState == STEP_WAIT) {
711 if (_totalGlobalMicroSeconds.micros32.base < _targetTime.micros32.base)
716 if (_totalGlobalMicroSeconds.micros64 < _targetTime.micros64)
721 _stepState = STEP_INIT;
725 _setSampleRate(trueSampleRate);
728 uint64_t deltaTimeMicroSeconds64 = (uint64_t)_deltaTimeMicroSeconds;
729 _deltaTimeSecondsTimesFixed32Max = ((deltaTimeMicroSeconds64 << 32) - deltaTimeMicroSeconds64) * MICROS_TO_SECONDS;
732 _microSeconds = _totalGlobalMicroSeconds;
762void Engine::_setSampleRate(
float sampleRate) {
An analog analog source that contains a value constrained to a finite range (typically in [0,...
Definition PqCore.h:444
virtual float mapTo(float toLow, float toHigh)
Maps value to new range.
Definition PqCore.h:455
virtual float get()
Returns value in [0, 1].
Definition PqCore.h:452
AnalogSource(Engine &engine=Engine::primary())
Constructor.
Definition PqCore.h:447
virtual float get()
Returns value (typically between 0 and 1, may vary depending on class).
Definition PqCore.h:300
virtual float mapTo(float toLow, float toHigh)
Maps value to new range.
Definition PqCore.h:307
static float digitalToAnalog(bool b)
Converts digital (bool) value to analog (float) value.
Definition PqCore.h:320
static bool analogToDigital(float f)
Converts analog (float) value to digital (bool) value.
Definition PqCore.h:317
virtual float put(float value)
Pushes value into the unit.
Definition PqCore.h:314
A digital source that contains a true/false value.
Definition PqCore.h:462
virtual bool eventTriggered(EventType eventType)
Returns true iff an event of a certain type has been triggered.
Definition PqCore.h:509
virtual bool putOn(bool value)
Pushes value into the unit.
Definition PqCore.h:476
virtual void onRise(EventCallback callback)
Registers event callback on rise event.
Definition PqCore.h:494
DigitalSource(Engine &engine=Engine::primary())
Constructor.
Definition PqCore.h:465
virtual void onFall(EventCallback callback)
Registers event callback on fall event.
Definition PqCore.h:497
virtual bool rose()
Returns true if the value rose.
Definition PqCore.h:479
virtual int8_t changeState()
Difference between current and previous value of the unit.
Definition PqCore.h:491
virtual bool toggle()
Switches between on and off.
Definition PqCore.h:488
virtual bool changed()
Returns true if the value changed.
Definition PqCore.h:485
virtual bool isOn()
Returns true iff the input is "on".
Definition PqCore.h:469
virtual bool fell()
Returns true if the value fell.
Definition PqCore.h:482
virtual void onChange(EventCallback callback)
Registers event callback on change event.
Definition PqCore.h:500
A generic class representing a simple digital (true/false)unit.
Definition PqCore.h:386
DigitalUnit(Engine &engine=Engine::primary())
Constructor.
Definition PqCore.h:389
virtual bool putOn(bool value)
Pushes value into the unit.
Definition PqCore.h:424
virtual int getInt()
Returns value as integer (0 or 1).
Definition PqCore.h:399
virtual float get()
Returns value as float (either 0.0 or 1.0).
Definition PqCore.h:402
virtual float mapTo(float toLow, float toHigh)
Maps value to new range.
Definition PqCore.h:427
virtual bool off()
Sets output to "off" (ie. false, 0).
Definition PqCore.h:408
virtual bool isOn()
Returns true iff the input is "on".
Definition PqCore.h:393
virtual float put(float value)
Pushes value into the unit.
Definition PqCore.h:415
virtual bool isOff()
Returns true iff the input is "off".
Definition PqCore.h:396
virtual bool on()
Sets output to "on" (ie. true, 1).
Definition PqCore.h:405
The main Plaquette static class containing all the units.
Definition PqCore.h:60
void preStep()
Updates all components (calls step() on all of them).
Definition PqCore.h:671
uint64_t microSeconds(bool referenceTime=true) const
Returns time in microseconds.
Definition PqCore.cpp:107
size_t nUnits()
Returns the current number of units.
Definition PqCore.h:95
bool timeStep()
Performs additional tasks after the class to step().
Definition PqCore.h:681
static Engine & primary()
Returns the main instance of Plaquette.
Definition PqCore.cpp:31
float sampleRate() const
Returns sample rate.
Definition PqCore.h:140
bool step()
Function to be used within the PlaquetteLib context (needs to be called at top of loop() method).
Definition PqCore.h:744
float deltaTimeSecondsTimesFixed32Max() const
Returns time between steps, expressed in fixed point propotion.
Definition PqCore.h:149
uint32_t deltaTimeMicroSeconds() const
Returns time between steps (in microseconds).
Definition PqCore.h:146
bool isPrimary() const
Returns true if this Engine is the main.
Definition PqCore.h:155
bool randomTrigger(float timeWindow)
Randomly triggers an event about once per time window, on average.
Definition PqCore.cpp:213
void end()
Optional function to be used within the PlaquetteLib context.
Definition PqCore.cpp:90
void sampleRate(float sampleRate)
Sets sample rate to a fixed value, thus disabling auto sampling rate.
Definition PqCore.cpp:200
void autoSampleRate()
Enables auto sample rate mode (default).
Definition PqCore.cpp:190
void postBegin()
Performs additional tasks after the class to begin().
Definition PqCore.cpp:82
float seconds(bool referenceTime=true) const
Returns time in seconds.
Definition PqCore.cpp:99
float samplePeriod() const
Returns sample period.
Definition PqCore.h:143
uint32_t milliSeconds(bool referenceTime=true) const
Returns time in milliseconds.
Definition PqCore.cpp:103
void preBegin(unsigned long baudrate=PLAQUETTE_SERIAL_BAUD_RATE)
Initializes all components (calls begin() on all of them).
Definition PqCore.cpp:55
void samplePeriod(float samplePeriod)
Sets sample period to a fixed value, thus disabling auto sampling rate.
Definition PqCore.cpp:209
bool hasAutoSampleRate() const
Returns true iff the auto sample rate mode is enabled (default).
Definition PqCore.h:128
unsigned long nSteps() const
Returns number of steps.
Definition PqCore.h:125
void begin(unsigned long baudrate=PLAQUETTE_SERIAL_BAUD_RATE)
Function to be used within the PlaquetteLib context (needs to be called at top of setup() method).
Definition PqCore.h:740
Manages event listeners for Plaquette units.
Definition PqEvents.h:44
void step()
Performs a single step of the event manager.
Definition PqEvents.cpp:40
Superclass for pin-based components.
Definition PqCore.h:647
uint8_t pin() const
Returns the pin this component is attached to.
Definition PqCore.h:653
virtual void mode(uint8_t mode)
Changes the mode of the component.
Definition PqCore.h:659
uint8_t mode() const
Returns the mode of the component.
Definition PqCore.h:656
A generic class representing a unit in the system.
Definition PqCore.h:335
uint32_t milliSeconds() const
Returns engine time in milliseconds.
Definition PqCore.h:351
uint64_t microSeconds() const
Returns engine time in microseconds.
Definition PqCore.h:354
float seconds() const
Returns engine time in seconds.
Definition PqCore.h:348
unsigned long nSteps() const
Returns number of engine steps.
Definition PqCore.h:357
virtual void onEvent(EventCallback callback, EventType eventType)
Registers event callback.
Definition PqCore.cpp:246
virtual bool eventTriggered(EventType eventType)
Returns true iff an event of a certain type has been triggered.
Definition PqCore.h:371
Engine * engine() const
Returns the engine that owns this unit.
Definition PqCore.h:382
float samplePeriod() const
Returns enginesample period.
Definition PqCore.h:363
float sampleRate() const
Returns engine sample rate.
Definition PqCore.h:360