2016-10-11 22:25:55 +02:00
|
|
|
/****************************************************************************
|
2021-04-08 11:50:31 +02:00
|
|
|
* apps/examples/buttons/buttons_main.c
|
2016-10-11 22:25:55 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-10-11 22:25:55 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-10-11 22:25:55 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-10-11 22:25:55 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
2023-02-04 14:56:06 +01:00
|
|
|
#include <unistd.h>
|
2016-10-11 22:25:55 +02:00
|
|
|
|
|
|
|
#include <nuttx/input/buttons.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-08 11:50:31 +02:00
|
|
|
#ifndef CONFIG_INPUT_BUTTONS
|
|
|
|
# error "CONFIG_INPUT_BUTTONS is not defined in the configuration"
|
2016-10-11 22:25:55 +02:00
|
|
|
#endif
|
|
|
|
|
2021-04-08 11:50:31 +02:00
|
|
|
#ifndef CONFIG_INPUT_BUTTONS_NPOLLWAITERS
|
|
|
|
# define CONFIG_INPUT_BUTTONS_NPOLLWAITERS 2
|
2016-10-11 22:25:55 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLES_BUTTONS_SIGNO
|
2023-03-25 00:07:59 +01:00
|
|
|
# define CONFIG_EXAMPLES_BUTTONS_SIGNO 32
|
2016-10-11 22:25:55 +02:00
|
|
|
#endif
|
|
|
|
|
2021-04-08 11:50:31 +02:00
|
|
|
#ifndef CONFIG_INPUT_BUTTONS_POLL_DELAY
|
|
|
|
# define CONFIG_INPUT_BUTTONS_POLL_DELAY 1000
|
2016-10-11 22:25:55 +02:00
|
|
|
#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
|
|
|
|
|
2017-02-16 18:00:43 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-10-11 22:25:55 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_BUTTONS_NAMES
|
2017-02-16 18:00:43 +01:00
|
|
|
static const char button_name[CONFIG_EXAMPLES_BUTTONS_QTD][16] =
|
2016-10-11 22:25:55 +02:00
|
|
|
{
|
2017-02-16 18:00:43 +01:00
|
|
|
CONFIG_EXAMPLES_BUTTONS_NAME0
|
|
|
|
#if CONFIG_EXAMPLES_BUTTONS_QTD > 1
|
|
|
|
, CONFIG_EXAMPLES_BUTTONS_NAME1
|
|
|
|
#endif
|
|
|
|
#if CONFIG_EXAMPLES_BUTTONS_QTD > 2
|
|
|
|
, CONFIG_EXAMPLES_BUTTONS_NAME2
|
|
|
|
#endif
|
|
|
|
#if CONFIG_EXAMPLES_BUTTONS_QTD > 3
|
|
|
|
, CONFIG_EXAMPLES_BUTTONS_NAME3
|
|
|
|
#endif
|
|
|
|
#if CONFIG_EXAMPLES_BUTTONS_QTD > 4
|
|
|
|
, CONFIG_EXAMPLES_BUTTONS_NAME4
|
|
|
|
#endif
|
|
|
|
#if CONFIG_EXAMPLES_BUTTONS_QTD > 5
|
|
|
|
, CONFIG_EXAMPLES_BUTTONS_NAME5
|
|
|
|
#endif
|
|
|
|
#if CONFIG_EXAMPLES_BUTTONS_QTD > 6
|
|
|
|
, CONFIG_EXAMPLES_BUTTONS_NAME6
|
|
|
|
#endif
|
|
|
|
#if CONFIG_EXAMPLES_BUTTONS_QTD > 7
|
|
|
|
, CONFIG_EXAMPLES_BUTTONS_NAME7
|
|
|
|
#endif
|
2016-10-11 22:25:55 +02:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static bool g_button_daemon_started;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: button_daemon
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int button_daemon(int argc, char *argv[])
|
|
|
|
{
|
2019-04-29 22:53:38 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_BUTTONS_POLL
|
2022-02-15 12:20:02 +01:00
|
|
|
struct pollfd fds[1];
|
2016-10-11 22:25:55 +02:00
|
|
|
#endif
|
|
|
|
|
2019-04-29 22:53:38 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_BUTTONS_SIGNAL
|
2016-10-11 22:25:55 +02:00
|
|
|
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;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
int fd;
|
2018-08-14 14:45:16 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
UNUSED(i);
|
2016-10-11 22:25:55 +02:00
|
|
|
|
|
|
|
/* 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);
|
2020-11-11 01:36:21 +01:00
|
|
|
fd = open(CONFIG_EXAMPLES_BUTTONS_DEVPATH, O_RDONLY | O_NONBLOCK);
|
2016-10-11 22:25:55 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-11-11 01:36:21 +01:00
|
|
|
printf("button_daemon: Supported BUTTONs 0x%02x\n",
|
|
|
|
(unsigned int)supported);
|
2016-10-11 22:25:55 +02:00
|
|
|
|
2019-04-29 22:53:38 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_BUTTONS_SIGNAL
|
2016-10-11 22:25:55 +02:00
|
|
|
/* Define the notifications events */
|
|
|
|
|
|
|
|
btnevents.bn_press = supported;
|
|
|
|
btnevents.bn_release = supported;
|
2019-01-27 16:48:29 +01:00
|
|
|
|
|
|
|
btnevents.bn_event.sigev_notify = SIGEV_SIGNAL;
|
|
|
|
btnevents.bn_event.sigev_signo = CONFIG_EXAMPLES_BUTTONS_SIGNO;
|
2016-10-11 22:25:55 +02:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
2020-11-18 05:28:11 +01:00
|
|
|
|
|
|
|
/* Ignore the default signal action */
|
|
|
|
|
|
|
|
signal(CONFIG_EXAMPLES_BUTTONS_SIGNO, SIG_IGN);
|
2016-10-11 22:25:55 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Now loop forever, waiting BUTTONs events */
|
|
|
|
|
|
|
|
for (; ; )
|
|
|
|
{
|
2019-04-29 22:53:38 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_BUTTONS_SIGNAL
|
2016-10-11 22:25:55 +02:00
|
|
|
struct siginfo value;
|
|
|
|
sigset_t set;
|
|
|
|
#endif
|
|
|
|
|
2019-04-29 22:53:38 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_BUTTONS_POLL
|
2016-10-11 22:25:55 +02:00
|
|
|
bool timeout;
|
|
|
|
int nbytes;
|
|
|
|
#endif
|
|
|
|
|
2019-04-29 22:53:38 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_BUTTONS_SIGNAL
|
2016-10-11 22:25:55 +02:00
|
|
|
/* Wait for a signal */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, CONFIG_EXAMPLES_BUTTONS_SIGNO);
|
2016-10-11 22:25:55 +02:00
|
|
|
ret = sigwaitinfo(&set, &value);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
2020-11-11 01:36:21 +01:00
|
|
|
printf("button_daemon: ERROR: sigwaitinfo() failed: %d\n",
|
|
|
|
errcode);
|
2016-10-11 22:25:55 +02:00
|
|
|
goto errout_with_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
sample = (btn_buttonset_t)value.si_value.sival_int;
|
|
|
|
#endif
|
|
|
|
|
2019-04-29 22:53:38 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_BUTTONS_POLL
|
2016-10-11 22:25:55 +02:00
|
|
|
/* Prepare the File Descriptor for poll */
|
|
|
|
|
2022-02-15 12:20:02 +01:00
|
|
|
memset(fds, 0, sizeof(fds));
|
2016-10-11 22:25:55 +02:00
|
|
|
|
|
|
|
fds[0].fd = fd;
|
|
|
|
fds[0].events = POLLIN;
|
|
|
|
|
|
|
|
timeout = false;
|
|
|
|
|
2022-02-15 12:20:02 +01:00
|
|
|
ret = poll(fds, 1, CONFIG_INPUT_BUTTONS_POLL_DELAY);
|
2016-10-11 22:25:55 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2021-04-08 11:50:31 +02:00
|
|
|
else if (ret > CONFIG_INPUT_BUTTONS_NPOLLWAITERS)
|
2016-10-11 22:25:55 +02:00
|
|
|
{
|
|
|
|
printf("button_daemon: ERROR poll reported: %d\n", errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In any event, read until the pipe is empty */
|
|
|
|
|
2022-02-15 12:20:02 +01:00
|
|
|
do
|
2016-10-11 22:25:55 +02:00
|
|
|
{
|
2022-02-15 12:20:02 +01:00
|
|
|
nbytes = read(fds[0].fd, (void *)&sample, sizeof(btn_buttonset_t));
|
2016-10-11 22:25:55 +02:00
|
|
|
|
2022-02-15 12:20:02 +01:00
|
|
|
if (nbytes <= 0)
|
|
|
|
{
|
|
|
|
if (nbytes == 0 || errno == EAGAIN)
|
2016-10-11 22:25:55 +02:00
|
|
|
{
|
2022-02-15 12:20:02 +01:00
|
|
|
if ((fds[0].revents & POLLIN) != 0)
|
2016-10-11 22:25:55 +02:00
|
|
|
{
|
2022-02-15 12:20:02 +01:00
|
|
|
printf("button_daemon: ERROR no read data\n");
|
2016-10-11 22:25:55 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-15 12:20:02 +01:00
|
|
|
else if (errno != EINTR)
|
2016-10-11 22:25:55 +02:00
|
|
|
{
|
2022-02-15 12:20:02 +01:00
|
|
|
printf("button_daemon: read failed: %d\n", errno);
|
2016-10-11 22:25:55 +02:00
|
|
|
}
|
|
|
|
|
2022-02-15 12:20:02 +01:00
|
|
|
nbytes = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (timeout)
|
|
|
|
{
|
|
|
|
printf("button_daemon: ERROR? Poll timeout, "
|
|
|
|
"but data read\n");
|
|
|
|
printf(" (might just be a race "
|
|
|
|
"condition)\n");
|
|
|
|
}
|
2016-10-11 22:25:55 +02:00
|
|
|
}
|
2022-02-15 12:20:02 +01:00
|
|
|
|
|
|
|
/* Suppress error report if no read data on the next time
|
|
|
|
* through
|
|
|
|
*/
|
|
|
|
|
|
|
|
fds[0].revents = 0;
|
2016-10-11 22:25:55 +02:00
|
|
|
}
|
2022-02-15 12:20:02 +01:00
|
|
|
while (nbytes > 0);
|
2016-10-11 22:25:55 +02:00
|
|
|
#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
|
2020-11-11 01:38:06 +01:00
|
|
|
printf("Sample = %jd\n", (intmax_t)sample);
|
2016-10-11 22:25:55 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Make sure that everything is displayed */
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
usleep(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_fd:
|
2020-01-02 13:09:50 +01:00
|
|
|
close(fd);
|
2016-10-11 22:25:55 +02:00
|
|
|
|
|
|
|
errout:
|
|
|
|
g_button_daemon_started = false;
|
|
|
|
|
|
|
|
printf("button_daemon: Terminating\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* buttons_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = task_create("button_daemon", CONFIG_EXAMPLES_BUTTONS_PRIORITY,
|
|
|
|
CONFIG_EXAMPLES_BUTTONS_STACKSIZE, button_daemon,
|
2017-03-04 15:48:27 +01:00
|
|
|
NULL);
|
2016-10-11 22:25:55 +02:00
|
|
|
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;
|
|
|
|
}
|