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 
25 #include "acquisition_thread.h"
26 
27 #include <interfaces/JoystickInterface.h>
28 
29 using namespace fawkes;
30 
31 /** @class JoystickSensorThread "sensor_thread.h"
32  * Joystick sensor thread.
33  * This thread integrates into the Fawkes main loop at the sensor hook and
34  * publishes new data when available from the JoystickAcquisitionThread.
35  * @author Tim Niemueller
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 void
50 {
51  joystick_if_ = blackboard->open_for_writing<JoystickInterface>("Joystick");
52 }
53 
54 void
56 {
57  blackboard->close(joystick_if_);
58 }
59 
60 void
62 {
63  if (aqt_->lock_if_new_data()) {
64  joystick_if_->set_num_axes(aqt_->num_axes());
65  joystick_if_->set_num_buttons(aqt_->num_buttons());
66  joystick_if_->set_pressed_buttons(aqt_->pressed_buttons());
67  joystick_if_->set_axis(aqt_->axis_values());
68  joystick_if_->write();
69  aqt_->unlock();
70  }
71 }
Joystick acqusition thread for Linux joystick API.
bool lock_if_new_data()
Lock data if fresh.
unsigned int pressed_buttons() const
Pressed buttons.
char num_buttons() const
Get number of buttons.
char num_axes() const
Get number of axes.
float * axis_values()
Get values for the axes.
virtual void init()
Initialize the thread.
virtual void finalize()
Finalize the thread.
JoystickSensorThread(JoystickAcquisitionThread *aqt)
Constructor.
virtual void loop()
Code to execute in the thread.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Definition: blackboard.h:44
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.
Thread aspect to use blocked timing.
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:501
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.
Thread class encapsulation of pthreads.
Definition: thread.h:46
Fawkes library namespace.