diff --git a/examples/README.txt b/examples/README.txt index 83da8083d..a1ca10c2f 100644 --- a/examples/README.txt +++ b/examples/README.txt @@ -177,7 +177,7 @@ examples/bridge examples/buttons ^^^^^^^^^^^^^^^^ -To be provided + To be provided examples/can ^^^^^^^^^^^^ diff --git a/examples/archbuttons/Makefile b/examples/archbuttons/Makefile index a0df8d8f8..670c0fcf0 100644 --- a/examples/archbuttons/Makefile +++ b/examples/archbuttons/Makefile @@ -1,5 +1,5 @@ ############################################################################ -# apps/examples/buttons/Makefile +# apps/examples/archbuttons/Makefile # # Copyright (C) 2011-2012 Gregory Nutt. All rights reserved. # Author: Gregory Nutt diff --git a/examples/buttons/.gitignore b/examples/buttons/.gitignore new file mode 100644 index 000000000..fa1ec7579 --- /dev/null +++ b/examples/buttons/.gitignore @@ -0,0 +1,11 @@ +/Make.dep +/.depend +/.built +/*.asm +/*.obj +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src diff --git a/examples/buttons/Kconfig b/examples/buttons/Kconfig new file mode 100644 index 000000000..adf92905f --- /dev/null +++ b/examples/buttons/Kconfig @@ -0,0 +1,80 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_BUTTONS + bool "Buttons driver example" + default n + depends on ARCH_HAVE_BUTTONS + ---help--- + Enable the Buttons drivers example + +if EXAMPLES_BUTTONS + +config EXAMPLES_BUTTONS_PROGNAME + string "Program name" + default "buttons" + 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_BUTTONS_PRIORITY + int "LED task priority" + default 100 + +config EXAMPLES_BUTTONS_STACKSIZE + int "LED stack size" + default 2048 + +config EXAMPLES_BUTTONS_DEVPATH + string "LED device path" + default "/dev/buttons" + +config EXAMPLES_BUTTONS_NAMES + bool "Show Buttons Names" + default n + ---help--- + Enable to show the button's name in the application. + +if EXAMPLES_BUTTONS_NAMES + +config EXAMPLES_BUTTONS_QTD +int "Quantity of Buttons in the Board" +default 8 + +config EXAMPLES_BUTTONS_NAME0 +string "Button 0 Name" +default "BUTTON0" + +config EXAMPLES_BUTTONS_NAME1 +string "Button 1 Name" +default "BUTTON1" + +config EXAMPLES_BUTTONS_NAME2 +string "Button 2 Name" +default "BUTTON2" + +config EXAMPLES_BUTTONS_NAME3 +string "Button 3 Name" +default "BUTTON3" + +config EXAMPLES_BUTTONS_NAME4 +string "Button 4 Name" +default "BUTTON4" + +config EXAMPLES_BUTTONS_NAME5 +string "Button 5 Name" +default "BUTTON5" + +config EXAMPLES_BUTTONS_NAME6 +string "Button 6 Name" +default "BUTTON6" + +config EXAMPLES_BUTTONS_NAME7 +string "Button 7 Name" +default "BUTTON7" + +endif # EXAMPLES_BUTTONS_NAMES +endif # EXAMPLES_BUTTONS diff --git a/examples/buttons/Make.defs b/examples/buttons/Make.defs new file mode 100644 index 000000000..6266506b0 --- /dev/null +++ b/examples/buttons/Make.defs @@ -0,0 +1,39 @@ +############################################################################ +# apps/examples/buttons/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_BUTTONS),y) +CONFIGURED_APPS += examples/buttons +endif diff --git a/examples/buttons/Makefile b/examples/buttons/Makefile new file mode 100644 index 000000000..69c12278f --- /dev/null +++ b/examples/buttons/Makefile @@ -0,0 +1,56 @@ +############################################################################ +# apps/examples/buttons/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 + +# LED driver test built-in application info + +CONFIG_EXAMPLES_BUTTONS_PRIORITY ?= SCHED_PRIORITY_DEFAULT +CONFIG_EXAMPLES_BUTTONS_STACKSIZE ?= 2048 + +APPNAME = buttons +PRIORITY = $(CONFIG_EXAMPLES_BUTTONS_PRIORITY) +STACKSIZE = $(CONFIG_EXAMPLES_BUTTONS_STACKSIZE) + +# LED driver test + +ASRCS = +CSRCS = +MAINSRC = buttons_main.c + +CONFIG_EXAMPLES_BUTTONS_PROGNAME ?= buttons$(EXEEXT) +PROGNAME = $(CONFIG_EXAMPLES_BUTTONS_PROGNAME) + +include $(APPDIR)/Application.mk diff --git a/examples/buttons/buttons_main.c b/examples/buttons/buttons_main.c new file mode 100644 index 000000000..d8538b833 --- /dev/null +++ b/examples/buttons/buttons_main.c @@ -0,0 +1,410 @@ +/**************************************************************************** + * examples/buttons/buttons_main.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef CONFIG_BUTTONS +# error "CONFIG_BUTTONS is not defined in the configuration" +#endif + +#if defined(CONFIG_DISABLE_SIGNALS) && defined(CONFIG_DISABLE_POLL) +# error "You need at least SIGNALS or POLL support to read buttons" +#endif + +#if !defined(CONFIG_DISABLE_SIGNALS) && !defined(CONFIG_DISABLE_POLL) +# define USE_NOTIFY_SIGNAL 1 +#else +# define USE_NOFITY_POLL 1 +#endif + +#ifndef CONFIG_BUTTONS_NPOLLWAITERS +# define CONFIG_BUTTONS_NPOLLWAITERS 2 +#endif + +#ifndef CONFIG_EXAMPLES_BUTTONS_SIGNO +# define CONFIG_EXAMPLES_BUTTONS_SIGNO 13 +#endif + +#ifndef CONFIG_BUTTONS_POLL_DELAY +# define CONFIG_BUTTONS_POLL_DELAY 1000 +#endif + +#ifndef CONFIG_EXAMPLES_BUTTONS_NAME0 +# define CONFIG_EXAMPLES_BUTTONS_NAME0 "BUTTON0" +#endif + +#ifndef CONFIG_EXAMPLES_BUTTONS_NAME1 +# define CONFIG_EXAMPLES_BUTTONS_NAME1 "BUTTON1" +#endif + +#ifndef CONFIG_EXAMPLES_BUTTONS_NAME2 +# define CONFIG_EXAMPLES_BUTTONS_NAME2 "BUTTON2" +#endif + +#ifndef CONFIG_EXAMPLES_BUTTONS_NAME3 +# define CONFIG_EXAMPLES_BUTTONS_NAME3 "BUTTON3" +#endif + +#ifndef CONFIG_EXAMPLES_BUTTONS_NAME4 +# define CONFIG_EXAMPLES_BUTTONS_NAME4 "BUTTON4" +#endif + +#ifndef CONFIG_EXAMPLES_BUTTONS_NAME5 +# define CONFIG_EXAMPLES_BUTTONS_NAME5 "BUTTON5" +#endif + +#ifndef CONFIG_EXAMPLES_BUTTONS_NAME6 +# define CONFIG_EXAMPLES_BUTTONS_NAME6 "BUTTON6" +#endif + +#ifndef CONFIG_EXAMPLES_BUTTONS_NAME7 +# define CONFIG_EXAMPLES_BUTTONS_NAME7 "BUTTON7" +#endif + +#define BUTTON_MAX 8 + +#ifndef CONFIG_EXAMPLES_BUTTONS_QTD +# define CONFIG_EXAMPLES_BUTTONS_QTD BUTTON_MAX +#endif + +#if CONFIG_EXAMPLES_BUTTONS_QTD > 8 +# error "CONFIG_EXAMPLES_BUTTONS_QTD > 8" +#endif + +#ifdef CONFIG_EXAMPLES_BUTTONS_NAMES +char button_name[CONFIG_EXAMPLES_BUTTONS_QTD][16] = +{ + CONFIG_EXAMPLES_BUTTONS_NAME0, + CONFIG_EXAMPLES_BUTTONS_NAME1, + CONFIG_EXAMPLES_BUTTONS_NAME2, + CONFIG_EXAMPLES_BUTTONS_NAME3, + CONFIG_EXAMPLES_BUTTONS_NAME4, + CONFIG_EXAMPLES_BUTTONS_NAME5, + CONFIG_EXAMPLES_BUTTONS_NAME6, + CONFIG_EXAMPLES_BUTTONS_NAME7, +}; +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static bool g_button_daemon_started; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: button_daemon + ****************************************************************************/ + +static int button_daemon(int argc, char *argv[]) +{ +#ifdef USE_NOTIFY_POLL + struct pollfd fds[CONFIG_BUTTONS_NPOLLWAITERS]; +#endif + +#ifdef USE_NOTIFY_SIGNAL + struct btn_notify_s btnevents; +#endif + + btn_buttonset_t supported; + btn_buttonset_t sample = 0; + +#ifdef CONFIG_EXAMPLES_BUTTONS_NAMES + btn_buttonset_t oldsample = 0; + int i; +#endif + + int ret; + int fd; + + /* Indicate that we are running */ + + g_button_daemon_started = true; + printf("button_daemon: Running\n"); + + /* Open the BUTTON driver */ + + printf("button_daemon: Opening %s\n", CONFIG_EXAMPLES_BUTTONS_DEVPATH); + fd = open(CONFIG_EXAMPLES_BUTTONS_DEVPATH, O_RDONLY|O_NONBLOCK); + if (fd < 0) + { + int errcode = errno; + printf("button_daemon: ERROR: Failed to open %s: %d\n", + CONFIG_EXAMPLES_BUTTONS_DEVPATH, errcode); + goto errout; + } + + /* Get the set of BUTTONs supported */ + + ret = ioctl(fd, BTNIOC_SUPPORTED, + (unsigned long)((uintptr_t)&supported)); + if (ret < 0) + { + int errcode = errno; + printf("button_daemon: ERROR: ioctl(BTNIOC_SUPPORTED) failed: %d\n", + errcode); + goto errout_with_fd; + } + + printf("button_daemon: Supported BUTTONs 0x%02x\n", (unsigned int)supported); + +#ifdef USE_NOTIFY_SIGNAL + /* Define the notifications events */ + + btnevents.bn_press = supported; + btnevents.bn_release = supported; + btnevents.bn_signo = CONFIG_EXAMPLES_BUTTONS_SIGNO; + + /* Register to receive a signal when buttons are pressed/released */ + + ret = ioctl(fd, BTNIOC_REGISTER, + (unsigned long)((uintptr_t)&btnevents)); + if (ret < 0) + { + int errcode = errno; + printf("button_daemon: ERROR: ioctl(BTNIOC_SUPPORTED) failed: %d\n", + errcode); + goto errout_with_fd; + } +#endif + + /* Now loop forever, waiting BUTTONs events */ + + for (; ; ) + { +#ifdef USE_NOTIFY_SIGNAL + struct siginfo value; + sigset_t set; +#endif + +#ifdef USE_NOTIFY_POLL + bool timeout; + bool pollin; + int nbytes; +#endif + +#ifdef USE_NOTIFY_SIGNAL + /* Wait for a signal */ + + (void)sigemptyset(&set); + (void)sigaddset(&set, CONFIG_EXAMPLES_BUTTONS_SIGNO); + ret = sigwaitinfo(&set, &value); + if (ret < 0) + { + int errcode = errno; + printf("button_daemon: ERROR: sigwaitinfo() failed: %d\n", errcode); + goto errout_with_fd; + } + + sample = (btn_buttonset_t)value.si_value.sival_int; +#endif + +#ifdef USE_NOTIFY_POLL + /* Prepare the File Descriptor for poll */ + + memset(fds, 0, sizeof(struct pollfd)*CONFIG_BUTTONS_NPOLLWAITERS); + + fds[0].fd = fd; + fds[0].events = POLLIN; + + timeout = false; + pollin = false; + + ret = poll(fds, CONFIG_BUTTONS_NPOLLWAITERS, CONFIG_BUTTONS_POLL_DELAY); + + printf("\nbutton_daemon: poll returned: %d\n", ret); + if (ret < 0) + { + int errcode = errno; + printf("button_daemon: ERROR poll failed: %d\n", errcode); + } + else if (ret == 0) + { + printf("button_daemon: Timeout\n"); + timeout = true; + } + else if (ret > CONFIG_BUTTONS_NPOLLWAITERS) + { + printf("button_daemon: ERROR poll reported: %d\n", errno); + } + else + { + pollin = true; + } + + /* In any event, read until the pipe is empty */ + + for (i = 0; i < CONFIG_BUTTONS_NPOLLWAITERS; i++) + { + do + { + nbytes = read(fds[i].fd, (void *)&sample, + sizeof(btn_buttonset_t)); + + if (nbytes <= 0) + { + if (nbytes == 0 || errno == EAGAIN) + { + if ((fds[i].revents & POLLIN) != 0) + { + printf("button_daemon: ERROR no read data[%d]\n", i); + } + } + else if (errno != EINTR) + { + printf("button_daemon: read[%d] failed: %d\n", i, errno); + } + + nbytes = 0; + } + else + { + if (timeout) + { + printf("button_daemon: ERROR? Poll timeout, but data read[%d]\n", i); + printf(" (might just be a race condition)\n"); + } + } + + /* Suppress error report if no read data on the next time through */ + + fds[i].revents = 0; + } + while (nbytes > 0); + } +#endif + +#ifdef CONFIG_EXAMPLES_BUTTONS_NAMES + /* Print name of all pressed/release button */ + + for (i = 0; i < CONFIG_EXAMPLES_BUTTONS_QTD; i++) + { + if ((sample & (1 << i)) && !(oldsample & (1 << i))) + { + printf("%s was pressed\n", button_name[i]); + } + + if (!(sample & (1 << i)) && (oldsample & (1 << i))) + { + printf("%s was released\n", button_name[i]); + } + } + + oldsample = sample; +#else + printf("Sample = %d\n", sample); +#endif + + /* Make sure that everything is displayed */ + + fflush(stdout); + + usleep(1000); + } + +errout_with_fd: + (void)close(fd); + +errout: + g_button_daemon_started = false; + + printf("button_daemon: Terminating\n"); + return EXIT_FAILURE; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * buttons_main + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int buttons_main(int argc, FAR char *argv[]) +#endif +{ + FAR char *buttonargv[2]; + int ret; + + printf("buttons_main: Starting the button_daemon\n"); + if (g_button_daemon_started) + { + printf("buttons_main: button_daemon already running\n"); + return EXIT_SUCCESS; + } + + buttonargv[0] = "button_daemon"; + buttonargv[1] = NULL; + + ret = task_create("button_daemon", CONFIG_EXAMPLES_BUTTONS_PRIORITY, + CONFIG_EXAMPLES_BUTTONS_STACKSIZE, button_daemon, + (FAR char * const *)buttonargv); + if (ret < 0) + { + int errcode = errno; + printf("buttons_main: ERROR: Failed to start button_daemon: %d\n", + errcode); + return EXIT_FAILURE; + } + + printf("buttons_main: button_daemon started\n"); + return EXIT_SUCCESS; +}