OS  v1.7.5
Documentation
Loading...
Searching...
No Matches
input.hpp
1#ifndef QOS_EDGE_CHECK
2#define QOS_EDGE_CHECK
3
4#include "include/types.hpp"
5#include "include/list.hpp"
6#include "include/timer.hpp"
7#include "include/helper.hpp"
8
9namespace qOS {
10
11
21 namespace input {
22
24 class watcher;
25 class channel;
36 enum class event {
37 NONE = 0,
38 EXCEPTION,
39 ON_CHANGE,
47 IN_BAND,
51 DELTA,
52 STEP_UP,
53 STEP_DOWN,
55 MAX_EVENTS,
56 STEP = STEP_UP
58 };
59
63 enum class type {
66 };
67
68
69 using digitalValue_t = int;
70 using analogValue_t = uint32_t;
71
78 using digitalReaderFcn_t = digitalValue_t (*)( uint8_t );
79
86 using analogReaderFcn_t = analogValue_t (*)( uint8_t );
87
88
89
95 using eventCallback_t = void(*)( channel& );
96
97 class channel : protected node {
99 protected:
100 event lastEvent{ event::NONE };
101 uint8_t number;
102 void *userData{ nullptr };
103 eventCallback_t callback{ nullptr };
104 qOS::clock_t tChange{ 0U };
105 qOS::clock_t tSteadyHigh{ 0xFFFFFFFFU };
106 qOS::clock_t tSteadyLow{ 0xFFFFFFFFU };
107 virtual void updateReading( bool act ) noexcept = 0;
108 virtual void evaluateState( void ) noexcept = 0;
109 virtual bool isValidConfig( void ) const noexcept = 0;
110 virtual void setInitalState( void ) noexcept = 0;
111 inline void dispatchEvent( event e ) noexcept
112 {
113 lastEvent = e;
114 callback( *this );
115 }
117 public:
119 virtual ~channel() {}
120 channel( uint8_t channelNumber ) : number( channelNumber ) {}
127 virtual type getType( void ) const noexcept = 0;
132 inline event getEvent( void ) const noexcept
133 {
134 return lastEvent;
135 }
141 inline bool setCallback( const eventCallback_t cb ) noexcept
142 {
143 callback = cb;
144 return ( cb != callback );
145 }
151 inline bool setChannel( const uint8_t inputChannel ) noexcept
152 {
153 bool retValue = false;
154
155 if ( inputChannel < 32U ) {
156 number = inputChannel;
157 retValue = true;
158 }
159
160 return retValue;
161 }
166 inline uint8_t getChannel( void ) const noexcept
167 {
168 return number;
169 }
174 inline void setUserData( void* pUserData ) noexcept
175 {
176 userData = pUserData;
177 }
182 inline void* getUserData( void ) noexcept
183 {
184 return userData;
185 }
191 virtual bool isShared( void ) const noexcept = 0;
198 virtual bool setTime( const event e, const qOS::duration_t t ) noexcept = 0;
205 virtual bool setParameter( const event e, const analogValue_t p ) noexcept = 0;
211 virtual uint8_t getCount( void ) const noexcept = 0;
213 virtual bool setReader( digitalReaderFcn_t r ) noexcept = 0;
214 virtual bool setReader( analogReaderFcn_t r ) noexcept = 0;
217 virtual bool unShare( void ) noexcept = 0;
218 friend class watcher;
219 };
220
221
225 class digitalChannel : public channel {
226 using channelStateFcn_t = void(*)( digitalChannel& );
227 private:
228 digitalValue_t value{ 0 };
229 digitalValue_t *ptrValue{ &value };
230 digitalReaderFcn_t reader{ nullptr };
231 channelStateFcn_t channelState{ nullptr };
232 bool negate{ false };
233 qOS::clock_t pulsationInterval{ 250U };
234 uint8_t pulsationCount{ 0 };
235 void updateReading( bool act ) noexcept override;
236 void setInitalState( void ) noexcept override;
237 bool isValidConfig( void ) const noexcept override
238 {
239 return true;
240 }
241 void evaluateState( void ) noexcept override
242 {
243 channelState( *this );
244 }
245 static void fallingEdgeState( digitalChannel& c );
246 static void risingEdgeState( digitalChannel& c );
247 static void steadyInHighState( digitalChannel& c );
248 static void steadyInLowState( digitalChannel& c );
249 digitalChannel( digitalChannel const& ) = delete;
250 void operator=( digitalChannel const& ) = delete;
251 public:
253 virtual ~digitalChannel() {}
260 digitalChannel( const uint8_t inputChannel, bool invert = false ) : channel( inputChannel ), negate( invert) {}
265 type getType( void ) const noexcept override
266 {
268 }
275 bool setTime( const event e, const qOS::duration_t t ) noexcept override;
282 bool setParameter( const event e, const analogValue_t p ) noexcept override;
288 uint8_t getCount( void ) const noexcept override
289 {
290 return pulsationCount;
291 }
297 bool isShared( void ) const noexcept override
298 {
299 return ( &value != ptrValue );
300 }
307 bool setReader( digitalReaderFcn_t r ) noexcept override
308 {
309 reader = r;
310 return true;
311 }
313 bool setReader( analogReaderFcn_t r ) noexcept override
314 {
315 (void)r;
316 return false;
317 }
318 bool unShare( void ) noexcept override
319 {
320 ptrValue = &value;
321 return true;
322 }
324 friend class watcher;
325 };
326
330 class analogChannel : public channel {
331 using channelStateFcn_t = void(*)( analogChannel& );
332 private:
333 analogValue_t value{ 0 };
334 analogValue_t *ptrValue{ &value };
335 analogReaderFcn_t reader{ nullptr };
336 channelStateFcn_t channelState{ nullptr };
337 analogValue_t high{ 800U };
338 analogValue_t low{ 200U };
339 analogValue_t lastStep{ 0U };
340 analogValue_t lastSampled{ 0U };
341 analogValue_t delta{ 0xFFFFFFFFU };
342 analogValue_t step{ 0xFFFFFFFFU };
343 analogValue_t hysteresis{ 20U };
344 qOS::clock_t tSteadyBand{ 0xFFFFFFFFU };
345
346 void updateReading( bool act ) noexcept override;
347 void setInitalState( void ) noexcept override;
348 bool isValidConfig( void ) const noexcept override
349 {
350 return ( high - low ) > hysteresis;
351 }
352 void evaluateState( void ) noexcept override
353 {
354 channelState( *this );
355 }
356 static void lowThresholdState( analogChannel& c );
357 static void highThresholdState( analogChannel& c );
358 static void inBandState( analogChannel& c );
359 static void steadyInHighState( analogChannel& c );
360 static void steadyInLowState( analogChannel& c );
361 static void steadyInBandState( analogChannel& c );
362 analogChannel( analogChannel const& ) = delete;
363 void operator=( analogChannel const& ) = delete;
364 public:
366 virtual ~analogChannel() {}
376 analogChannel( const uint8_t inputChannel, const analogValue_t lowerThreshold, const analogValue_t upperThreshold, const analogValue_t h = 20 )
377 : channel( inputChannel ),
378 high( upperThreshold ),
379 low( lowerThreshold ),
380 hysteresis( h ) {}
385 type getType( void ) const noexcept override
386 {
388 }
395 bool setTime( const event e, const qOS::duration_t t ) noexcept override;
402 bool setParameter( const event e, const analogValue_t p ) noexcept override;
408 uint8_t getCount( void ) const noexcept override
409 {
410 return 0;
411 }
417 bool isShared( void ) const noexcept override
418 {
419 return ( &value != ptrValue );
420 }
422 bool setReader( digitalReaderFcn_t r ) noexcept override
423 {
424 (void)r;
425 return false;
426 }
434 bool setReader( analogReaderFcn_t r ) noexcept override
435 {
436 reader = r;
437 return true;
438 }
444 bool unShare( void ) noexcept override
445 {
446 ptrValue = &value;
447 return true;
448 }
449 friend class watcher;
450 };
451
452
456 class watcher : protected node {
457 private:
458 eventCallback_t exception{ nullptr };
459 list digitalChannels;
460 list analogChannels;
461 qOS::timer waitDebounce;
462 qOS::duration_t debounceTime{ 100_ms };
463 digitalReaderFcn_t digitalReader{ nullptr };
464 analogReaderFcn_t analogReader{ nullptr };
465 watcher( watcher const& ) = delete;
466 void operator=( watcher const& ) = delete;
467 public:
469 virtual ~watcher() {}
476 watcher( const qOS::duration_t dt = 100_ms ) : debounceTime( dt ) {}
488 watcher( const digitalReaderFcn_t& rDigital, const analogReaderFcn_t& rAnalog, const qOS::duration_t timeDebounce = 100_ms ) :
489 debounceTime( timeDebounce ), digitalReader( rDigital ), analogReader( rAnalog ) {}
495 bool add( channel& c ) noexcept;
502 inline bool add( channel& c, eventCallback_t cb ) noexcept
503 {
504 (void)c.setCallback( cb );
505 return add( c );
506 }
514 inline bool add( channel&c, digitalReaderFcn_t fcn, eventCallback_t cb ) noexcept
515 {
516 bool retValue;
517 (void)c.setCallback( cb );
518 retValue = add( c );
519 (void)c.setReader( fcn );
520 return retValue;
521 }
529 inline bool add( channel&c, analogReaderFcn_t fcn, eventCallback_t cb ) noexcept
530 {
531 bool retValue;
532 (void)c.setCallback( cb );
533 retValue = add( c );
534 (void)c.setReader( fcn );
535 return retValue;
536 }
542 bool remove( channel& c ) noexcept;
549 bool watch( void ) noexcept;
551 inline bool operator()( void )
552 {
553 return watch();
554 }
556 };
558 }
559
561}
562
563#endif /*QOS_EDGE_CHECK*/
An analog input-channel object.
Definition input.hpp:330
bool setParameter(const event e, const analogValue_t p) noexcept override
Set the parameter for the specified event.
bool setTime(const event e, const qOS::duration_t t) noexcept override
Set the timeout for the specified event.
analogChannel(const uint8_t inputChannel, const analogValue_t lowerThreshold, const analogValue_t upperThreshold, const analogValue_t h=20)
Constructor for the analog input channel instance.
Definition input.hpp:376
uint8_t getCount(void) const noexcept override
Get pulsation count for the digital input.
Definition input.hpp:408
type getType(void) const noexcept override
Get the channel type.
Definition input.hpp:385
bool isShared(void) const noexcept override
Check if the channel value is shared with other channel with the same (pin) number.
Definition input.hpp:417
bool setReader(analogReaderFcn_t r) noexcept override
Assign the function that is in charge of reading the specific analog input.
Definition input.hpp:434
bool unShare(void) noexcept override
Unshares the specified input channel if was marked as shared.
Definition input.hpp:444
Definition input.hpp:97
virtual uint8_t getCount(void) const noexcept=0
Get pulsation count for the digital input.
virtual bool setTime(const event e, const qOS::duration_t t) noexcept=0
Set the timeout for the specified event.
event getEvent(void) const noexcept
Retrieves the last event for the given input channel.
Definition input.hpp:132
bool setCallback(const eventCallback_t cb) noexcept
Set the callback function when event are detected on the input input channel.
Definition input.hpp:141
virtual bool setParameter(const event e, const analogValue_t p) noexcept=0
Set the parameter for the specified event.
uint8_t getChannel(void) const noexcept
Get the channel(pin) number.
Definition input.hpp:166
void * getUserData(void) noexcept
Get the channel user-data.
Definition input.hpp:182
virtual type getType(void) const noexcept=0
Get the channel type.
virtual bool isShared(void) const noexcept=0
Check if the channel value is shared with other channel with the same (pin) number.
virtual bool unShare(void) noexcept=0
void setUserData(void *pUserData) noexcept
Set the channel user-data.
Definition input.hpp:174
bool setChannel(const uint8_t inputChannel) noexcept
Set/Change the channel(pin) number.
Definition input.hpp:151
A digital input-channel object.
Definition input.hpp:225
bool setTime(const event e, const qOS::duration_t t) noexcept override
Set the timeout for the specified event.
type getType(void) const noexcept override
Get the channel type.
Definition input.hpp:265
uint8_t getCount(void) const noexcept override
Get pulsation count for the digital input.
Definition input.hpp:288
digitalChannel(const uint8_t inputChannel, bool invert=false)
Constructor for the digital input channel instance.
Definition input.hpp:260
bool setParameter(const event e, const analogValue_t p) noexcept override
Set the parameter for the specified event.
bool isShared(void) const noexcept override
Check if the channel value is shared with other channel with the same (pin) number.
Definition input.hpp:297
bool setReader(digitalReaderFcn_t r) noexcept override
Assign the function that is in charge of reading the specific digital input.
Definition input.hpp:307
The digital input-channel watcher class.
Definition input.hpp:456
bool watch(void) noexcept
Watch for the state and events for all channels registered inside the watcher instance (Non-Blocking ...
bool remove(channel &c) noexcept
Remove a channel to the watcher instance.
bool add(channel &c, analogReaderFcn_t fcn, eventCallback_t cb) noexcept
Add a channel to the watcher instance.
Definition input.hpp:529
bool add(channel &c) noexcept
Add a channel to the watcher instance.
watcher(const digitalReaderFcn_t &rDigital, const analogReaderFcn_t &rAnalog, const qOS::duration_t timeDebounce=100_ms)
Constructor for the input-watcher instance.
Definition input.hpp:488
bool add(channel &c, digitalReaderFcn_t fcn, eventCallback_t cb) noexcept
Add a channel to the watcher instance.
Definition input.hpp:514
bool add(channel &c, eventCallback_t cb) noexcept
Add a channel to the watcher instance.
Definition input.hpp:502
watcher(const qOS::duration_t dt=100_ms)
Constructor for the input-watcher instance.
Definition input.hpp:476
A list object (Generic double-linked)
Definition list.hpp:119
A list-node object (Used internally)
Definition list.hpp:62
A non-blocking Timer object.
Definition timer.hpp:26
timeCount_t duration_t
The typedef that specified an time quantity, usually expressed in milliseconds.
Definition clock.hpp:18
timeCount_t clock_t
A unsigned integer to hold ticks count. Epochs counter.
Definition clock.hpp:15
int digitalValue_t
Definition input.hpp:69
void(*)(channel &) eventCallback_t
A pointer to the input-channel event callback.
Definition input.hpp:95
type
An enum class to define the types of input channels.
Definition input.hpp:63
digitalValue_t(*)(uint8_t) digitalReaderFcn_t
A pointer to the wrapper function that reads the specific digital input-channel.
Definition input.hpp:78
analogValue_t(*)(uint8_t) analogReaderFcn_t
A pointer to the wrapper function that reads the specific analog input-channel.
Definition input.hpp:86
event
An enum with all the possible events that can be detected by the watcher class for a specified input-...
Definition input.hpp:36
uint32_t analogValue_t
Definition input.hpp:70
OS/Kernel interfaces.
Definition bytebuffer.hpp:7