00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 #include "Beeper.hh"
00027
00028
00029
00030 #include <unistd.h>
00031 #include <fcntl.h>
00032 #include <boost/bind.hpp>
00033 #include "Exception.hh"
00034
00035 #define prefix_
00036
00037
00038
00039
00040
00041 prefix_ senf::Beeper::Beeper(std::string const & device)
00042 : timer_( "senf::Beeper::timer", boost::bind(&Beeper::timeout, this), 0, false)
00043 {
00044 if ((fd_ = ::open(device.c_str(), O_WRONLY)) == -1) {
00045 SENF_THROW_SYSTEM_EXCEPTION("Could not open device for Beeper.");
00046 }
00047 }
00048
00049 prefix_ senf::Beeper::~Beeper()
00050 {
00051 stop_beep();
00052 ::close(fd_);
00053 }
00054
00055 prefix_ void senf::Beeper::beep_sync(float freq, unsigned length, unsigned count, unsigned delay)
00056 {
00057 for (unsigned i = 0; i < count; ++i) {
00058 if (! start_beep( freq)) return;
00059 ::usleep( 1000 * length);
00060 stop_beep();
00061 if ( i+1 < count)
00062 ::usleep( 1000 * delay);
00063 }
00064 }
00065
00066 prefix_ void senf::Beeper::beep_async(float freq, unsigned length, unsigned count, unsigned delay)
00067 {
00068 if (! start_beep( freq)) return;
00069 timer_.timeout( senf::ClockService::now() + senf::ClockService::milliseconds(length));
00070 params_.action = false;
00071 if (count > 1) {
00072 params_.freq = freq;
00073 params_.length = length;
00074 params_.count = count;
00075 params_.delay = delay;
00076 }
00077 }
00078
00079 prefix_ void senf::Beeper::timeout()
00080 {
00081 if (params_.action) {
00082 if (! start_beep( params_.freq)) return;
00083 timer_.timeout( senf::ClockService::now() + senf::ClockService::milliseconds(params_.length));
00084 params_.action = false;
00085 } else {
00086 stop_beep();
00087 if (--params_.count > 0) {
00088 timer_.timeout( senf::ClockService::now() + senf::ClockService::milliseconds(params_.delay));
00089 params_.action = true;
00090 }
00091 }
00092 }
00093
00094
00095 #undef prefix_
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106