Fawkes API  Fawkes Development Version
remote_bb_poster.cpp
1 
2 /***************************************************************************
3  * remote_bb_poster.h - Joystick handler writing to remote blackboard
4  *
5  * Created: Sat Jan 29 12:10:53 2011
6  * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "remote_bb_poster.h"
24 
25 #include <blackboard/remote.h>
26 #include <interfaces/JoystickInterface.h>
27 #include <logging/logger.h>
28 
29 using namespace fawkes;
30 
31 /** @class JoystickRemoteBlackBoardPoster "remote_bb_poster.h"
32  * Glue to post new data to a RemoteBlackBoard.
33  * @author Tim Niemueller
34  */
35 
36 /** Constructor.
37  * @param host remote bb host to connect to
38  * @param port remote bb port to connect to
39  * @param logger logger
40  */
42  unsigned short int port,
43  Logger * logger)
44 : logger_(logger)
45 {
46  bb_ = new RemoteBlackBoard(host, port);
47 
48  joystick_if_ = bb_->open_for_writing<JoystickInterface>("Joystick");
49  warning_printed_ = false;
50 }
51 
52 /** Destructor. */
54 {
55  bb_->close(joystick_if_);
56  delete bb_;
57 }
58 
59 void
60 JoystickRemoteBlackBoardPoster::joystick_changed(unsigned int pressed_buttons, float *axis_values)
61 {
62  if (!bb_->is_alive()) {
63  if (bb_->try_aliveness_restore()) {
64  logger_->log_info("Joystick", "Connection re-established, writing data");
65  warning_printed_ = false;
66  }
67  }
68 
69  try {
70  joystick_if_->set_pressed_buttons(pressed_buttons);
71  joystick_if_->set_axis(axis_values);
72  joystick_if_->write();
73  } catch (Exception &e) {
74  if (!warning_printed_) {
75  e.print_trace();
76  logger_->log_warn("Joystick",
77  "Lost connection to BlackBoard, "
78  "will try to re-establish");
79  warning_printed_ = true;
80  }
81  }
82 }
83 
84 void
85 JoystickRemoteBlackBoardPoster::joystick_plugged(char num_axes, char num_buttons)
86 {
87  joystick_if_->set_num_axes(num_axes);
88  joystick_if_->set_num_buttons(num_buttons);
89  joystick_if_->write();
90 }
91 
92 void
94 {
95  joystick_if_->set_num_axes(0);
96  joystick_if_->set_num_buttons(0);
97  joystick_if_->write();
98 }
JoystickRemoteBlackBoardPoster::joystick_changed
virtual void joystick_changed(unsigned int pressed_buttons, float *axis_values)
Definition: remote_bb_poster.cpp:60
JoystickRemoteBlackBoardPoster::~JoystickRemoteBlackBoardPoster
~JoystickRemoteBlackBoardPoster()
Destructor.
Definition: remote_bb_poster.cpp:53
JoystickRemoteBlackBoardPoster::joystick_plugged
virtual void joystick_plugged(char num_axes, char num_buttons)
Definition: remote_bb_poster.cpp:85
fawkes::BlackBoard::try_aliveness_restore
virtual bool try_aliveness_restore()=0
JoystickRemoteBlackBoardPoster::joystick_unplugged
virtual void joystick_unplugged()
Definition: remote_bb_poster.cpp:93
fawkes::Logger::log_info
virtual void log_info(const char *component, const char *format,...)=0
fawkes::JoystickInterface::set_axis
void set_axis(unsigned int index, const float new_axis)
Set axis value at given index.
Definition: JoystickInterface.cpp:375
fawkes::RemoteBlackBoard
Definition: remote.h:53
fawkes::JoystickInterface::set_num_buttons
void set_num_buttons(const uint8_t new_num_buttons)
Set num_buttons value.
Definition: JoystickInterface.cpp:243
fawkes::BlackBoard::close
virtual void close(Interface *interface)=0
fawkes::Logger
Definition: logger.h:41
fawkes
fawkes::Logger::log_warn
virtual void log_warn(const char *component, const char *format,...)=0
fawkes::Exception::print_trace
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:601
JoystickRemoteBlackBoardPoster::JoystickRemoteBlackBoardPoster
JoystickRemoteBlackBoardPoster(const char *host, unsigned short int port, fawkes::Logger *logger)
Constructor.
Definition: remote_bb_poster.cpp:41
fawkes::BlackBoard::is_alive
virtual bool is_alive() const =0
fawkes::JoystickInterface::set_num_axes
void set_num_axes(const uint8_t new_num_axes)
Set num_axes value.
Definition: JoystickInterface.cpp:208
fawkes::JoystickInterface::set_pressed_buttons
void set_pressed_buttons(const uint32_t new_pressed_buttons)
Set pressed_buttons value.
Definition: JoystickInterface.cpp:317
fawkes::Interface::write
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:499
fawkes::BlackBoard::open_for_writing
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
fawkes::JoystickInterface
Definition: JoystickInterface.h:39
fawkes::Exception
Definition: exception.h:41