Fawkes API  Fawkes Development Version
remote_bb_poster.cpp
00001 
00002 /***************************************************************************
00003  *  remote_bb_poster.h - Joystick handler writing to remote blackboard
00004  *
00005  *  Created: Sat Jan 29 12:10:53 2011
00006  *  Copyright  2006-2011  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022 
00023 #include "remote_bb_poster.h"
00024 
00025 #include <blackboard/remote.h>
00026 #include <logging/logger.h>
00027 #include <interfaces/JoystickInterface.h>
00028 
00029 using namespace fawkes;
00030 
00031 /** @class JoystickRemoteBlackBoardPoster "remote_bb_poster.h"
00032  * Glue to post new data to a RemoteBlackBoard.
00033  * @author Tim Niemueller
00034  */
00035 
00036 /** Constructor.
00037  * @param host remote bb host to connect to
00038  * @param port remote bb port to connect to
00039  * @param logger logger
00040  */
00041 JoystickRemoteBlackBoardPoster::JoystickRemoteBlackBoardPoster(const char *host,
00042                                                          unsigned short int port,
00043                                                          Logger *logger)
00044   : __logger(logger)
00045 {
00046   __bb = new RemoteBlackBoard(host, port);
00047 
00048   __joystick_if = __bb->open_for_writing<JoystickInterface>("Joystick");
00049   __warning_printed = false;
00050 }
00051 
00052 /** Destructor. */
00053 JoystickRemoteBlackBoardPoster::~JoystickRemoteBlackBoardPoster()
00054 {
00055   __bb->close(__joystick_if);
00056   delete __bb;
00057 }
00058 
00059 void
00060 JoystickRemoteBlackBoardPoster::joystick_changed(unsigned int pressed_buttons,
00061                                                  float *axis_values)
00062 {
00063   if ( ! __bb->is_alive() ) {
00064     if ( __bb->try_aliveness_restore() ) {
00065       __logger->log_info("Joystick", "Connection re-established, writing data");
00066       __warning_printed = false;
00067     }
00068   }
00069   
00070   try {
00071     __joystick_if->set_pressed_buttons(pressed_buttons);
00072     __joystick_if->set_axis(axis_values);
00073     __joystick_if->write();
00074   } catch (Exception &e) {
00075     if ( ! __warning_printed ) {
00076       e.print_trace();
00077       __logger->log_warn("Joystick", "Lost connection to BlackBoard, "
00078                          "will try to re-establish");
00079       __warning_printed = true;
00080     }
00081   }
00082 }
00083 
00084 void
00085 JoystickRemoteBlackBoardPoster::joystick_plugged(char num_axes, char num_buttons)
00086 {
00087   __joystick_if->set_num_axes( num_axes );
00088   __joystick_if->set_num_buttons( num_buttons );
00089   __joystick_if->write();
00090 }
00091 
00092 void
00093 JoystickRemoteBlackBoardPoster::joystick_unplugged()
00094 {
00095   __joystick_if->set_num_axes( 0 );
00096   __joystick_if->set_num_buttons( 0 );
00097   __joystick_if->write();
00098 }