23 #include "button_thread.h"
25 #include <alproxies/allauncherproxy.h>
26 #include <alproxies/dcmproxy.h>
27 #include <alproxies/alaudioplayerproxy.h>
28 #include <alproxies/almemoryproxy.h>
29 #include <alproxies/alsentinelproxy.h>
30 #include <alcore/alerror.h>
31 #include <almemoryfastaccess/almemoryfastaccess.h>
33 #include <interfaces/NaoSensorInterface.h>
34 #include <interfaces/SwitchInterface.h>
36 #include <boost/bind.hpp>
41 using namespace fawkes;
43 #define POWEROFF_PATH "/sbin/poweroff"
59 :
Thread(
"NaoQiButtonThread",
Thread::OPMODE_WAITFORWAKEUP),
74 __sound_longpling = __sound_pling = -1;
75 __sound_bumper_left = __sound_bumper_right = -1;
76 last_shutdown_actcount = 0;
78 __cfg_chest_triple_long_click_shutdown =
false;
80 __cfg_chest_triple_long_click_shutdown =
81 config->
get_bool(
"/hardware/nao/chestbut_triple_long_click_shutdown");
86 AL::ALPtr<AL::ALLauncherProxy> launcher(
new AL::ALLauncherProxy(
naoqi_broker));
87 bool is_auplayer_available = launcher->isModulePresent(
"ALAudioPlayer");
88 bool is_alsentinel_available = launcher->isModulePresent(
"ALSentinel");
90 if (! is_auplayer_available) {
94 AL::ALPtr<AL::ALAudioPlayerProxy>(
new AL::ALAudioPlayerProxy(
naoqi_broker));
95 __sound_longpling = __auplayer->loadFile(RESDIR
"/sounds/longpling.wav");
96 __sound_pling = __auplayer->loadFile(RESDIR
"/sounds/pling.wav");
98 __auplayer->loadFile(RESDIR
"/sounds/metal_click_1_left.wav");
99 __sound_bumper_right =
100 __auplayer->loadFile(RESDIR
"/sounds/metal_click_1_right.wav");
103 if (is_alsentinel_available) {
105 AL::ALPtr<AL::ALSentinelProxy>
107 alsentinel->enableDefaultActionSimpleClick(
false);
108 alsentinel->enableDefaultActionDoubleClick(
false);
109 alsentinel->enableDefaultActionTripleClick(
false);
111 }
catch (AL::ALError& e) {
112 throw Exception(
"Checking module availability failed: %s",
113 e.toString().c_str());
139 __chestbut_remote_enabled =
false;
140 __lfoot_bumper_remote_enabled = __rfoot_bumper_remote_enabled =
false;
141 __head_front_remote_enabled = __head_middle_remote_enabled =
142 __head_rear_remote_enabled =
false;
161 __chestbut_if = NULL;
162 __lfoot_bumper_if = NULL;
163 __rfoot_bumper_if = NULL;
164 __head_front_if = NULL;
165 __head_middle_if = NULL;
166 __head_rear_if = NULL;
170 __auplayer->unloadFile(__sound_longpling);
171 __auplayer->unloadFile(__sound_pling);
172 __auplayer->unloadFile(__sound_bumper_left);
173 __auplayer->unloadFile(__sound_bumper_right);
189 process_pattern_button(__chestbut_if, __sensor_if->
chest_button(),
190 time_diff_sec, __chestbut_remote_enabled,
191 __sound_pling, __sound_longpling);
193 time_diff_sec, __head_front_remote_enabled);
195 time_diff_sec, __head_middle_remote_enabled);
197 time_diff_sec, __head_rear_remote_enabled);
201 __lfoot_bumper_remote_enabled, __sound_bumper_left);
205 __rfoot_bumper_remote_enabled, __sound_bumper_right);
207 if (__cfg_chest_triple_long_click_shutdown &&
213 if (__auplayer) __auplayer->playFile(RESDIR
"/sounds/naoshutdown.wav");
216 if (stat(POWEROFF_PATH, &s) == -1) {
220 if (s.st_mode & S_ISUID) {
221 int rv = system(POWEROFF_PATH);
222 if (rv == -1 || (WEXITSTATUS(rv) != 0)) {
236 bool enabled,
float value,
float history,
237 unsigned int activations,
238 unsigned int short_act,
unsigned int long_act)
255 float sensor_value,
float time_diff_sec,
256 bool &remote_enabled,
257 int sound_short,
int sound_long)
260 process_messages(switch_if, remote_enabled, value);
261 value = std::max(value, sensor_value);
263 bool enabled =
false;
264 float history = switch_if->
history();
269 pattern_button_logic(value, time_diff_sec, enabled, history,
270 activations, short_act, long_act,
271 sound_short, sound_long);
273 set_interface(switch_if, enabled, value, history,
274 activations, short_act, long_act);
280 float left_value,
float right_value,
282 bool &remote_enabled,
int sound_id)
285 process_messages(switch_if, remote_enabled, value);
286 value = std::max(std::max(value, left_value), right_value);
288 bool enabled =
false;
289 float history = switch_if->
history();
294 bumpers_logic(value, time_diff_sec, enabled, history, activations, sound_id);
296 set_interface(switch_if, enabled, value, history,
297 activations, short_act, long_act);
304 bool &remote_enabled,
float &value)
309 if (msg->is_enabled()) {
310 value = std::min(0.5f, msg->value());
312 value = std::max(0.49f, msg->value());
321 remote_enabled =
true;
328 remote_enabled =
false;
335 if (remote_enabled) value = 1.;
340 NaoQiButtonThread::pattern_button_logic(
float value,
float time_diff_sec,
341 bool &enabled,
float &history,
342 unsigned int &activations,
343 unsigned int &short_act,
344 unsigned int &long_act,
345 int sound_short,
int sound_long)
348 if (history > 0.025 ) {
352 if (__auplayer && (sound_long != -1)) __auplayer->play(sound_long);
355 if (__auplayer && (sound_short != -1)) __auplayer->play(sound_short);
357 }
else if ( history < -2.0 ) {
359 short_act = long_act = 0;
363 if ( history < 0. ) {
364 history -= time_diff_sec;
366 history = - time_diff_sec;
371 if ( history > 0. ) {
372 history += time_diff_sec;
374 history = time_diff_sec;
379 if (history < -120.) {
381 }
else if (history > 120.) {
388 NaoQiButtonThread::bumpers_logic(
float value,
float time_diff_sec,
389 bool &enabled,
float &history,
390 unsigned int &activations,
int sound_id)
394 if ( history < 0. ) {
395 history -= time_diff_sec;
397 history = - time_diff_sec;
401 if (history <= 0. ) {
402 if (__auplayer && (sound_id != -1)) __auplayer->play(sound_id);
407 if ( history > 0. ) {
408 history += time_diff_sec;
410 history = time_diff_sec;
415 if (history < -120.) {
417 }
else if (history > 120.) {