appkit  1.5.1
RoadNarrows Robotics Application Kit
StateKb.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_appkit
8 //
9 // File: StateKb.h
10 //
11 /*! \file
12  *
13  * $LastChangedDate: 2013-05-06 10:03:14 -0600 (Mon, 06 May 2013) $
14  * $Rev: 2907 $
15  *
16  * \brief Keyboard StateKb derived state class interface.
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  * \author Daniel Packard (daniel@roadnarrows.com)
20  *
21  * \par Copyright
22  * \h_copy 2012-2017. RoadNarrows LLC.\n
23  * http://www.roadnarrows.com\n
24  * All Rights Reserved
25  */
26 /*
27  * @EulaBegin@
28  *
29  * Permission is hereby granted, without written agreement and without
30  * license or royalty fees, to use, copy, modify, and distribute this
31  * software and its documentation for any purpose, provided that
32  * (1) The above copyright notice and the following two paragraphs
33  * appear in all copies of the source code and (2) redistributions
34  * including binaries reproduces these notices in the supporting
35  * documentation. Substantial modifications to this software may be
36  * copyrighted by their authors and need not follow the licensing terms
37  * described here, provided that the new terms are clearly indicated in
38  * all files where they apply.
39  *
40  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
41  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
42  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
43  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
44  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
45  * THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
48  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
49  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
50  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
51  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
52  *
53  * @EulaEnd@
54  */
55 ////////////////////////////////////////////////////////////////////////////////
56 
57 #ifndef _RNR_STATE_KB_H
58 #define _RNR_STATE_KB_H
59 
60 #include <stdio.h>
61 #include <termios.h>
62 
63 #include <string>
64 #include <map>
65 
66 #include "rnr/rnrconfig.h"
67 
68 #include "rnr/appkit/State.h"
69 
70 /*!
71  * \brief RoadNarrows Robotics
72  */
73 namespace rnr
74 {
75  //----------------------------------------------------------------------------
76  // StateKb Class
77  //----------------------------------------------------------------------------
78 
79  /*!
80  * Keyboard state class.
81  *
82  * The keyboard state receives non-blocking input from stdin.
83  * The events are:
84  * \li KbEventError
85  * \li KbEventEof
86  * \li KbEvnetTimeOut
87  * \li 8-bit ascii code
88  */
89  class StateKb : public State
90  {
91  public:
92  static int ClassObjRefCnt; ///< object reference count
93  static int OrigInputStatusFlags; ///< original status flags
94  static struct termios OrigInputTio; ///< original terminal i/o
95 
96  //
97  // Specical keyboard event ids.
98  //
99  static const int KbEventError = -2; ///< input error
100  static const int KbEventEof = -1; ///< end of file
101  static const int KbEventTimeOut = 256; ///< time out
102 
103  /*!
104  * \brief Initialization constructor.
105  *
106  * \param nStateId State id. Must be state machine unique.
107  * \param strStateName State name.
108  * \param strRefTag State reference id.
109  */
110  StateKb(int nStateId,
111  const std::string &strStateName="",
112  const std::string &strRefTag="") :
113  State(nStateId, strStateName, strRefTag)
114  {
115  if( ++ClassObjRefCnt == 1 )
116  {
117  configInput();
118  }
119  }
120 
121  /*!
122  * \brief List constructor.
123  *
124  * \param nStateId State id. Must be state machine unique.
125  * \param strStateName State name.
126  * \param strRefTag State reference id.
127  * \param listStateEvents Declaration list of allocated state events.
128  * NULL terminated.
129  */
130  StateKb(int nStateId,
131  const std::string &strStateName,
132  const std::string &strRefTag,
133  StateEvent *listStateEvents[]) :
134  State(nStateId, strStateName, strRefTag, listStateEvents)
135  {
136  if( ++ClassObjRefCnt == 1 )
137  {
138  configInput();
139  }
140  }
141 
142  /*!
143  * \brief Destructor.
144  */
145  virtual ~StateKb()
146  {
147  if( --ClassObjRefCnt == 0 )
148  {
149  restoreInput();
150  }
151  }
152 
153  /*!
154  * \brief Receive next event.
155  *
156  * This Keyboard state class receives keyboard input.
157  *
158  * The receive event may set state internal variables used specifically by
159  * the state.
160  *
161  * \return Returns event id.
162  */
163  virtual int receiveEvent();
164 
165  protected:
166  friend class StateEvent; ///< friend
167 
168  /*!
169  * \brief Configure stdin input.
170  */
171  void configInput();
172 
173  /*!
174  * \brief Restore stdin original configuration.
175  */
176  void restoreInput();
177  };
178 
179 } // namespace rnr
180 
181 
182 #endif // _RNR_STATE_KB_H
static int ClassObjRefCnt
object reference count
Definition: StateKb.h:92
static const int KbEventEof
end of file
Definition: StateKb.h:100
virtual ~StateKb()
Destructor.
Definition: StateKb.h:145
static struct termios OrigInputTio
original terminal i/o
Definition: StateKb.h:94
static const int KbEventTimeOut
time out
Definition: StateKb.h:101
void restoreInput()
Restore stdin original configuration.
Definition: StateKb.cxx:194
void configInput()
Configure stdin input.
Definition: StateKb.cxx:170
static const int KbEventError
input error
Definition: StateKb.h:99
virtual int receiveEvent()
Receive next event.
Definition: StateKb.cxx:104
StateKb(int nStateId, const std::string &strStateName, const std::string &strRefTag, StateEvent *listStateEvents[])
List constructor.
Definition: StateKb.h:130
State base class interface.
static int OrigInputStatusFlags
original status flags
Definition: StateKb.h:93
StateKb(int nStateId, const std::string &strStateName="", const std::string &strRefTag="")
Initialization constructor.
Definition: StateKb.h:110
RoadNarrows Robotics.
Definition: Camera.h:74