peripherals  1.4.2
RoadNarrows Robotics Hardware Peripherals Package
HID.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Peripherals
4 //
5 // Library: libhid
6 //
7 // File: HID.h
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2015-02-25 14:46:44 -0700 (Wed, 25 Feb 2015) $
12  * $Rev: 3872 $
13  *
14  * \brief Common Human Interface Device Interface.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  * \author Daniel Packard (daniel@roadnarrows.com)
18  *
19  * \copyright
20  * \h_copy 2012-2017. RoadNarrows LLC.\n
21  * http://www.roadnarrows.com\n
22  * All Rights Reserved
23  */
24 /*
25  * @EulaBegin@
26  * @EulaEnd@
27  */
28 ////////////////////////////////////////////////////////////////////////////////
29 
30 #ifndef _HID_H
31 #define _HID_H
32 
33 #include <string>
34 #include <map>
35 
36 #include "rnr/rnrconfig.h"
37 #include "rnr/log.h"
38 
39 
40 /*!
41  * \ingroup periph_hid
42  * \defgroup periph_hid_base HID Abstract Interfaces
43  *
44  * \{
45  *
46  * Human Interface Device abstract interfaces.
47  */
48 
49 //
50 // Common HID feature states
51 //
52 #define HID_BTTN_UP 0 ///< button/key state is up or unpressed
53 #define HID_BTTN_DOWN 1 ///< button/key state is down or pressed
54 
55 #define HID_FEAT_INPUT 0x01 ///< input to host feature
56 #define HID_FEAT_OUTPUT 0x02 ///< output to device feature
57 /*!
58  * \}
59  */
60 
61 
62 /*!
63  * \brief RoadNarrows Robotics standard namespace.
64  */
65 namespace rnr
66 {
67  /*!
68  * \ingroup periph_hid_base
69  * \brief Supported HID classes.
70  */
71  typedef enum
72  {
73  HIDClassUnknown = 0, ///< class is unknown or not initialized
74 
75  HIDClassKeyboardMouse, ///< keyboards/mice class
76  HIDClassXbox360, ///< Microsoft Xbox360 game console family
77 
78  HIDClassNumOf ///< number of supported classes (keep last)
79  } HIDClass;
80 
81  /*!
82  * \ingroup periph_hid_base
83  * \brief Feature property types.
84  */
85  typedef enum
86  {
87  HIDFeatTypeUnknown = 0, ///< unknown feature property type
88 
89  HIDFeatTypeBiState, ///< binary state property type
90  HIDFeatTypeRange, ///< range between [min,max] property type
91  HIDFeatTypeEnum, ///< discrete enumeration feature property type
92 
93  HIDFeatTypeNumOf ///< number of feature property types(keep last)
94  } HIDFeatType;
95 
96  /*!
97  * \ingroup periph_hid_base
98  * \brief Input Human Interface Device Abstract Base Class.
99  */
100  class HIDInput
101  {
102  public:
103  static const int MNEM_START = 1024; ///< user mapped mnemonic starting index
104  static const int T_UPDATE_DFT = 33; ///< default update timeout (msec)
105 
106  typedef std::map<int, int> FeatMap_T; ///< device feature id - mnemonic map
107 
108  /*!
109  * \brief Default initialization constructor.
110  *
111  * \param eHIDClass Actual input HID class.
112  */
113  HIDInput(HIDClass eHIDClass=HIDClassUnknown)
114  : m_eHIDClass(eHIDClass),
115  m_bIsConnected(false),
116  m_bIsLinked(false),
117  m_nError(0),
118  m_bFatal(false)
119  {
120  }
121 
122  /*!
123  * \brief Destructor.
124  */
125  virtual ~HIDInput() { }
126 
127  /*!
128  * \brief Open connection to HID.
129  *
130  * \copydoc doc_return_std
131  */
132  virtual int open() = 0;
133 
134  /*!
135  * \brief Close connection to HID.
136  *
137  * \copydoc doc_return_std
138  */
139  virtual int close() = 0;
140 
141  /*!
142  * \brief Read device and update HID state.
143  *
144  * \param uMSec Block wait at most the given milliseconds for events.\n
145  * If zero, then update() will handle any already-pending
146  * events and then immediately return (non-blocking).\n
147  * Otherwise, if no events are currently pending, update()
148  * will block waiting for events for up specified timeout.
149  * If an event arrives update() will return early.
150  *
151  * \copydoc doc_return_std
152  */
153  virtual int update(uint_t uMSec=T_UPDATE_DFT) = 0;
154 
155  /*!
156  * \brief Get the HID class.
157  *
158  * \return Returns \ref HIDClass
159  */
160  virtual HIDClass getHIDClass() { return m_eHIDClass; }
161 
162  /*!
163  * \brief Query if HID is connected.
164  *
165  * \return Returns true if (physically) connected, else false.
166  */
167  virtual bool isConnected() { return m_bIsConnected; }
168 
169  /*!
170  * \brief Query if HID is linked.
171  *
172  * \return Returns true if linked and responding, else false.
173  */
174  virtual bool isLinked() { return m_bIsLinked; }
175 
176  /*!
177  * \brief Associate user mnemonic to the device feature (e.g. button) id.
178  *
179  * \param iFeatId Device-specific feature id (or previous mnemonic).
180  * \param iMnem User-specific mnemonic.
181  *
182  * \copydoc doc_return_std
183  */
184  virtual int assocFeature(int iFeatId, int iMnem)
185  {
186  FeatMap_T::iterator pos;
187  int iFeatIdFixed;
188 
189  if( (pos = m_featMap.find(iFeatId)) != m_featMap.end() )
190  {
191  iFeatIdFixed = pos->second;
192  m_featMap.erase(pos);
193  m_featMap[iMnem] = iFeatIdFixed;
194  return OK;
195  }
196  else
197  {
198  return RC_ERROR;
199  }
200  }
201 
202  /*!
203  * \brief Get the value associated with the mapped user mnemonic.
204  *
205  * \param iMnem User-specific mnemonic.
206  *
207  * \return Feature current value.
208  */
209  virtual int getFeatureVal(int iMnem) = 0;
210 
211  /*!
212  * \brief Set the value associated with the mapped user mnemonic.
213  *
214  * \param iMnem User-specific mnemonic.
215  * \param [in] nVal New input HID output value (e.g.LED).
216  * Meaning is specific to input.
217  *
218  * \copydoc doc_return_std
219  */
220  virtual int setFeatureVal(int iMnem, int nVal) = 0;
221 
222  /*!
223  * \brief Get the feature properties.
224  *
225  * \param iMnem User-specific mnemonic.
226  * \param [out] eFeatType Feature property type.
227  * \param [out] nDir Feature is an input (HID_FEAT_INPUT) to host
228  * and/or an output (HID_FEAT_OUTPUT) to device.
229  * \param [out] nMin Feature minimum value.
230  * \param [out] nMax Feature maximum value.
231  * \param [out] nStep Feature step size between [min,max]
232  *
233  * \copydoc doc_return_std
234  */
235  virtual int getFeatureProp(int iMnem,
236  HIDFeatType &eFeatType,
237  int &nDir,
238  int &nMin,
239  int &nMax,
240  int &nStep) = 0;
241 
242  /*!
243  * \brief Get the current full state of the device.
244  *
245  * \return Return pointer to the device-specific input state.
246  */
247  virtual void *getCurrentState() { return NULL; }
248 
249  /*!
250  * \brief Ping device if it is connected and is responding.
251  *
252  * \return Returns true if the HID is responding, else false.
253  */
254  virtual bool ping() { return false; }
255 
256  /*!
257  * \brief Test to see if HID is in a fatal errored state.
258  *
259  * \return Returns true if fatal, else false.
260  */
261  virtual bool isFatal() const { return m_bFatal; }
262 
263  /*!
264  * \brief Get the last HID-specific error number.
265  *
266  * Errors are \h_lt 0, no error is 0.
267  *
268  * \return Returns HID-specific error number.
269  */
270  virtual int getError() const { return m_nError; }
271 
272  /*!
273  * \brief Get the string associated with the error number.
274  *
275  * \param nError HID-specific error number.
276  *
277  * \return Null-terminated error string.
278  */
279  virtual const char *getStrError(int nError) const
280  {
281  return nError < 0? "Error": "";
282  }
283 
284  /*!
285  * \brief Get HID product name.
286  *
287  * \return String.
288  */
289  virtual std::string getProdName()
290  {
291  return m_strProdName;
292  }
293 
294  protected:
295  HIDClass m_eHIDClass; ///< actual HID class
296  std::string m_strProdName; ///< product name
297  bool m_bIsConnected; ///< HID is [not] physically connected
298  bool m_bIsLinked; ///< HID is [not] linked and communicating
299  int m_nError; ///< last error number (HID specific)
300  bool m_bFatal; ///< HID is [not] in a fatal condition
301  FeatMap_T m_featMap; ///< feature-userid association map
302 
303  /*!
304  * \brief Set connection state.
305  *
306  * \param bNewState New connection state.
307  */
308  virtual void setConnectionState(bool bNewState)
309  {
310  if( bNewState != m_bIsConnected )
311  {
312  LOGDIAG3("HID is %s.", (bNewState? "connected": "not connected"));
313  }
314  m_bIsConnected = bNewState;
315  if( !m_bIsConnected )
316  {
317  setLinkState(false);
318  }
319  }
320 
321  /*!
322  * \brief Set new link state.
323  *
324  * \param bNewState New link state.
325  */
326  virtual void setLinkState(bool bNewState)
327  {
328  if( bNewState != m_bIsLinked )
329  {
330  LOGDIAG3("HID is %s.", (bNewState? "linked": "not linked"));
331  }
332  m_bIsLinked = bNewState;
333  }
334  };
335 } // namespace rnr
336 
337 
338 #endif // _HID_H
virtual std::string getProdName()
Get HID product name.
Definition: HID.h:289
unknown feature property type
Definition: HID.h:87
static const int T_UPDATE_DFT
default update timeout (msec)
Definition: HID.h:104
virtual int getError() const
Get the last HID-specific error number.
Definition: HID.h:270
std::string m_strProdName
product name
Definition: HID.h:296
virtual int getFeatureVal(int iMnem)=0
Get the value associated with the mapped user mnemonic.
keyboards/mice class
Definition: HID.h:75
virtual int setFeatureVal(int iMnem, int nVal)=0
Set the value associated with the mapped user mnemonic.
std::map< int, int > FeatMap_T
device feature id - mnemonic map
Definition: HID.h:106
int m_nError
last error number (HID specific)
Definition: HID.h:299
static const int MNEM_START
user mapped mnemonic starting index
Definition: HID.h:103
HIDInput(HIDClass eHIDClass=HIDClassUnknown)
Default initialization constructor.
Definition: HID.h:113
number of supported classes (keep last)
Definition: HID.h:78
virtual int close()=0
Close connection to HID.
FeatMap_T m_featMap
feature-userid association map
Definition: HID.h:301
virtual HIDClass getHIDClass()
Get the HID class.
Definition: HID.h:160
virtual void * getCurrentState()
Get the current full state of the device.
Definition: HID.h:247
virtual void setConnectionState(bool bNewState)
Set connection state.
Definition: HID.h:308
virtual bool isConnected()
Query if HID is connected.
Definition: HID.h:167
range between [min,max] property type
Definition: HID.h:90
virtual ~HIDInput()
Destructor.
Definition: HID.h:125
virtual int assocFeature(int iFeatId, int iMnem)
Associate user mnemonic to the device feature (e.g. button) id.
Definition: HID.h:184
bool m_bIsLinked
HID is [not] linked and communicating.
Definition: HID.h:298
Input Human Interface Device Abstract Base Class.
Definition: HID.h:100
virtual bool ping()
Ping device if it is connected and is responding.
Definition: HID.h:254
virtual int update(uint_t uMSec=T_UPDATE_DFT)=0
Read device and update HID state.
binary state property type
Definition: HID.h:89
HIDClass m_eHIDClass
actual HID class
Definition: HID.h:295
virtual bool isLinked()
Query if HID is linked.
Definition: HID.h:174
virtual bool isFatal() const
Test to see if HID is in a fatal errored state.
Definition: HID.h:261
HIDFeatType
Feature property types.
Definition: HID.h:85
virtual const char * getStrError(int nError) const
Get the string associated with the error number.
Definition: HID.h:279
discrete enumeration feature property type
Definition: HID.h:91
bool m_bFatal
HID is [not] in a fatal condition.
Definition: HID.h:300
bool m_bIsConnected
HID is [not] physically connected.
Definition: HID.h:297
virtual void setLinkState(bool bNewState)
Set new link state.
Definition: HID.h:326
number of feature property types(keep last)
Definition: HID.h:93
class is unknown or not initialized
Definition: HID.h:73
RoadNarrows Robotics standard namespace.
Definition: HID.h:65
HIDClass
Supported HID classes.
Definition: HID.h:71
virtual int open()=0
Open connection to HID.
virtual int getFeatureProp(int iMnem, HIDFeatType &eFeatType, int &nDir, int &nMin, int &nMax, int &nStep)=0
Get the feature properties.
Microsoft Xbox360 game console family.
Definition: HID.h:76