YARP
Yet Another Robot Platform
JoypadControlNetUtils.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include <yarp/os/BufferedPort.h>
7 #include <yarp/sig/Vector.h>
8 #include <yarp/os/LogStream.h>
9 #include <yarp/os/Time.h>
10 #include <mutex>
11 
12 //TODO: finish the single port mode.. the struct below is for this purpose
13 struct JoyData : public yarp::os::Portable
14 {
21 
22  bool read(yarp::os::ConnectionReader& connection) override;
23  bool write(yarp::os::ConnectionWriter& connection) const override;
24 };
25 
26 namespace JoypadControl {
27 
29 {
30  bool valid{false};
31  unsigned int count{0};
32  std::string name;
34 
35  LoopablePort() = default;
36  virtual ~LoopablePort() = default;
37 
38  virtual void useCallback() = 0;
39 
40  virtual void onTimeout(double sec) = 0;
41 };
42 
43 template <typename T>
44 struct JoyPort :
46  public yarp::os::BufferedPort<T>
47 {
49 
50  double now;
52  std::mutex mutex;
53 
54  JoyPort() : now(yarp::os::Time::now())
55  {
56  contactable = this;
57  }
60 
62  void onRead(T& datum) override
63  {
65  mutex.lock();
66  storage = datum;
67  mutex.unlock();
68  }
69 
70  void onTimeout(double sec) override
71  {
72  if((yarp::os::Time::now() - now) > sec)
73  {
74  mutex.lock();
75  storage = T(count);
76  mutex.unlock();
77  }
78  }
79 };
80 
81 } // namespace JoypadControl
82 
83 //----------
84 template<>
86  return 64;
87  }
int BottleTagMap< unsigned char >()
contains the definition of a Vector type
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:61
void useCallback()
Use own onRead() method as callback.
An interface for reading from a network connection.
An interface for writing to a network connection.
An abstract port.
Definition: Contactable.h:35
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:26
A callback for typed data from a port.
yarp::rosmsg::std_msgs::Time Time
Definition: Time.h:21
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:121
The main, catch-all namespace for YARP.
Definition: dirs.h:16
yarp::sig::VectorOf< unsigned char > Hats
yarp::sig::Vector Buttons
yarp::sig::Vector Sticks
yarp::sig::Vector Balls
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
yarp::sig::Vector Axes
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
yarp::sig::Vector Touch
yarp::os::BufferedPort< T > bufferedPort
void onRead(T &datum) override
Callback method.
void onTimeout(double sec) override
virtual void useCallback()=0
virtual ~LoopablePort()=default
virtual void onTimeout(double sec)=0
yarp::os::Contactable * contactable