Bernhard Reutner-Fischer | 45de074 | 2009-08-21 13:18:31 +0200 | [diff] [blame] | 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 the GPL v2 or later, see the file LICENSE in this tarball. |
| 8 | * |
| 9 | */ |
| 10 | #include "libbb.h" |
| 11 | |
| 12 | #include <linux/kd.h> |
| 13 | #ifndef CLOCK_TICK_RATE |
Denys Vlasenko | 0da1c0a | 2009-08-22 18:00:39 +0200 | [diff] [blame] | 14 | # define CLOCK_TICK_RATE 1193180 |
Bernhard Reutner-Fischer | 45de074 | 2009-08-21 13:18:31 +0200 | [diff] [blame] | 15 | #endif |
| 16 | |
Bernhard Reutner-Fischer | 45de074 | 2009-08-21 13:18:31 +0200 | [diff] [blame] | 17 | /* defaults */ |
Bernhard Reutner-Fischer | 00ea82e | 2009-08-21 14:40:29 +0200 | [diff] [blame] | 18 | #ifndef CONFIG_FEATURE_BEEP_FREQ |
| 19 | # define FREQ (4000) |
| 20 | #else |
| 21 | # define FREQ (CONFIG_FEATURE_BEEP_FREQ) |
| 22 | #endif |
Denys Vlasenko | 0da1c0a | 2009-08-22 18:00:39 +0200 | [diff] [blame] | 23 | #ifndef CONFIG_FEATURE_BEEP_LENGTH_MS |
Bernhard Reutner-Fischer | 00ea82e | 2009-08-21 14:40:29 +0200 | [diff] [blame] | 24 | # define LENGTH (30) |
| 25 | #else |
Denys Vlasenko | 0da1c0a | 2009-08-22 18:00:39 +0200 | [diff] [blame] | 26 | # define LENGTH (CONFIG_FEATURE_BEEP_LENGTH_MS) |
Bernhard Reutner-Fischer | 00ea82e | 2009-08-21 14:40:29 +0200 | [diff] [blame] | 27 | #endif |
Bernhard Reutner-Fischer | 45de074 | 2009-08-21 13:18:31 +0200 | [diff] [blame] | 28 | #define DELAY (0) |
| 29 | #define REPETITIONS (1) |
| 30 | |
Bernhard Reutner-Fischer | 45de074 | 2009-08-21 13:18:31 +0200 | [diff] [blame] | 31 | int beep_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 0da1c0a | 2009-08-22 18:00:39 +0200 | [diff] [blame] | 32 | int beep_main(int argc, char **argv) |
Bernhard Reutner-Fischer | 45de074 | 2009-08-21 13:18:31 +0200 | [diff] [blame] | 33 | { |
| 34 | int speaker = get_console_fd_or_die(); |
Denys Vlasenko | 0da1c0a | 2009-08-22 18:00:39 +0200 | [diff] [blame] | 35 | unsigned length, delay, rep; |
| 36 | unsigned tickrate_div_freq; |
| 37 | int c; |
Bernhard Reutner-Fischer | 45de074 | 2009-08-21 13:18:31 +0200 | [diff] [blame] | 38 | |
Denys Vlasenko | 0da1c0a | 2009-08-22 18:00:39 +0200 | [diff] [blame] | 39 | c = 'n'; |
| 40 | while (c != -1) { |
| 41 | if (c == 'n') { |
| 42 | tickrate_div_freq = CLOCK_TICK_RATE / FREQ; |
| 43 | length = LENGTH; |
| 44 | delay = DELAY; |
| 45 | rep = REPETITIONS; |
| 46 | } |
| 47 | c = getopt(argc, argv, "f:l:d:r:n"); |
| 48 | /* TODO: -s, -c: |
| 49 | * pipe stdin to stdout, but also beep after each line (-s) or char (-c) |
| 50 | */ |
| 51 | switch (c) { |
| 52 | case 'f': |
| 53 | /* TODO: what "-f 0" should do? */ |
| 54 | tickrate_div_freq = (unsigned)CLOCK_TICK_RATE / xatou(optarg); |
| 55 | continue; |
| 56 | case 'l': |
| 57 | length = xatou(optarg); |
| 58 | continue; |
| 59 | case 'd': |
| 60 | /* TODO: |
| 61 | * -d N, -D N |
| 62 | * specify a delay of N milliseconds between repetitions. |
| 63 | * -d specifies that this delay should only occur between beeps, |
| 64 | * that is, it should not occur after the last repetition. |
| 65 | * -D indicates that the delay should occur after every repetition |
| 66 | */ |
| 67 | delay = xatou(optarg); |
| 68 | continue; |
| 69 | case 'r': |
| 70 | rep = xatou(optarg); |
| 71 | continue; |
| 72 | case 'n': |
| 73 | case -1: |
| 74 | break; |
| 75 | default: |
| 76 | bb_show_usage(); |
| 77 | } |
Bernhard Reutner-Fischer | 45de074 | 2009-08-21 13:18:31 +0200 | [diff] [blame] | 78 | while (rep) { |
| 79 | //bb_info_msg("rep[%d] freq=%d, length=%d, delay=%d", rep, freq, length, delay); |
Denys Vlasenko | 0da1c0a | 2009-08-22 18:00:39 +0200 | [diff] [blame] | 80 | xioctl(speaker, KIOCSOUND, (void*)(long)tickrate_div_freq); |
Bernhard Reutner-Fischer | 45de074 | 2009-08-21 13:18:31 +0200 | [diff] [blame] | 81 | usleep(1000 * length); |
Denys Vlasenko | 0da1c0a | 2009-08-22 18:00:39 +0200 | [diff] [blame] | 82 | ioctl(speaker, KIOCSOUND, (void*)0); |
| 83 | if (--rep) |
Bernhard Reutner-Fischer | 45de074 | 2009-08-21 13:18:31 +0200 | [diff] [blame] | 84 | usleep(delay); |
| 85 | } |
Bernhard Reutner-Fischer | 45de074 | 2009-08-21 13:18:31 +0200 | [diff] [blame] | 86 | } |
Denys Vlasenko | 0da1c0a | 2009-08-22 18:00:39 +0200 | [diff] [blame] | 87 | |
Bernhard Reutner-Fischer | 45de074 | 2009-08-21 13:18:31 +0200 | [diff] [blame] | 88 | if (ENABLE_FEATURE_CLEAN_UP) |
| 89 | close(speaker); |
| 90 | return EXIT_SUCCESS; |
| 91 | } |
| 92 | /* |
| 93 | * so, e.g. Beethoven's 9th symphony "Ode an die Freude" would be |
| 94 | * something like: |
| 95 | a=$((220*3)) |
| 96 | b=$((247*3)) |
| 97 | c=$((262*3)) |
| 98 | d=$((294*3)) |
| 99 | e=$((329*3)) |
| 100 | f=$((349*3)) |
| 101 | g=$((392*3)) |
| 102 | #./beep -f$d -l200 -r2 -n -f$e -l100 -d 10 -n -f$c -l400 -f$g -l200 |
| 103 | ./beep -f$e -l200 -r2 \ |
| 104 | -n -d 100 -f$f -l200 \ |
| 105 | -n -f$g -l200 -r2 \ |
| 106 | -n -f$f -l200 \ |
| 107 | -n -f$e -l200 \ |
Denys Vlasenko | 0da1c0a | 2009-08-22 18:00:39 +0200 | [diff] [blame] | 108 | -n -f$d -l200 \ |
| 109 | -n -f$c -l200 -r2 \ |
| 110 | -n -f$d -l200 \ |
| 111 | -n -f$e -l200 \ |
| 112 | -n -f$e -l400 \ |
Bernhard Reutner-Fischer | 45de074 | 2009-08-21 13:18:31 +0200 | [diff] [blame] | 113 | -n -f$d -l100 \ |
| 114 | -n -f$d -l200 \ |
| 115 | */ |