1 | /* |
---|
2 | * pcmmio_din 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 dio_read_bit(_x) (_x & 1) |
---|
21 | #endif |
---|
22 | |
---|
23 | void pcmmio_dio_read( |
---|
24 | int *dio |
---|
25 | ) |
---|
26 | { |
---|
27 | int bit; |
---|
28 | |
---|
29 | for ( bit=0 ; bit<48 ; bit++ ) { |
---|
30 | dio[bit] = dio_read_bit(bit); |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | void pcmmio_dio_printf( |
---|
35 | int *dio |
---|
36 | ) |
---|
37 | { |
---|
38 | struct timespec ts; |
---|
39 | int bit; |
---|
40 | |
---|
41 | (void) rtems_clock_get_uptime( &ts ); |
---|
42 | printf( "%ld:%ld ", ts.tv_sec, ts.tv_nsec ); |
---|
43 | for ( bit=0 ; bit<48 ; bit+=4 ) { |
---|
44 | printf( |
---|
45 | "%d%d%d%d%s", |
---|
46 | dio[bit + 0], dio[bit + 1], |
---|
47 | dio[bit + 2], dio[bit + 3], |
---|
48 | ((bit == 44) ? "\n" : " " ) |
---|
49 | ); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | char pcmmio_din_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 | int main_pcmmio_din(int argc, char **argv) |
---|
59 | { |
---|
60 | int milliseconds; |
---|
61 | int maximum; |
---|
62 | int iterations; |
---|
63 | char ch; |
---|
64 | bool changed; |
---|
65 | bool verbose; |
---|
66 | struct getopt_data getopt_reent; |
---|
67 | int dio_last[48]; |
---|
68 | int dio_current[48]; |
---|
69 | |
---|
70 | /* |
---|
71 | * Parse arguments here |
---|
72 | */ |
---|
73 | milliseconds = 1000; |
---|
74 | maximum = 1; |
---|
75 | verbose = false; |
---|
76 | |
---|
77 | memset(&getopt_reent, 0, sizeof(getopt_data)); |
---|
78 | while ((ch = getopt_r(argc, argv, "i:p:v", &getopt_reent)) != -1) { |
---|
79 | switch (ch) { |
---|
80 | case 'i': /* maximum iterations */ |
---|
81 | maximum = rtems_shell_str2int( getopt_reent.optarg ); |
---|
82 | break; |
---|
83 | case 'p': /* sampling period */ |
---|
84 | milliseconds = rtems_shell_str2int( getopt_reent.optarg ); |
---|
85 | break; |
---|
86 | case 'v': /* verbose*/ |
---|
87 | verbose = true; |
---|
88 | break; |
---|
89 | default: |
---|
90 | printf( pcmmio_din_usage, argv[0] ); |
---|
91 | return -1; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | if ( maximum != 1 ) |
---|
96 | printf( |
---|
97 | "Polling discrete inputs for %d iterations with %d msec period\n", |
---|
98 | maximum, |
---|
99 | milliseconds |
---|
100 | ); |
---|
101 | |
---|
102 | /* |
---|
103 | * Now sample in the loop |
---|
104 | */ |
---|
105 | changed = false; |
---|
106 | |
---|
107 | iterations = 1; |
---|
108 | while (1) { |
---|
109 | pcmmio_dio_read(dio_current); |
---|
110 | |
---|
111 | if ( iterations == 1 ) |
---|
112 | changed = true; |
---|
113 | else if ( memcmp( dio_last, dio_current, sizeof(dio_current) ) ) |
---|
114 | changed = true; |
---|
115 | |
---|
116 | if ( verbose || changed ) { |
---|
117 | pcmmio_dio_printf(dio_current); |
---|
118 | memcpy( dio_last, dio_current, sizeof(dio_current) ); |
---|
119 | changed = false; |
---|
120 | } |
---|
121 | |
---|
122 | if (iterations++ >= maximum ) |
---|
123 | break; |
---|
124 | |
---|
125 | (void) rtems_task_wake_after( RTEMS_MILLISECONDS_TO_TICKS(milliseconds) ); |
---|
126 | } |
---|
127 | return 0; |
---|
128 | } |
---|
129 | |
---|
130 | rtems_shell_cmd_t Shell_PCMMIO_DIN_Command = { |
---|
131 | "pcmmio_din", /* name */ |
---|
132 | "Read PCMMIO Discrete Inputs", /* usage */ |
---|
133 | "pcmmio", /* topic */ |
---|
134 | main_pcmmio_din, /* command */ |
---|
135 | NULL, /* alias */ |
---|
136 | NULL /* next */ |
---|
137 | }; |
---|
138 | |
---|
139 | rtems_shell_alias_t Shell_PCMMIO_DIN_Alias = { |
---|
140 | "pcmmio_din", /* command */ |
---|
141 | "din" /* alias */ |
---|
142 | }; |
---|
143 | |
---|