libpruio  0.0.2
AM33xx-PRU driver for digital input / output and analog input
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
button.c
Go to the documentation of this file.
1 /*! \file button.c
2 \brief Example: get state of pullup input.
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 button button.c -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 #define PIN P8_07
30 
31 /*! \brief wait for keystroke or timeout
32 \param mseconds timeout value in milliseconds
33 \returns 0 if timeout, 1 if input available, -1 if error
34 
35 Wait for a keystroke or timeout and return which of the events happened.
36 
37 */
38 int
39 isleep(unsigned int mseconds)
40 {
41  fd_set set;
42  struct timeval timeout;
43 
44  /* Initialize the file descriptor set. */
45  FD_ZERO(&set);
46  FD_SET(STDIN_FILENO, &set);
47 
48  /* Initialize the timeout data structure. */
49  timeout.tv_sec = 0;
50  timeout.tv_usec = mseconds * 1000;
51 
52  return TEMP_FAILURE_RETRY(select(FD_SETSIZE,
53  &set, NULL, NULL,
54  &timeout));
55 }
56 
57 int main(int argc, char **argv)
58 {
59  PruIo *io = pruio_new(0, 0x98, 0, 1); /* create new driver structure */
60  do {
61  if (io->Errr) {
62  printf("initialisation failed (%s)\n", io->Errr); break;}
63 
65  printf("failed setting PIN (%s)\n", io->Errr); break;}
66 
67  if (pruio_config(io, 0, 0x1FE, 0, 4, 0)) {
68  printf("config failed (%s)\n", io->Errr); break;}
69 
70  struct termios oldt, newt; // make terminal non-blocking
71  tcgetattr( STDIN_FILENO, &oldt );
72  newt = oldt;
73  newt.c_lflag &= ~( ICANON | ECHO );
74  newt.c_cc[VMIN] = 0;
75  newt.c_cc[VTIME] = 0;
76  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
77 
78  while(!isleep(1)) { // run loop until keystroke
79  printf("\r%1X", pruio_gpio_get(io, PIN));
80  fflush(STDIN_FILENO);
81  }
82  tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // reset terminal
83 
84  printf("\n");
85  } while (0);
86 
87  pruio_destroy(io); /* destroy driver structure */
88  return 0;
89 }