Plaquette
 
Loading...
Searching...
No Matches
StreamIn.h
1/*
2 * StraemIn.h
3 *
4 * (c) 2015 Sofian Audry :: info(@)sofianaudry(.)com
5 * (c) 2015 Thomas O Fredericks :: tof(@)t-o-f(.)info
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef STREAM_IN_H_
22#define STREAM_IN_H_
23
24#include "PqCore.h"
25
26namespace pq {
27
29class StreamIn : public AnalogSource {
30public:
36
42 StreamIn(Stream& stream, Engine& engine = Engine::primary());
43
45 virtual float mapTo(float toLow, float toHigh) { return mapFrom01(get(), toLow, toHigh); }
46
48 virtual bool updated() { return _valueUpdated; }
49
51 virtual void onUpdate(EventCallback callback) { onEvent(callback, EVENT_UPDATE); }
52
53protected:
54 // Core Plaquette methods.
55 virtual void begin();
56 virtual void step();
57
59 virtual bool eventTriggered(EventType eventType) {
60 switch (eventType) {
61 case EVENT_UPDATE: return updated();
62 default: return AnalogSource::eventTriggered(eventType);
63 }
64 }
65
66 // Internal use: keep track of next incoming value in a non-blocking way.
67 float _nextValue;
68 float _nextFraction;
69 bool _nextIsValid : 1;
70 bool _nextIsNegative : 1;
71 bool _nextIsFraction : 1;
72 bool _valueUpdated : 1;
73
74 // The stream.
75 Stream* _stream;
76};
77
78}
79
80#endif
An analog analog source that contains a value constrained to a finite range (typically in [0,...
Definition PqCore.h:444
virtual float get()
Returns value in [0, 1].
Definition PqCore.h:452
The main Plaquette static class containing all the units.
Definition PqCore.h:60
static Engine & primary()
Returns the main instance of Plaquette.
Definition PqCore.cpp:31
Stream/serial input. Reads float values using Arduino built-in parseFloat().
Definition StreamIn.h:29
virtual bool eventTriggered(EventType eventType)
Returns true iff an event of a certain type has been triggered.
Definition StreamIn.h:59
virtual bool updated()
Returns true iff value was changed.
Definition StreamIn.h:48
virtual void onUpdate(EventCallback callback)
Registers event callback on finish event.
Definition StreamIn.h:51
virtual float mapTo(float toLow, float toHigh)
Maps value to new range.
Definition StreamIn.h:45
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