Skip to content

Commit 5c867b2

Browse files
raiden00placassis
authored andcommitted
examples/nxscope: control stream interval from CLI
add the ability to control the stream interval from the CLI which is useful for performance testing Signed-off-by: raiden00pl <raiden00@railab.me>
1 parent b8718d8 commit 5c867b2

1 file changed

Lines changed: 97 additions & 8 deletions

File tree

examples/nxscope/nxscope_main.c

Lines changed: 97 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@
2929
#include <sys/boardctl.h>
3030

3131
#include <assert.h>
32+
#include <errno.h>
33+
#include <getopt.h>
3234
#include <pthread.h>
3335
#include <stdio.h>
36+
#include <stdlib.h>
3437
#include <unistd.h>
3538
#include <math.h>
3639

3740
#ifdef CONFIG_EXAMPLES_NXSCOPE_TIMER
3841
# include <sys/ioctl.h>
3942
# include <fcntl.h>
40-
# include <stdlib.h>
4143
# include <signal.h>
4244
# include <nuttx/timers/timer.h>
4345
#endif
@@ -52,7 +54,8 @@
5254
# error "math library must be selected for this example"
5355
#endif
5456

55-
#define SIN_DT (0.01f)
57+
#define SIN_DT (0.01f)
58+
#define STREAM_THREAD_USLEP 100
5659

5760
/****************************************************************************
5861
* Private Types
@@ -61,6 +64,7 @@
6164
struct nxscope_thr_env_s
6265
{
6366
FAR struct nxscope_s *nxs;
67+
int interval;
6468
};
6569

6670
/****************************************************************************
@@ -93,12 +97,76 @@ static int nxscope_cb_start(FAR void *priv, bool start)
9397
return OK;
9498
}
9599

100+
/****************************************************************************
101+
* Name: nxscope_show_usage
102+
****************************************************************************/
103+
104+
static void nxscope_show_usage(FAR const char *progname)
105+
{
106+
printf("Usage: %s [-i <stream_interval_us>]\n", progname);
107+
printf(" [-m <main_interval_us>]\n");
108+
}
109+
110+
/****************************************************************************
111+
* Name: nxscope_parse_args
112+
****************************************************************************/
113+
114+
static int nxscope_parse_args(int argc, FAR char *argv[],
115+
FAR int *stream_interval,
116+
FAR int *main_interval)
117+
{
118+
unsigned long value = 0;
119+
int opt = 0;
120+
121+
DEBUGASSERT(argv);
122+
DEBUGASSERT(stream_interval);
123+
DEBUGASSERT(main_interval);
124+
125+
while ((opt = getopt(argc, argv, "i:m:")) != -1)
126+
{
127+
switch (opt)
128+
{
129+
case 'i':
130+
{
131+
value = strtoul(optarg, NULL, 10);
132+
if (value == 0)
133+
{
134+
printf("ERROR: invalid interval: %s\n", optarg);
135+
return -EINVAL;
136+
}
137+
138+
*stream_interval = (int)value;
139+
break;
140+
}
141+
142+
case 'm':
143+
{
144+
value = strtoul(optarg, NULL, 10);
145+
if (value == 0)
146+
{
147+
printf("ERROR: invalid interval: %s\n", optarg);
148+
return -EINVAL;
149+
}
150+
151+
*main_interval = (int)value;
152+
break;
153+
}
154+
155+
default:
156+
printf("ERROR: unsupported argument\n");
157+
return -EINVAL;
158+
}
159+
}
160+
161+
return OK;
162+
}
163+
96164
#ifdef CONFIG_EXAMPLES_NXSCOPE_TIMER
97165
/****************************************************************************
98166
* Name: nxscope_timer_init
99167
****************************************************************************/
100168

101-
static int nxscope_timer_init(void)
169+
static int nxscope_timer_init(int interval_us)
102170
{
103171
int fd = 0;
104172
int ret = 0;
@@ -116,8 +184,7 @@ static int nxscope_timer_init(void)
116184

117185
/* Set the timer interval */
118186

119-
ret = ioctl(fd, TCIOC_SETTIMEOUT,
120-
CONFIG_EXAMPLES_NXSCOPE_TIMER_INTERVAL);
187+
ret = ioctl(fd, TCIOC_SETTIMEOUT, interval_us);
121188
if (ret < 0)
122189
{
123190
printf("ERROR: Failed to set the timer interval: %d\n", errno);
@@ -197,7 +264,7 @@ static FAR void *nxscope_samples_thr(FAR void *arg)
197264
#ifdef CONFIG_EXAMPLES_NXSCOPE_TIMER
198265
/* Initialize timer for periodic signal. */
199266

200-
ret = nxscope_timer_init();
267+
ret = nxscope_timer_init(envp->interval);
201268
if (ret < 0)
202269
{
203270
printf("ERROR: nxscope_timer_init() failed: %d\n", errno);
@@ -307,7 +374,7 @@ static FAR void *nxscope_samples_thr(FAR void *arg)
307374
goto errout;
308375
}
309376
#else
310-
usleep(100);
377+
usleep(envp->interval);
311378
#endif
312379
}
313380

@@ -421,6 +488,7 @@ int main(int argc, FAR char *argv[])
421488
{
422489
struct nxscope_s nxs;
423490
int ret = OK;
491+
int interval;
424492
pthread_t thread;
425493
struct nxscope_thr_env_s env;
426494
struct nxscope_cfg_s nxs_cfg;
@@ -438,6 +506,27 @@ int main(int argc, FAR char *argv[])
438506
struct nxscope_dummy_cfg_s nxs_dummy_cfg;
439507
#endif
440508

509+
/* Default settings */
510+
511+
interval = CONFIG_EXAMPLES_NXSCOPE_MAIN_INTERVAL;
512+
#ifdef CONFIG_EXAMPLES_NXSCOPE_TIMER
513+
env.interval = CONFIG_EXAMPLES_NXSCOPE_TIMER_INTERVAL;
514+
#else
515+
env.interval = STREAM_THREAD_USLEP;
516+
#endif
517+
518+
/* Parse args */
519+
520+
ret = nxscope_parse_args(argc, argv, &env.interval, &interval);
521+
if (ret < 0)
522+
{
523+
nxscope_show_usage(argv[0]);
524+
return EXIT_FAILURE;
525+
}
526+
527+
printf("stream interval = %d\n", env.interval);
528+
printf("main interval = %d\n", interval);
529+
441530
#ifndef CONFIG_NSH_ARCHINIT
442531
/* Perform architecture-specific initialization (if configured) */
443532

@@ -736,7 +825,7 @@ int main(int argc, FAR char *argv[])
736825
printf("ERROR: nxscope_recv failed %d\n", ret);
737826
}
738827

739-
usleep(CONFIG_EXAMPLES_NXSCOPE_MAIN_INTERVAL);
828+
usleep(interval);
740829
}
741830

742831
errout:

0 commit comments

Comments
 (0)