From bda053296fb8ad5d7a3792fe47f7457dc9472d2a Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Thu, 6 Jun 2024 19:53:01 +0530 Subject: [PATCH] samples: Add blinky tone - Use `tone` and `noTone` functions to blink led at 1hz with duty cycle 50% on any digital pin Signed-off-by: Ayush Singh --- samples/blinky_tone/CMakeLists.txt | 13 ++++++ samples/blinky_tone/README.rst | 71 ++++++++++++++++++++++++++++++ samples/blinky_tone/prj.conf | 3 ++ samples/blinky_tone/src/main.cpp | 20 +++++++++ 4 files changed, 107 insertions(+) create mode 100644 samples/blinky_tone/CMakeLists.txt create mode 100644 samples/blinky_tone/README.rst create mode 100644 samples/blinky_tone/prj.conf create mode 100644 samples/blinky_tone/src/main.cpp diff --git a/samples/blinky_tone/CMakeLists.txt b/samples/blinky_tone/CMakeLists.txt new file mode 100644 index 00000000..e5c58ee3 --- /dev/null +++ b/samples/blinky_tone/CMakeLists.txt @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +cmake_path(SET ZephyrBase $ENV{ZEPHYR_BASE}) +set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${BOARD}/${BOARD}.overlay) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(blinky) + +target_sources(app PRIVATE src/main.cpp) + +zephyr_compile_options(-Wno-unused-variable -Wno-comment) diff --git a/samples/blinky_tone/README.rst b/samples/blinky_tone/README.rst new file mode 100644 index 00000000..46f141c0 --- /dev/null +++ b/samples/blinky_tone/README.rst @@ -0,0 +1,71 @@ +.. _blinky-tone-sample: + +Blinky Tone +############ + +Overview +******** + +This Arduino Blinky sample blinks an LED forever using the `tone` and `noTone`. + +Requirements +************ + +Your board must: + +#. Have an LED connected via a GPIO pin (these are called "User LEDs" on many of + Zephyr's :ref:`boards`). +#. Have the LED configured using the ``led0`` devicetree alias. + +Building and Running +******************** + +Build and flash Blinky as follows, + +```sh +$> west build -p -b arduino_nano_33_ble samples/basic/blinky_tone/ -DZEPHYR_EXTRA_MODULES=/home/$USER/zephyrproject/modules/lib/Arduino-Core-Zephyr + +$> west flash --bossac=/home/$USER/.arduino15/packages/arduino/tools/bossac/1.9.1-arduino2/bossac +``` + +After flashing, the LED starts to blink. If a runtime error occurs, the sample +exits without printing to the console. + +Adding board support +******************** + +To add support for your board, add something like this to your devicetree: + +.. code-block:: DTS + + / { + aliases { + led0 = &myled0; + }; + + leds { + compatible = "gpio-leds"; + myled0: led_0 { + gpios = <&gpio0 13 GPIO_ACTIVE_LOW>; + }; + }; + }; + +The above sets your board's ``led0`` alias to use pin 13 on GPIO controller +``gpio0``. The pin flags :c:macro:`GPIO_ACTIVE_HIGH` mean the LED is on when +the pin is set to its high state, and off when the pin is in its low state. + +Tips: + +- See :dtcompatible:`gpio-leds` for more information on defining GPIO-based LEDs + in devicetree. + +- If you're not sure what to do, check the devicetrees for supported boards which + use the same SoC as your target. See :ref:`get-devicetree-outputs` for details. + +- See :zephyr_file:`include/zephyr/dt-bindings/gpio/gpio.h` for the flags you can use + in devicetree. + +- If the LED is built in to your board hardware, the alias should be defined in + your :ref:`BOARD.dts file `. Otherwise, you can + define one in a :ref:`devicetree overlay `. diff --git a/samples/blinky_tone/prj.conf b/samples/blinky_tone/prj.conf new file mode 100644 index 00000000..100a4977 --- /dev/null +++ b/samples/blinky_tone/prj.conf @@ -0,0 +1,3 @@ +CONFIG_GPIO=y +CONFIG_CPLUSPLUS=y +CONFIG_ARDUINO_API=y \ No newline at end of file diff --git a/samples/blinky_tone/src/main.cpp b/samples/blinky_tone/src/main.cpp new file mode 100644 index 00000000..62fda53d --- /dev/null +++ b/samples/blinky_tone/src/main.cpp @@ -0,0 +1,20 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + */ + +/* Blink inbuilt LED with tone */ + +#include + +void setup() { + pinMode(LED_BUILTIN, OUTPUT); +} + +void loop() { + tone(LED_BUILTIN, 1, 10000); + delay(15000); + tone(LED_BUILTIN, 1); + delay(10000); + noTone(LED_BUILTIN); + delay(5000); +}