diff --git a/code/nrf-connect/CMakeLists.txt b/code/nrf-connect/CMakeLists.txt index e4f3269..dbbd3cf 100644 --- a/code/nrf-connect/CMakeLists.txt +++ b/code/nrf-connect/CMakeLists.txt @@ -4,6 +4,4 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(b_parasite) -# set(BOARD_ROOT .) - -target_sources(app PRIVATE src/main.c src/prst/shtc3.c src/prst/adc.c) +target_sources(app PRIVATE src/main.c src/prst/shtc3.c src/prst/adc.c src/prst/led.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 c3cee1d..92a81f2 100644 --- a/code/nrf-connect/boards/arm/bparasite_nrf52840/bparasite_nrf52840.dts +++ b/code/nrf-connect/boards/arm/bparasite_nrf52840/bparasite_nrf52840.dts @@ -7,11 +7,6 @@ compatible = "rbaron,bparasite_nrf52840"; chosen { - // zephyr,console = &uart0; - // zephyr,shell-uart = &uart0; - // zephyr,uart-mcumgr = &uart0; - // zephyr,bt-mon-uart = &uart0; - // zephyr,bt-c2h-uart = &uart0; zephyr,sram = &sram0; zephyr,flash = &flash0; zephyr,code-partition = &slot0_partition; @@ -21,6 +16,14 @@ io-channels = <&adc 0>, <&adc 1>, <&adc 2>; }; + leds { + compatible = "gpio-leds"; + led0: led_0 { + // P0.28. + gpios = <&gpio0 28 GPIO_ACTIVE_HIGH>; + }; + }; + 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 395cc4d..3c42972 100644 --- a/code/nrf-connect/src/main.c +++ b/code/nrf-connect/src/main.c @@ -2,15 +2,15 @@ #include #include "prst/adc.h" +#include "prst/led.h" #include "prst/macros.h" #include "prst/shtc3.h" LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG); int main(void) { - if (prst_adc_init() != 0) { - LOG_ERR("Error initializing ADC."); - } + RET_IF_ERR(prst_adc_init()); + RET_IF_ERR(prst_led_init()); prst_adc_read_t batt; prst_adc_soil_moisture_t soil; @@ -20,11 +20,13 @@ int main(void) { RET_IF_ERR(prst_adc_soil_read(batt.voltage, &soil)); RET_IF_ERR(prst_adc_photo_read(batt.voltage, &photo)); - // LOG_INF("Batt: %d mV", batt.millivolts); - // LOG_INF("Soil: %.0f %% (%.3f mV)", 100 * soil.percentage, - // soil.adc_read.voltage); + LOG_INF("Batt: %d mV", batt.millivolts); + LOG_INF("Soil: %.0f %% (%.3f mV)", 100 * soil.percentage, + 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/led.c b/code/nrf-connect/src/prst/led.c new file mode 100644 index 0000000..10c1f7e --- /dev/null +++ b/code/nrf-connect/src/prst/led.c @@ -0,0 +1,17 @@ +#include "led.h" + +#include + +#include "macros.h" + +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; + } + 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 new file mode 100644 index 0000000..e8519ef --- /dev/null +++ b/code/nrf-connect/src/prst/led.h @@ -0,0 +1,33 @@ +#ifndef _PRST_LED_H_ +#define _PRST_LED_H_ + +#include +#include + +#include "macros.h" + +#define PRST_LED_FLASH_PERIOD_MS 400 + +extern struct gpio_dt_spec led; + +int prst_led_init(); + +static inline int prst_led_on() { + return gpio_pin_set_dt(&led, 1); +} + +static inline int prst_led_off() { + return gpio_pin_set_dt(&led, 0); +} + +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)); + k_msleep(PRST_LED_FLASH_PERIOD_MS / 2); + } + return 0; +} + +#endif // _PRST_LED_H_ \ No newline at end of file