Beeper.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #include "Beeper.hh"
18 //#include "Beeper.ih"
19 
20 // Custom includes
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <boost/bind.hpp>
24 #include "Exception.hh"
25 
26 #define prefix_
27 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 // senf::Beeper
31 
32 prefix_ senf::Beeper::Beeper(std::string const & device)
33  : timer_( "senf::Beeper::timer", boost::bind(&Beeper::timeout, this), ClockService::clock_type(0), false)
34 {
35  if ((fd_ = ::open(device.c_str(), O_WRONLY)) == -1) {
36  SENF_THROW_SYSTEM_EXCEPTION("Could not open device for Beeper.");
37  }
38 }
39 
41 {
42  stop_beep();
43  ::close(fd_);
44 }
45 
46 prefix_ void senf::Beeper::beep_sync(float freq, unsigned length, unsigned count, unsigned delay)
47 {
48  for (unsigned i = 0; i < count; ++i) {
49  if (! start_beep( freq)) return;
50  ::usleep( 1000 * length);
51  stop_beep();
52  if ( i+1 < count)
53  ::usleep( 1000 * delay);
54  }
55 }
56 
57 prefix_ void senf::Beeper::beep_async(float freq, unsigned length, unsigned count, unsigned delay)
58 {
59  if (! start_beep( freq)) return;
61  params_.action = false; // stop beep
62  if (count > 1) {
63  params_.freq = freq;
64  params_.length = length;
65  params_.count = count;
66  params_.delay = delay;
67  }
68 }
69 
70 prefix_ void senf::Beeper::timeout()
71 {
72  if (params_.action) {
73  if (! start_beep( params_.freq)) return;
75  params_.action = false;
76  } else {
77  stop_beep();
78  if (--params_.count > 0) {
80  params_.action = true;
81  }
82  }
83 }
84 
85 //-/////////////////////////////////////////////////////////////////////////////////////////////////
86 #undef prefix_
87 
88 
89 // Local Variables:
90 // mode: c++
91 // fill-column: 100
92 // c-file-style: "senf"
93 // indent-tabs-mode: nil
94 // ispell-local-dictionary: "american"
95 // compile-command: "scons -u test"
96 // comment-column: 40
97 // End:
#define SENF_THROW_SYSTEM_EXCEPTION(desc)
Definition: Exception.hh:321
bool start_beep(float freq)
start playing.
void beep_async(float freq, unsigned length, unsigned count=1, unsigned delay=100)
play beep asynchronous
Definition: Beeper.cc:57
void stop_beep()
stop playing.
static SENF_CLOCKSERVICE_CONSTEXPR clock_type milliseconds(int64_type const &v)
void timeout(ClockService::clock_type const &timeout, bool initiallyEnabled=true)
Beeper(std::string const &device="/dev/console")
Construct a new Beeper for the given device.
Definition: Beeper.cc:32
#define prefix_
Definition: Beeper.cc:26
Exception public header.
void beep_sync(float freq, unsigned length, unsigned count=1, unsigned delay=100)
play beep synchronous
Definition: Beeper.cc:46
Helper class to beep the pc speaker.
Definition: Beeper.hh:39
static clock_type now()
virtual void close()
Beeper public header.