Skip to content

Commit

Permalink
app: update application to use the custom blink API
Browse files Browse the repository at this point in the history
Update the application to use the new custom blink API. It changes the
blink period if the sensor reports a proximity value that is 1.

Signed-off-by: Gerard Marull-Paretas <[email protected]>
  • Loading branch information
gmarull authored and carlescufi committed Apr 9, 2024
1 parent 6f162aa commit ed6ee24
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
# This file contains selected Kconfig options for the application.

CONFIG_SENSOR=y
CONFIG_BLINK=y
44 changes: 38 additions & 6 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@

#include <zephyr/kernel.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/logging/log.h>

#include <app/drivers/blink.h>

#include <app_version.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(main, CONFIG_APP_LOG_LEVEL);

#define BLINK_PERIOD_MS_STEP 100U
#define BLINK_PERIOD_MS_MAX 1000U

int main(void)
{
int ret;
const struct device *sensor;
unsigned int period_ms = BLINK_PERIOD_MS_MAX;
const struct device *sensor, *blink;
struct sensor_value last_val = { 0 }, val;

printk("Zephyr Example Application %s\n", APP_VERSION_STRING);

Expand All @@ -23,9 +31,21 @@ int main(void)
return 0;
}

while (1) {
struct sensor_value val;
blink = DEVICE_DT_GET(DT_NODELABEL(blink_led));
if (!device_is_ready(blink)) {
LOG_ERR("Blink LED not ready");
return 0;
}

ret = blink_off(blink);
if (ret < 0) {
LOG_ERR("Could not turn off LED (%d)", ret);
return 0;
}

printk("Use the sensor to change LED blinking period\n");

while (1) {
ret = sensor_sample_fetch(sensor);
if (ret < 0) {
LOG_ERR("Could not fetch sample (%d)", ret);
Expand All @@ -38,9 +58,21 @@ int main(void)
return 0;
}

printk("Sensor value: %d\n", val.val1);
if ((last_val.val1 == 0) && (val.val1 == 1)) {
if (period_ms == 0U) {
period_ms = BLINK_PERIOD_MS_MAX;
} else {
period_ms -= BLINK_PERIOD_MS_STEP;
}

printk("Proximity detected, setting LED period to %u ms\n",
period_ms);
blink_set_period_ms(blink, period_ms);
}

last_val = val;

k_sleep(K_MSEC(1000));
k_sleep(K_MSEC(100));
}

return 0;
Expand Down

0 comments on commit ed6ee24

Please sign in to comment.