Points&Forces (core)
Software tools facilitating the task of surveying architecture
parallel_read.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2008-2009 Pierre SMARS (smars@yuntech.edu.tw)
3  This program is free software: you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation, either version 2 of the License, or
6  (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 #include <dev/ppbus/ppi.h>
17 #include <dev/ppbus/ppbconf.h>
18 #include <iostream>
19 #include <fcntl.h>
20 #include "SimpleOpt.h"
21 
23 
24 CSimpleOpt::SOption g_rgOptions[] = {
25  { OPT_HELP,_T("-?"), SO_NONE },
26  { OPT_HELP,_T("-h"), SO_NONE },
27  { OPT_HELP,_T("--help"), SO_NONE },
28  { OPT_DATA,_T("-d"), SO_NONE },
29  { OPT_DATA,_T("--data"), SO_NONE },
30  { OPT_STATUS,_T("-s"), SO_NONE },
31  { OPT_STATUS,_T("--status"), SO_NONE },
33 };
34 
35 //---------------------------------------------------------------------------
36 int error(int val)
37 {
38  std::cerr << "parallel_read:" << std::endl;
39  std::cerr << " read data and/or status lines from the parallel port and write them to the standard output" << std::endl;
40  std::cerr << "syntax: parallel_read [-?|-h|--help] [-s|--status|-d|--data] > std_output" << std::endl;
41  std::cerr << " -d: return data line [default]" << std::endl;
42  std::cerr << " -s: return status line" << std::endl;
43  std::cerr << " example: a returned value of '1101'" << std::endl;
44  std::cerr << " means that data1, data3 and data4 are set to '1'" << std::endl;
45  std::cerr << "author: P.Smars, 2008-9" << std::endl;
46  std::cerr << "version: 2009-05-11" << std::endl;
47  return val;
48 }
49 //---------------------------------------------------------------------------
50 void binary(int h)
51 {
52  if (h==1)
53  std::cout << h;
54  else
55  {
56  binary(h/2);
57  std::cout << h%2;
58  }
59 }
60 //---------------------------------------------------------------------------
61 int main( int argc, char *argv[] )
62 {
63  int type = 0;
64  CSimpleOpt args(argc, argv, g_rgOptions);
65  while (args.Next())
66  {
67  if (args.LastError() == SO_SUCCESS)
68  {
69  if (args.OptionId() == OPT_HELP)
70  return error(0);
71  else if (args.OptionId() == OPT_STATUS)
72  type = 1;
73  else if (args.OptionId() == OPT_DATA)
74  type = 0;
75  else
76  return error(-1);
77  } else {
78  std::cerr << "Invalid argument: " << args.OptionText() << std::endl;
79  return error(args.LastError());
80  }
81  }
82  if (args.FileCount() != 0) return error(-2);
83  int fd = open ("/dev/ppi0", O_RDWR);
84  if (type==0)
85  {
86  u_int8_t data;
87  ioctl( fd, PPIGDATA, &data );
88  if (data==0)
89  std::cout << "0";
90  else
91  binary(data);
92  }
93  else if (type==1)
94  {
95  u_int8_t status;
96  ioctl( fd, PPIGSTATUS, &status );
97  if (status & SELECT) std::cout << 1; else std::cout << 0;
98  if (status & PERROR) std::cout << 1; else std::cout << 0;
99  if (status & nBUSY) std::cout << 0; else std::cout << 1;
100  if (status & nACK) std::cout << 1; else std::cout << 0;
101  if (status & nFAULT) std::cout << 1; else std::cout << 0;
102  }
103  std::cout << std::endl;
104 }
105 
A cross-platform command line library which can parse almost any of the standard command line formats...
#define SO_END_OF_OPTIONS
this option definition must be the last entry in the table
Definition: SimpleOpt.h:307
#define CSimpleOpt
TCHAR version dependent on if _UNICODE is defined.
Definition: SimpleOpt.h:1048
#define _T(s)
Definition: SimpleOpt.h:197
@ SO_SUCCESS
No error.
Definition: SimpleOpt.h:204
@ SO_NONE
Definition: SimpleOpt.h:286
int main(int argc, char *argv[])
int error(int val)
CSimpleOpt::SOption g_rgOptions[]
void binary(int h)
@ OPT_HELP
@ OPT_STATUS
@ OPT_DATA
int fd
int val