Cooler_

ajuda deficientes exemplo 1

Sep 4th, 2014
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.70 KB | None | 0 0
  1. /*
  2. autor: Antonio Costa aka Cooler_
  3.  
  4. Esse programa é uma PoC, para ajudar a deficientes a se comunicar usando um controle remoto de TV,ideia é
  5. se aperta tecla "1", o computador fala uma determinada frase, se aperta tecla 2 outra frase...
  6. aperta tecla "1"
  7. computador fala "quero um copo d'água"
  8. "2":
  9. "Estou com fome"
  10. ...
  11.  
  12.  
  13. demonstrativo do que se pode automatizar usando "espeak" para ajudar um deficiente... tem milhares de outras formas melhores... mas fica um PoC rápido.
  14.  
  15.  
  16. usando um arduino com seguinte código, interessante usar um ARM com webcam com opencv para detectar movimentos e pdoer escolher frases em um array etc...
  17. ---
  18. #include <IRremote.h>
  19.  
  20. int RECV_PIN = 6;
  21. IRrecv irrecv(RECV_PIN);
  22. decode_results results;
  23.  
  24. void setup()
  25. {
  26.   Serial.begin(9600);
  27.   irrecv.enableIRIn();
  28. }
  29.  
  30. void loop() {
  31.   if (irrecv.decode(&results)) {
  32.     Serial.println(String(results.value,HEX) );
  33.    
  34.     irrecv.resume();
  35.   } else {
  36.     Serial.println("0");
  37.   }  
  38.   delay(850);
  39. }
  40. ---
  41.  
  42.  
  43. Voce vai precisar da biblioteca  libX11-dev e libxtst-dev, também do espeak...
  44. $ sudo apt-get install libX11-dev libxtst-dev espeak
  45. $ make
  46.  
  47. exemplo Espeak:
  48.  
  49. $ sudo yum install espeak
  50.  
  51. $ espeak "Luke, I am your father." -s 2
  52.  
  53. $ espeak -v pt "Luke, eu sou seu pai." -s 2
  54.  
  55.  */
  56. #include <stdio.h>    
  57. #include <stdlib.h>
  58. #include <string.h>  
  59. #include <unistd.h>
  60. #include <alloca.h>
  61. #include <termios.h>  
  62. #include <fcntl.h>
  63. #include <time.h>
  64. #include <sys/resource.h>
  65. #include <X11/Xlib.h>
  66. #include <X11/extensions/XTest.h>
  67. #include <X11/keysym.h>
  68.  
  69. #define ENTER() type_key(XK_Linefeed)
  70.  
  71. // sempre concatene com 'B' a  taxa de transmissão ,"9600" padrão do AVR
  72. #define BAUDRATE B9600
  73.  
  74. // macros debug
  75. #define BUGVIEW 1
  76.  
  77. #define DEBUG(x, s...) do { \
  78.         if (!BUGVIEW) { break; } \
  79.         time_t t = time(NULL); \
  80.         char *d = ctime(&t); \
  81.         fprintf(stderr, "%.*s %s[%d] %s(): ", \
  82.         (int)strlen(d) - 1, d, __FILE__, \
  83.         __LINE__, __FUNCTION__); \
  84.         fprintf(stderr, x, ## s); \
  85.     } while (0);
  86.  
  87. // vai pegar os dados a cada 5 segundos
  88. #define SECOND 5
  89.  
  90. int foo,revert;
  91. Display *display;
  92. Window winFocus;
  93.  
  94. void no_write_coredump ( void );
  95. void type_key ( unsigned c );
  96. int serialboot( const char* serialport, int baud );
  97. int serialread( int fd, char* buf, char until, int max );
  98.  
  99. void banner()
  100. {
  101.     fprintf(stdout,"\nFollow patern: ./This_Code <SerialPort>\n"
  102.         "SIntetizador de frases pro controle remoto :-] v0.1 !!!\n"
  103.         "Coded By Cooler_\n"
  104.         "\n");
  105. }
  106.  
  107.  
  108. int main(int argc, char *argv[])
  109. {
  110.  
  111.  int baudrate = BAUDRATE, fd = 0;
  112.  char buf[17];
  113.  char *serialport = alloca( sizeof(char)*17 );
  114.  
  115.     memset(serialport,0,17);
  116.     no_write_coredump();
  117.  
  118.     if( argc < 2 )
  119.     {
  120.         banner();
  121.         exit(EXIT_SUCCESS);
  122.     }
  123.  
  124.    
  125.     if( strnlen(argv[1],18) <= 16)
  126.     {
  127. // iniciamos detalhes para automação das teclas
  128.         if( (display=XOpenDisplay(NULL)) == NULL)
  129.         {
  130.                 DEBUG("%s: can't open %s\n", argv[0], XDisplayName(NULL));
  131.                 exit(1);
  132.         }
  133.  
  134.         if( XTestQueryExtension(display, &foo, &foo, &foo, &foo) == False )
  135.         {
  136.                 DEBUG("XTEST extension missing\n");
  137.                 exit(1);
  138.         }
  139.  
  140.         XGetInputFocus(display, &winFocus, &revert);
  141.  
  142.         strncpy(serialport,argv[1],16);
  143.         fd=serialboot(serialport, baudrate);
  144.         fprintf(stdout,"Serial:%s\n",serialport);
  145.  
  146.         if( fd <= 0 )
  147.         {
  148.                 DEBUG("Erro\nProcure saber se o dispositivo esta conectado!!\n");
  149.                 exit(EXIT_SUCCESS);
  150.         }
  151.  
  152.    
  153.         while(1)
  154.         {
  155.                 memset(buf,0,16);
  156. // efetuamos leitura e deixamos em "buf"
  157.                 serialread(fd, buf, '\n',16);
  158.                 usleep(620000);
  159.                 puts(buf);
  160.  
  161.                 if( strnlen(buf,9) > 3 )
  162.                 {
  163.                     fprintf(stdout,"\n string: %s \n tamanho: %d",buf,strlen(buf));
  164.  
  165. // button number 1 , buf == E13DDA28 então...
  166.                     if( strstr(buf,"FF30CF") || strstr(buf,"E13DDA28") )
  167.                     {
  168. // se apertar o botão 1 no controle da samsung ou LG, usamos "espeak" para "falar" , exemplo pedindo comida
  169.                         execl ("/usr/bin/espeak"," ","\"I want food\"",NULL);
  170.                     }
  171.  
  172. // button number 2
  173.                     if( strstr(buf,"FF18E7") || strstr(buf,"AD586662") )
  174.                     {
  175. // se apertar o botão 2 no controle da samsung ou LG... exemplo pedindo água
  176.                         execl ("/usr/bin/espeak"," ","\"I want water\"",NULL);
  177.                     }
  178.  
  179. // button number 3
  180.                     if( strstr(buf,"FF7A85") || strstr(buf,"273009C4") )
  181.                     {
  182. // vai tocar um mp3
  183.                         execl ("/usr/bin/mpg123"," ","/home/fulano/mp3/acdc/*",NULL);
  184.                     }
  185.  
  186.                 }  
  187.    
  188.         }
  189.     } else {
  190.         DEBUG("\nErro\nentrada muito grande...\n");
  191.         exit(1);
  192.     }
  193.  
  194.     if( display )
  195.         XCloseDisplay(display);
  196.  
  197.     if( fd )
  198.         close(fd);
  199.  
  200.  exit(0);    
  201.  
  202. }
  203.  
  204.  
  205.  
  206.  
  207. void no_write_coredump (void)
  208. {
  209.     struct rlimit rlim;
  210.  
  211.     rlim.rlim_cur = 0;
  212.     rlim.rlim_max = 0;
  213.     setrlimit(RLIMIT_CORE, &rlim);
  214. }
  215.  
  216.  
  217. // o que vai fazer precionar a tecla
  218. void type_key( unsigned c )
  219. {
  220.     XTestFakeKeyEvent(display, XKeysymToKeycode(display, c), True, CurrentTime);
  221.     XTestFakeKeyEvent(display, XKeysymToKeycode(display, c), False, CurrentTime);
  222.     XFlush(display);
  223. }
  224.  
  225. // leitura serial
  226. int serialread(int fd, char* buf, char until,int max)
  227. {
  228.  char b[1];
  229.  int i = 0;
  230.  
  231.     do {
  232.         int n = read( fd, b, 1 );  
  233.  
  234.         if( n==-1 )
  235.             return -1;    
  236.         if( !n )
  237.         {
  238.             usleep(16000);
  239.             continue;
  240.         }
  241.  
  242.         buf[i] = b[0];
  243.         i++;
  244.  
  245.     } while ( b[0]!=until && max != i );
  246.  
  247.     buf[i] = 0;  
  248.  
  249.  return 0;
  250. }
  251.  
  252. // open() inicial
  253. int serialboot(const char* serialport, int baud)
  254. {
  255.  struct termios toptions;
  256.  int fd;
  257.  
  258.     fd = open( serialport, O_RDWR | O_NOCTTY | O_NDELAY );
  259.  
  260.     if( fd == -1 )  
  261.     {
  262.         DEBUG("serialboot: não foi possivel abrir a porta ");
  263.         close(fd);
  264.         exit(1);
  265.     }
  266.    
  267.     if( tcgetattr(fd, &toptions) < 0)
  268.     {
  269.         DEBUG("serialboot: nao foi possivel pegar atributos do terminal");
  270.         close(fd);
  271.         exit(1);
  272.     }
  273.  
  274.     speed_t brate = baud;
  275.     cfsetispeed(&toptions, brate);
  276.     cfsetospeed(&toptions, brate);
  277.    // para default recv com termios.h
  278.     toptions.c_cflag &= ~PARENB;
  279.     toptions.c_cflag &= ~CSTOPB;
  280.     toptions.c_cflag &= ~CSIZE;
  281.     toptions.c_cflag |= CS8;
  282.     toptions.c_cflag &= ~CRTSCTS;
  283.     toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
  284.     toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
  285.     toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  286.     toptions.c_oflag &= ~OPOST;
  287.  
  288.     // olhe http://unixwiz.net/techtips/termios-vmin-vtime.html
  289.     toptions.c_cc[VMIN]  = 0;
  290.     toptions.c_cc[VTIME] = 20;
  291.    
  292.     if( tcsetattr(fd, TCSANOW, &toptions) < 0)
  293.     {
  294.         DEBUG("serialboot: nao foi possivel adicionar atributos no term erro 1");
  295.         return -1;
  296.     }
  297.  
  298.  return fd;
  299. }
Advertisement
Add Comment
Please, Sign In to add comment