mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <ahmad@a3f.at>
To: barebox@lists.infradead.org
Subject: [PATCH 5/7] commands: add beep command
Date: Sun, 31 Jan 2021 21:18:44 +0100	[thread overview]
Message-ID: <20210131201846.3440477-6-ahmad@a3f.at> (raw)
In-Reply-To: <20210131201846.3440477-1-ahmad@a3f.at>

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 <ahmad@a3f.at>
---
 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
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2021-01-31 20:18 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-31 20:18 [PATCH 0/7] Add sound and GRUB beep tune support Ahmad Fatoum
2021-01-31 20:18 ` [PATCH 1/7] sandbox: migrate to SDL 2.0 Ahmad Fatoum
2021-01-31 20:18 ` [PATCH 2/7] drivers: add sound card driver support Ahmad Fatoum
2021-01-31 20:18 ` [PATCH 3/7] sound: add basic synthesizers for PCM beeper use Ahmad Fatoum
2021-01-31 20:18 ` [PATCH 4/7] sound: add SDL 2.0 sound driver Ahmad Fatoum
2021-01-31 20:18 ` Ahmad Fatoum [this message]
2021-01-31 20:18 ` [PATCH 6/7] sound: add PWM beeper support Ahmad Fatoum
2021-01-31 20:18 ` [PATCH 7/7] sound: add gpio-beeper support Ahmad Fatoum
2021-02-08 10:42 ` [PATCH 0/7] Add sound and GRUB beep tune support Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210131201846.3440477-6-ahmad@a3f.at \
    --to=ahmad@a3f.at \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox