From 8cfb87ef32f98e357631edc8c0130eae27f1c4e6 Mon Sep 17 00:00:00 2001 From: rbaron Date: Mon, 14 Nov 2022 22:37:03 +0100 Subject: [PATCH] Configs button --- code/nrf-connect/CMakeLists.txt | 8 ++++- .../bparasite_nrf52840/bparasite_nrf52840.dts | 8 +++++ code/nrf-connect/src/main.c | 6 ++-- code/nrf-connect/src/prst/button.c | 29 +++++++++++++++++++ code/nrf-connect/src/prst/button.h | 7 +++++ code/nrf-connect/src/prst/led.c | 5 +--- code/nrf-connect/src/prst/led.h | 6 +++- 7 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 code/nrf-connect/src/prst/button.c create mode 100644 code/nrf-connect/src/prst/button.h diff --git a/code/nrf-connect/CMakeLists.txt b/code/nrf-connect/CMakeLists.txt index dbbd3cf..a9d90ee 100644 --- a/code/nrf-connect/CMakeLists.txt +++ b/code/nrf-connect/CMakeLists.txt @@ -4,4 +4,10 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(b_parasite) -target_sources(app PRIVATE src/main.c src/prst/shtc3.c src/prst/adc.c src/prst/led.c) +target_sources(app PRIVATE + src/main.c + src/prst/shtc3.c + src/prst/adc.c + src/prst/led.c + src/prst/button.c +) diff --git a/code/nrf-connect/boards/arm/bparasite_nrf52840/bparasite_nrf52840.dts b/code/nrf-connect/boards/arm/bparasite_nrf52840/bparasite_nrf52840.dts index 92a81f2..8f27267 100644 --- a/code/nrf-connect/boards/arm/bparasite_nrf52840/bparasite_nrf52840.dts +++ b/code/nrf-connect/boards/arm/bparasite_nrf52840/bparasite_nrf52840.dts @@ -24,6 +24,14 @@ }; }; + buttons { + compatible = "gpio-keys"; + button0: button_0 { + gpios = <&gpio0 12 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>; + label = "Push button switch 0"; + }; + }; + soil_pwm: soil_pwm { compatible = "pwm-fixed"; pwms = <&pwm0 0 PWM_MSEC(100) PWM_POLARITY_INVERTED>; diff --git a/code/nrf-connect/src/main.c b/code/nrf-connect/src/main.c index 3c42972..5e09571 100644 --- a/code/nrf-connect/src/main.c +++ b/code/nrf-connect/src/main.c @@ -2,6 +2,7 @@ #include #include "prst/adc.h" +#include "prst/button.h" #include "prst/led.h" #include "prst/macros.h" #include "prst/shtc3.h" @@ -11,6 +12,9 @@ LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG); int main(void) { RET_IF_ERR(prst_adc_init()); RET_IF_ERR(prst_led_init()); + RET_IF_ERR(prst_button_init()); + + RET_IF_ERR(prst_led_flash(2)); prst_adc_read_t batt; prst_adc_soil_moisture_t soil; @@ -25,8 +29,6 @@ int main(void) { soil.adc_read.voltage); LOG_INF("Photo: %u lx (%.3f mV)", photo.brightness, soil.adc_read.voltage); - prst_led_flash(3); - k_msleep(500); } } diff --git a/code/nrf-connect/src/prst/button.c b/code/nrf-connect/src/prst/button.c new file mode 100644 index 0000000..04596ad --- /dev/null +++ b/code/nrf-connect/src/prst/button.c @@ -0,0 +1,29 @@ +#include "button.h" + +#include +#include + +#include "led.h" +#include "macros.h" + +LOG_MODULE_REGISTER(button, LOG_LEVEL_DBG); + +static struct gpio_dt_spec button = + GPIO_DT_SPEC_GET(DT_NODELABEL(button0), gpios); + +static struct gpio_callback cb_data; + +static void button_pressed(const struct device *dev, struct gpio_callback *cb, + uint32_t pins) { + LOG_INF("Button pressed"); + prst_led_toggle(); +} + +int prst_button_init() { + RET_IF_ERR(!device_is_ready(button.port)); + RET_IF_ERR(gpio_pin_configure_dt(&button, GPIO_INPUT)); + RET_IF_ERR(gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE)); + gpio_init_callback(&cb_data, button_pressed, BIT(button.pin)); + RET_IF_ERR(gpio_add_callback(button.port, &cb_data)); + return 0; +} \ No newline at end of file diff --git a/code/nrf-connect/src/prst/button.h b/code/nrf-connect/src/prst/button.h new file mode 100644 index 0000000..374a914 --- /dev/null +++ b/code/nrf-connect/src/prst/button.h @@ -0,0 +1,7 @@ +#ifndef _PRST_BUTTON_H_ +#define _PRST_BUTTON_H_ + +// Inits button driver and registers callback. +int prst_button_init(); + +#endif // _PRST_BUTTON_H_ \ No newline at end of file diff --git a/code/nrf-connect/src/prst/led.c b/code/nrf-connect/src/prst/led.c index 10c1f7e..25b428b 100644 --- a/code/nrf-connect/src/prst/led.c +++ b/code/nrf-connect/src/prst/led.c @@ -9,9 +9,6 @@ LOG_MODULE_REGISTER(led, LOG_LEVEL_DBG); struct gpio_dt_spec led = GPIO_DT_SPEC_GET(DT_NODELABEL(led0), gpios); int prst_led_init() { - if (!device_is_ready(led.port)) { - LOG_ERR("DEV NOT READY"); - return -1; - } + RET_IF_ERR(!device_is_ready(led.port)); return gpio_pin_configure_dt(&led, GPIO_OUTPUT); } \ No newline at end of file diff --git a/code/nrf-connect/src/prst/led.h b/code/nrf-connect/src/prst/led.h index e8519ef..11c7301 100644 --- a/code/nrf-connect/src/prst/led.h +++ b/code/nrf-connect/src/prst/led.h @@ -20,11 +20,15 @@ static inline int prst_led_off() { return gpio_pin_set_dt(&led, 0); } +static inline int prst_led_toggle() { + return gpio_pin_toggle_dt(&led); +} + static inline int prst_led_flash(int times) { LOG_MODULE_DECLARE(led, LOG_LEVEL_DBG); RET_IF_ERR(prst_led_off()); for (int i = 0; i < 2 * times; i++) { - RET_IF_ERR(gpio_pin_toggle_dt(&led)); + RET_IF_ERR(prst_led_toggle()); k_msleep(PRST_LED_FLASH_PERIOD_MS / 2); } return 0;