Skip to content

Commit

Permalink
samples: Add is31fl3216a LED sample
Browse files Browse the repository at this point in the history
This demonstrates and tests is31fl3216a driver functionality.

Signed-off-by: Guy Morand <guy.morand@bytesatwork.ch>
  • Loading branch information
morandg authored and carlescufi committed Jun 19, 2023
1 parent 890363a commit d87d934
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 0 deletions.
9 changes: 9 additions & 0 deletions samples/drivers/led_is31fl3216a/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(led_is31fl3216a)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
42 changes: 42 additions & 0 deletions samples/drivers/led_is31fl3216a/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.. _is31fl3216a:

is31fl3216a: 16 channels PWD LEDs controller
############################################

Overview
********

This sample controls up to 16 LEDs connected to a is31fl3216a driver.

Each LED is gradually pulsed until it reach 100% of luminosity and gradually
turned off again.

Once each LED was pulsed, multiple LEDs are pulse simultaneously using the
``write_channels`` LED API.

Test pattern
============

For each LED:
- Increase the luminosity until 100% is reached
- Decrease the luminosity until completely turned off

- Increase the luminosity of LEDs 2 to 4 until 100% is reached
- Decrease the luminosity of LEDs 2 to 4 until completely turned off

Building and Running
********************

This sample can be built and executed when the devicetree has an I2C device node
with compatible :dtcompatible:`issi,is31fl3216a` enabled, along with the relevant
bus controller node also being enabled.

As an example this sample provides a DTS overlay for the :ref:`lpcxpresso55s28`
board (:file:`boards/lpcxpresso55s28.overlay`). It assumes that a I2C
_is31fl3216a LED driver (with 16 LEDs wired) is connected to the I2C bus at
address 0x74.

References
**********

- is31fl3216a: https://lumissil.com/assets/pdf/core/IS31FL3216A_DS.pdf
19 changes: 19 additions & 0 deletions samples/drivers/led_is31fl3216a/boards/arduino_i2c.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2023 Endor AG
*
* SPDX-License-Identifier: Apache-2.0
*/

/*
* Example configuration of a is31fl3216a device on an Arduino I2C bus.
*
* Device address 0x74 is assumed. Your device may have a different
* address; check your device documentation if unsure.
*/
&arduino_i2c {
status = "okay";
is31fl3216a@74 {
compatible = "issi,is31fl3216a";
reg = <0x74>;
};
};
15 changes: 15 additions & 0 deletions samples/drivers/led_is31fl3216a/boards/lpcxpresso55s28.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) 2023 Endor AG
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/dt-bindings/led/led.h>

&flexcomm4 {
status = "okay";
is31fl3216a@74 {
compatible = "issi,is31fl3216a";
reg = <0x74>;
};
};
2 changes: 2 additions & 0 deletions samples/drivers/led_is31fl3216a/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_LOG=y
CONFIG_LED=y
8 changes: 8 additions & 0 deletions samples/drivers/led_is31fl3216a/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
sample:
description: Demonstration of the is31fl3216a LED driver
name: is31fl3216a sample
tests:
sample.drivers.led.is31fl3216a:
filter: dt_compat_enabled("issi,is31fl3216a")
tags: LED
depends_on: i2c
105 changes: 105 additions & 0 deletions samples/drivers/led_is31fl3216a/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2023 Endor AG
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/device.h>
#include <zephyr/drivers/led.h>
#include <zephyr/kernel.h>

#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(main, CONFIG_LOG_DEFAULT_LEVEL);

#define MAX_BRIGHTNESS 100
#define LAST_LED 15

#define SLEEP_DELAY_MS 100
#define FADE_DELAY_MS 10
#define WRITE_CHANNELS_LEDS_COUNT 3
#define WRITE_CHANNELS_LED_START 2

static void pulse_led(int led, const struct device *const dev)
{
int status;
uint8_t percent;

for (percent = 1 ; percent <= MAX_BRIGHTNESS ; percent++) {
status = led_set_brightness(dev, led, percent);
if (status) {
LOG_ERR("Could not change brightness: %i", status);
return;
}
k_msleep(FADE_DELAY_MS);
}
k_msleep(SLEEP_DELAY_MS);
for (percent = MAX_BRIGHTNESS;
percent <= MAX_BRIGHTNESS; percent--) {
status = led_set_brightness(dev, led, percent);
if (status) {
LOG_ERR("Could not change brightness: %i", status);
return;
}
k_msleep(FADE_DELAY_MS);
}
}

static void pulse_leds(const struct device *const dev)
{
int status;
uint8_t brightness[WRITE_CHANNELS_LEDS_COUNT];
uint8_t percent;

for (percent = 1; percent <= MAX_BRIGHTNESS; percent++) {
memset(brightness, percent, sizeof(brightness));
status = led_write_channels(dev, WRITE_CHANNELS_LED_START,
WRITE_CHANNELS_LEDS_COUNT,
brightness);
if (status) {
LOG_ERR("Could not change brightness: %i", status);
return;
}
k_msleep(FADE_DELAY_MS);
}
k_msleep(SLEEP_DELAY_MS);
for (percent = MAX_BRIGHTNESS;
percent <= MAX_BRIGHTNESS; percent--) {
memset(brightness, percent, sizeof(brightness));
status = led_write_channels(dev, WRITE_CHANNELS_LED_START,
WRITE_CHANNELS_LEDS_COUNT,
brightness);
if (status) {
LOG_ERR("Could not change brightness: %i", status);
return;
}
k_msleep(FADE_DELAY_MS);
}
}

int main(void)
{
int led;
const struct device *const is31fl3216a =
DEVICE_DT_GET_ANY(issi_is31fl3216a);

if (!is31fl3216a) {
LOG_ERR("No device with compatible issi,is31fl3216a found");
return 0;
} else if (!device_is_ready(is31fl3216a)) {
LOG_ERR("LED controller %s is not ready", is31fl3216a->name);
return 0;
}

LOG_INF("Found LED controller %s", is31fl3216a->name);
for (;;) {
LOG_INF("Pulsing single LED");
for (led = 0 ; led <= LAST_LED ; led++) {
pulse_led(led, is31fl3216a);
}
LOG_INF("Pulsing multiple LEDs");
pulse_leds(is31fl3216a);
}

return 0;
}

0 comments on commit d87d934

Please sign in to comment.