From 22d43409f215e16cb2242ad41a4dd1e1bbcdcc9f Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Mon, 18 Sep 2017 17:03:20 -0600 Subject: [PATCH] apps/examples/apa102: Add a Rainbow example for APA102 LED Strip --- examples/README.txt | 5 + examples/apa102/.gitignore | 11 ++ examples/apa102/Kconfig | 22 ++++ examples/apa102/Make.defs | 39 +++++++ examples/apa102/Makefile | 53 +++++++++ examples/apa102/apa102_main.c | 196 ++++++++++++++++++++++++++++++++++ 6 files changed, 326 insertions(+) create mode 100644 examples/apa102/.gitignore create mode 100644 examples/apa102/Kconfig create mode 100644 examples/apa102/Make.defs create mode 100644 examples/apa102/Makefile create mode 100644 examples/apa102/apa102_main.c diff --git a/examples/README.txt b/examples/README.txt index 72f1941f3..98535fc79 100644 --- a/examples/README.txt +++ b/examples/README.txt @@ -87,6 +87,11 @@ examples/alarm CONFIG_EXAMPLES_ALARM_DEVPATH - RTC device path (/dev/rtc0) ONFIG_EXAMPLES_ALARM_SIGNO - Alarm signal +examples/ap102 +^^^^^^^^^^^^^^ + + Rainbow example for APA102 LED Strip. + examples/bastest ^^^^^^^^^^^^^^^^ This directory contains a small program that will mount a ROMFS file system diff --git a/examples/apa102/.gitignore b/examples/apa102/.gitignore new file mode 100644 index 000000000..fa1ec7579 --- /dev/null +++ b/examples/apa102/.gitignore @@ -0,0 +1,11 @@ +/Make.dep +/.depend +/.built +/*.asm +/*.obj +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src diff --git a/examples/apa102/Kconfig b/examples/apa102/Kconfig new file mode 100644 index 000000000..520251f4a --- /dev/null +++ b/examples/apa102/Kconfig @@ -0,0 +1,22 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_APA102 + bool "APA102 LED Strip example" + default n + ---help--- + Enable the APA102 example + +if EXAMPLES_APA102 + +config EXAMPLES_APA102_PROGNAME + string "Program name" + default "apa102" + depends on BUILD_KERNEL + ---help--- + This is the name of the program that will be use when the NSH ELF + program is installed. + +endif diff --git a/examples/apa102/Make.defs b/examples/apa102/Make.defs new file mode 100644 index 000000000..06d2c3905 --- /dev/null +++ b/examples/apa102/Make.defs @@ -0,0 +1,39 @@ +############################################################################ +# apps/examples/apa102/Make.defs +# Adds selected applications to apps/ build +# +# Copyright (C) 2015 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_APA102),y) +CONFIGURED_APPS += examples/apa102 +endif diff --git a/examples/apa102/Makefile b/examples/apa102/Makefile new file mode 100644 index 000000000..895c9cf23 --- /dev/null +++ b/examples/apa102/Makefile @@ -0,0 +1,53 @@ +############################################################################ +# apps/examples/apa102/Makefile +# +# Copyright (C) 2017 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 + +# APA102 built-in application info + +APPNAME = apa102 +PRIORITY = SCHED_PRIORITY_DEFAULT +STACKSIZE = 2048 + +# APA102 Test + +ASRCS = +CSRCS = +MAINSRC = apa102_main.c + +CONFIG_EXAMPLES_APA102_PROGNAME ?= apa102$(EXEEXT) +PROGNAME = $(CONFIG_EXAMPLES_APA102_PROGNAME) + +include $(APPDIR)/Application.mk diff --git a/examples/apa102/apa102_main.c b/examples/apa102/apa102_main.c new file mode 100644 index 000000000..98bcbdfb7 --- /dev/null +++ b/examples/apa102/apa102_main.c @@ -0,0 +1,196 @@ +/**************************************************************************** + * examples/apa102/apa102_main.c + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Copyright (c) 2015-2017 Pololu Corporation. + * 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 +#include +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define CONFIG_EXAMPLES_APA102_DEVNAME "/dev/leddrv0" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +#define NUM_LEDS 30 +#define US_DELAY 1000 + +struct apa102_ledstrip_s ledstrip[NUM_LEDS]; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: hsvtorgb + * + * Description: + * Converts a color from HSV to RGB. + * + * Note: This function is based on Pololu example: + * https://github.com/pololu/apa102-arduino/blob/master/examples/Rainbow/Rainbow.ino + * + * Input Parameters: + * h is hue, as a number between 0 and 360. + * s is the saturation, as a number between 0 and 255. + * v is the value, as a number between 0 and 255. + * + ****************************************************************************/ + +static struct apa102_ledstrip_s hsvtorgb(uint16_t h, uint8_t s, uint8_t v) +{ + struct apa102_ledstrip_s led; + uint8_t f = (h % 60) * 255 / 60; + uint8_t p = (255 - s) * (uint16_t)v / 255; + uint8_t q = (255 - f * (uint16_t)s / 255) * (uint16_t)v / 255; + uint8_t t = (255 - (255 - f) * (uint16_t)s / 255) * (uint16_t)v / 255; + uint8_t r = 0; + uint8_t g = 0; + uint8_t b = 0; + + switch((h / 60) % 6) + { + case 0: + r = v; + g = t; + b = p; + break; + + case 1: + r = q; + g = v; + b = p; + break; + + case 2: + r = p; + g = v; + b = t; + break; + + case 3: + r = p; + g = q; + b = v; + break; + + case 4: + r = t; + g = p; + b = v; + break; + + case 5: + r = v; + g = p; + b = q; + break; + } + + led.red = r; + led.green = g; + led.blue = b; + led.bright = 0; + + return led; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * apa102_main + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int apa102_main(int argc, char *argv[]) +#endif +{ + int fd; + int ret; + int led; + int miles = 10000; + + fd = open(CONFIG_EXAMPLES_APA102_DEVNAME, O_RDWR); + if (fd < 0) + { + fprintf(stderr, "ERROR: Failed to open %s: %d\n", + CONFIG_EXAMPLES_APA102_DEVNAME, errno); + return -1; + } + + for (; ; ) + { + /* Fill the LED Strip */ + + for (led = 0; led < NUM_LEDS; led++) + { + uint8_t p = miles - led * 8; + ledstrip[led] = hsvtorgb((uint32_t)p * 359 / 256, 255, 255); + } + + /* Write the new colors in all the LEDs */ + + ret = write(fd, ledstrip, 4 * NUM_LEDS); + if (ret < 0) + { + _err("ERROR: write LED Strip failed: %d\n", errno); + } + + usleep(US_DELAY); + miles += (US_DELAY / 1000); + } + + close(fd); + return 0; +}