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

Last change on this file since 6955473 was 6955473, checked in by Joel Sherrill <joel.sherrill@…>, on 07/23/09 at 14:39:20

2009-07-23 Joel Sherrill <joel.sherrill@…>

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