2012-02-15 18:51:30 +01:00
|
|
|
/****************************************************************************
|
2021-03-19 21:08:04 +01:00
|
|
|
* apps/examples/qencoder/qe_main.c
|
2012-02-15 18:51:30 +01: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
|
2012-02-15 18:51:30 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-02-15 18:51:30 +01: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.
|
2012-02-15 18:51:30 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2012-07-30 18:51:43 +02:00
|
|
|
#include <string.h>
|
2012-02-15 18:51:30 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
2021-03-19 21:05:53 +01:00
|
|
|
#include <inttypes.h>
|
2012-02-15 18:51:30 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/sensors/qencoder.h>
|
|
|
|
|
|
|
|
#include "qe.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-08-10 10:21:49 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_QENCODER_HAVE_MAXPOS
|
|
|
|
# if CONFIG_EXAMPLES_QENCODER_MAXPOS == 0
|
|
|
|
# error CONFIG_EXAMPLES_QENCODER_MAXPOS not specified
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2012-02-15 18:51:30 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct qe_example_s g_qeexample;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-07-30 18:51:43 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: qe_devpath
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void qe_devpath(FAR const char *devpath)
|
|
|
|
{
|
|
|
|
/* Get rid of any old device path */
|
|
|
|
|
|
|
|
if (g_qeexample.devpath)
|
|
|
|
{
|
|
|
|
free(g_qeexample.devpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The set-up the new device path by copying the string */
|
|
|
|
|
|
|
|
g_qeexample.devpath = strdup(devpath);
|
|
|
|
}
|
|
|
|
|
2012-02-15 18:51:30 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: qe_help
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void qe_help(void)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("\nUsage: qe [OPTIONS]\n\n");
|
|
|
|
printf("OPTIONS include:\n");
|
|
|
|
printf(" [-p devpath] QE device path\n");
|
|
|
|
printf(" [-n samples] Number of samples\n");
|
|
|
|
printf(" [-t msec] Delay between samples (msec)\n");
|
|
|
|
printf(" [-r] Reset the position to zero\n");
|
|
|
|
printf(" [-h] Shows this message and exits\n\n");
|
2012-02-15 18:51:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: arg_string
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-03-13 13:47:59 +01:00
|
|
|
static int arg_string(FAR char **arg, FAR char **value)
|
2012-02-15 18:51:30 +01:00
|
|
|
{
|
|
|
|
FAR char *ptr = *arg;
|
|
|
|
|
|
|
|
if (ptr[2] == '\0')
|
|
|
|
{
|
|
|
|
*value = arg[1];
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*value = &ptr[2];
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: arg_decimal
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-03-13 13:47:59 +01:00
|
|
|
static int arg_decimal(FAR char **arg, FAR long *value)
|
2012-02-15 18:51:30 +01:00
|
|
|
{
|
|
|
|
FAR char *string;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = arg_string(arg, &string);
|
|
|
|
*value = strtol(string, NULL, 10);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: parse_args
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-03-13 13:47:59 +01:00
|
|
|
static void parse_args(int argc, FAR char **argv)
|
2012-02-15 18:51:30 +01:00
|
|
|
{
|
|
|
|
FAR char *ptr;
|
2012-07-30 18:51:43 +02:00
|
|
|
FAR char *str;
|
2012-02-15 18:51:30 +01:00
|
|
|
long value;
|
|
|
|
int index;
|
|
|
|
int nargs;
|
|
|
|
|
|
|
|
g_qeexample.reset = false;
|
2019-10-07 10:37:56 +02:00
|
|
|
g_qeexample.nloops = CONFIG_EXAMPLES_QENCODER_NSAMPLES;
|
2012-02-15 20:12:19 +01:00
|
|
|
g_qeexample.delay = CONFIG_EXAMPLES_QENCODER_DELAY;
|
2012-02-15 18:51:30 +01:00
|
|
|
|
|
|
|
for (index = 1; index < argc; )
|
|
|
|
{
|
|
|
|
ptr = argv[index];
|
|
|
|
if (ptr[0] != '-')
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Invalid options format: %s\n", ptr);
|
2012-02-15 18:51:30 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ptr[1])
|
|
|
|
{
|
|
|
|
case 'n':
|
|
|
|
nargs = arg_decimal(&argv[index], &value);
|
|
|
|
if (value < 0 || value > INT_MAX)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Sample count out of range: %ld\n", value);
|
2012-02-15 18:51:30 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-02-15 20:12:19 +01:00
|
|
|
g_qeexample.nloops = (unsigned int)value;
|
|
|
|
index += nargs;
|
|
|
|
break;
|
|
|
|
|
2012-07-30 18:51:43 +02:00
|
|
|
case 'p':
|
|
|
|
nargs = arg_string(&argv[index], &str);
|
|
|
|
qe_devpath(str);
|
|
|
|
index += nargs;
|
|
|
|
break;
|
|
|
|
|
2012-02-15 20:12:19 +01:00
|
|
|
case 't':
|
|
|
|
nargs = arg_decimal(&argv[index], &value);
|
|
|
|
if (value < 0 || value > INT_MAX)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Sample delay out of range: %ld\n", value);
|
2012-02-15 20:12:19 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_qeexample.delay = (unsigned int)value;
|
2012-02-15 18:51:30 +01:00
|
|
|
index += nargs;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
g_qeexample.reset = true;
|
|
|
|
index++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
qe_help();
|
|
|
|
exit(EXIT_SUCCESS);
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2012-02-15 18:51:30 +01:00
|
|
|
default:
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Unsupported option: %s\n", ptr);
|
2012-02-15 18:51:30 +01:00
|
|
|
qe_help();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-08-30 22:13:50 +02:00
|
|
|
* Name: qe_main
|
2012-02-15 18:51:30 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-06 17:23:23 +02:00
|
|
|
int main(int argc, FAR char *argv[])
|
2012-02-15 18:51:30 +01:00
|
|
|
{
|
|
|
|
int32_t position;
|
|
|
|
int fd;
|
|
|
|
int exitval = EXIT_SUCCESS;
|
|
|
|
int ret;
|
|
|
|
int nloops;
|
|
|
|
|
2016-11-22 12:54:12 +01:00
|
|
|
/* Set the default values */
|
2012-07-30 18:51:43 +02:00
|
|
|
|
2016-11-22 12:54:12 +01:00
|
|
|
qe_devpath(CONFIG_EXAMPLES_QENCODER_DEVPATH);
|
2012-07-30 18:51:43 +02:00
|
|
|
|
2012-02-15 18:51:30 +01:00
|
|
|
/* Parse command line arguments */
|
|
|
|
|
|
|
|
parse_args(argc, argv);
|
|
|
|
|
|
|
|
/* Open the encoder device for reading */
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("qe_main: Hardware initialized. Opening the encoder device: %s\n",
|
2016-01-19 19:33:22 +01:00
|
|
|
g_qeexample.devpath);
|
2012-07-30 18:51:43 +02:00
|
|
|
|
|
|
|
fd = open(g_qeexample.devpath, O_RDONLY);
|
2012-02-15 18:51:30 +01:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("qe_main: open %s failed: %d\n", g_qeexample.devpath, errno);
|
2012-02-15 18:51:30 +01:00
|
|
|
exitval = EXIT_FAILURE;
|
2021-06-15 22:23:58 +02:00
|
|
|
goto errout;
|
2012-02-15 18:51:30 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:21:49 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_QENCODER_HAVE_MAXPOS
|
|
|
|
/* Set the maximum encoder positions */
|
|
|
|
|
|
|
|
ret = ioctl(fd, QEIOC_SETPOSMAX,
|
|
|
|
(unsigned long)CONFIG_EXAMPLES_QENCODER_MAXPOS);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
printf("qe_main: ioctl(QEIOC_SETMAXPOS) failed: %d\n", errno);
|
|
|
|
exitval = EXIT_FAILURE;
|
|
|
|
goto errout_with_dev;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-02-15 18:51:30 +01:00
|
|
|
/* Reset the count if so requested */
|
|
|
|
|
|
|
|
if (g_qeexample.reset)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("qe_main: Resetting the count...\n");
|
2012-02-15 18:51:30 +01:00
|
|
|
ret = ioctl(fd, QEIOC_RESET, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("qe_main: ioctl(QEIOC_RESET) failed: %d\n", errno);
|
2012-02-15 18:51:30 +01:00
|
|
|
exitval = EXIT_FAILURE;
|
|
|
|
goto errout_with_dev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now loop the appropriate number of times, displaying the collected
|
|
|
|
* encoder samples.
|
|
|
|
*/
|
|
|
|
|
2014-11-25 18:45:00 +01:00
|
|
|
printf("qe_main: Number of samples: %u\n", g_qeexample.nloops);
|
2021-03-19 21:08:04 +01:00
|
|
|
for (nloops = 0;
|
|
|
|
!g_qeexample.nloops || nloops < g_qeexample.nloops;
|
|
|
|
nloops++)
|
2012-02-15 18:51:30 +01:00
|
|
|
{
|
|
|
|
/* Flush any output before the loop entered or from the previous pass
|
|
|
|
* through the loop.
|
|
|
|
*/
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
fflush(stdout);
|
2012-02-15 18:51:30 +01:00
|
|
|
|
|
|
|
/* Get the positions data using the ioctl */
|
|
|
|
|
|
|
|
ret = ioctl(fd, QEIOC_POSITION, (unsigned long)((uintptr_t)&position));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("qe_main: ioctl(QEIOC_POSITION) failed: %d\n", errno);
|
2012-02-15 18:51:30 +01:00
|
|
|
exitval = EXIT_FAILURE;
|
|
|
|
goto errout_with_dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print the sample data on successful return */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2021-03-19 21:05:53 +01:00
|
|
|
printf("qe_main: %3d. %" PRIi32 "\n", nloops + 1, position);
|
2012-02-15 18:51:30 +01:00
|
|
|
}
|
2012-02-15 20:12:19 +01:00
|
|
|
|
|
|
|
/* Delay a little bit */
|
|
|
|
|
|
|
|
usleep(g_qeexample.delay * 1000);
|
2012-02-15 18:51:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
errout_with_dev:
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
errout:
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("Terminating!\n");
|
|
|
|
fflush(stdout);
|
2012-02-15 18:51:30 +01:00
|
|
|
return exitval;
|
|
|
|
}
|