Skip to content

Commit ed6ee24

Browse files
gmarullcarlescufi
authored andcommitted
app: update application to use the custom blink API
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]>
1 parent 6f162aa commit ed6ee24

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

Diff for: app/prj.conf

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
# This file contains selected Kconfig options for the application.
55

66
CONFIG_SENSOR=y
7+
CONFIG_BLINK=y

Diff for: app/src/main.c

+38-6
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@
55

66
#include <zephyr/kernel.h>
77
#include <zephyr/drivers/sensor.h>
8+
#include <zephyr/logging/log.h>
9+
10+
#include <app/drivers/blink.h>
11+
812
#include <app_version.h>
913

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

16+
#define BLINK_PERIOD_MS_STEP 100U
17+
#define BLINK_PERIOD_MS_MAX 1000U
18+
1319
int main(void)
1420
{
1521
int ret;
16-
const struct device *sensor;
22+
unsigned int period_ms = BLINK_PERIOD_MS_MAX;
23+
const struct device *sensor, *blink;
24+
struct sensor_value last_val = { 0 }, val;
1725

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

@@ -23,9 +31,21 @@ int main(void)
2331
return 0;
2432
}
2533

26-
while (1) {
27-
struct sensor_value val;
34+
blink = DEVICE_DT_GET(DT_NODELABEL(blink_led));
35+
if (!device_is_ready(blink)) {
36+
LOG_ERR("Blink LED not ready");
37+
return 0;
38+
}
2839

40+
ret = blink_off(blink);
41+
if (ret < 0) {
42+
LOG_ERR("Could not turn off LED (%d)", ret);
43+
return 0;
44+
}
45+
46+
printk("Use the sensor to change LED blinking period\n");
47+
48+
while (1) {
2949
ret = sensor_sample_fetch(sensor);
3050
if (ret < 0) {
3151
LOG_ERR("Could not fetch sample (%d)", ret);
@@ -38,9 +58,21 @@ int main(void)
3858
return 0;
3959
}
4060

41-
printk("Sensor value: %d\n", val.val1);
61+
if ((last_val.val1 == 0) && (val.val1 == 1)) {
62+
if (period_ms == 0U) {
63+
period_ms = BLINK_PERIOD_MS_MAX;
64+
} else {
65+
period_ms -= BLINK_PERIOD_MS_STEP;
66+
}
67+
68+
printk("Proximity detected, setting LED period to %u ms\n",
69+
period_ms);
70+
blink_set_period_ms(blink, period_ms);
71+
}
72+
73+
last_val = val;
4274

43-
k_sleep(K_MSEC(1000));
75+
k_sleep(K_MSEC(100));
4476
}
4577

4678
return 0;

0 commit comments

Comments
 (0)