libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
button.c
Go to the documentation of this file.
1 
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 "libpruio/pruio.h"
27 #include "libpruio/pruio_pins.h"
28 
30 #define PIN P8_07
31 
39 int
40 isleep(unsigned int mseconds)
41 {
42  fd_set set;
43  struct timeval timeout;
44 
45  /* Initialize the file descriptor set. */
46  FD_ZERO(&set);
47  FD_SET(STDIN_FILENO, &set);
48 
49  /* Initialize the timeout data structure. */
50  timeout.tv_sec = 0;
51  timeout.tv_usec = mseconds * 1000;
52 
53  return TEMP_FAILURE_RETRY(select(FD_SETSIZE,
54  &set, NULL, NULL,
55  &timeout));
56 }
57 
59 int main(int argc, char **argv)
60 {
61  pruIo *io = pruio_new(PRUIO_DEF_ACTIVE, 4, 0x98, 0);
62  do {
63  if (io->Errr) {
64  printf("initialisation failed (%s)\n", io->Errr); break;}
65 
66  if (pruio_config(io, 1, 0x1FE, 0, 4)) {
67  printf("config failed (%s)\n", io->Errr); break;}
68 
69  struct termios oldt, newt; // make terminal non-blocking
70  tcgetattr( STDIN_FILENO, &oldt );
71  newt = oldt;
72  newt.c_lflag &= ~( ICANON | ECHO );
73  newt.c_cc[VMIN] = 0;
74  newt.c_cc[VTIME] = 0;
75  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
76 
77  while(!isleep(1)) { // run loop until keystroke
78  printf("\r%1X", pruio_gpio_Value(io, PIN));
79  fflush(STDIN_FILENO);
80  }
81  tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // reset terminal
82 
83  printf("\n");
84  } while (0);
85 
86  pruio_destroy(io); /* destroy driver structure */
87  return 0;
88 }
VAR io
create new driver UDT
Definition: 1.bas:18
int isleep(unsigned int mseconds)
Wait for keystroke or timeout.
Definition: button.c:40
int main(int argc, char **argv)
The main function.
Definition: button.c:59
#define PIN
The header pin to use.
Definition: button.c:30
char * pruio_config(pruIo *Io, uint32 Samp, uint32 Mask, uint32 Tmr, uint16 Mds)
Wrapper function for PruIo::config().
uint32 pruio_gpio_Value(pruIo *Io, uint8 Ball)
Wrapper function for GpioUdt::Value().
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