libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
io_input.c
Go to the documentation of this file.
1 
17 #define _GNU_SOURCE 1
18 #include "stdio.h"
19 #include <termios.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include "libpruio/pruio.h"
25 
26 
34 int
35 isleep(unsigned int mseconds)
36 {
37  fd_set set;
38  struct timeval timeout;
39 
40  /* Initialize the file descriptor set. */
41  FD_ZERO(&set);
42  FD_SET(STDIN_FILENO, &set);
43 
44  /* Initialize the timeout data structure. */
45  timeout.tv_sec = 0;
46  timeout.tv_usec = mseconds * 1000;
47 
48  return TEMP_FAILURE_RETRY(select(FD_SETSIZE,
49  &set, NULL, NULL,
50  &timeout));
51 }
52 
54 int main(int argc, char **argv)
55 {
56  pruIo *io = pruio_new(PRUIO_DEF_ACTIVE, 4, 0x98, 0);
57  do { // pseudo loop to avoid goto
58  if (io->Errr) {
59  printf("initialisation failed (%s)\n", io->Errr); break;}
60 
61  if (pruio_config(io, 1, 0x1FE, 0, 4)) {
62  printf("config failed (%s)\n", io->Errr); break;}
63 
64  struct termios oldt, newt; // make terminal non-blocking
65  tcgetattr( STDIN_FILENO, &oldt );
66  newt = oldt;
67  newt.c_lflag &= ~( ICANON | ECHO );
68  newt.c_cc[VMIN] = 0;
69  newt.c_cc[VTIME] = 0;
70  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
71 
72  while(!isleep(1)) { // run loop until keystroke
73  printf("\r%8X %8X %8X %8X %4X %4X %4X %4X %4X %4X %4X %4X"
74  , io->Gpio->Raw[0]->Mix, io->Gpio->Raw[1]->Mix, io->Gpio->Raw[2]->Mix, io->Gpio->Raw[3]->Mix
75  , io->Adc->Value[1], io->Adc->Value[2], io->Adc->Value[3], io->Adc->Value[4]
76  , io->Adc->Value[5], io->Adc->Value[6], io->Adc->Value[7], io->Adc->Value[8]);
77  fflush(STDIN_FILENO);
78  }
79  tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // reset terminal
80 
81  printf("\n");
82  } while (0);
83 
84  pruio_destroy(io); /* destroy driver structure */
85  return 0;
86 }
VAR io
create new driver UDT
Definition: 1.bas:18
int isleep(unsigned int mseconds)
Wait for keystroke or timeout.
Definition: io_input.c:35
int main(int argc, char **argv)
The main function.
Definition: io_input.c:54
char * pruio_config(pruIo *Io, uint32 Samp, uint32 Mask, uint32 Tmr, uint16 Mds)
Wrapper function for PruIo::config().
pruIo * pruio_new(uint16 Act, uint8 Av, uint32 OpD, uint8 SaD)
Wrapper function for the constructor PruIo::PruIo().
void pruio_destroy(pruIo *Io)
Wrapper function for the destructor PruIo::~PruIo().
@ PRUIO_DEF_ACTIVE
Activate all subsystems.
Definition: pruio.h:531
const ANY_PTR NULL
The NULL pointer.
Wrapper structure for PruIo.
Definition: pruio.h:550