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