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