yarp-devices
WiimoteSensor.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __WIIMOTE_SENSOR_HPP__
4 #define __WIIMOTE_SENSOR_HPP__
5 
6 #include <mutex>
7 
8 #include <poll.h>
9 #include <xwiimote.h>
10 
11 #include <yarp/os/Thread.h>
12 
13 #include <yarp/dev/DeviceDriver.h>
14 #include <yarp/dev/IAnalogSensor.h>
15 
16 namespace roboticslab
17 {
18 
30 {
32  : accelX(0), accelY(0), accelZ(0),
33  buttonA(false), buttonB(false),
34  button1(false), button2(false)
35  {}
36 
37  int accelX;
38  int accelY;
39  int accelZ;
40 
41  bool buttonA;
42  bool buttonB;
43 
44  bool button1;
45  bool button2;
46 };
47 
52 class WiimoteDispatcherThread : public yarp::os::Thread
53 {
54 public:
55 
57  WiimoteDispatcherThread() : iface(nullptr)
58  { }
59 
61  void beforeStart() override;
62 
64  void run() override;
65 
67  void setInterfacePointer(struct xwii_iface * iface)
68  {
69  this->iface = iface;
70  }
71 
74 
75 private:
76 
77  struct xwii_iface * iface;
78  struct xwii_event event;
79 
80  WiimoteEventData eventData;
81  mutable std::mutex eventDataMutex;
82 
83  struct pollfd fds[2];
84  int fds_num;
85 };
86 
91 class WiimoteSensor : public yarp::dev::DeviceDriver,
92  public yarp::dev::IAnalogSensor
93 {
94 public:
95 
97  : iface(nullptr),
98  calibZeroX(0), calibZeroY(0), calibZeroZ(0),
99  calibOneX(0), calibOneY(0), calibOneZ(0)
100  { }
101 
102  ~WiimoteSensor() override
103  { close(); }
104 
105  // --------- DeviceDriver Declarations. Implementation in DeviceDriverImpl.cpp ---------
106  bool open(yarp::os::Searchable& config) override;
107  bool close() override;
108 
109  // --------- IAnalogSensor Declarations. Implementation in IAnalogSensorImpl.cpp ---------
115  int read(yarp::sig::Vector &out) override;
116 
122  int getState(int ch) override;
123 
128  int getChannels() override;
129 
134  int calibrateSensor() override;
135 
141  int calibrateSensor(const yarp::sig::Vector& value) override;
142 
148  int calibrateChannel(int ch) override;
149 
156  int calibrateChannel(int ch, double value) override;
157 
158 private:
159 
160  static char * getDevicePath(int id);
161 
162  struct xwii_iface * iface;
163 
164  int calibZeroX, calibZeroY, calibZeroZ;
165  int calibOneX, calibOneY, calibOneZ;
166  bool yawActive;
167 
168  WiimoteDispatcherThread dispatcherThread;
169 };
170 
171 } // namespace roboticslab
172 
173 #endif // __WIIMOTE_SENSOR_HPP__
Thread that listens to Wiimote events.
Definition: WiimoteSensor.hpp:53
void setInterfacePointer(struct xwii_iface *iface)
Set pointer to xwii_iface.
Definition: WiimoteSensor.hpp:67
void run() override
Main body of the new thread.
Definition: WiimoteDispatcherThread.cpp:31
WiimoteDispatcherThread()
Constructor.
Definition: WiimoteSensor.hpp:57
void beforeStart() override
Called just before a new thread starts.
Definition: WiimoteDispatcherThread.cpp:16
WiimoteEventData getEventData() const
Retrieve event data object.
Definition: WiimoteDispatcherThread.cpp:102
Implementation for the Wiimote controller.
Definition: WiimoteSensor.hpp:93
int getState(int ch) override
Definition: IAnalogSensorImpl.cpp:39
int calibrateChannel(int ch) override
Definition: IAnalogSensorImpl.cpp:67
int getChannels() override
Definition: IAnalogSensorImpl.cpp:46
int calibrateSensor() override
Definition: IAnalogSensorImpl.cpp:53
int read(yarp::sig::Vector &out) override
Definition: IAnalogSensorImpl.cpp:9
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6
Holds key state data from input events.
Definition: WiimoteSensor.hpp:30