libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
button2.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 
67  printf("GPIO configuration failed (%s)\n", io->Errr); break;}
68 
69  if (pruio_config(io, 1, 0x1FE, 0, 4)) {
70  printf("config failed (%s)\n", io->Errr); break;}
71 
72  struct termios oldt, newt; // make terminal non-blocking
73  tcgetattr( STDIN_FILENO, &oldt );
74  newt = oldt;
75  newt.c_lflag &= ~( ICANON | ECHO );
76  newt.c_cc[VMIN] = 0;
77  newt.c_cc[VTIME] = 0;
78  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
79 
80  while(!isleep(1)) { // run loop until keystroke
81  printf("\r%1X", pruio_gpio_Value(io, PIN));
82  fflush(STDIN_FILENO);
83  }
84  tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // reset terminal
85 
86  printf("\n");
87 
88 /* The following line is optional, the DESTRUCTOR does the job by default */
89  if (pruio_gpio_config(io, PIN, PRUIO_PIN_RESET)) { // re-configure pin
90  printf("GPIO configuration failed (%s)\n", io->Errr); break;}
91  } while (0);
92 
93  pruio_destroy(io); /* destroy driver structure */
94  return 0;
95 }
VAR io
create new driver UDT
Definition: 1.bas:18
int isleep(unsigned int mseconds)
Wait for keystroke or timeout.
Definition: button2.c:40
int main(int argc, char **argv)
The main function.
Definition: button2.c:59
#define PIN
The header pin to use.
Definition: button2.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().
@ PRUIO_GPIO_IN_0
GPIO input (pulldown resistor).
Definition: pruio.h:493
char * pruio_gpio_config(pruIo *Io, uint8 Ball, uint8 Modus)
Wrapper function for GpioUdt::config().
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