Add 01_blinky_button
This commit is contained in:
1 parent
7e8ce94550
commit
cd2fda47ea
5 files changed
+140
No files matched your search
@@ -0,0 +1,102 @@
|
||||
#include <stdio.h>
|
||||
|
||||
// Include core zephyr kernel facilities e.g. k_sleep()
|
||||
#include <zephyr/kernel.h>
|
||||
|
||||
// Inlcude Zephyr's device model types and helpers
|
||||
#include <zephyr/device.h>
|
||||
|
||||
// Access the C macros for reading information from the devicetree at build time
|
||||
#include <zephyr/devicetree.h>
|
||||
|
||||
// Include GPIO driver API and the typed GPIO helpers required
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
|
||||
|
||||
#if !DT_NODE_HAS_STATUS(DT_ALIAS(led0), okay)
|
||||
#error "Board does not define alias led0. Add a board overlay"
|
||||
#endif
|
||||
|
||||
#if !DT_NODE_HAS_STATUS(DT_ALIAS(sw0), okay)
|
||||
#error "Board does not define alias sw0. Add a board overlay"
|
||||
#endif
|
||||
|
||||
#if DT_HAS_ALIAS(led0)
|
||||
static const struct gpio_dt_spec led_red =
|
||||
GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
|
||||
#endif
|
||||
|
||||
#if DT_HAS_ALIAS(led1)
|
||||
static const struct gpio_dt_spec led_blue =
|
||||
GPIO_DT_SPEC_GET(DT_ALIAS(led1), gpios);
|
||||
#endif
|
||||
|
||||
#if DT_HAS_ALIAS(led2)
|
||||
static const struct gpio_dt_spec led_green =
|
||||
GPIO_DT_SPEC_GET(DT_ALIAS(led2), gpios);
|
||||
#endif
|
||||
|
||||
#if DT_HAS_ALIAS(sw0)
|
||||
static const struct gpio_dt_spec button =
|
||||
GPIO_DT_SPEC_GET(DT_ALIAS(sw0), gpios);
|
||||
#endif
|
||||
|
||||
|
||||
int main(void) {
|
||||
|
||||
bool led_state = true;
|
||||
|
||||
// Make sure the GPIO is ready
|
||||
#if DT_HAS_ALIAS(led0)
|
||||
if (!gpio_is_ready_dt(&led_red)) {
|
||||
return 0;
|
||||
}
|
||||
gpio_pin_configure_dt(&led_red, GPIO_OUTPUT_ACTIVE);
|
||||
#endif
|
||||
|
||||
#if DT_HAS_ALIAS(led1)
|
||||
if (!gpio_is_ready_dt(&led_blue)) {
|
||||
return 0;
|
||||
}
|
||||
gpio_pin_configure_dt(&led_blue, GPIO_OUTPUT_ACTIVE);
|
||||
#endif
|
||||
|
||||
#if DT_HAS_ALIAS(led2)
|
||||
if (!gpio_is_ready_dt(&led_green)) {
|
||||
return 0;
|
||||
}
|
||||
gpio_pin_configure_dt(&led_green, GPIO_OUTPUT_INACTIVE);
|
||||
#endif
|
||||
|
||||
#if DT_HAS_ALIAS(sw0)
|
||||
if (!gpio_is_ready_dt(&button)) {
|
||||
return 0;
|
||||
}
|
||||
gpio_pin_configure_dt(&button, GPIO_INPUT);
|
||||
#endif
|
||||
|
||||
while (1) {
|
||||
|
||||
#if DT_HAS_ALIAS(led0)
|
||||
gpio_pin_toggle_dt(&led_red);
|
||||
// gpio_pin_set_dt(&led_red, led_state);
|
||||
#endif
|
||||
|
||||
#if DT_HAS_ALIAS(led1)
|
||||
gpio_pin_toggle_dt(&led_blue);
|
||||
// gpio_pin_set_dt(&led_blue, led_state);
|
||||
#endif
|
||||
|
||||
uint8_t button_status = gpio_pin_get_dt(&button);
|
||||
gpio_pin_set_dt(&led_green, button_status);
|
||||
|
||||
led_state = !led_state;
|
||||
printf("LED state: %s\n", led_state ? "ON" : "OFF");
|
||||
|
||||
k_msleep(500);
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user