iop_sdevice.h
Go to the documentation of this file.
1 /** @file iop_sdevice.h Virtual device for signal based io */
2 
3 /*
4  FAU Discrete Event Systems Library (libfaudes)
5 
6  Copyright (C) 2008, Thomas Moor
7  Exclusive copyright is granted to Klaus Schmidt
8 
9 */
10 
11 
12 
13 #ifndef FAUDES_SDEVICE_H
14 #define FAUDES_SDEVICE_H
15 
16 #include "corefaudes.h"
17 #include "iop_vdevice.h"
18 
19 
20 
21 namespace faudes {
22 
23 
24 /**
25  * Configuration of a signal based output mapping.
26  *
27  * An output mapping consists of a list of actions that to be performed when the
28  * event is executed. Each action may set or clear the physical output line
29  * specified by an abstract bitaddress.
30  *
31  */
32 
34 
36 
37 public:
38 
39  /** Default constructor (no actions at all) */
41 
42  /** Copy - constructor */
44  { DoAssign(rOtherAttr); };
45 
46  /** Test for default value (never) */
47  virtual bool IsDefault(void) const {return false;};
48 
49  /** Clear */
50  virtual void Clear(void) {mActions.clear();};
51 
52  /** Enum for action */
53  typedef enum { Set, Clr, Inv } Value;
54 
55  /** Typedef for a single output action */
56  class Action {
57  public:
58  Action(void) {mBit=0;mValue=Clr;};
59  int mBit; //// Bitaddress (numeric)
60  Value mValue; //// Value to set (enum: set,clr,inv)
61  };
62 
63  /** List of actions to perform */
64  std::vector<Action> mActions;
65 
66  protected:
67 
68  /**
69  * Copy method
70  *
71  * @param rSrcAttr
72  * Source to copy from
73  * @return
74  * Ref to this attribute
75  */
76  void DoAssign(const AttributeSignalOutput& rSrcAttr);
77 
78  /**
79  * Reads the attribute from TokenReader, see AttributeVoid for public wrappers.
80  *
81  * If the current token indicates an output mapping, the method reads that
82  * section. Else it does nothing. Exceptions may only be thrown
83  * on invalid data within the section. The label argument is ignored, we the hardcoded
84  * output for output device attributes. The context argument is ignored.
85  *
86  * @param rTr
87  * TokenReader to read from
88  * @param rLabel
89  * Section to read
90  * @param pContext
91  * Read context to provide contextual information
92  *
93  * @exception Exception
94  * - IO error (id 1)
95  */
96  virtual void DoRead(TokenReader& rTr,const std::string& rLabel="", const Type* pContext=0);
97 
98  /**
99  * Writes the attribute to TokenWriter, see AttributeVoid for public wrappers.
100  *
101  * Writes the output mapping data. The label argument is ignored, we use
102  * the hardcoded section "Output". The context argument is ignored.
103  *
104  * @param rTw
105  * TokenWriter to write to
106  * @param rLabel
107  * Section to write
108  * @param pContext
109  * Read context to provide contextual information
110  *
111  * @exception Exception
112  * - IO error (id 2)
113  */
114  virtual void DoWrite(TokenWriter& rTw,const std::string& rLabel="", const Type* pContext=0) const;
115 
116 }; // end class AttributeSignalOutput
117 
118 
119 
120 
121 
122 
123 /**
124  * Configuration of a signal based input mapping.
125  *
126  * A input mapping consists of a list of abstract bitaddresses with an
127  * edge polarity each. When on one of respecive physical input lines an edge
128  * with matching priority is sensed, the logical event is triggered.
129  *
130  */
131 
133 
135 
136 public:
137 
138  /** Default constructor (no triggers) */
140 
141  /** Copy constructor */
143  { DoAssign(rOtherAttr); };
144 
145  /** Test for default value (never) */
146  virtual bool IsDefault(void) const {return false;};
147 
148  /** Clear */
149  virtual void Clear(void) {mTriggers.clear();};
150 
151  /** Typedef for a single input trigger */
152  class Trigger {
153  public:
154  Trigger(void) {mBit=0; mPos=false; mNeg=false;};
155  int mBit; //// Bitaddress
156  bool mPos; //// Positive edge triggers event
157  bool mNeg; //// Negative edge triggers event
158  };
159 
160  /** List of triggers */
161  std::vector<Trigger> mTriggers;
162 
163  protected:
164 
165  /**
166  * Copy method
167  *
168  * @param rSrcAttr
169  * Source to copy from
170  * @return
171  * Ref to this attribute
172  */
173  void DoAssign(const AttributeSignalInput& rSrcAttr);
174 
175  /**
176  * Reads the attribute from TokenReader, see AttributeVoid for public wrappers.
177  *
178  * If the current token indicates a input mapping, the method reads that
179  * section. Else it does nothing. Exceptions may only be thrown
180  * on invalid data within the section. The label argument is ignored, we use the
181  * hardcoded section "Input" for input attributes. The context argument is ignored.
182  *
183  * @param rTr
184  * TokenReader to read from
185  * @param rLabel
186  * Section to read
187  * @param pContext
188  * Read context to provide contextual information
189  *
190  * @exception Exception
191  * - IO error (id 1)
192  */
193  virtual void DoRead(TokenReader& rTr,const std::string& rLabel="", const Type* pContext=0);
194 
195 
196  /**
197  * Writes the attribute to TokenWriter, see AttributeVoid for public wrappers.
198  *
199  * Writes the input mapping data.The label argument is ignored, we use the
200  * hardcoded section "Input". The context argument is ignored.
201  *
202  * @param rTw
203  * TokenWriter to write to
204  * @param rLabel
205  * Section to write
206  * @param pContext
207  * Read context to provide contextual information
208  *
209  * @exception Exception
210  * - IO error (id 2)
211  */
212 
213  virtual void DoWrite(TokenWriter& rTw,const std::string& rLabel="", const Type* pContext=0) const;
214 
215 }; // end class AttributeSignalInput
216 
217 
218 
219 
220 /**
221  * Configuration of a signal based input or output
222  *
223  * This class is derived from the AttributeDeviceEvent to specialise
224  * for signal based input and output mapping.
225  *
226  */
227 
229 
231 
232  public:
233 
234  /** Default constructor (no mapping at all) */
235  AttributeSignalEvent(void);
236 
237  /** Copy constructor */
238  AttributeSignalEvent(const AttributeSignalEvent& rOtherAttr);
239 
240  /** Test for default value (never) */
241  virtual bool IsDefault(void) const {return false;};
242 
243  /** Clear */
244  virtual void Clear(void) { AttributeDeviceEvent::Clear(); };
245 
246  /** Get output mapping */
247  const AttributeSignalOutput* Outputp(void) const {
248  return static_cast<AttributeSignalOutput*>(mpOutputAttribute); };
250  return static_cast<AttributeSignalOutput*>(mpOutputAttribute); };
251 
252  /** Get input mapping */
253  const AttributeSignalInput* Inputp(void) const {
254  return static_cast<AttributeSignalInput*>(mpInputAttribute); };
256  return static_cast<AttributeSignalInput*>(mpInputAttribute); };
257 
258  protected:
259 
260  /** DoAssign */
261  void DoAssign(const AttributeSignalEvent& rSrc)
263 
264  /** Prototype, input (construct on first use static) */
265  static const AttributeSignalInput* InputPrototypep(void);
266 
267  /** Prototype, output (construct on first use static) */
268  static const AttributeSignalOutput* OutputPrototypep(void);
269 
270 }; // class AttributeSignalEvent
271 
272 
273 
274 /**
275  * An sDevice implements signal based semantics for faudes events.
276  *
277  * The class is configured by AttributeSignalEvents, which in turn consist of
278  * either a AttributeSignalInput or a AttributeSignalOutput. Thus,
279  * we set and clear output signals for output events, while edges on input
280  * signals trigger input events.
281  *
282  * Sensing edges is done by separate thread that is started by Start().
283  *
284  * This class is still virtual in that it does not provide the code for actually
285  * reading or writing signals. See cDevice and pDevice for derived device classes
286  * that implement digital io via comedi or the parallel printer port, respectively.
287  *
288  * @ingroup IODevicePlugin
289  */
290 
291 class FAUDES_API sDevice : public vDevice {
292 
293  // provide all access to background task
294  friend void* SDeviceSynchro(void*);
295 
296  public:
297 
298  // have my types
302 
303  /**
304  * Default constructor
305  */
306  sDevice(void);
307 
308  /**
309  * copy-constructor
310  */
311  sDevice(const sDevice& rOtherDevice);
312 
313  /**
314  * Explicit destructor.
315  */
316  virtual ~sDevice(void);
317 
318 
319  /**
320  * Clear all configuration.
321  * This implies Stop().
322  */
323  virtual void Clear(void);
324 
325  /**
326  *
327  * Build up internal data-structure (e.g. signal-event - mapping)
328  *
329  */
330  virtual void Compile(void);
331 
332  /**
333  * Insert event as input event.
334  * Note: you can only configure events
335  * while the device is down.
336  *
337  * @param event
338  * Event by name.
339  */
340  void InsInputEvent(const std::string& event);
341 
342 
343  /**
344  * Add a trigger condition.
345  * Note: you can only add a trigger condition to an
346  * input event; you can only (re)-configure events
347  * while the device is down.
348  *
349  * @param event
350  * Event
351  * @param trigger
352  * Trigger to append
353  */
354  void AppendTrigger(const std::string& event, const Trigger& trigger);
355 
356  /**
357  * Insert event as output event.
358  * Note: you can only configure event
359  * while the device is down.
360  *
361  * @param event
362  * Event by name.
363  */
364  void InsOutputEvent(const std::string& event);
365 
366  /**
367  * Add an action.
368  * Note: you can only add a actions to an
369  * output event; you can only (re)-configure events
370  * while the device is down.
371  *
372  * @param event
373  * Event
374  * @param action
375  * Action to append
376  */
377  void AppendAction(const std::string& event, const Action& action);
378 
379  /**
380  * Report max bit address.
381  *
382  * @return
383  * Set of all configured inputs
384  */
385  int MaxBitAddress(void) const { return mMaxBitAddress;};
386 
387 
388  /**
389  * Activate the device. This function enables output execution and input reading.
390  * It starts the background thread for edge detection and input event buffer.
391  *
392  * @exception Exception
393  * - Not yet configured (id 551)
394  */
395  virtual void Start(void);
396 
397  /**
398  * Deactivate the device. This function disables output execution and input reading.
399  * It stops the backhround thread and resets all output signals to 0.
400  */
401  virtual void Stop(void);
402 
403 
404  /**
405  * Clear dynamic data and restart device
406  */
407  virtual void Reset(void);
408 
409 
410  /**
411  * Run output command.
412  *
413  * @exception Exception
414  * - unknown output event (id 65)
415  */
416  virtual void WriteOutput(Idx output);
417 
418 
419 
420  /**
421  * Set output signal.
422  *
423  *
424  * This function provides user level access to output signals.
425  * It executes the virtual pre and post hook methods
426  * and the virtual actual harware access to set the signal level
427  * via DoWriteSignal(int,bool).
428  *
429  * @param bitaddr
430  * Abstract bit address
431  * @param value
432  * True for active/high/1/set;
433  * false for passive/low/0/clr;
434  *
435  */
436  void WriteSignal(int bitaddr, bool value);
437 
438  /**
439  * Get input signal.
440  *
441  * This function provides user level access to input signals.
442  * It executes the virtual pre and post hook methods
443  * and the virtual actual harware access to sample an input signal via DoReadSignal(int).
444  *
445  * @param bitaddr
446  * Abstract bit address
447  * @return
448  * True for logic level high;
449  */
450  bool ReadSignal(int bitaddr);
451 
452 
453  /**
454  * Report cycle time
455  *
456  * @return
457  * Actual cycle time in ussecs
458  */
459 
460  virtual int CycleTime() const;
461 
462  /**
463  * Set cycle time
464  *
465  * @param cycleTime
466  * Desired cycle time in usecs
467  */
468 
469  virtual void CycleTime(int cycleTime);
470 
471  protected:
472 
473  /** Overall configuration (with actual type) */
475 
476  /** Reverse input map: map signal edges to logical event sets */
477  std::map<int, EventSet> mInputPosEdgeIndexMap;
478 
479  /** Reverse input map: map signal edges to logical event sets */
480  std::map<int, EventSet> mInputNegEdgeIndexMap;
481 
482  /** Reverse output map: map signal addres to events that affect the resp. line */
483  std::map<int, EventSet> mOutputLevelIndexMap;
484 
485  /** Address range */
487 
488  /**
489  * Writes non-event-related configuration to TokenWriter
490  *
491  * Device data will be written bottom-to-top along the class-hierarchy,
492  * see also vDevice::DoWritePreface.
493  *
494  * Note: in order to keep the outputfile-layout as simple as possible no label will
495  * be used to separate this data-section.
496  *
497  * @param rTw
498  * TokenWriter to write
499  * @param rLabel
500  * Section to write
501  * @param pContext
502  * Context to provide contextual information
503  *
504  * */
505  void DoWritePreface(TokenWriter& rTw, const std::string& rLabel, const Type* pContext=0) const;
506 
507  /**
508  * Reads non-event-related configuration from TokenReader
509  *
510  * Device date is read bottom-to-top along the class-hierarchy;
511  * see also vDevice::DoReadPreface.
512  *
513  *
514  * Note: in order to keep the inputfile-layout as simple as possible no label will
515  * be used to separate this data-section.
516  *
517  * @param rTr
518  * TokenReader to read from
519  * @param rLabel
520  * Section to read
521  * @param pContext
522  * Read context to provide contextual information
523  *
524  * */
525  virtual void DoReadPreface(TokenReader& rTr,const std::string& rLabel="", const Type* pContext=0);
526 
527  /**
528  * IO Hook, inputs
529  *
530  * The background thread calls this hook before reading input signals.
531  * You may reimplement this method to e.g. prepare a process image.
532  *
533  * @return
534  * True on success, false on error
535  *
536  */
537  virtual bool DoReadSignalsPre(void) {return true;};
538 
539  /**
540  * IO Hook, inputs
541  *
542  * The background thread calls this hook after reading input signals.
543  * You may reimplement this method to e.g. release a process image.
544  *
545  *
546  */
547  virtual void DoReadSignalsPost(void) {};
548 
549  /**
550  * Sample input signal.
551  *
552  * Reimplement this function to implements actual harware access to sample an input signal.
553  * This function gets
554  * called from the periodic background thread to sense edges.
555  * It is guaranteed that the pre-hook was called befor and returned "true".
556  * The post-hook will be invoked after relevant signals have been sampled.
557  *
558  * @param bitaddr
559  * Abstract bit address
560  * @return
561  * True for logic level high;
562  */
563  virtual bool DoReadSignal(int bitaddr)=0;
564 
565  /**
566  * IO Hook, outputs
567  *
568  * This hook is invoked before writing a signal value.
569  * You may reimplement this method to e.g. assemble a process image.
570  *
571  * @return
572  * True on success, false on error
573  *
574  */
575  virtual bool DoWriteSignalsPre(void) {return true;};
576 
577  /**
578  * IO Hook, output
579  *
580  * This hook is invoked after writing a signal value.
581  * You may reimplement this method to e.g. assemble a process image.
582  *
583  *
584  */
585  virtual void DoWriteSignalsPost(void) {};
586 
587  /**
588  * Reimplement this function in a derived class for actual harware access
589  * to set or clear an output signal. Hooks are executed appropriately.
590  * If the device is configured for synchronous write, the background task
591  * is used to write signals. Otherwise, signal values are written instantly.
592  *
593  * @param bitaddr
594  * Abstract bit address
595  * @param value
596  * True for active/high/1/set;
597  * false for passive/low/0/clr;
598  *
599  */
600  virtual void DoWriteSignal(int bitaddr, bool value)=0;
601 
602 
603  /**
604  * Loop hook.
605  * This function is called once during each cycle of the
606  * backgroud thread
607  *
608  */
609  virtual void DoLoopCallback(void) {};
610 
611 
612  /** Background: mutex for below shared variables*/
613  faudes_mutex_t mMutex;
614 
615  /** True for synchronous output writes */
617 
618  private:
619 
620  /** Background: thread handle (global) */
621  faudes_thread_t mThreadSynchro;
622 
623  /** Cycle time of background thread in nsecs (shared) */
625 
626  /** Background: cycle counter (shared) */
628 
629  /** Background: cycle count (global only) */
631 
632  /** Background: type def for edge detection */
633  typedef struct {
634  bool current; //// current value (most recent reading)
635  bool past; //// past value (reading before)
636  bool posrel; //// positive edge is relevant for some input event
637  bool negrel; //// negative edge is relevant for some input event
638  bool pos; //// positive edge detected
639  bool neg; //// negative edge detected
640  bool lost; //// relevant edge has been lost
641  } Edges;
642 
643  /** Background: accumulated edges (shared) */
645 
646  /** Background: recently accumulated edges (global only) */
648 
649  /** Background: some input event did occur (shared) */
651 
652  /** Background: type def output values */
653  typedef struct {
654  bool current; //// current value (actual line level)
655  bool next; //// next value (buffered line level, flush on sync write)
656  bool edge; //// value will change
657  bool lost; //// edgle will be lost
658  bool rel; //// relevant to some output event
659  } Levels;
660 
661  /** Background: accumulated output values (shared) */
663 
664  /** Background: terminate-flag for background task */
666 
667  /** clear all input states */
668  void ClrInputSignals(void);
669 
670  /** clear all output values */
671  void ClrOutputSignals(void);
672 
673 };
674 
675 // declare background thread
676 void* SDeviceSynchro(void*);
677 
678 }
679 
680 
681 #endif
682 
#define FAUDES_API
Interface export/import symbols: windows.
Definition: cfl_platform.h:81
#define FAUDES_TYPE_DECLARATION(ftype, ctype, cbase)
faudes type declaration macro
Definition: cfl_types.h:867
Attribute for the configuration of a input or output mapping.
Definition: iop_vdevice.h:68
virtual void Clear(void)
Clear.
Definition: iop_vdevice.cpp:82
void DoAssign(const AttributeDeviceEvent &rSrcAttr)
Assignment.
Definition: iop_vdevice.cpp:90
Configuration of a signal based input or output.
Definition: iop_sdevice.h:228
void DoAssign(const AttributeSignalEvent &rSrc)
DoAssign.
Definition: iop_sdevice.h:261
virtual void Clear(void)
Clear.
Definition: iop_sdevice.h:244
const AttributeSignalInput * Inputp(void) const
Get input mapping.
Definition: iop_sdevice.h:253
virtual bool IsDefault(void) const
Test for default value (never)
Definition: iop_sdevice.h:241
AttributeSignalOutput * Outputp(void)
Definition: iop_sdevice.h:249
const AttributeSignalOutput * Outputp(void) const
Get output mapping.
Definition: iop_sdevice.h:247
AttributeSignalInput * Inputp(void)
Definition: iop_sdevice.h:255
Typedef for a single input trigger.
Definition: iop_sdevice.h:152
Configuration of a signal based input mapping.
Definition: iop_sdevice.h:132
AttributeSignalInput(const AttributeSignalInput &rOtherAttr)
Copy constructor.
Definition: iop_sdevice.h:142
virtual void Clear(void)
Clear.
Definition: iop_sdevice.h:149
AttributeSignalInput(void)
Default constructor (no triggers)
Definition: iop_sdevice.h:139
virtual bool IsDefault(void) const
Test for default value (never)
Definition: iop_sdevice.h:146
std::vector< Trigger > mTriggers
List of triggers.
Definition: iop_sdevice.h:161
Typedef for a single output action.
Definition: iop_sdevice.h:56
Configuration of a signal based output mapping.
Definition: iop_sdevice.h:33
virtual void Clear(void)
Clear.
Definition: iop_sdevice.h:50
virtual bool IsDefault(void) const
Test for default value (never)
Definition: iop_sdevice.h:47
std::vector< Action > mActions
List of actions to perform.
Definition: iop_sdevice.h:64
AttributeSignalOutput(const AttributeSignalOutput &rOtherAttr)
Copy - constructor.
Definition: iop_sdevice.h:43
AttributeSignalOutput(void)
Default constructor (no actions at all)
Definition: iop_sdevice.h:40
Minimal Attribute.
Set of indices with symbolic names and attributes.
Definition: cfl_nameset.h:564
A TokenReader reads sequential tokens from a file or string.
A TokenWriter writes sequential tokens to a file, a string or stdout.
Base class of all libFAUDES objects that participate in the run-time interface.
Definition: cfl_types.h:239
An sDevice implements signal based semantics for faudes events.
Definition: iop_sdevice.h:291
sDevice(const sDevice &rOtherDevice)
copy-constructor
int mCycleCount
Background: cycle counter (shared)
Definition: iop_sdevice.h:627
bool mCancelRequest
Background: terminate-flag for background task.
Definition: iop_sdevice.h:665
virtual void DoReadSignalsPost(void)
IO Hook, inputs.
Definition: iop_sdevice.h:547
int mCycleTime
Cycle time of background thread in nsecs (shared)
Definition: iop_sdevice.h:624
int mMaxBitAddress
Address range.
Definition: iop_sdevice.h:486
AttributeSignalOutput::Value Value
Definition: iop_sdevice.h:301
virtual void DoWriteSignalsPost(void)
IO Hook, output.
Definition: iop_sdevice.h:585
std::map< int, EventSet > mInputNegEdgeIndexMap
Reverse input map: map signal edges to logical event sets.
Definition: iop_sdevice.h:480
Edges * mpRecentInputEdges
Background: recently accumulated edges (global only)
Definition: iop_sdevice.h:647
virtual bool DoWriteSignalsPre(void)
IO Hook, outputs.
Definition: iop_sdevice.h:575
faudes_thread_t mThreadSynchro
Background: thread handle (global)
Definition: iop_sdevice.h:621
int MaxBitAddress(void) const
Report max bit address.
Definition: iop_sdevice.h:385
Edges * mpInputEdges
Background: accumulated edges (shared)
Definition: iop_sdevice.h:644
bool mSyncWrite
True for synchronous output writes.
Definition: iop_sdevice.h:616
bool mInputReady
Background: some input event did occur (shared)
Definition: iop_sdevice.h:650
virtual void DoLoopCallback(void)
Loop hook.
Definition: iop_sdevice.h:609
faudes_mutex_t mMutex
Background: mutex for below shared variables.
Definition: iop_sdevice.h:609
virtual bool DoReadSignal(int bitaddr)=0
Sample input signal.
virtual void DoWriteSignal(int bitaddr, bool value)=0
Reimplement this function in a derived class for actual harware access to set or clear an output sign...
virtual bool DoReadSignalsPre(void)
IO Hook, inputs.
Definition: iop_sdevice.h:537
AttributeSignalInput::Trigger Trigger
Definition: iop_sdevice.h:299
TaNameSet< AttributeSignalEvent > * pConfiguration
Overall configuration (with actual type)
Definition: iop_sdevice.h:474
int mRecentCycleCount
Background: cycle count (global only)
Definition: iop_sdevice.h:630
std::map< int, EventSet > mInputPosEdgeIndexMap
Reverse input map: map signal edges to logical event sets.
Definition: iop_sdevice.h:477
AttributeSignalOutput::Action Action
Definition: iop_sdevice.h:300
std::map< int, EventSet > mOutputLevelIndexMap
Reverse output map: map signal addres to events that affect the resp.
Definition: iop_sdevice.h:483
Levels * mpOutputLevels
Background: accumulated output values (shared)
Definition: iop_sdevice.h:662
Virtual base class to define the interface for event io.
Definition: iop_vdevice.h:261
Includes all libFAUDES headers, no plugins.
Virtual device for interface definition
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)
void * SDeviceSynchro(void *arg)
Background: type def for edge detection
Definition: iop_sdevice.h:633
Background: type def output values
Definition: iop_sdevice.h:653

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen