summaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2021-02-22 10:39:39 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2021-02-22 10:39:39 +0100
commit2b1f426e511bf5b41c84d05ec09949ebd5dc216e (patch)
treefcf06dba00432686c8c0bd5137357d2aabf356f8 /commands
parent774f731a63500deec5d529bb17a75a983f3a4629 (diff)
parentfdb315c4a0764c43916236363a0060972108f5d5 (diff)
downloadbarebox-2b1f426e511bf5b41c84d05ec09949ebd5dc216e.tar.gz
barebox-2b1f426e511bf5b41c84d05ec09949ebd5dc216e.tar.xz
Merge branch 'for-next/sound'
Diffstat (limited to 'commands')
-rw-r--r--commands/Kconfig7
-rw-r--r--commands/Makefile1
-rw-r--r--commands/beep.c99
3 files changed, 107 insertions, 0 deletions
diff --git a/commands/Kconfig b/commands/Kconfig
index 1667e50cc6..520ad4b1de 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -1474,6 +1474,13 @@ config CMD_FBTEST
Framebuffer test command that allows to produce a number of
test patterns on a screen.
+config CMD_BEEP
+ def_bool y
+ depends on SOUND
+ prompt "Beep"
+ help
+ Play beeps. Accepts same format as GRUB play
+
config CMD_READLINE
tristate
prompt "readline"
diff --git a/commands/Makefile b/commands/Makefile
index dc285cd00e..034c0e6383 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -58,6 +58,7 @@ obj-$(CONFIG_CMD_LSMOD) += lsmod.o
obj-$(CONFIG_CMD_INSMOD) += insmod.o
obj-$(CONFIG_CMD_SPLASH) += splash.o
obj-$(CONFIG_CMD_FBTEST) += fbtest.o
+obj-$(CONFIG_CMD_BEEP) += beep.o
obj-$(CONFIG_USB_GADGET_DFU) += dfu.o
obj-$(CONFIG_USB_GADGET_SERIAL) += usbserial.o
obj-$(CONFIG_CMD_GPIO) += gpio.o
diff --git a/commands/beep.c b/commands/beep.c
new file mode 100644
index 0000000000..29569fb51a
--- /dev/null
+++ b/commands/beep.c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// SPDX-FileCopyrightText: © 2021 Ahmad Fatoum
+
+#include <common.h>
+#include <command.h>
+#include <sound.h>
+#include <getopt.h>
+
+static int do_beep(int argc, char *argv[])
+{
+ int ret, i, opt;
+ u32 tempo, total_us = 0;
+ bool wait = false;
+
+ while((opt = getopt(argc, argv, "wc")) > 0) {
+ switch(opt) {
+ case 'w':
+ wait = true;
+ break;
+ case 'c':
+ return beep_cancel();
+ default:
+ return COMMAND_ERROR_USAGE;
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc == 0 || argc % 2 != 1)
+ return COMMAND_ERROR_USAGE;
+
+ ret = kstrtou32(argv[0], 0, &tempo);
+ if (ret || tempo == 0)
+ return COMMAND_ERROR_USAGE;
+
+ tempo = 60 * USEC_PER_SEC / tempo;
+
+ if (argc == 1) {
+ ret = beep(BELL_DEFAULT_FREQUENCY, tempo);
+ if (ret)
+ return ret;
+
+ total_us += tempo;
+ goto out;
+ }
+
+ for (i = 1; i < argc; i += 2) {
+ u32 pitch = 0, duration;
+ u16 val;
+
+ ret = kstrtou16(argv[i], 0, &val);
+ if (ret)
+ return COMMAND_ERROR_USAGE;
+
+ if (val)
+ pitch = clamp_t(unsigned, val, 20, 20000);
+
+ ret = kstrtou16(argv[i+1], 0, &val);
+ if (ret)
+ return COMMAND_ERROR_USAGE;
+
+ duration = val * tempo;
+
+ ret = beep(pitch, duration);
+ if (ret)
+ return ret;
+
+ total_us += duration;
+ }
+
+out:
+ if (wait)
+ beep_wait(total_us);
+
+ return 0;
+}
+
+/* https://www.gnu.org/software/grub/manual/grub/html_node/play.html */
+BAREBOX_CMD_HELP_START(beep)
+BAREBOX_CMD_HELP_TEXT("Tempo is an unsigned 32bit number. It's followed by pairs of unsigned")
+BAREBOX_CMD_HELP_TEXT("16bit numbers for pitch and duration.")
+BAREBOX_CMD_HELP_TEXT("The tempo is the base for all note durations. 60 gives a 1-second base,")
+BAREBOX_CMD_HELP_TEXT("120 gives a half-second base, etc. Pitches are Hz.")
+BAREBOX_CMD_HELP_TEXT("Set pitch to 0 to produce a rest.")
+BAREBOX_CMD_HELP_TEXT("When only tempo is given, a beep of duration 1 at bell frequency results.")
+BAREBOX_CMD_HELP_TEXT("")
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT ("-c", "cancel pending beeps")
+BAREBOX_CMD_HELP_OPT ("-w", "wait until beep is over")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(beep)
+ .cmd = do_beep,
+ BAREBOX_CMD_DESC("play a GRUB beep tune")
+ BAREBOX_CMD_OPTS("tempo [pitch1 duration1 [pitch2 duration2] ...]")
+ BAREBOX_CMD_HELP(cmd_beep_help)
+ BAREBOX_CMD_GROUP(CMD_GRP_CONSOLE)
+BAREBOX_CMD_END