Utils: added Beeper helper :)
[senf.git] / senf / Utils / Beeper.cc
1 // $Id$
2 //
3 // Copyright (C) 2010
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief TypeInfo non-inline non-template implementation */
25
26 #include "Beeper.hh"
27 //#include "Beeper.ih"
28
29 // Custom includes
30 #include <unistd.h>
31 #include <sys/ioctl.h>
32 #include <fcntl.h>
33 #include <linux/kd.h>
34 #include <boost/bind.hpp>
35 #include "Exception.hh"
36
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 ///////////////////////////////////////////////////////////////////////////
41 // senf::Beeper
42
43 prefix_ senf::Beeper::Beeper(std::string const & device)
44     : timer_( "senf::Beeper::timer", boost::bind(&Beeper::timeout, this), 0, false)
45 {
46     if ((fd_ = ::open(device.c_str(), O_WRONLY)) == -1) {
47         SENF_THROW_SYSTEM_EXCEPTION("Could not open device for Beeper.");
48     }
49 }
50
51 prefix_ senf::Beeper::~Beeper()
52 {
53     stop_beep();
54     ::close(fd_);
55 }
56
57 prefix_ bool senf::Beeper::start_beep(float freq)
58 {
59     return ::ioctl(fd_, KIOCSOUND, (int)(CLOCK_TICK_RATE/freq)) == 0;
60 }
61
62 prefix_ void senf::Beeper::stop_beep()
63 {
64     ::ioctl(fd_, KIOCSOUND, 0);
65 }
66
67 prefix_ void senf::Beeper::beep_sync(float freq, unsigned length, unsigned count, unsigned delay)
68 {
69     for (unsigned i = 0; i < count; ++i) {
70         if (! start_beep( freq)) return;
71         ::usleep( 1000 * length);
72         stop_beep();
73         if ( i+1 < count)
74            ::usleep( 1000 * delay);
75     }
76 }
77
78 prefix_ void senf::Beeper::beep_async(float freq, unsigned length, unsigned count, unsigned delay)
79 {
80     if (! start_beep( freq)) return;
81     timer_.timeout( senf::ClockService::now() + senf::ClockService::milliseconds(length));
82     params_.action = false;  // stop beep
83     if (count > 1) {
84         params_.freq = freq;
85         params_.length = length;
86         params_.count = count;
87         params_.delay = delay;
88     }
89 }
90
91 prefix_ void senf::Beeper::timeout()
92 {
93     if (params_.action) {
94         if (! start_beep( params_.freq)) return;
95         timer_.timeout( senf::ClockService::now() + senf::ClockService::milliseconds(params_.length));
96         params_.action = false;
97     } else {
98         stop_beep();
99         if (--params_.count > 0) {
100             timer_.timeout( senf::ClockService::now() + senf::ClockService::milliseconds(params_.delay));
101             params_.action = true;
102         }
103     }
104 }
105
106 ///////////////////////////////cc.e////////////////////////////////////////
107 #undef prefix_
108
109 \f
110 // Local Variables:
111 // mode: c++
112 // fill-column: 100
113 // c-file-style: "senf"
114 // indent-tabs-mode: nil
115 // ispell-local-dictionary: "american"
116 // compile-command: "scons -u test"
117 // comment-column: 40
118 // End: