From 9095bd4f1bc1a1802fb0871b289bca6262a00930 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Fri, 23 Feb 2018 10:49:38 -0600 Subject: [PATCH] apps/graphics/ft80x: Add interfaces to control the backlight. apps/examples/ft80x: Fade the display on and off between each example. --- examples/ft80x/ft80x_main.c | 16 ++-- graphics/ft80x/Makefile | 2 +- graphics/ft80x/ft80x_backlight.c | 133 +++++++++++++++++++++++++++++++ include/graphics/ft80x.h | 38 +++++++++ 4 files changed, 183 insertions(+), 6 deletions(-) create mode 100644 graphics/ft80x/ft80x_backlight.c diff --git a/examples/ft80x/ft80x_main.c b/examples/ft80x/ft80x_main.c index 93504f998..205872433 100644 --- a/examples/ft80x/ft80x_main.c +++ b/examples/ft80x/ft80x_main.c @@ -166,6 +166,10 @@ static int ft80x_showname(int fd, FAR struct ft80x_dlbuffer_s *buffer, struct ft80x_cmd_text_s text; int ret; + /* Mkae sure that the backlight off */ + + (void)ft80x_backlight_set(fd, 0); + /* Create the display list */ ret = ft80x_dl_start(fd, buffer, false); @@ -206,9 +210,10 @@ static int ft80x_showname(int fd, FAR struct ft80x_dlbuffer_s *buffer, return ret; } - /* Wait bit so that the user can read the name */ + /* Fade on, then wait bit so that the user can read the name */ - sleep(2); + (void)ft80x_backlight_fade(fd, 100, 1000); + (void)sleep(1); return OK; } @@ -236,7 +241,7 @@ static int ft80x_example(int fd, FAR struct ft80x_dlbuffer_s *buffer, return ret; } - /* Then executte the example */ + /* Then execute the example */ ret = example->func(fd, buffer); if (ret < 0) @@ -245,9 +250,10 @@ static int ft80x_example(int fd, FAR struct ft80x_dlbuffer_s *buffer, return ret; } - /* Wait a bit */ + /* Wait a bit, then fade out */ - sleep(4); + sleep(3); + (void)ft80x_backlight_fade(fd, 0, 1000); return OK; } diff --git a/graphics/ft80x/Makefile b/graphics/ft80x/Makefile index adf352f33..8d5089edc 100644 --- a/graphics/ft80x/Makefile +++ b/graphics/ft80x/Makefile @@ -41,7 +41,7 @@ include $(APPDIR)/Make.defs ASRCS = CSRCS = ft80x_dl.c ft80x_ramg.c ft80x_ramdl.c ft80x_ramcmd.c ft80x_coproc.c -CSRCS += ft80x_touch.c ft80x_regs.c +CSRCS += ft80x_touch.c ft80x_backlight.c ft80x_regs.c AOBJS = $(ASRCS:.S=$(OBJEXT)) COBJS = $(CSRCS:.c=$(OBJEXT)) diff --git a/graphics/ft80x/ft80x_backlight.c b/graphics/ft80x/ft80x_backlight.c new file mode 100644 index 000000000..009e21a20 --- /dev/null +++ b/graphics/ft80x/ft80x_backlight.c @@ -0,0 +1,133 @@ +/**************************************************************************** + * apps/graphics/ft80x/ft80x_backlight.c + * + * Copyright (C) 2018 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include + +#include + +#include "graphics/ft80x.h" +#include "ft80x.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ft80x_backlight_set + * + * Description: + * Set the backlight intensity via the PWM duty. + * + * Input Parameters: + * fd - The file descriptor of the FT80x device. Opened by the caller + * with write access. + * duty - The new backlight duty (as a percentage 0..100) + * delay - The duration of the fade in milliseconds. + * + * Returned Value: + * Zero (OK) on success. A negated errno value on failure. + * + ****************************************************************************/ + +int ft80x_backlight_set(int fd, uint8_t duty) +{ + uint16_t duty128; + int ret; + + DEBUGASSERT(duty <= 100); + + /* 0% corresponds to the value 0, but 100% corresponds to the value 128 */ + + duty128 = ((uint16_t)duty << 7) / 100; + + /* Perform the IOCTL to set the backlight duty */ + + ret = ft80x_putreg8(fd, FT80X_REG_PWM_DUTY, (uint8_t)duty128); + if (ret < 0) + { + ft80x_err("ERROR: ft80x_putreg8 failed: %d\n", ret); + } + + return ret; +} + +/**************************************************************************** + * Name: ft80x_backlight_fade + * + * Description: + * Change the backlight intensity with a controllable fade. + * + * Input Parameters: + * fd - The file descriptor of the FT80x device. Opened by the caller + * with write access. + * duty - The terminal duty (as a percentage 0..100) + * delay - The duration of the fade in milliseconds (10..16700) + * + * Returned Value: + * Zero (OK) on success. A negated errno value on failure. + * + ****************************************************************************/ + +int ft80x_backlight_fade(int fd, uint8_t duty, uint16_t delay) +{ + struct ft80x_fade_s fade; + int ret; + + DEBUGASSERT(duty <= 100); + + /* Perform the IOCTL to perform the fade */ + + fade.delay = duty; + fade.delay = delay; + + ret = ioctl(fd, FT80X_IOC_FADE, (unsigned long)((uintptr_t)&fade)); + if (ret < 0) + { + int errcode = errno; + ft80x_err("ERROR: ioctl(FT80X_IOC_FADE) failed: %d\n", errcode); + ret = -errcode; + } + + return ret; +} diff --git a/include/graphics/ft80x.h b/include/graphics/ft80x.h index 6e557304d..91cb7152c 100644 --- a/include/graphics/ft80x.h +++ b/include/graphics/ft80x.h @@ -326,6 +326,44 @@ int ft80x_ramg_write(int fd, unsigned int offset, FAR const void *data, int ft80x_touch_gettransform(int fd, FAR uint32_t matrix[6]); +/**************************************************************************** + * Name: ft80x_backlight_set + * + * Description: + * Set the backlight intensity via the PWM duty. + * + * Input Parameters: + * fd - The file descriptor of the FT80x device. Opened by the caller + * with write access. + * duty - The new backlight duty (as a percentage 0..100) + * delay - The duration of the fade in milliseconds. + * + * Returned Value: + * Zero (OK) on success. A negated errno value on failure. + * + ****************************************************************************/ + +int ft80x_backlight_set(int fd, uint8_t duty); + +/**************************************************************************** + * Name: ft80x_backlight_fade + * + * Description: + * Change the backlight intensity with a controllable fade. + * + * Input Parameters: + * fd - The file descriptor of the FT80x device. Opened by the caller + * with write access. + * duty - The terminal duty (as a percentage 0..100) + * delay - The duration of the fade in milliseconds (10..16700). + * + * Returned Value: + * Zero (OK) on success. A negated errno value on failure. + * + ****************************************************************************/ + +int ft80x_backlight_fade(int fd, uint8_t duty, uint16_t delay); + #undef EXTERN #ifdef __cplusplus }