From 855d2e70bfd576e847926ad579db2822ea09d1f2 Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Tue, 23 Feb 2016 06:53:35 -0600 Subject: [PATCH] apps/examples/rgbled: Add an example using the RGB LED driver --- ChangeLog.txt | 3 + examples/Kconfig | 1 + examples/README.txt | 6 ++ examples/rgbled/.gitignore | 11 ++++ examples/rgbled/Kconfig | 30 ++++++++++ examples/rgbled/Make.defs | 39 +++++++++++++ examples/rgbled/Makefile | 56 ++++++++++++++++++ examples/rgbled/rgbled.c | 116 +++++++++++++++++++++++++++++++++++++ 8 files changed, 262 insertions(+) create mode 100644 examples/rgbled/.gitignore create mode 100644 examples/rgbled/Kconfig create mode 100644 examples/rgbled/Make.defs create mode 100644 examples/rgbled/Makefile create mode 100644 examples/rgbled/rgbled.c diff --git a/ChangeLog.txt b/ChangeLog.txt index 7cdd934be..1eb4e6e67 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1555,4 +1555,7 @@ * apps/examples/leds: An example to demonstrate use of LED driver (2016-02-20). * apps/examples/smp: An verifying SMP configurations (2016-02-21). + * apps/examples/rgbled: Example using the RGB LED driver to + drive an RGB LED via PWM. From Alan Carvalho de Assis + (2016-02-23). diff --git a/examples/Kconfig b/examples/Kconfig index 396f51e18..5549ef9ea 100644 --- a/examples/Kconfig +++ b/examples/Kconfig @@ -64,6 +64,7 @@ source "$APPSDIR/examples/posix_spawn/Kconfig" source "$APPSDIR/examples/qencoder/Kconfig" source "$APPSDIR/examples/random/Kconfig" source "$APPSDIR/examples/relays/Kconfig" +source "$APPSDIR/examples/rgbled/Kconfig" source "$APPSDIR/examples/rgmp/Kconfig" source "$APPSDIR/examples/romfs/Kconfig" source "$APPSDIR/examples/sendmail/Kconfig" diff --git a/examples/README.txt b/examples/README.txt index 63a822ef3..0d5861102 100644 --- a/examples/README.txt +++ b/examples/README.txt @@ -1695,6 +1695,12 @@ examples/relays NuttX is built as a protected, supervisor kernel (CONFIG_BUILD_PROTECTED or CONFIG_BUILD_KERNEL). +examples/rgbled +^^^^^^^^^^^^^^^ + + This example demonstrates the use of the RGB led driver to drive an RGB LED + with PWM outputs so that all color characteristcs of RGB LED can be controlled. + examples/rgmp ^^^^^^^^^^^^^ diff --git a/examples/rgbled/.gitignore b/examples/rgbled/.gitignore new file mode 100644 index 000000000..fa1ec7579 --- /dev/null +++ b/examples/rgbled/.gitignore @@ -0,0 +1,11 @@ +/Make.dep +/.depend +/.built +/*.asm +/*.obj +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src diff --git a/examples/rgbled/Kconfig b/examples/rgbled/Kconfig new file mode 100644 index 000000000..99ff6d372 --- /dev/null +++ b/examples/rgbled/Kconfig @@ -0,0 +1,30 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_RGBLED + bool "RGB LED Test" + default n + ---help--- + Enable the RGB LED example + +if EXAMPLES_RGBLED + +config EXAMPLES_RGBLED_PROGNAME + string "Program name" + default "rgbled" + depends on BUILD_KERNEL + ---help--- + This is the name of the program that will be use when the NSH ELF + program is installed. + +config EXAMPLES_RGBLED_PRIORITY + int "RGBLED task priority" + default 100 + +config EXAMPLES_RGBLED_STACKSIZE + int "RGBLED stack size" + default 2048 + +endif # EXAMPLES_RGBLED diff --git a/examples/rgbled/Make.defs b/examples/rgbled/Make.defs new file mode 100644 index 000000000..013a6ea93 --- /dev/null +++ b/examples/rgbled/Make.defs @@ -0,0 +1,39 @@ +############################################################################ +# apps/examples/rgbled/Make.defs +# Adds selected applications to apps/ build +# +# Copyright (C) 2016 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. +# +############################################################################ + +ifeq ($(CONFIG_EXAMPLES_RGBLED),y) +CONFIGURED_APPS += examples/rgbled +endif diff --git a/examples/rgbled/Makefile b/examples/rgbled/Makefile new file mode 100644 index 000000000..01db844c0 --- /dev/null +++ b/examples/rgbled/Makefile @@ -0,0 +1,56 @@ +############################################################################ +# apps/examples/rgbled/Makefile +# +# Copyright (C) 2016 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. +# +############################################################################ + +-include $(TOPDIR)/Make.defs + +# RGBLED built-in application info + +CONFIG_EXAMPLES_RGBLED_PRIORITY ?= SCHED_PRIORITY_DEFAULT +CONFIG_EXAMPLES_RGBLED_STACKSIZE ?= 2048 + +APPNAME = rgbled +PRIORITY = $(CONFIG_EXAMPLES_RGBLED_PRIORITY) +STACKSIZE = $(CONFIG_EXAMPLES_RGBLED_STACKSIZE) + +# RGBLED Example + +ASRCS = +CSRCS = +MAINSRC = rgbled.c + +CONFIG_EXAMPLES_RGBLED_PROGNAME ?= rgbled$(EXEEXT) +PROGNAME = $(CONFIG_EXAMPLES_RGBLED_PROGNAME) + +include $(APPDIR)/Application.mk diff --git a/examples/rgbled/rgbled.c b/examples/rgbled/rgbled.c new file mode 100644 index 000000000..71d8cc5d4 --- /dev/null +++ b/examples/rgbled/rgbled.c @@ -0,0 +1,116 @@ +/**************************************************************************** + * examples/rgbled/rgbled.c + * + * Copyright (C) 2016 Gregory Nutt. All rights reserved. + * Author: Alan Carvalho de Assis + * + * 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 + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define RGBLED "/dev/rgb0" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * rgbled_main + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int rgbled_main(int argc, char *argv[]) +#endif +{ + int red = 255; + int green = 0; + int blue = 0; + int sred = -1; + int sgreen = 1; + int sblue = 0; + int fd; + char buffer[8]; + + fd = open(RGBLED, O_WRONLY); + + if (fd < 0) + { + printf("Error opening %s!\n", RGBLED); + return -1; + } + + while(1) + { + red += sred; + green += sgreen; + blue += sblue; + + if (green == 255) + { + sred = 0; + sgreen = -1; + sblue = 1; + } + + if (blue == 255) + { + sred = 1; + sgreen = 0; + sblue = -1; + } + + if (red == 255) + { + sred = -1; + sblue = 0; + sgreen = 1; + } + + sprintf(buffer, "#%02X%02X%02X", red, green, blue); + (void)write(fd, buffer, 8); + usleep(5000); + } + + return 0; +}