23 #include "button_thread.h"
25 #include <alcore/alerror.h>
26 #include <almemoryfastaccess/almemoryfastaccess.h>
27 #include <alproxies/alaudioplayerproxy.h>
28 #include <alproxies/allauncherproxy.h>
29 #include <alproxies/almemoryproxy.h>
30 #include <alproxies/alsentinelproxy.h>
31 #include <alproxies/dcmproxy.h>
32 #include <interfaces/NaoSensorInterface.h>
33 #include <interfaces/SwitchInterface.h>
36 #include <boost/bind.hpp>
42 #define POWEROFF_PATH "/sbin/poweroff"
57 :
Thread(
"NaoQiButtonThread",
Thread::OPMODE_WAITFORWAKEUP),
70 sound_longpling_ = sound_pling_ = -1;
71 sound_bumper_left_ = sound_bumper_right_ = -1;
72 last_shutdown_actcount = 0;
74 cfg_chest_triple_long_click_shutdown_ =
false;
76 cfg_chest_triple_long_click_shutdown_ =
77 config->
get_bool(
"/hardware/nao/chestbut_triple_long_click_shutdown");
83 AL::ALPtr<AL::ALLauncherProxy> launcher(
new AL::ALLauncherProxy(
naoqi_broker));
84 bool is_auplayer_available = launcher->isModulePresent(
"ALAudioPlayer");
85 bool is_alsentinel_available = launcher->isModulePresent(
"ALSentinel");
87 if (!is_auplayer_available) {
90 auplayer_ = AL::ALPtr<AL::ALAudioPlayerProxy>(
new AL::ALAudioPlayerProxy(
naoqi_broker));
91 sound_longpling_ = auplayer_->loadFile(RESDIR
"/sounds/longpling.wav");
92 sound_pling_ = auplayer_->loadFile(RESDIR
"/sounds/pling.wav");
93 sound_bumper_left_ = auplayer_->loadFile(RESDIR
"/sounds/metal_click_1_left.wav");
94 sound_bumper_right_ = auplayer_->loadFile(RESDIR
"/sounds/metal_click_1_right.wav");
97 if (is_alsentinel_available) {
99 AL::ALPtr<AL::ALSentinelProxy> alsentinel(
new AL::ALSentinelProxy(
naoqi_broker));
100 alsentinel->enableDefaultActionSimpleClick(
false);
101 alsentinel->enableDefaultActionDoubleClick(
false);
102 alsentinel->enableDefaultActionTripleClick(
false);
104 }
catch (AL::ALError &e) {
105 throw Exception(
"Checking module availability failed: %s", e.toString().c_str());
124 chestbut_remote_enabled_ =
false;
125 lfoot_bumper_remote_enabled_ = rfoot_bumper_remote_enabled_ =
false;
126 head_front_remote_enabled_ = head_middle_remote_enabled_ = head_rear_remote_enabled_ =
false;
145 lfoot_bumper_if_ = NULL;
146 rfoot_bumper_if_ = NULL;
147 head_front_if_ = NULL;
148 head_middle_if_ = NULL;
149 head_rear_if_ = NULL;
153 auplayer_->unloadFile(sound_longpling_);
154 auplayer_->unloadFile(sound_pling_);
155 auplayer_->unloadFile(sound_bumper_left_);
156 auplayer_->unloadFile(sound_bumper_right_);
170 process_pattern_button(chestbut_if_,
173 chestbut_remote_enabled_,
176 process_pattern_button(head_front_if_,
179 head_front_remote_enabled_);
180 process_pattern_button(head_middle_if_,
183 head_middle_remote_enabled_);
184 process_pattern_button(head_rear_if_,
187 head_rear_remote_enabled_);
189 process_bumpers(lfoot_bumper_if_,
193 lfoot_bumper_remote_enabled_,
196 process_bumpers(rfoot_bumper_if_,
200 rfoot_bumper_remote_enabled_,
201 sound_bumper_right_);
203 if (cfg_chest_triple_long_click_shutdown_ && chestbut_if_->
long_activations() == 3
208 auplayer_->playFile(RESDIR
"/sounds/naoshutdown.wav");
211 if (stat(POWEROFF_PATH, &s) == -1) {
214 if (s.st_mode & S_ISUID) {
215 int rv = system(POWEROFF_PATH);
216 if (rv == -1 || (WEXITSTATUS(rv) != 0)) {
231 unsigned int activations,
232 unsigned int short_act,
233 unsigned int long_act)
252 bool & remote_enabled,
257 process_messages(switch_if, remote_enabled, value);
258 value = std::max(value, sensor_value);
260 bool enabled =
false;
261 float history = switch_if->
history();
266 pattern_button_logic(value,
276 set_interface(switch_if, enabled, value, history, activations, short_act, long_act);
284 bool & remote_enabled,
288 process_messages(switch_if, remote_enabled, value);
289 value = std::max(std::max(value, left_value), right_value);
291 bool enabled =
false;
292 float history = switch_if->
history();
297 bumpers_logic(value, time_diff_sec, enabled, history, activations, sound_id);
299 set_interface(switch_if, enabled, value, history, activations, short_act, long_act);
303 NaoQiButtonThread::process_messages(
SwitchInterface *switch_if,
bool &remote_enabled,
float &value)
307 if (msg->is_enabled()) {
308 value = std::min(0.5f, msg->value());
310 value = std::max(0.49f, msg->value());
316 remote_enabled =
true;
320 remote_enabled =
false;
332 NaoQiButtonThread::pattern_button_logic(
float value,
336 unsigned int &activations,
337 unsigned int &short_act,
338 unsigned int &long_act,
343 if (history > 0.025 ) {
347 if (auplayer_ && (sound_long != -1))
348 auplayer_->play(sound_long);
351 if (auplayer_ && (sound_short != -1))
352 auplayer_->play(sound_short);
354 }
else if (history < -2.0 ) {
356 short_act = long_act = 0;
376 if (history < -120.) {
378 }
else if (history > 120.) {
384 NaoQiButtonThread::bumpers_logic(
float value,
388 unsigned int &activations,
400 if (history <= 0. ) {
401 if (auplayer_ && (sound_id != -1))
402 auplayer_->play(sound_id);
415 if (history < -120.) {
417 }
else if (history > 120.) {