source: multiio/commands/main_multiio_adc.c

Last change on this file was e38d987, checked in by Joel Sherrill <joel.sherrill@…>, on 03/18/11 at 13:19:33

2011-03-18 Joel Sherrill <joel.sherrill@…>

  • ChangeLog?, Makefile, commands/Makefile, commands/main_multiio_adc.c, commands/main_multiio_benchmark.c, commands/main_multiio_dac.c, commands/main_multiio_din.c, commands/main_multiio_dout.c, commands/main_multiio_irq.c, commands/multiio_commands.h, include/multiio.h, stub/multio_stub.c: New files.
  • Property mode set to 100644
File size: 4.1 KB
Line 
1/*
2 *  adc command
3 *
4 *  COPYRIGHT (c) 1989-2009.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.rtems.com/license/LICENSE.
10 *
11 *  $Id$
12 */
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <rtems.h>
17#include <rtems/shell.h>
18#include <rtems/stringto.h>
19#include "multiio.h"
20
21#define __need_getopt_newlib
22#include <getopt.h>
23
24static void adc_read(
25  float *adc,
26  int    max
27)
28{
29  int   input;
30 
31  for ( input=0 ; input<max ; input++ ) {
32    adc[input] = rtems_adc_get_channel_voltage(input);
33  }
34}
35
36static void adc_printf(
37  float *adc,
38  int    max
39)
40{
41  struct timespec ts;
42
43  (void) rtems_clock_get_uptime( &ts );
44  printf(
45    "%03ld:%06ld %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f\n"
46    "%11s%7.4f %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f\n",
47    ts.tv_sec, ts.tv_nsec/1000,
48    adc[0], adc[1], adc[2], adc[3], adc[4], adc[5], adc[6], adc[7],
49    "", adc[8], adc[9], adc[10], adc[11], adc[12], adc[13], adc[14], adc[15]
50  );
51}
52
53char static adc_usage[] =
54  "Usage: %s [-i iterations] [-p period] [-v]\n"
55  "Where: maximum iterations defaults to 1\n"
56  "       the period is in milliseconds and defaults to 1000\n";
57
58#define PRINT_USAGE() \
59   printf( adc_usage, argv[0] )
60
61int main_multiio_adc(int argc, char **argv)
62{
63  int                 milliseconds;
64  int                 maximum;
65  int                 iterations;
66  char                ch;
67  bool                changed;
68  bool                verbose;
69  struct getopt_data  getopt_reent;
70  float               *adc_last = NULL;
71  float               *adc_current = NULL;
72  int                  adcs;
73  const  char         *s;
74
75  /*
76   * Parse arguments here
77   */
78  milliseconds = 1000;
79  maximum = 1;
80  verbose = false;
81
82  memset(&getopt_reent, 0, sizeof(getopt_data));
83  while ((ch = getopt_r(argc, argv, "i:p:v", &getopt_reent)) != -1) {
84    switch (ch) {
85      case 'i': /* maximum iterations */
86        s = getopt_reent.optarg;
87        if ( rtems_string_to_int( s, &maximum, NULL, 0 ) ) {
88          printf( "Maximum iterations (%s) is not a number\n", s );
89          PRINT_USAGE();
90          return -1;
91        }
92
93        break;
94      case 'p': /* sampling period */
95        s = getopt_reent.optarg;
96        if ( rtems_string_to_int( s, &milliseconds, NULL, 0 ) ) {
97          printf( "Sampling period (%s) is not a number\n", s );
98          PRINT_USAGE();
99          return -1;
100        }
101        if ( milliseconds == 0 ) {
102          printf( "Sampling period (%d) is 0\n", milliseconds );
103          PRINT_USAGE();
104          return -1;
105        }
106
107        break;
108      case 'v': /* verbose*/
109        verbose = true;
110        break;
111      default:
112        PRINT_USAGE();
113        return -1;
114    }
115  }
116
117  if ( maximum != 1 ) {
118    printf(
119      "Polling analog inputs for %d iterations with %d msec period\n",
120      maximum,
121      milliseconds
122    );
123  }
124 
125  adcs        = rtems_adc_get_maximum();
126  adc_last    = calloc( 1, adcs * sizeof(float) );
127  adc_current = calloc( 1, adcs * sizeof(float) );
128
129  /*
130   *  Now sample in the loop
131   */
132  changed = false;
133
134  iterations = 1;
135  while (1) {
136    adc_read(adc_current, adcs);
137   
138    if ( iterations == 1 )
139      changed = true;
140    else if ( memcmp( adc_last, adc_current, sizeof(adc_current) ) )
141      changed = true;
142   
143    if ( verbose || changed ) {
144      adc_printf(adc_current, adcs);
145      memcpy( adc_last, adc_current, sizeof(adc_current) );
146      changed = false;
147    }
148
149    if (iterations++ >= maximum )
150      break;
151
152    (void) rtems_task_wake_after( RTEMS_MILLISECONDS_TO_TICKS(milliseconds) );
153  }
154  return 0;
155}
156
157rtems_shell_cmd_t Shell_MULTIIO_ADC_Command = {
158  "multiio_adc",                            /* name */
159  "Read Analog Inputs",                     /* usage */
160  "multiio",                                /* topic */
161  main_multiio_adc,                         /* command */
162  NULL,                                     /* alias */
163  NULL                                      /* next */
164};
165
166rtems_shell_alias_t Shell_MULTIIO_ADC_Alias = {
167  "multiio_adc",         /* command */
168  "adc"                  /* alias */
169};
170
Note: See TracBrowser for help on using the repository browser.