2015-10-13 15:46:42 +02:00
|
|
|
/****************************************************************************
|
2022-03-08 23:05:12 +01:00
|
|
|
* apps/examples/zerocross/zerocross_main.c
|
2015-10-13 15:46:42 +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
|
2015-10-13 15:46:42 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-10-13 15:46:42 +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.
|
2015-10-13 15:46:42 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
2023-02-01 14:44:33 +01:00
|
|
|
#include <sys/param.h>
|
2015-10-13 15:46:42 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2023-02-04 14:56:06 +01:00
|
|
|
#include <unistd.h>
|
2015-10-13 15:46:42 +02:00
|
|
|
|
|
|
|
#include <nuttx/sensors/zerocross.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2020-11-18 06:26:11 +01:00
|
|
|
|
2015-10-13 15:46:42 +02:00
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
2017-08-24 21:37:36 +02:00
|
|
|
#ifndef CONFIG_SENSORS_ZEROCROSS
|
|
|
|
# error "CONFIG_SENSORS_ZEROCROSS is not defined in the configuration"
|
2015-10-13 15:46:42 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLES_ZEROCROSS_DEVNAME
|
|
|
|
# define CONFIG_EXAMPLES_ZEROCROSS_DEVNAME "/dev/zc0"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_EXAMPLES_ZEROCROSS_SIGNO
|
2023-03-25 00:07:59 +01:00
|
|
|
# define CONFIG_EXAMPLES_ZEROCROSS_SIGNO 32
|
2015-10-13 15:46:42 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* zerocross_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
2019-01-27 16:48:29 +01:00
|
|
|
struct sigevent event;
|
2015-10-13 15:46:42 +02:00
|
|
|
int fd;
|
|
|
|
int tmp;
|
|
|
|
int ret;
|
2016-06-11 22:49:55 +02:00
|
|
|
int errcode = EXIT_FAILURE;
|
2015-10-13 15:46:42 +02:00
|
|
|
|
|
|
|
/* Open the zerocross device */
|
|
|
|
|
|
|
|
fd = open(CONFIG_EXAMPLES_ZEROCROSS_DEVNAME, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Failed to open %s: %d\n",
|
|
|
|
CONFIG_EXAMPLES_ZEROCROSS_DEVNAME, errno);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register to receive a signal on every zero cross event */
|
|
|
|
|
2019-01-27 16:48:29 +01:00
|
|
|
event.sigev_notify = SIGEV_SIGNAL;
|
|
|
|
event.sigev_signo = CONFIG_EXAMPLES_ZEROCROSS_SIGNO;
|
2015-10-13 15:46:42 +02:00
|
|
|
|
2019-01-27 16:48:29 +01:00
|
|
|
ret = ioctl(fd, ZCIOC_REGISTER, (unsigned long)((uintptr_t)&event));
|
2015-10-13 15:46:42 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: ioctl(ZCIOC_REGISTER) failed: %d\n", errno);
|
|
|
|
goto errout_with_fd;
|
|
|
|
}
|
|
|
|
|
2020-11-18 05:28:11 +01:00
|
|
|
/* Ignore the default signal action */
|
|
|
|
|
|
|
|
signal(CONFIG_EXAMPLES_ZEROCROSS_SIGNO, SIG_IGN);
|
|
|
|
|
2015-10-13 15:46:42 +02:00
|
|
|
/* Then loop, receiving signals indicating zero cross events. */
|
|
|
|
|
|
|
|
for (; ; )
|
|
|
|
{
|
|
|
|
struct siginfo value;
|
|
|
|
sigset_t set;
|
|
|
|
ssize_t nread;
|
|
|
|
|
|
|
|
/* Wait for a signal */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, CONFIG_EXAMPLES_ZEROCROSS_SIGNO);
|
2015-10-13 15:46:42 +02:00
|
|
|
ret = sigwaitinfo(&set, &value);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: sigwaitinfo() failed: %d\n", errno);
|
|
|
|
goto errout_with_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Show the value accompanying the signal */
|
|
|
|
|
|
|
|
printf("Signal received!\n");
|
|
|
|
printf("Sample = %d\n", value.si_value.sival_int);
|
|
|
|
}
|
|
|
|
|
2016-06-11 22:49:55 +02:00
|
|
|
errcode = EXIT_SUCCESS;
|
2015-10-13 15:46:42 +02:00
|
|
|
|
|
|
|
errout_with_fd:
|
|
|
|
close(fd);
|
2016-06-11 22:49:55 +02:00
|
|
|
return errcode;
|
2015-10-13 15:46:42 +02:00
|
|
|
}
|