libpruio  0.0
AM33xx-PRU driver for digital input / output and analog input
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
io_input.c
Go to the documentation of this file.
1 /*! \file io_input.c
2 \brief Example: print digital and analog inputs.
3 
4 This file contains an example on how to use libpruio to print out the
5 state of the digital GPIOs and the analog input lines.
6 
7 Licence: GPLv3
8 
9 Copyright 2014 by Thomas{ dOt ]Freiherr[ At ]gmx[ DoT }net
10 
11 
12 Compile by:
13 
14 gcc -Wall -o io_input io_input.c /usr/local/lib/freebasic/fbrt0.o -lpruio -L"/usr/local/lib/freebasic/" -lfb -lpthread -lprussdrv -ltermcap -lsupc++ -Wno-unused-variable
15 
16 */
17 
18 
19 #define _GNU_SOURCE 1
20 #include "stdio.h"
21 #include <termios.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/time.h>
26 #include "../c_wrapper/pruio_c_wrapper.h"
27 #include "../c_wrapper/pruio_pins.h"
28 
29 
30 /*! \brief wait for keystroke or timeout
31 \param mseconds timeout value in milliseconds
32 \returns 0 if timeout, 1 if input available, -1 if error
33 
34 Wait for a keystroke or timeout and return which of the events happened.
35 
36 */
37 int
38 isleep(unsigned int mseconds)
39 {
40  fd_set set;
41  struct timeval timeout;
42 
43  /* Initialize the file descriptor set. */
44  FD_ZERO(&set);
45  FD_SET(STDIN_FILENO, &set);
46 
47  /* Initialize the timeout data structure. */
48  timeout.tv_sec = 0;
49  timeout.tv_usec = mseconds * 1000;
50 
51  return TEMP_FAILURE_RETRY(select(FD_SETSIZE,
52  &set, NULL, NULL,
53  &timeout));
54 }
55 
56 int main(int argc, char **argv)
57 {
58  PruIo *io = pruio_new(0, 0x98, 0, 1); /* create new driver structure */
59  do {
60  if (io->Errr) {
61  printf("initialisation failed (%s)\n", io->Errr); break;}
62 
63  if (pruio_config(io, 0, 0x1FE, 0, 4, 0)) {
64  printf("config failed (%s)\n", io->Errr); break;}
65 
66  struct termios oldt, newt; // make terminal non-blocking
67  tcgetattr( STDIN_FILENO, &oldt );
68  newt = oldt;
69  newt.c_lflag &= ~( ICANON | ECHO );
70  newt.c_cc[VMIN] = 0;
71  newt.c_cc[VTIME] = 0;
72  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
73 
74  while(!isleep(1)) { // run loop until keystroke
75  printf("\r%12o %12o %12o %12o %4X %4X %4X %4X %4X %4X %4X %4X"
76  , io->Gpio[0].Stat , io->Gpio[1].Stat , io->Gpio[2].Stat , io->Gpio[3].Stat
77  , io->Value[1], io->Value[2], io->Value[3], io->Value[4], io->Value[5], io->Value[6], io->Value[7], io->Value[8]);
78  fflush(STDIN_FILENO);
79  }
80  tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // reset terminal
81 
82  printf("\n");
83  } while (0);
84 
85  pruio_destroy(io); /* destroy driver structure */
86  return 0;
87 }