OSSIA
Open Scenario System for Interactive Application
phidgetspp.hpp
1 #pragma once
2 #include <ossia/network/phidgets/detail/sensors.hpp>
3 
4 #include <eggs/variant.hpp>
5 
6 #include <algorithm>
7 #include <functional>
8 #include <iostream>
9 #include <memory>
10 #include <string>
11 #include <vector>
12 namespace ppp
13 {
14 class phidgets_manager;
15 
16 struct phidget_device
17 {
18  friend class phidgets_manager;
19 
20 public:
21  ~phidget_device();
22 
23  std::function<void(int, const char*)> onError;
24 
25  PhidgetHandle handle() const;
26  const std::string& name() const;
27  const std::string& type() const;
28  const std::string& label() const;
29  int serial() const;
30 
31  /*
32  bool is_channel() const
33  {
34  int ok;
35  Phidget_getIsChannel(m_handle, &ok);
36  return (bool)ok;
37  }
38  */
39  bool is_attached() const
40  {
41  int ok;
42  Phidget_getAttached(m_handle, &ok);
43  return (bool)ok;
44  }
45  bool is_hub_port_device() const
46  {
47  int ok;
48  Phidget_getIsHubPortDevice(m_handle, &ok);
49  return (bool)ok;
50  }
51  bool is_local() const
52  {
53  int ok;
54  Phidget_getIsLocal(m_handle, &ok);
55  return (bool)ok;
56  }
57  bool is_remote() const
58  {
59  int ok;
60  Phidget_getIsRemote(m_handle, &ok);
61  return (bool)ok;
62  }
63  bool channel_count() const
64  {
65  uint32_t ok;
66  Phidget_getDeviceChannelCount(m_handle, PHIDCHCLASS_NOTHING, &ok);
67  return (bool)ok;
68  }
69 
70  Phidget_DeviceClass device_class() const { return m_class; }
71  Phidget_DeviceID device_id() const { return m_id; }
72 
73  void set_label(const std::string& n);
74 
75 private:
76  phidget_device(PhidgetHandle hdl);
77  phidget_device(const phidget_device&) = delete;
78  phidget_device(phidget_device&&) = delete;
79  phidget_device& operator=(const phidget_device&) = delete;
80  phidget_device& operator=(phidget_device&&) = delete;
81 
82  PhidgetHandle m_handle{};
83 
84  Phidget_DeviceClass m_class{};
85  Phidget_DeviceID m_id{};
86 
87  std::string m_name;
88  std::string m_type;
89  std::string m_label;
90  int m_serial{};
91 
92  // std::unique_ptr<interface_kit> m_ik;
93 };
94 
95 struct phidget_channel
96 {
97  friend class phidgets_manager;
98 
99 public:
100  ~phidget_channel();
101 
102  std::function<void(int, const char*)> onError;
103 
104  PhidgetHandle handle() const;
105  const std::string& name() const;
106  const std::string& className() const;
107  /*
108  bool is_channel() const
109  {
110  int ok;
111  Phidget_getIsChannel(m_handle, &ok);
112  return (bool)ok;
113  }
114  */
115  bool is_attached() const
116  {
117  int ok;
118  Phidget_getAttached(m_handle, &ok);
119  return (bool)ok;
120  }
121 
122  bool is_local() const
123  {
124  int ok;
125  Phidget_getIsLocal(m_handle, &ok);
126  return (bool)ok;
127  }
128 
129  bool is_remote() const
130  {
131  int ok;
132  Phidget_getIsRemote(m_handle, &ok);
133  return (bool)ok;
134  }
135 
136 private:
137  phidget_channel(PhidgetHandle device, PhidgetHandle hdl);
138  phidget_channel(const phidget_channel&) = delete;
139  phidget_channel(phidget_channel&&) = delete;
140  phidget_channel& operator=(const phidget_channel&) = delete;
141  phidget_channel& operator=(phidget_channel&&) = delete;
142 
143  PhidgetHandle m_device{};
144  PhidgetHandle m_handle{};
145 
146  Phidget_ChannelClass m_class{};
147  Phidget_ChannelSubclass m_subclass{};
148  int m_channel_id{};
149 
150  std::string m_name;
151  std::string m_className;
152 };
153 
154 using phidget_ptr = std::shared_ptr<phidget_device>;
155 class phidgets_manager
156 {
157 public:
158  phidgets_manager();
159  phidgets_manager(const phidgets_manager&) = delete;
160  phidgets_manager(phidgets_manager&&) = delete;
161  phidgets_manager& operator=(const phidgets_manager&) = delete;
162  phidgets_manager& operator=(phidgets_manager&&) = delete;
163  ~phidgets_manager();
164 
165  std::function<void(phidget_ptr)> onDeviceCreated;
166  std::function<void(phidget_ptr)> onDeviceDestroyed;
167 
168  void open();
169 
170  const std::vector<phidget_ptr>& phidgets() const { return m_phidgets; }
171 
172 private:
173  PhidgetManagerHandle m_hdl{};
174 
175  std::vector<phidget_ptr> m_phidgets;
176 };
177 }