libpruio  0.0
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(filedesc, &set);
37  FD_SET(STDIN_FILENO, &set);
38 
39  /* Initialize the timeout data structure. */
40  timeout.tv_sec = 0;
41  timeout.tv_usec = mseconds * 1000;
42 
43  /* select returns 0 if timeout, 1 if input available, -1 if error. */
44  return TEMP_FAILURE_RETRY(select(FD_SETSIZE,
45  &set, NULL, NULL,
46  &timeout));
47 }
48 
49 #define PIN 24
50 #define OUT_K pruio_gpio_out(io, PIN, 1) ; isleep(250) ; pruio_gpio_out(io, PIN, 0) ; isleep(150) ;
51 #define OUT_L pruio_gpio_out(io, PIN, 1) ; isleep(750) ; pruio_gpio_out(io, PIN, 0) ; isleep(150) ;
52 #define OUT_S OUT_K ; OUT_K ; OUT_K ; isleep(250)
53 #define OUT_O OUT_L ; OUT_L ; OUT_L ; isleep(250)
54 
55 int main(int argc, char **argv)
56 {
57  PruIo *io = pruio_new(0, 0x98, 0, 1); /* create new driver UDT */
58  do {
59  if (io->Errr) {
60  printf("initialisation failed (%s)\n", io->Errr); break;}
61 
63  printf("pin configuration failed (%s)\n", io->Errr); break;}
64 
65  if (pruio_config(io, 0, 0x1FE, 0, 4, 0)) {
66  printf("config failed (%s)\n", io->Errr); break;}
67 
68  printf("watch SOS code on user LED 3 (near ethernet connector)\n\n");
69  printf("execute the following command to get rid of mmc1 triggers\n");
70  printf(" sudo su && echo none > /sys/class/leds/beaglebone:green:usr3/trigger && exit\n\n");
71  printf("press any key to quit");
72 
73  struct termios oldt, newt; /* make terminal non-blocking */
74  tcgetattr( STDIN_FILENO, &oldt );
75  newt = oldt;
76  newt.c_lflag &= ~( ICANON | ECHO );
77  newt.c_cc[VMIN] = 0;
78  newt.c_cc[VTIME] = 1;
79  tcsetattr( STDIN_FILENO, TCSANOW, &newt );
80 
81  while(0 >= getchar()) { /* run loop until keystroke */
82  OUT_S;
83  OUT_O;
84  OUT_S;
85  isleep(1500);
86  }
87 
88  tcsetattr( STDIN_FILENO, TCSANOW, &oldt ); /* reset terminal */
89 
90  pruio_gpio_out(io, PIN, 0);
92  printf("pin re-configuration failed (%s)\n", io->Errr);
93  } while (0);
94  printf("\n");
95 
96  pruio_destroy(io); /* destroy driver structure */
97  return 0;
98 }