basename,dirname,freeramdisk,rx,raidautorun,runsv,chvt: skip "--" argument
[oweals/busybox.git] / miscutils / beep.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * beep implementation for busybox
4  *
5  * Copyright (C) 2009 Bernhard Reutner-Fischer
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  *
9  */
10
11 //usage:#define beep_trivial_usage
12 //usage:       "-f FREQ -l LEN -d DELAY -r COUNT -n"
13 //usage:#define beep_full_usage "\n\n"
14 //usage:       "Options:"
15 //usage:     "\n        -f      Frequency in Hz"
16 //usage:     "\n        -l      Length in ms"
17 //usage:     "\n        -d      Delay in ms"
18 //usage:     "\n        -r      Repetitions"
19 //usage:     "\n        -n      Start new tone"
20
21 #include "libbb.h"
22
23 #include <linux/kd.h>
24 #ifndef CLOCK_TICK_RATE
25 # define CLOCK_TICK_RATE 1193180
26 #endif
27
28 /* defaults */
29 #ifndef CONFIG_FEATURE_BEEP_FREQ
30 # define FREQ (4000)
31 #else
32 # define FREQ (CONFIG_FEATURE_BEEP_FREQ)
33 #endif
34 #ifndef CONFIG_FEATURE_BEEP_LENGTH_MS
35 # define LENGTH (30)
36 #else
37 # define LENGTH (CONFIG_FEATURE_BEEP_LENGTH_MS)
38 #endif
39 #define DELAY (0)
40 #define REPETITIONS (1)
41
42 int beep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
43 int beep_main(int argc, char **argv)
44 {
45         int speaker = get_console_fd_or_die();
46         unsigned tickrate_div_freq = tickrate_div_freq; /* for compiler */
47         unsigned length = length;
48         unsigned delay = delay;
49         unsigned rep = rep;
50         int c;
51
52         c = 'n';
53         while (c != -1) {
54                 if (c == 'n') {
55                         tickrate_div_freq = CLOCK_TICK_RATE / FREQ;
56                         length = LENGTH;
57                         delay = DELAY;
58                         rep = REPETITIONS;
59                 }
60                 c = getopt(argc, argv, "f:l:d:r:n");
61 /* TODO: -s, -c:
62  * pipe stdin to stdout, but also beep after each line (-s) or char (-c)
63  */
64                 switch (c) {
65                 case 'f':
66 /* TODO: what "-f 0" should do? */
67                         tickrate_div_freq = (unsigned)CLOCK_TICK_RATE / xatou(optarg);
68                         continue;
69                 case 'l':
70                         length = xatou(optarg);
71                         continue;
72                 case 'd':
73 /* TODO:
74  * -d N, -D N
75  * specify a delay of N milliseconds between repetitions.
76  * -d specifies that this delay should only occur between beeps,
77  * that is, it should not occur after the last repetition.
78  * -D indicates that the delay should occur after every repetition
79  */
80                         delay = xatou(optarg);
81                         continue;
82                 case 'r':
83                         rep = xatou(optarg);
84                         continue;
85                 case 'n':
86                 case -1:
87                         break;
88                 default:
89                         bb_show_usage();
90                 }
91                 while (rep) {
92 //bb_info_msg("rep[%d] freq=%d, length=%d, delay=%d", rep, freq, length, delay);
93                         xioctl(speaker, KIOCSOUND, (void*)(uintptr_t)tickrate_div_freq);
94                         usleep(1000 * length);
95                         ioctl(speaker, KIOCSOUND, (void*)0);
96                         if (--rep)
97                                 usleep(1000 * delay);
98                 }
99         }
100
101         if (ENABLE_FEATURE_CLEAN_UP)
102                 close(speaker);
103         return EXIT_SUCCESS;
104 }
105 /*
106  * so, e.g. Beethoven's 9th symphony "Ode an die Freude" would be
107  * something like:
108 a=$((220*3))
109 b=$((247*3))
110 c=$((262*3))
111 d=$((294*3))
112 e=$((329*3))
113 f=$((349*3))
114 g=$((392*3))
115 #./beep -f$d -l200 -r2 -n -f$e -l100 -d 10 -n -f$c -l400 -f$g -l200
116 ./beep -f$e -l200 -r2 \
117         -n -d 100 -f$f -l200 \
118         -n -f$g -l200 -r2 \
119         -n -f$f -l200 \
120         -n -f$e -l200 \
121         -n -f$d -l200 \
122         -n -f$c -l200 -r2 \
123         -n -f$d -l200 \
124         -n -f$e -l200 \
125         -n -f$e -l400 \
126         -n -f$d -l100 \
127         -n -f$d -l200 \
128 */