Add a beep command that's compatible with GRUB's

  play "tempo pitch1 duration1..."

Unlike the GRUB command, playing a tune is not a blocking operating.
For this reason barebox, additionally supports:
  * -w: wait until tune is over
  * -c: cancel playing tune

Additionally, `beep tempo` can be used to ring the bell at a frequency
chosen by the sound card.

Examples:

  # 1-up
  beep 1750 523 1 392 1 523 1 659 1 784 1 1047 1 784 1 415 1 523 \
        1 622 1 831 1 622 1 831 1 1046 1 1244 1 1661 1

  # 1-second beep
  beep 60

Signed-off-by: Ahmad Fatoum <[email protected]>
---
 commands/Kconfig  |  7 ++++
 commands/Makefile |  1 +
 commands/beep.c   | 99 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 107 insertions(+)
 create mode 100644 commands/beep.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 03ddfc887074..b672f0c16a85 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 dc285cd00e1b..034c0e6383d3 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 000000000000..4e90215cbf26
--- /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 diraction2] ...]")
+       BAREBOX_CMD_HELP(cmd_beep_help)
+       BAREBOX_CMD_GROUP(CMD_GRP_CONSOLE)
+BAREBOX_CMD_END
-- 
2.30.0


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

Reply via email to