OSVR-Core
VrpnButtonServer.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_VrpnButtonServer_h_GUID_264EFE6E_C328_45AE_B296_10980DD053AE
26 #define INCLUDED_VrpnButtonServer_h_GUID_264EFE6E_C328_45AE_B296_10980DD053AE
27 
28 // Internal includes
29 #include "DeviceConstructionData.h"
31 
32 // Library/third-party includes
33 #include <vrpn_Button.h>
34 
35 // Standard includes
36 #include <cmath>
37 
38 namespace osvr {
39 namespace connection {
40  class VrpnButtonServer : public vrpn_Button_Filter,
41  public ButtonServerInterface {
42  public:
43  typedef vrpn_Button_Filter Base;
45  : vrpn_Button_Filter(init.getQualifiedName().c_str(), init.conn) {
46  m_setNumChannels(
47  std::min(*init.obj.getButtons(),
48  OSVR_ChannelCount(vrpn_BUTTON_MAX_BUTTONS)));
49  // Initialize data
50  memset(Base::buttons, 0, sizeof(Base::buttons));
51  memset(Base::lastbuttons, 0, sizeof(Base::lastbuttons));
52 
53  // Report interface out.
54  init.obj.returnButtonInterface(*this);
55  }
56 
57  virtual bool setValue(value_type val, OSVR_ChannelCount chan,
58  util::time::TimeValue const &tv) {
59  if (chan >= m_getNumChannels()) {
60  return false;
61  }
62  Base::buttons[chan] = val;
63  m_reportChanges(tv);
64  return true;
65  }
66 
67  virtual void setValues(value_type val[], OSVR_ChannelCount chans,
68  util::time::TimeValue const &tv) {
69  if (chans > m_getNumChannels()) {
70  chans = m_getNumChannels();
71  }
72  for (OSVR_ChannelCount i = 0; i < chans; ++i) {
73  Base::buttons[i] = val[i];
74  }
75  m_reportChanges(tv);
76  }
77 
78  private:
79  OSVR_ChannelCount m_getNumChannels() {
80  return static_cast<OSVR_ChannelCount>(Base::num_buttons);
81  }
82  void m_setNumChannels(OSVR_ChannelCount chans) {
83  Base::num_buttons = chans;
84  }
85  void m_reportChanges(util::time::TimeValue const &tv) {
86  util::time::toStructTimeval(Base::timestamp, tv);
87  Base::report_changes();
88  }
89  };
90 
91 } // namespace connection
92 } // namespace osvr
93 
94 #endif // INCLUDED_VrpnButtonServer_h_GUID_264EFE6E_C328_45AE_B296_10980DD053AE
uint32_t OSVR_ChannelCount
The integer type specifying a number of channels/sensors or a channel/sensor index.
Definition: ChannelCountC.h:51
virtual void setValues(value_type val[], OSVR_ChannelCount chans, util::time::TimeValue const &tv)
Sets the values from the val array at channels [0, chans), and reports changes with the given timesta...
Definition: VrpnButtonServer.h:67
Interface for external access to generating button reports.
Definition: ButtonServerInterface.h:45
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
virtual bool setValue(value_type val, OSVR_ChannelCount chan, util::time::TimeValue const &tv)
Sets the value to val at channel chan, and reports changes with the given timestamp.
Definition: VrpnButtonServer.h:57
Definition: VrpnButtonServer.h:40
Definition: DeviceConstructionData.h:41
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
void returnButtonInterface(osvr::connection::ButtonServerInterface &iface)
Returns a button interface through the pointer-pointer.
Definition: DeviceInitObject.cpp:90