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(const char *host, unsigned short int port, fawkes::Logger *logger)
Constructor.
virtual void joystick_plugged(char num_axes, char num_buttons)
A (new) joystick has been plugged in.
virtual void joystick_unplugged()
The joystick has been unplugged and is no longer available.
virtual void joystick_changed(unsigned int pressed_buttons, float *axis_values)
Joystick data changed.
virtual bool try_aliveness_restore()=0
Try to restore the aliveness of the BlackBoard instance.
virtual bool is_alive() const =0
Check if the BlackBoard is still alive.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
virtual void close(Interface *interface)=0
Close interface.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:601
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:494
JoystickInterface Fawkes BlackBoard Interface.
void set_pressed_buttons(const uint32_t new_pressed_buttons)
Set pressed_buttons value.
void set_num_axes(const uint8_t new_num_axes)
Set num_axes value.
void set_num_buttons(const uint8_t new_num_buttons)
Set num_buttons value.
void set_axis(unsigned int index, const float new_axis)
Set axis value at given index.
Interface for logging.
Definition: logger.h:42
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
Remote BlackBoard.
Definition: remote.h:49
Fawkes library namespace.