24#if (defined(ARDUINO) && ARDUINO >= 100) || defined(EPOXY_DUINO)
38#include "HybridArrayList.h"
40#include "pq_globals.h"
41#include "pq_constrain.h"
43#include "pq_phase_utils.h"
98 size_t nUnits() {
return _unitsEndIndex - _unitsBeginIndex; }
107 float seconds(
bool referenceTime=
true)
const;
128 unsigned long nSteps()
const {
return _nSteps; }
146 float samplePeriod()
const {
return (_samplePeriod ? _samplePeriod : (_samplePeriod = frequencyToPeriod(_sampleRate))); }
178 void add(
Unit* component);
181 void remove(
Unit* component);
187 unsigned long _clock()
const {
return _clockFunction ? _clockFunction() : micros(); }
190 micro_seconds_t _updateGlobalMicroSeconds()
const;
195 static HybridArrayList<Unit*, PLAQUETTE_MAX_UNITS>& units();
198 size_t _unitsBeginIndex;
199 size_t _unitsEndIndex;
205 mutable float _samplePeriod;
208 float _targetSampleRate;
211 micro_seconds_t _microSeconds;
215 micro_seconds_t _targetTime;
225 StepState _stepState;
228 uint32_t _deltaTimeMicroSeconds;
231 float _deltaTimeSecondsTimesFixed32Max;
234 uint32_t _targetDeltaTimeMicroSeconds;
237 unsigned long _nSteps;
240 bool _autoSampleRate;
243 bool _beginCompleted;
249 EventManager _eventManager;
252 mutable micro_seconds_t _totalGlobalMicroSeconds;
255 unsigned long (*_clockFunction)();
259 Engine(
const Engine&);
260 Engine& operator=(
const Engine&);
264extern Engine& Plaquette;
267void referenceClock(
unsigned long (*clockFunction)());
270unsigned long nSteps();
273bool hasAutoSampleRate();
276void autoSampleRate();
279void sampleRate(
float sampleRate);
282void samplePeriod(
float samplePeriod);
301bool randomTrigger(
float timeWindow);
313 virtual float get() {
return 0.0f; }
316 operator float() {
return get(); }
320 virtual float mapTo(
float toLow,
float toHigh) {
return constrain(
get(), toLow, toHigh); }
327 virtual float put([[maybe_unused]]
float value) {
return get(); }
345 using Setter = void (Obj::*)(
float);
346 using Getter = float (Obj::*)()
const;
349 : _obj(obj), _set(set), _get(
get) {}
352 return (_obj->*_get)();
355 float put(
float v)
override {
378 virtual void begin() {}
379 virtual void step() {}
383 virtual void clearEvents();
409 virtual bool eventTriggered([[maybe_unused]] EventType eventType) {
return false; }
412 virtual void onEvent(EventCallback callback, EventType eventType);
431 virtual bool isOn() {
return false; }
453 virtual float put(
float value) {
462 virtual bool putOn([[maybe_unused]]
bool value) {
return isOn(); }
465 virtual float mapTo(
float toLow,
float toHigh) {
return isOn() ? toHigh : toLow; }
468 operator bool() {
return isOn(); }
474 explicit operator float() {
return Unit::operator float(); }
487 virtual ~AnalogSource() {}
490 virtual float get() {
return _value; }
493 virtual float mapTo(
float toLow,
float toHigh) {
return mapFrom01(
get(), toLow, toHigh); }
507 virtual bool isOn() {
return _onValue; }
514 virtual bool putOn(
bool value) {
return (_onValue = value); }
532 virtual void onRise(EventCallback callback) {
onEvent(callback, EVENT_RISE); }
535 virtual void onFall(EventCallback callback) {
onEvent(callback, EVENT_FALL); }
541 void _updateChangeState() {
542 _changeState = (int8_t)_onValue - (int8_t)_prevOnValue;
543 _prevOnValue = _onValue;
549 case EVENT_CHANGE:
return changed();
550 case EVENT_RISE:
return rose();
551 case EVENT_FALL:
return fell();
560 bool _prevOnValue : 1;
563 int8_t _changeState : 2;
575 typedef typename remove_cvref<T>::type U;
578 static int test(...);
583 enum { value = (
sizeof(test(make())) ==
sizeof(
char)) };
587#define PQ_FLOW_OPERATOR_ERROR \
588 "Invalid use of operator>>: right-hand operand must be a Plaquette flowable object such as a unit or parameter."
596template <
typename L,
typename R,
597 enable_if_t< is_flowable<L>::value &&
599inline void operator>>(L&&, R&&) {
601 (void)
sizeof(flow_error<R>);
605template <typename T, enable_if_t<!is_flowable<T>::value,
int> = 0>
606inline void operator>>(
float, T&&) {
607 static_assert(always_false<T>::value, PQ_FLOW_OPERATOR_ERROR);
610template <typename T, enable_if_t<!is_flowable<T>::value,
int> = 0>
611inline void operator>>(
double, T&&) {
612 static_assert(always_false<T>::value, PQ_FLOW_OPERATOR_ERROR);
616inline float operator>>(
float value, Flowable& unit) {
617 return unit.put(value);
621inline float operator>>(Flowable& source, Flowable& sink) {
622 return pq::operator>>(source.get(), sink);
625inline float operator>>(
double value, Flowable& unit) {
626 return pq::operator>>(
static_cast<float>(value), unit);
629inline float operator>>(
bool value, Flowable& unit) {
635inline float operator>>(
float v, Flowable&& dst) {
640inline float operator>>(Flowable& src, Flowable&& dst) {
641 return dst.put(src.get());
645inline float operator>>(
const Flowable& src, Flowable&& dst) {
646 return dst.put(
const_cast<Flowable&
>(src).get());
658template <typename I, pq::enable_if_t<pq::is_integral<I>::value,
int> = 0>
659inline float operator>>(I value, Flowable& unit) {
660 return unit.put(
static_cast<float>(value));
664template <typename I, pq::enable_if_t<pq::is_integral<I>::value,
int> = 0>
665inline float operator>>(I value, Flowable&& unit) {
666 return unit.put(
static_cast<float>(value));
730 uint8_t
pin()
const {
return _pin; }
733 uint8_t
mode()
const {
return _mode; }
750 for (
size_t i=_unitsBeginIndex; i != _unitsEndIndex; i++) {
755 _eventManager.
step();
760 _updateGlobalMicroSeconds();
763 _deltaTimeMicroSeconds = _totalGlobalMicroSeconds.micros32.base - _microSeconds.micros32.base;
764 float trueSampleRate = (_deltaTimeMicroSeconds ? SECONDS_TO_MICROS / _deltaTimeMicroSeconds : PLAQUETTE_MAX_SAMPLE_RATE);
767 if (!_autoSampleRate) {
770 if (_stepState == STEP_INIT) {
772 _targetTime = _microSeconds;
773 _targetTime.micros32.base += _targetDeltaTimeMicroSeconds;
776 if (_targetTime.micros32.base >= _microSeconds.micros32.base) {
777 _stepState = STEP_WAIT;
780 _targetTime.micros32.overflows++;
781 _stepState = STEP_WAIT_OVERFLOW;
786 if (_stepState == STEP_WAIT) {
788 if (_totalGlobalMicroSeconds.micros32.base < _targetTime.micros32.base)
793 if (_totalGlobalMicroSeconds.micros64 < _targetTime.micros64)
798 _stepState = STEP_INIT;
802 _setSampleRate(trueSampleRate);
805 uint64_t deltaTimeMicroSeconds64 = (uint64_t)_deltaTimeMicroSeconds;
806 _deltaTimeSecondsTimesFixed32Max = ((deltaTimeMicroSeconds64 << 32) - deltaTimeMicroSeconds64) * MICROS_TO_SECONDS;
809 _microSeconds = _totalGlobalMicroSeconds;
839void Engine::_setSampleRate(
float sampleRate) {
An analog analog source that contains a value constrained to a finite range (typically in [0,...
Definition PqCore.h:482
AnalogSource(Engine &engine)
Constructor.
Definition PqCore.h:485
virtual float mapTo(float toLow, float toHigh)
Maps value to new range.
Definition PqCore.h:493
virtual float get()
Returns value in [0, 1].
Definition PqCore.h:490
A digital source that contains a true/false value.
Definition PqCore.h:500
virtual bool eventTriggered(EventType eventType)
Returns true iff an event of a certain type has been triggered.
Definition PqCore.h:547
virtual bool putOn(bool value)
Pushes value into the unit.
Definition PqCore.h:514
virtual void onRise(EventCallback callback)
Registers event callback on rise event.
Definition PqCore.h:532
virtual void onFall(EventCallback callback)
Registers event callback on fall event.
Definition PqCore.h:535
virtual bool rose()
Returns true if the value rose.
Definition PqCore.h:517
virtual int8_t changeState()
Difference between current and previous value of the unit.
Definition PqCore.h:529
DigitalSource(Engine &engine)
Constructor.
Definition PqCore.h:503
virtual bool toggle()
Switches between on and off.
Definition PqCore.h:526
virtual bool changed()
Returns true if the value changed.
Definition PqCore.h:523
virtual bool isOn()
Returns true iff the input is "on".
Definition PqCore.h:507
virtual bool fell()
Returns true if the value fell.
Definition PqCore.h:520
virtual void onChange(EventCallback callback)
Registers event callback on change event.
Definition PqCore.h:538
A generic class representing a simple digital (true/false)unit.
Definition PqCore.h:424
virtual bool putOn(bool value)
Pushes value into the unit.
Definition PqCore.h:462
virtual int getInt()
Returns value as integer (0 or 1).
Definition PqCore.h:437
virtual float get()
Returns value as float (either 0.0 or 1.0).
Definition PqCore.h:440
virtual float mapTo(float toLow, float toHigh)
Maps value to new range.
Definition PqCore.h:465
virtual bool off()
Sets output to "off" (ie. false, 0).
Definition PqCore.h:446
DigitalUnit(Engine &engine)
Constructor.
Definition PqCore.h:427
virtual bool isOn()
Returns true iff the input is "on".
Definition PqCore.h:431
virtual float put(float value)
Pushes value into the unit.
Definition PqCore.h:453
virtual bool isOff()
Returns true iff the input is "off".
Definition PqCore.h:434
virtual bool on()
Sets output to "on" (ie. true, 1).
Definition PqCore.h:443
The main Plaquette static class containing all the units.
Definition PqCore.h:63
void preStep()
Updates all components (calls step() on all of them).
Definition PqCore.h:748
uint64_t microSeconds(bool referenceTime=true) const
Returns time in microseconds.
Definition PqCore.cpp:104
size_t nUnits()
Returns the current number of units.
Definition PqCore.h:98
bool timeStep()
Performs additional tasks after the class to step().
Definition PqCore.h:758
static Engine & primary()
Returns the main instance of Plaquette.
Definition PqCore.cpp:30
float sampleRate() const
Returns sample rate.
Definition PqCore.h:143
bool step()
Function to be used within the PlaquetteLib context (needs to be called at top of loop() method).
Definition PqCore.h:821
float deltaTimeSecondsTimesFixed32Max() const
Returns time between steps, expressed in fixed point propotion.
Definition PqCore.h:152
uint32_t deltaTimeMicroSeconds() const
Returns time between steps (in microseconds).
Definition PqCore.h:149
bool isPrimary() const
Returns true if this Engine is the main.
Definition PqCore.h:158
bool randomTrigger(float timeWindow)
Randomly triggers an event about once per time window, on average.
Definition PqCore.cpp:208
void end()
Optional function to be used within the PlaquetteLib context.
Definition PqCore.cpp:87
void sampleRate(float sampleRate)
Sets sample rate to a fixed value, thus disabling auto sampling rate.
Definition PqCore.cpp:195
void autoSampleRate()
Enables auto sample rate mode (default).
Definition PqCore.cpp:185
void postBegin()
Performs additional tasks after the class to begin().
Definition PqCore.cpp:79
float seconds(bool referenceTime=true) const
Returns time in seconds.
Definition PqCore.cpp:96
void referenceClock(unsigned long(*clockFunction)())
Sets base function returning microseconds.
Definition PqCore.cpp:212
void begin()
Function to be used within the PlaquetteLib context (needs to be called at top of setup() method).
Definition PqCore.h:817
float samplePeriod() const
Returns sample period.
Definition PqCore.h:146
uint32_t milliSeconds(bool referenceTime=true) const
Returns time in milliseconds.
Definition PqCore.cpp:100
void samplePeriod(float samplePeriod)
Sets sample period to a fixed value, thus disabling auto sampling rate.
Definition PqCore.cpp:204
bool hasAutoSampleRate() const
Returns true iff the auto sample rate mode is enabled (default).
Definition PqCore.h:131
unsigned long nSteps() const
Returns number of steps.
Definition PqCore.h:128
void preBegin()
Initializes all components (calls begin() on all of them).
Definition PqCore.cpp:56
Manages event listeners for Plaquette units.
Definition PqEvents.h:48
void step()
Performs a single step of the event manager.
Definition PqEvents.cpp:40
static float digitalToAnalog(bool b)
Converts digital (bool) value to analog (float) value.
Definition PqCore.h:333
static bool analogToDigital(float f)
Converts analog (float) value to digital (bool) value.
Definition PqCore.h:330
virtual float mapTo(float toLow, float toHigh)
Maps value to new range.
Definition PqCore.h:320
virtual float get()
Returns value (typically between 0 and 1, may vary depending on class).
Definition PqCore.h:313
virtual float put(float value)
Pushes value into the unit.
Definition PqCore.h:327
float put(float v) override
Pushes value into the unit.
Definition PqCore.h:355
float get() override
Returns value (typically between 0 and 1, may vary depending on class).
Definition PqCore.h:351
Superclass for pin-based components.
Definition PqCore.h:724
uint8_t pin() const
Returns the pin this component is attached to.
Definition PqCore.h:730
virtual void mode(uint8_t mode)
Changes the mode of the component.
Definition PqCore.h:736
uint8_t mode() const
Returns the mode of the component.
Definition PqCore.h:733
A generic class representing a unit in the system.
Definition PqCore.h:373
uint32_t milliSeconds() const
Returns engine time in milliseconds.
Definition PqCore.h:389
uint64_t microSeconds() const
Returns engine time in microseconds.
Definition PqCore.h:392
float seconds() const
Returns engine time in seconds.
Definition PqCore.h:386
unsigned long nSteps() const
Returns number of engine steps.
Definition PqCore.h:395
virtual void onEvent(EventCallback callback, EventType eventType)
Registers event callback.
Definition PqCore.cpp:240
virtual bool eventTriggered(EventType eventType)
Returns true iff an event of a certain type has been triggered.
Definition PqCore.h:409
Engine * engine() const
Returns the engine that owns this unit.
Definition PqCore.h:420
float samplePeriod() const
Returns enginesample period.
Definition PqCore.h:401
float sampleRate() const
Returns engine sample rate.
Definition PqCore.h:398
Definition pq_traits.h:109