Created
December 26, 2025 13:57
-
-
Save 64lines/13db33626fb765673b67e7ef2da6325f to your computer and use it in GitHub Desktop.
Spining LEDs for nRF9161
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright (c) 2016 Intel Corporation | |
| * | |
| * SPDX-License-Identifier: Apache-2.0 | |
| */ | |
| #include <stdio.h> | |
| #include <zephyr/kernel.h> | |
| #include <zephyr/drivers/gpio.h> | |
| /* 1000 msec = 1 sec */ | |
| #define DELAY 100 | |
| #define LED_SPINS 50 | |
| /* The devicetree node identifier for the "led0" alias. */ | |
| #define LED0_NODE DT_ALIAS(led0) | |
| #define LED1_NODE DT_ALIAS(led1) | |
| #define LED2_NODE DT_ALIAS(led2) | |
| #define LED3_NODE DT_ALIAS(led3) | |
| /* | |
| * A build error on this line means your board is unsupported. | |
| * See the sample documentation for information on how to fix this. | |
| */ | |
| static const struct gpio_dt_spec led0_node = GPIO_DT_SPEC_GET(LED0_NODE, gpios); | |
| static const struct gpio_dt_spec led1_node = GPIO_DT_SPEC_GET(LED1_NODE, gpios); | |
| static const struct gpio_dt_spec led2_node = GPIO_DT_SPEC_GET(LED2_NODE, gpios); | |
| static const struct gpio_dt_spec led3_node = GPIO_DT_SPEC_GET(LED3_NODE, gpios); | |
| void animate_spining_leds(int run_times, bool is_clockwise) { | |
| int ret; | |
| int index = 0; | |
| int times = 0; | |
| int j_index = 0; | |
| unsigned long gpio_output_status; | |
| struct gpio_dt_spec leds[4] = { led0_node, led1_node, led3_node, led2_node }; | |
| for (int j = 0; j < 4; j++) { | |
| if (!gpio_is_ready_dt(&leds[j])) { | |
| return; | |
| } | |
| } | |
| ret = gpio_pin_configure_dt(&leds[2], GPIO_OUTPUT_ACTIVE); | |
| if (ret < 0) { | |
| return; | |
| } | |
| for(;;) { | |
| for (int j = 0; j < 4; j++) { | |
| j_index = is_clockwise ? j : -j; | |
| gpio_output_status = ((index - j_index) % 4 == 0) ? GPIO_OUTPUT_ACTIVE : GPIO_OUTPUT_INACTIVE; | |
| ret = gpio_pin_configure_dt(&leds[j], gpio_output_status); | |
| if (ret < 0) { | |
| return; | |
| } | |
| } | |
| if (times == run_times) { | |
| break; | |
| } | |
| k_msleep(DELAY); | |
| index++; | |
| times++; | |
| } | |
| } | |
| void animate_back_and_forth_spining_leds() { | |
| bool is_clockwise; | |
| for(;;) { | |
| is_clockwise = false; | |
| animate_spining_leds(LED_SPINS, is_clockwise); | |
| is_clockwise = true; | |
| animate_spining_leds(LED_SPINS, is_clockwise); | |
| } | |
| } | |
| int main(void) | |
| { | |
| animate_back_and_forth_spining_leds(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment