libpruio  0.0.2
AM33xx-PRU driver for digital input / output and analog input
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
sos.c
Go to the documentation of this file.
1 /*! \file sos.c
2 \brief Example: blink user LED 3.
3 
4 This file contains an example on how to use libpruio to control the
5 user LED 3 (near ethernet connector) on the beaglebone board.
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 sos sos.c /usr/local/lib/freebasic/fbrt0.o -lpruio -L"/usr/local/lib/freebasic/" -lfb -lpthread -lprussdrv -ltermcap -lsupc++
15 
16 */
17 
18 #define _GNU_SOURCE 1
19 #include "stdio.h"
20 #include <termios.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <sys/time.h>
25 #include "../c_wrapper/pruio_c_wrapper.h" /* include header */
26 
27 
28 int
29 isleep(unsigned int mseconds)
30 {
31  fd_set set;
32  struct timeval timeout;
33 
34  /* Initialize the file descriptor set. */
35  FD_ZERO(&set);
36  FD_SET(STDIN_FILENO, &set);
37 
38  /* Initialize the timeout data structure. */
39  timeout.tv_sec = 0;
40  timeout.tv_usec = mseconds * 1000;
41 
42  /* select returns 0 if timeout, 1 if input available, -1 if error. */
43  return TEMP_FAILURE_RETRY(select(FD_SETSIZE,
44  &set, NULL, NULL,
45  &timeout));
46 }
47 
48 #define PIN 24
49 #define OUT_K pruio_gpio_out(io, PIN, 1) ; isleep(250) ; pruio_gpio_out(io, PIN, 0) ; isleep(150) ;
50 #define OUT_L pruio_gpio_out(io, PIN, 1) ; isleep(750) ; pruio_gpio_out(io, PIN, 0) ; isleep(150) ;
51 #define OUT_S OUT_K ; OUT_K ; OUT_K ; isleep(250)
52 #define OUT_O OUT_L ; OUT_L ; OUT_L ; isleep(250)
53 
54 int main(int argc, char **argv)
55 {
56  PruIo *io = pruio_new(0, 0x98, 0, 1); /* create new driver UDT */
57  do {
58  if (io->Errr) {
59  printf("initialisation failed (%s)\n", io->Errr); break;}
60 
62  printf("pin configuration failed (%s)\n", io->Errr); break;}
63 
64  if (pruio_config(io, 0, 0x1FE, 0, 4, 0)) {
65  printf("config failed (%s)\n", io->Errr); break;}
66 
67  printf("watch SOS code on user LED 3 (near ethernet connector)\n\n");
68  printf("execute the following command to get rid of mmc1 triggers\n");
69  printf(" sudo su && echo none > /sys/class/leds/beaglebone:green:usr3/trigger && exit\n\n");
70  printf("press any key to quit");
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] = 1;
78  tcsetattr( STDIN_FILENO, TCSANOW, &newt );
79 
80  while(0 >= getchar()) { /* run loop until keystroke */
81  OUT_S;
82  OUT_O;
83  OUT_S;
84  isleep(1500);
85  }
86 
87  tcsetattr( STDIN_FILENO, TCSANOW, &oldt ); /* reset terminal */
88 
89  pruio_gpio_out(io, PIN, 0);
91  printf("pin re-configuration failed (%s)\n", io->Errr);
92  } while (0);
93  printf("\n");
94 
95  pruio_destroy(io); /* destroy driver structure */
96  return 0;
97 }