diff --git a/samples/sensor/isl29035/CMakeLists.txt b/samples/sensor/isl29035/CMakeLists.txt new file mode 100644 index 0000000000000..c709e05c87bea --- /dev/null +++ b/samples/sensor/isl29035/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.13.1) +include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) +project(isl29035) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/samples/sensor/isl29035/README.rst b/samples/sensor/isl29035/README.rst new file mode 100644 index 0000000000000..5f362e2af41fd --- /dev/null +++ b/samples/sensor/isl29035/README.rst @@ -0,0 +1,70 @@ +.. _isl29035: + +ISL29035: Digital Light Sensor +############################## + +Overview +******** + +If trigger is not enabled the sample displays measured light intensity +every 2 seconds. + +If trigger is enabled the sample displays light intensity from the +ISL29035 sensor every 10 seconds if it is within +/- 50 lux of the last +read sample. If the sensor detects an intensity outside that range the +application wakes, displays the intensity, resets the intensity range +window to center on the new value, then continues as before. + +Requirements +************ + +This sample uses an external breakout for the sensor. A devicetree +overlay must be provided to connect the sensor to the I2C bus and +identify the interrupt signal. + +Building and Running +******************** + +.. zephyr-app-commands:: + :zephyr-app: samples/sensor/isl29035 + :board: nrf52_pca10040 + :goals: build + :compact: + +Sample Output +============= + +.. code-block:: console + + *** Booting Zephyr OS build zephyr-v2.1.0-335-gfe020d937d43 *** + ALERT 365 lux outside range centered on 0 lux. + Next alert outside 315 .. 415 + [0:00:00.018] Ambient light sense: 365.234 + [0:00:10.023] Ambient light sense: 361.084 + ALERT 302 lux outside range centered on 365 lux. + Next alert outside 252 .. 352 + [0:00:13.276] Ambient light sense: 302.734 + ALERT 247 lux outside range centered on 302 lux. + Next alert outside 197 .. 297 + [0:00:14.619] Ambient light sense: 247.62 + ALERT 187 lux outside range centered on 247 lux. + Next alert outside 137 .. 237 + [0:00:16.141] Ambient light sense: 187.927 + ALERT 126 lux outside range centered on 187 lux. + Next alert outside 76 .. 176 + [0:00:16.410] Ambient light sense: 126.953 + ALERT 181 lux outside range centered on 126 lux. + Next alert outside 131 .. 231 + [0:00:17.843] Ambient light sense: 181.03 + ALERT 235 lux outside range centered on 181 lux. + Next alert outside 185 .. 285 + [0:00:18.022] Ambient light sense: 235.779 + ALERT 301 lux outside range centered on 235 lux. + Next alert outside 251 .. 351 + [0:00:23.126] Ambient light sense: 301.758 + ALERT 353 lux outside range centered on 301 lux. + Next alert outside 303 .. 403 + [0:00:23.305] Ambient light sense: 353.333 + [0:00:33.310] Ambient light sense: 365.112 + + diff --git a/samples/sensor/isl29035/boards/nrf52_pca10040.overlay b/samples/sensor/isl29035/boards/nrf52_pca10040.overlay new file mode 100644 index 0000000000000..77e69ee0e0356 --- /dev/null +++ b/samples/sensor/isl29035/boards/nrf52_pca10040.overlay @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +&i2c0 { /* SDA P0.26, SCL P0.27, ALERT P1.11 */ + isl29035@44 { + compatible = "isil,isl29035"; + reg = <0x44>; + label = "ISL29035"; + int-gpios = <&gpio0 11 GPIO_INT_ACTIVE_HIGH>; + }; +}; diff --git a/samples/sensor/isl29035/prj.conf b/samples/sensor/isl29035/prj.conf new file mode 100644 index 0000000000000..ad5f438858338 --- /dev/null +++ b/samples/sensor/isl29035/prj.conf @@ -0,0 +1,6 @@ +CONFIG_STDOUT_CONSOLE=y +CONFIG_I2C=y +CONFIG_SENSOR=y +CONFIG_ISL29035=y +CONFIG_ISL29035_TRIGGER_GLOBAL_THREAD=y +CONFIG_ISL29035_LUX_RANGE_4K=y diff --git a/samples/sensor/isl29035/sample.yaml b/samples/sensor/isl29035/sample.yaml new file mode 100644 index 0000000000000..6e6556ed81727 --- /dev/null +++ b/samples/sensor/isl29035/sample.yaml @@ -0,0 +1,8 @@ +sample: + name: ISL29035 Digital Light Sensor +tests: + sample.sensor.isl29035: + harness: console + tags: sensors + depends_on: i2c + platform_whitelist: nrf52_pca10040 diff --git a/samples/sensor/isl29035/src/main.c b/samples/sensor/isl29035/src/main.c new file mode 100644 index 0000000000000..5907cc059c138 --- /dev/null +++ b/samples/sensor/isl29035/src/main.c @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2019 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include + +#define LUX_ALERT_DELTA 50 + +static volatile bool alerted; +struct k_sem sem; + +#ifdef CONFIG_ISL29035_TRIGGER +static void trigger_handler(struct device *dev, + struct sensor_trigger *trig) +{ + alerted = !alerted; + k_sem_give(&sem); +} +#endif /* CONFIG_ISL29035_TRIGGER */ + +static const char *now_str(void) +{ + static char buf[16]; /* ...HH:MM:SS.MMM */ + u32_t now = k_uptime_get_32(); + unsigned int ms = now % MSEC_PER_SEC; + unsigned int s; + unsigned int min; + unsigned int h; + + now /= MSEC_PER_SEC; + s = now % 60U; + now /= 60U; + min = now % 60U; + now /= 60U; + h = now; + + snprintf(buf, sizeof(buf), "%u:%02u:%02u.%03u", + h, min, s, ms); + return buf; +} + +static void process_sample(struct device *dev) +{ + static bool last_alerted; + struct sensor_value val; + + if (sensor_sample_fetch(dev) < 0) { + printf("Sensor sample update error\n"); + return; + } + + if (sensor_channel_get(dev, SENSOR_CHAN_LIGHT, &val) < 0) { + printf("Cannot read ISL29035 value\n"); + return; + } + + int lux = val.val1; + + if (IS_ENABLED(CONFIG_ISL29035_TRIGGER) + && (alerted != last_alerted)) { + static int last_lux; + int rc; + struct sensor_trigger trig = { + .type = SENSOR_TRIG_THRESHOLD, + .chan = SENSOR_CHAN_ALL, + }; + struct sensor_value lo_thr = { MAX(lux - LUX_ALERT_DELTA, 0), }; + struct sensor_value hi_thr = { lux + LUX_ALERT_DELTA }; + + printf("ALERT %d lux outside range centered on %d lux." + "\nNext alert outside %d .. %d\n", + lux, last_lux, lo_thr.val1, hi_thr.val1); + last_lux = lux; + last_alerted = alerted; + + rc = sensor_attr_set(dev, SENSOR_CHAN_LIGHT, + SENSOR_ATTR_LOWER_THRESH, &lo_thr); + if (rc == 0) { + rc = sensor_attr_set(dev, SENSOR_CHAN_LIGHT, + SENSOR_ATTR_UPPER_THRESH, &hi_thr); + } + if (rc == 0) { + rc = sensor_trigger_set(dev, &trig, trigger_handler); + } + if (rc != 0) { + printf("Alert configuration failed: %d\n", rc); + } + } + + printf("[%s] %s: %g\n", now_str(), + IS_ENABLED(CONFIG_ISL29035_MODE_ALS) + ? "Ambient light sense" + : "IR sense", + sensor_value_to_double(&val)); +} + +void main(void) +{ + struct device *dev = device_get_binding("ISL29035"); + + if (dev == NULL) { + printf("Could not get ISL29035 device\n"); + return; + } + + k_sem_init(&sem, 0, 1); + alerted = true; + while (true) { + process_sample(dev); + + if (IS_ENABLED(CONFIG_ISL29035_TRIGGER)) { + k_sem_take(&sem, K_SECONDS(10)); + } else { + k_sleep(K_SECONDS(1)); + } + } +}