Skip to content

Instantly share code, notes, and snippets.

@64lines
Created December 26, 2025 14:41
Show Gist options
  • Select an option

  • Save 64lines/38c4089431428169d083edba7c492a1e to your computer and use it in GitHub Desktop.

Select an option

Save 64lines/38c4089431428169d083edba7c492a1e to your computer and use it in GitHub Desktop.
Button Event Example nRF9161
/*
* 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
#define SLEEP_TIME_MS 500
/* 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)
#define BUTTON0_NODE DT_ALIAS(sw0)
/*
* A build error on this line means your board is unsupported.
* See the sample documentation for information on how to fix this.
*/
static struct gpio_dt_spec led0_node = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
static struct gpio_dt_spec led1_node = GPIO_DT_SPEC_GET(LED1_NODE, gpios);
static struct gpio_dt_spec led2_node = GPIO_DT_SPEC_GET(LED2_NODE, gpios);
static struct gpio_dt_spec led3_node = GPIO_DT_SPEC_GET(LED3_NODE, gpios);
static struct gpio_dt_spec button0_node = GPIO_DT_SPEC_GET(BUTTON0_NODE, gpios);
static struct gpio_callback button_cb_data;
static bool is_clockwise;
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;
}
}
k_msleep(DELAY);
if (times == run_times) {
break;
}
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);
}
}
void button_pressed(const struct device *dev, struct gpio_callback *cb,
uint32_t pins)
{
is_clockwise = !is_clockwise;
printk("Button pressed at %" PRIu32 "\n", k_cycle_get_32());
}
int main(void)
{
int ret;
is_clockwise = true;
if (!gpio_is_ready_dt(&button0_node)) {
printk("Error: button device %s is not ready\n", button0_node.port->name);
return 0;
}
ret = gpio_pin_configure_dt(&button0_node, GPIO_INPUT);
if (ret != 0) {
printk("Error %d: failed to configure %s pin %d\n",
ret, button0_node.port->name, button0_node.pin);
return 0;
}
ret = gpio_pin_interrupt_configure_dt(&button0_node, GPIO_INT_EDGE_TO_ACTIVE);
if (ret != 0) {
printk("Error %d: failed to configure interrupt on %s pin %d\n",
ret, button0_node.port->name, button0_node.pin);
return 0;
}
gpio_init_callback(&button_cb_data, button_pressed, BIT(button0_node.pin));
gpio_add_callback(button0_node.port, &button_cb_data);
printk("Set up button at %s pin %d\n", button0_node.port->name, button0_node.pin);
if (led0_node.port && !gpio_is_ready_dt(&led0_node)) {
printk("Error %d: LED device %s is not ready; ignoring it\n",
ret, led0_node.port->name);
led0_node.port = NULL;
}
if (led0_node.port) {
ret = gpio_pin_configure_dt(&led0_node, GPIO_OUTPUT);
if (ret != 0) {
printk("Error %d: failed to configure LED device %s pin %d\n",
ret, led0_node.port->name, led0_node.pin);
led0_node.port = NULL;
} else {
printk("Set up LED at %s pin %d\n", led0_node.port->name, led0_node.pin);
}
}
printk("Press the button...\n");
if (led0_node.port) {
while (1) {
/* If we have an LED, match its state to the button's. */
animate_spining_leds(10, is_clockwise);
// k_msleep(SLEEP_TIME_MS);
}
}
// 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