source: multiio/pcmmio/original/main_pcmmio_adc.c @ c531584

Last change on this file since c531584 was c531584, checked in by Joel Sherrill <joel.sherrill@…>, on 02/17/11 at 18:59:01

2011-02-17 Joel Sherrill <joel.sherrill@…>

  • .cvsignore, main_pcmmio_adc.c, main_pcmmio_benchmark.c, main_pcmmio_irq.c, pcmmio_commands.h, pcmmio_shell.c: Clean up.
  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 *  pcmmio_adc command
3 *
4 *  COPYRIGHT (c) 1989-2011.
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 "pcmmio_commands.h"
15#include "mio_io.h"
16#include <rtems/stringto.h>
17
18#define __need_getopt_newlib
19#include <getopt.h>
20
21#if defined(TESTING)
22  #define adc_get_channel_voltage(_x) (_x + 1.5)
23#endif
24
25void pcmmio_adc_read(
26  float *adc
27)
28{
29  int   input;
30 
31  for ( input=0 ; input<16 ; input++ ) {
32    adc[input] = adc_get_channel_voltage(input);
33  }
34}
35
36void pcmmio_adc_printf(
37  float *adc,
38  int    channel
39)
40{
41  struct timespec ts;
42
43  (void) rtems_clock_get_uptime( &ts );
44  printf( "%03ld:%06ld ", ts.tv_sec, ts.tv_nsec/1000 );
45
46  if ( channel == -1 ) {
47    printf(
48      "%7.4f %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f\n"
49      "%11s%7.4f %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f %7.4f\n",
50      adc[0], adc[1], adc[2], adc[3], adc[4], adc[5], adc[6], adc[7],
51      "", adc[8], adc[9], adc[10], adc[11], adc[12], adc[13], adc[14], adc[15]
52    );
53  } else {
54    printf( "%7.4f\n", adc[channel] );
55  }
56}
57
58char pcmmio_adc_usage[] =
59  "Usage: %s [-i iterations] [-p period] [-v] [channel]\n"
60  "Where: maximum iterations defaults to 1\n"
61  "       the period is in milliseconds and defaults to 1000\n";
62
63#define PRINT_USAGE() \
64   printf( pcmmio_adc_usage, argv[0] )
65
66int main_pcmmio_adc(int argc, char **argv)
67{
68  int                 milliseconds;
69  int                 maximum;
70  int                 iterations;
71  int                 channel;
72  int                 i;
73  char                ch;
74  bool                changed;
75  bool                verbose;
76  struct getopt_data  getopt_reent;
77  float               adc_last[16];
78  float               adc_current[16];
79  const  char        *s;
80
81  /*
82   * Parse arguments here
83   */
84  milliseconds = 1000;
85  maximum      = 1;
86  channel      = -1;
87  verbose      = false;
88
89  memset(&getopt_reent, 0, sizeof(getopt_data));
90  while ((ch = getopt_r(argc, argv, "i:p:v", &getopt_reent)) != -1) {
91    switch (ch) {
92      case 'i': /* maximum iterations */
93        s = getopt_reent.optarg;
94        if ( rtems_string_to_int( s, &maximum, NULL, 0 ) ) {
95          printf( "Maximum iterations (%s) is not a number\n", s );
96          PRINT_USAGE();
97          return -1;
98        }
99
100        break;
101      case 'p': /* sampling period */
102        s = getopt_reent.optarg;
103        if ( rtems_string_to_int( s, &milliseconds, NULL, 0 ) ) {
104          printf( "Sampling period (%s) is not a number\n", s );
105          PRINT_USAGE();
106          return -1;
107        }
108        if ( milliseconds == 0 ) {
109          printf( "Sampling period (%d) is 0\n", milliseconds );
110          PRINT_USAGE();
111          return -1;
112        }
113
114        break;
115      case 'v': /* verbose*/
116        verbose = true;
117        break;
118      default:
119        PRINT_USAGE();
120        return -1;
121    }
122  }
123
124  if ( maximum != 1 ) {
125    printf(
126      "Polling analog inputs for %d iterations with %d msec period\n",
127      maximum,
128      milliseconds
129    );
130  }
131
132  /*
133   *  Is there a specific ADC we are interested in?
134   */
135  i = getopt_reent.optind;
136  if ( i < argc ) {
137    if ( rtems_string_to_int( argv[i], &channel, NULL, 0 ) ) {
138      printf( "ADC (%s) is not a number\n", argv[i] );
139      return -1;
140    }
141    if ( channel < 0 || channel > 15 ) {
142      puts( "ADC number must be 0-15" );
143      return -1;
144    }
145  }
146
147  /*
148   *  Now sample in the loop
149   */
150  changed = false;
151
152  iterations = 1;
153  while (1) {
154    pcmmio_adc_read(adc_current);
155   
156    if ( iterations == 1 ) {
157      changed = true;
158    } else if ( channel == -1 ) {
159      if ( memcmp( adc_last, adc_current, sizeof(adc_current) ) )
160        changed = true;
161    } else if ( adc_last[channel] != adc_current[channel] ) {
162       changed = true;
163    }
164
165    if ( verbose || changed ) {
166      pcmmio_adc_printf(adc_current, channel);
167      memcpy( adc_last, adc_current, sizeof(adc_current) );
168      changed = false;
169    }
170
171    if (iterations++ >= maximum )
172      break;
173
174    (void) rtems_task_wake_after( RTEMS_MILLISECONDS_TO_TICKS(milliseconds) );
175  }
176  return 0;
177}
178
179rtems_shell_cmd_t Shell_PCMMIO_ADC_Command = {
180  "pcmmio_adc",                                    /* name */
181  "Read PCMMIO Analog Inputs",                     /* usage */
182  "pcmmio",                                        /* topic */
183  main_pcmmio_adc,                                 /* command */
184  NULL,                                            /* alias */
185  NULL                                             /* next */
186};
187
188rtems_shell_alias_t Shell_PCMMIO_ADC_Alias = {
189  "pcmmio_adc",          /* command */
190  "adc"                  /* alias */
191};
192
Note: See TracBrowser for help on using the repository browser.