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