Fawkes API  Fawkes Development Version
sensor_thread.cpp
1 
2 /***************************************************************************
3  * sensor_thread.cpp - Joystick thread that pushes data into the interface
4  *
5  * Created: Sat Nov 22 18:05:55 2008
6  * Copyright 2006-2008 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 "sensor_thread.h"
24 #include "acquisition_thread.h"
25 
26 #include <interfaces/JoystickInterface.h>
27 
28 using namespace fawkes;
29 
30 /** @class JoystickSensorThread "sensor_thread.h"
31  * Joystick sensor thread.
32  * This thread integrates into the Fawkes main loop at the sensor hook and
33  * publishes new data when available from the JoystickAcquisitionThread.
34  * @author Tim Niemueller
35  */
36 
37 
38 /** Constructor.
39  * @param aqt JoystickAcquisitionThread to get data from
40  */
42  : Thread("JoystickSensorThread", Thread::OPMODE_WAITFORWAKEUP),
43  BlockedTimingAspect(BlockedTimingAspect::WAKEUP_HOOK_SENSOR_ACQUIRE)
44 {
45  __aqt = aqt;
46 }
47 
48 
49 void
51 {
52  __joystick_if = blackboard->open_for_writing<JoystickInterface>("Joystick");
53 }
54 
55 
56 void
58 {
59  blackboard->close(__joystick_if);
60 }
61 
62 
63 void
65 {
66  if ( __aqt->lock_if_new_data() ) {
67  __joystick_if->set_num_axes( __aqt->num_axes() );
68  __joystick_if->set_num_buttons( __aqt->num_buttons() );
69  __joystick_if->set_pressed_buttons( __aqt->pressed_buttons() );
70  __joystick_if->set_axis( __aqt->axis_values() );
71  __joystick_if->write();
72  __aqt->unlock();
73  }
74 }
unsigned int pressed_buttons() const
Pressed buttons.
virtual void loop()
Code to execute in the thread.
JoystickInterface Fawkes BlackBoard Interface.
float * axis_values()
Get values for the axes.
Fawkes library namespace.
void set_pressed_buttons(const uint32_t new_pressed_buttons)
Set pressed_buttons value.
JoystickSensorThread(JoystickAcquisitionThread *aqt)
Constructor.
Thread class encapsulation of pthreads.
Definition: thread.h:42
bool lock_if_new_data()
Lock data if fresh.
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:500
Joystick acqusition thread for Linux joystick API.
void set_num_axes(const uint8_t new_num_axes)
Set num_axes value.
Thread aspect to use blocked timing.
virtual void init()
Initialize the thread.
char num_buttons() const
Get number of buttons.
virtual void finalize()
Finalize the thread.
void set_axis(unsigned int index, const float new_axis)
Set axis value at given index.
char num_axes() const
Get number of axes.
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.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Definition: blackboard.h:44
virtual void close(Interface *interface)=0
Close interface.