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 <logging/logger.h>
27 #include <interfaces/JoystickInterface.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
61  float *axis_values)
62 {
63  if ( ! __bb->is_alive() ) {
64  if ( __bb->try_aliveness_restore() ) {
65  __logger->log_info("Joystick", "Connection re-established, writing data");
66  __warning_printed = false;
67  }
68  }
69 
70  try {
71  __joystick_if->set_pressed_buttons(pressed_buttons);
72  __joystick_if->set_axis(axis_values);
73  __joystick_if->write();
74  } catch (Exception &e) {
75  if ( ! __warning_printed ) {
76  e.print_trace();
77  __logger->log_warn("Joystick", "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 }
virtual void joystick_unplugged()
The joystick has been unplugged and is no longer available.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.
JoystickInterface Fawkes BlackBoard Interface.
Fawkes library namespace.
virtual void joystick_changed(unsigned int pressed_buttons, float *axis_values)
Joystick data changed.
void set_pressed_buttons(const uint32_t new_pressed_buttons)
Set pressed_buttons value.
virtual bool try_aliveness_restore()=0
Try to restore the aliveness of the BlackBoard instance.
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:500
void set_num_axes(const uint8_t new_num_axes)
Set num_axes value.
virtual void joystick_plugged(char num_axes, char num_buttons)
A (new) joystick has been plugged in.
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual bool is_alive() const =0
Check if the BlackBoard is still alive.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:619
void set_axis(unsigned int index, const float new_axis)
Set axis value at given index.
Remote BlackBoard.
Definition: remote.h:48
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
void set_num_buttons(const uint8_t new_num_buttons)
Set num_buttons value.
JoystickRemoteBlackBoardPoster(const char *host, unsigned short int port, fawkes::Logger *logger)
Constructor.
virtual void close(Interface *interface)=0
Close interface.
Interface for logging.
Definition: logger.h:34