Configs button

This commit is contained in:
rbaron 2022-11-14 22:37:03 +01:00
parent ffeca36c27
commit 8cfb87ef32
7 changed files with 61 additions and 8 deletions

View file

@ -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
)

View file

@ -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>;

View file

@ -2,6 +2,7 @@
#include <zephyr/zephyr.h>
#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);
}
}

View file

@ -0,0 +1,29 @@
#include "button.h"
#include <drivers/gpio.h>
#include <logging/log.h>
#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;
}

View file

@ -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_

View file

@ -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);
}

View file

@ -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;