YARP
Yet Another Robot Platform
JoypadControlNetUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include <yarp/os/BufferedPort.h>
10 #include <yarp/sig/Vector.h>
11 #include <yarp/os/LogStream.h>
12 #include <yarp/os/Time.h>
13 #include <mutex>
14 
15 //TODO: finish the single port mode.. the struct below is for this purpose
16 struct JoyData : public yarp::os::Portable
17 {
24 
25  bool read(yarp::os::ConnectionReader& connection) override;
26  bool write(yarp::os::ConnectionWriter& connection) const override;
27 };
28 
29 namespace JoypadControl {
30 
32 {
33  bool valid{false};
34  unsigned int count{0};
35  std::string name;
37 
38  LoopablePort() = default;
39  virtual ~LoopablePort() = default;
40 
41  virtual void useCallback() = 0;
42 
43  virtual void onTimeout(double sec) = 0;
44 };
45 
46 template <typename T>
47 struct JoyPort :
49  public yarp::os::BufferedPort<T>
50 {
52 
53  double now;
55  std::mutex mutex;
56 
57  JoyPort() : now(yarp::os::Time::now())
58  {
59  contactable = this;
60  }
63 
65  void onRead(T& datum) override
66  {
68  mutex.lock();
69  storage = datum;
70  mutex.unlock();
71  }
72 
73  void onTimeout(double sec) override
74  {
75  if((yarp::os::Time::now() - now) > sec)
76  {
77  mutex.lock();
78  storage = T(count);
79  mutex.unlock();
80  }
81  }
82 };
83 
84 } // namespace JoypadControl
85 
86 //----------
87 template<>
89  return 64;
90  }
int BottleTagMap< unsigned char >()
contains the definition of a Vector type
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:64
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:38
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:29
A callback for typed data from a port.
yarp::rosmsg::std_msgs::Time Time
Definition: Time.h:24
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
The main, catch-all namespace for YARP.
Definition: environment.h:18
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