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  }
LogStream.h
JoypadControl::JoyPort::bufferedPort
yarp::os::BufferedPort< T > bufferedPort
Definition: JoypadControlNetUtils.h:51
std_msgs::Time
yarp::rosmsg::std_msgs::Time Time
Definition: Time.h:24
JoypadControl::LoopablePort
Definition: JoypadControlNetUtils.h:32
JoypadControl::LoopablePort::LoopablePort
LoopablePort()=default
JoypadControl
Definition: JoypadControlNetUtils.h:29
yarp::os::Portable
This is a base class for objects that can be both read from and be written to the YARP network.
Definition: Portable.h:29
Vector.h
contains the definition of a Vector type
JoyData::read
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition: JoypadControlNetUtils.cpp:14
JoyData
Definition: JoypadControlNetUtils.h:17
yarp::os::TypedReaderCallback
A callback for typed data from a port.
Definition: TypedReaderCallback.h:31
JoypadControl::JoyPort::onRead
void onRead(T &datum) override
Callback method.
Definition: JoypadControlNetUtils.h:65
JoyData::Buttons
yarp::sig::Vector Buttons
Definition: JoypadControlNetUtils.h:18
JoypadControl::LoopablePort::useCallback
virtual void useCallback()=0
yarp::os::Time::now
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
JoyData::Axes
yarp::sig::Vector Axes
Definition: JoypadControlNetUtils.h:20
yarp::sig::VectorOf< double >
yarp::os::BufferedPort
A mini-server for performing network communication in the background.
Definition: BufferedPort.h:64
JoyData::Touch
yarp::sig::Vector Touch
Definition: JoypadControlNetUtils.h:22
yarp::os::ConnectionWriter
An interface for writing to a network connection.
Definition: ConnectionWriter.h:40
JoypadControl::LoopablePort::~LoopablePort
virtual ~LoopablePort()=default
JoyData::Balls
yarp::sig::Vector Balls
Definition: JoypadControlNetUtils.h:21
JoypadControl::JoyPort::JoyPort
JoyPort()
Definition: JoypadControlNetUtils.h:57
yarp::os::BufferedPort::useCallback
void useCallback()
Use own onRead() method as callback.
Definition: BufferedPort-inl.h:218
JoypadControl::JoyPort::useCallback
void useCallback() override
Definition: JoypadControlNetUtils.h:62
JoypadControl::LoopablePort::name
std::string name
Definition: JoypadControlNetUtils.h:35
JoypadControl::JoyPort
Definition: JoypadControlNetUtils.h:50
BottleTagMap< unsigned char >
int BottleTagMap< unsigned char >()
Definition: JoypadControlNetUtils.h:88
JoypadControl::JoyPort::storage
T storage
Definition: JoypadControlNetUtils.h:54
BufferedPort.h
JoyData::write
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
Definition: JoypadControlNetUtils.cpp:31
yarp::os::ConnectionReader
An interface for reading from a network connection.
Definition: ConnectionReader.h:40
JoyData::Sticks
yarp::sig::Vector Sticks
Definition: JoypadControlNetUtils.h:19
JoypadControl::LoopablePort::valid
bool valid
Definition: JoypadControlNetUtils.h:33
JoypadControl::LoopablePort::onTimeout
virtual void onTimeout(double sec)=0
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
JoypadControl::JoyPort::mutex
std::mutex mutex
Definition: JoypadControlNetUtils.h:55
JoypadControl::JoyPort::onTimeout
void onTimeout(double sec) override
Definition: JoypadControlNetUtils.h:73
Time.h
JoypadControl::JoyPort::now
double now
Definition: JoypadControlNetUtils.h:53
JoyData::Hats
yarp::sig::VectorOf< unsigned char > Hats
Definition: JoypadControlNetUtils.h:23
JoypadControl::LoopablePort::count
unsigned int count
Definition: JoypadControlNetUtils.h:34
yarp::os::Contactable
An abstract port.
Definition: Contactable.h:38
JoypadControl::LoopablePort::contactable
yarp::os::Contactable * contactable
Definition: JoypadControlNetUtils.h:36