Fawkes API  Fawkes Development Version
beep.cpp
1 
2 /***************************************************************************
3  * beep.cpp - Beeper utility class
4  *
5  * Created: Sun Apr 11 19:41:23 2010
6  * Copyright 2006-2010 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include "beep.h"
25 
26 #include <cstdio>
27 #include <cerrno>
28 #include <termios.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
31 #include <cstring>
32 #include <sys/ioctl.h>
33 #include <linux/kd.h>
34 #include <unistd.h>
35 
36 
37 /* From console_ioctl man page, explained in the beep too by
38  * Johnathan Nightingale:
39  * This number represents the fixed frequency of the original PC XT's
40  * timer chip (the 8254 AFAIR), which is approximately 1.193 MHz. This
41  * number is divided with the desired frequency to obtain a counter value,
42  * that is subsequently fed into the timer chip, tied to the PC speaker.
43  * The chip decreases this counter at every tick (1.193 MHz) and when it
44  * reaches zero, it toggles the state of the speaker (on/off, or in/out),
45  * resets the counter to the original value, and starts over. The end
46  * result of this is a tone at approximately the desired frequency. :)
47  */
48 #define CLOCK_TICK_RATE 1193180
49 
50 #define CONSOLE_FILE "/dev/console"
51 
52 /** @class BeepController "beep.h"
53  * Simple speaker beep controller.
54  * @author Tim Niemueller
55  */
56 
57 /** Constructor. */
59 {
60  __disable_beeping = false;
61 }
62 
63 /** Enable beeping.
64  * @param freq frequency to beep with
65  */
66 void
68 {
69  if (__disable_beeping) return;
70 
71  int beep_fd = open(CONSOLE_FILE, O_WRONLY);
72  if (beep_fd == -1) {
73  char errstr[1024];
74  strerror_r(errno, errstr, sizeof(errstr));
75  //logger->log_warn(name(), "Could not open console (%s). "
76  // "Disabling warning beeps.", errstr);
77  __disable_beeping = true;
78  } else {
79  if (ioctl(beep_fd, KIOCSOUND, (int)(CLOCK_TICK_RATE/freq)) < 0) {
80  //logger->log_warn(name(), "Starting to beep failed. Disabling warning beeps.");
81  __disable_beeping = true;
82  }
83  close(beep_fd);
84  }
85 }
86 
87 
88 /** Disable beeping. */
89 void
91 {
92  if (__disable_beeping) return;
93 
94  int beep_fd = open(CONSOLE_FILE, O_WRONLY);
95  if (beep_fd == -1) {
96  char errstr[1024];
97  strerror_r(errno, errstr, sizeof(errstr));
98  //logger->log_warn(name(), "Could not open console (%s) [stop]. "
99  // "Disabling warning beeps.", errstr);
100  __disable_beeping = true;
101  } else {
102  if (ioctl(beep_fd, KIOCSOUND, 0) < 0) {
103  //logger->log_warn(name(), "Stopping beeping failed. "
104  // "Disabling warning beeps.");
105  __disable_beeping = true;
106  }
107  close(beep_fd);
108  }
109 }
void beep_on(float freq=1000)
Enable beeping.
Definition: beep.cpp:67
void beep_off()
Disable beeping.
Definition: beep.cpp:90
BeepController()
Constructor.
Definition: beep.cpp:58