source: multiio/commands/main_multiio_din.c @ 0ecf946

Last change on this file since 0ecf946 was e38d987, checked in by Joel Sherrill <joel.sherrill@…>, on 03/18/11 at 13:19:33

2011-03-18 Joel Sherrill <joel.sherrill@…>

  • ChangeLog?, Makefile, commands/Makefile, commands/main_multiio_adc.c, commands/main_multiio_benchmark.c, commands/main_multiio_dac.c, commands/main_multiio_din.c, commands/main_multiio_dout.c, commands/main_multiio_irq.c, commands/multiio_commands.h, include/multiio.h, stub/multio_stub.c: New files.
  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 *  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 <stdio.h>
15#include <stdlib.h>
16#include <rtems.h>
17#include <rtems/shell.h>
18#include <rtems/stringto.h>
19#include "multiio.h"
20
21#define __need_getopt_newlib
22#include <getopt.h>
23
24static void dio_read(
25  int *dio,
26  int  max
27)
28{
29  int   bit;
30 
31  for ( bit=0 ; bit<max ; bit++ ) {
32    dio[bit] = rtems_din_get(bit);
33  }
34}
35
36static void dio_printf(
37  int *dio,
38  int  max
39)
40{
41  struct timespec ts;
42  int             bit;
43
44  /* TODO: Fix assumption that max is divisible by 4 and fits on a line */
45  (void) rtems_clock_get_uptime( &ts );
46  printf( "%ld:%ld ", ts.tv_sec, ts.tv_nsec );
47  for ( bit=0 ; bit<max ; bit+=4 ) {
48     printf(
49        "%d%d%d%d%s",
50        dio[bit +  0], dio[bit +  1],
51        dio[bit +  2], dio[bit +  3],
52        ((bit == 44) ? "\n" : " " )
53    );
54  }
55}
56
57static char din_usage[] =
58  "Usage: %s [-i iterations] [-p period] [-v]\n"
59  "Where: maximum iterations defaults to 1\n"
60  "       the period is in milliseconds and defaults to 1000\n";
61
62#define PRINT_USAGE() \
63   printf( din_usage, argv[0] )
64
65int main_multiio_din(int argc, char **argv)
66{
67  int                 milliseconds;
68  int                 maximum;
69  int                 iterations;
70  char                ch;
71  bool                changed;
72  bool                verbose;
73  struct getopt_data  getopt_reent;
74  int                 dio_last[48];
75  int                 dio_current[48];
76  const  char        *s;
77  int                 max_dins = rtems_din_get_maximum();
78
79  /*
80   * Parse arguments here
81   */
82  milliseconds = 1000;
83  maximum = 1;
84  verbose = false;
85
86  memset(&getopt_reent, 0, sizeof(getopt_data));
87  while ((ch = getopt_r(argc, argv, "i:p:v", &getopt_reent)) != -1) {
88    switch (ch) {
89      case 'i': /* maximum iterations */
90        s = getopt_reent.optarg;
91        if ( rtems_string_to_int( s, &maximum, NULL, 0 ) ) {
92          printf( "Maximum iterations (%s) is not a number\n", s );
93          PRINT_USAGE();
94          return -1;
95        }
96
97        break;
98      case 'p': /* sampling period */
99        s = getopt_reent.optarg;
100        if ( rtems_string_to_int( s, &milliseconds, NULL, 0 ) ) {
101          printf( "Sampling period (%s) is not a number\n", s );
102          PRINT_USAGE();
103          return -1;
104        }
105        if ( milliseconds == 0 ) {
106          printf( "Sampling period (%d) is 0\n", milliseconds );
107          PRINT_USAGE();
108          return -1;
109        }
110        break;
111      case 'v': /* verbose*/
112        verbose = true;
113        break;
114      default:
115        printf( din_usage, argv[0] );
116        return -1;
117    }
118  }
119
120  if ( maximum != 1 )
121    printf(
122      "Polling discrete inputs for %d iterations with %d msec period\n",
123      maximum,
124      milliseconds
125    );
126
127  /*
128   *  Now sample in the loop
129   */
130  changed = false;
131
132  iterations = 1;
133  while (1) {
134    dio_read( dio_current, max_dins );
135   
136    if ( iterations == 1 )
137      changed = true;
138    else if ( memcmp( dio_last, dio_current, sizeof(dio_current) ) )
139      changed = true;
140   
141    if ( verbose || changed ) {
142      dio_printf( dio_current, max_dins );
143      memcpy( dio_last, dio_current, sizeof(dio_current) );
144      changed = false;
145    }
146
147    if (iterations++ >= maximum )
148      break;
149
150    (void) rtems_task_wake_after( RTEMS_MILLISECONDS_TO_TICKS(milliseconds) );
151  }
152  return 0;
153}
154
155rtems_shell_cmd_t Shell_MULTIIO_DIN_Command = {
156  "multiio_din",                     /* name */
157  "Read Discrete Inputs",            /* usage */
158  "multiio",                         /* topic */
159  main_multiio_din,                  /* command */
160  NULL,                              /* alias */
161  NULL                               /* next */
162};
163
164rtems_shell_alias_t Shell_MULTIIO_DIN_Alias = {
165  "multiio_adc",         /* command */
166  "adc"                  /* alias */
167};
Note: See TracBrowser for help on using the repository browser.