From: tho Date: Wed, 23 Jun 2010 09:55:44 +0000 (+0000) Subject: Utils: added Beeper helper :) X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=0b8ca53e27f21515b66491c8df3d480c77810346;p=senf.git Utils: added Beeper helper :) git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1640 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Utils/Beeper.cc b/senf/Utils/Beeper.cc new file mode 100644 index 0000000..bb4517e --- /dev/null +++ b/senf/Utils/Beeper.cc @@ -0,0 +1,118 @@ +// $Id$ +// +// Copyright (C) 2010 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Thorsten Horstmann +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief TypeInfo non-inline non-template implementation */ + +#include "Beeper.hh" +//#include "Beeper.ih" + +// Custom includes +#include +#include +#include +#include +#include +#include "Exception.hh" + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////// +// senf::Beeper + +prefix_ senf::Beeper::Beeper(std::string const & device) + : timer_( "senf::Beeper::timer", boost::bind(&Beeper::timeout, this), 0, false) +{ + if ((fd_ = ::open(device.c_str(), O_WRONLY)) == -1) { + SENF_THROW_SYSTEM_EXCEPTION("Could not open device for Beeper."); + } +} + +prefix_ senf::Beeper::~Beeper() +{ + stop_beep(); + ::close(fd_); +} + +prefix_ bool senf::Beeper::start_beep(float freq) +{ + return ::ioctl(fd_, KIOCSOUND, (int)(CLOCK_TICK_RATE/freq)) == 0; +} + +prefix_ void senf::Beeper::stop_beep() +{ + ::ioctl(fd_, KIOCSOUND, 0); +} + +prefix_ void senf::Beeper::beep_sync(float freq, unsigned length, unsigned count, unsigned delay) +{ + for (unsigned i = 0; i < count; ++i) { + if (! start_beep( freq)) return; + ::usleep( 1000 * length); + stop_beep(); + if ( i+1 < count) + ::usleep( 1000 * delay); + } +} + +prefix_ void senf::Beeper::beep_async(float freq, unsigned length, unsigned count, unsigned delay) +{ + if (! start_beep( freq)) return; + timer_.timeout( senf::ClockService::now() + senf::ClockService::milliseconds(length)); + params_.action = false; // stop beep + if (count > 1) { + params_.freq = freq; + params_.length = length; + params_.count = count; + params_.delay = delay; + } +} + +prefix_ void senf::Beeper::timeout() +{ + if (params_.action) { + if (! start_beep( params_.freq)) return; + timer_.timeout( senf::ClockService::now() + senf::ClockService::milliseconds(params_.length)); + params_.action = false; + } else { + stop_beep(); + if (--params_.count > 0) { + timer_.timeout( senf::ClockService::now() + senf::ClockService::milliseconds(params_.delay)); + params_.action = true; + } + } +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// comment-column: 40 +// End: diff --git a/senf/Utils/Beeper.hh b/senf/Utils/Beeper.hh new file mode 100644 index 0000000..7a29d60 --- /dev/null +++ b/senf/Utils/Beeper.hh @@ -0,0 +1,109 @@ +// $Id$ +// +// Copyright (C) 2010 +// Fraunhofer Institute for Open Communication Systems (FOKUS) +// Competence Center NETwork research (NET), St. Augustin, GERMANY +// Thorsten Horstmann +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief Beeper public header */ + +#ifndef HH_SENF_Utils_Beeper_ +#define HH_SENF_Utils_Beeper_ 1 + +// Custom includes +#include +#include + +///////////////////////////////hh.p//////////////////////////////////////// + +namespace senf { + + /** \brief Helper class to beep the pc speaker + + This helper class allows you to beep the pc speaker with precision like frequency and + duration. The beep can be played synchronous and asynchronous. + + Typically the /dev/console device is used to control the local pc + speaker. Note, that opening /dev/console is a privileged operation on + most systems. + + \see manual page console_ioctl(4) for the according ioctl KIOCSOUND + */ + class Beeper + { + public: + Beeper(std::string const & device = "/dev/console"); + ///< Construct a new Beeper for the given device. + ~Beeper(); + + void beep_sync(float freq, unsigned length, unsigned count = 1, unsigned delay = 100); + ///< play beep synchronous + /**< \param freq frequency in Hz, where 0 +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/** \file + \brief Beeper unit tests */ + +// Custom includes +#include "Beeper.hh" +#include + +#include +#include + +#define prefix_ +///////////////////////////////cc.p//////////////////////////////////////// + +SENF_AUTO_UNIT_TEST(beep_beep) +{ + char const * enabled (getenv("SENF_BEEPER_TEST")); + BOOST_WARN_MESSAGE(enabled, "Set SENF_BEEPER_TEST to run the Beeper unit test"); + if (! enabled) + return; + + senf::Beeper myBeeper; + myBeeper.beep_sync(440.0, 200); + myBeeper.beep_sync(300.7, 400, 2, 100); + + sleep( 1); + + senf::scheduler::TimerEvent timer ( + "Beeper unit test timer", &senf::scheduler::terminate, + senf::ClockService::now() + senf::ClockService::seconds(2)); + + myBeeper.beep_async(300.7, 400, 2, 100); + senf::scheduler::process(); +} + +///////////////////////////////cc.e//////////////////////////////////////// +#undef prefix_ + + +// Local Variables: +// mode: c++ +// fill-column: 100 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// comment-column: 40 +// End: