Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- contato: [email protected] ou [email protected]
- autor: Antonio Costa aka Cooler_
- Esse programa é uma PoC, para ajudar a deficientes a se comunicar usando um controle remoto de TV,ideia é
- se aperta tecla "1", o computador fala uma determinada frase, se aperta tecla 2 outra frase...
- aperta tecla "1"
- computador fala "quero um copo d'água"
- "2":
- "Estou com fome"
- ...
- demonstrativo do que se pode automatizar usando "espeak" para ajudar um deficiente... tem milhares de outras formas melhores... mas fica um PoC rápido.
- 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...
- ---
- #include <IRremote.h>
- int RECV_PIN = 6;
- IRrecv irrecv(RECV_PIN);
- decode_results results;
- void setup()
- {
- Serial.begin(9600);
- irrecv.enableIRIn();
- }
- void loop() {
- if (irrecv.decode(&results)) {
- Serial.println(String(results.value,HEX) );
- irrecv.resume();
- } else {
- Serial.println("0");
- }
- delay(850);
- }
- ---
- Voce vai precisar da biblioteca libX11-dev e libxtst-dev, também do espeak...
- $ sudo apt-get install libX11-dev libxtst-dev espeak
- $ make
- exemplo Espeak:
- $ sudo yum install espeak
- $ espeak "Luke, I am your father." -s 2
- $ espeak -v pt "Luke, eu sou seu pai." -s 2
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <alloca.h>
- #include <termios.h>
- #include <fcntl.h>
- #include <time.h>
- #include <sys/resource.h>
- #include <X11/Xlib.h>
- #include <X11/extensions/XTest.h>
- #include <X11/keysym.h>
- #define ENTER() type_key(XK_Linefeed)
- // sempre concatene com 'B' a taxa de transmissão ,"9600" padrão do AVR
- #define BAUDRATE B9600
- // macros debug
- #define BUGVIEW 1
- #define DEBUG(x, s...) do { \
- if (!BUGVIEW) { break; } \
- time_t t = time(NULL); \
- char *d = ctime(&t); \
- fprintf(stderr, "%.*s %s[%d] %s(): ", \
- (int)strlen(d) - 1, d, __FILE__, \
- __LINE__, __FUNCTION__); \
- fprintf(stderr, x, ## s); \
- } while (0);
- // vai pegar os dados a cada 5 segundos
- #define SECOND 5
- int foo,revert;
- Display *display;
- Window winFocus;
- void no_write_coredump ( void );
- void type_key ( unsigned c );
- int serialboot( const char* serialport, int baud );
- int serialread( int fd, char* buf, char until, int max );
- void banner()
- {
- fprintf(stdout,"\nFollow patern: ./This_Code <SerialPort>\n"
- "SIntetizador de frases pro controle remoto :-] v0.1 !!!\n"
- "Coded By Cooler_\n"
- "\n");
- }
- int main(int argc, char *argv[])
- {
- int baudrate = BAUDRATE, fd = 0;
- char buf[17];
- char *serialport = alloca( sizeof(char)*17 );
- memset(serialport,0,17);
- no_write_coredump();
- if( argc < 2 )
- {
- banner();
- exit(EXIT_SUCCESS);
- }
- if( strnlen(argv[1],18) <= 16)
- {
- // iniciamos detalhes para automação das teclas
- if( (display=XOpenDisplay(NULL)) == NULL)
- {
- DEBUG("%s: can't open %s\n", argv[0], XDisplayName(NULL));
- exit(1);
- }
- if( XTestQueryExtension(display, &foo, &foo, &foo, &foo) == False )
- {
- DEBUG("XTEST extension missing\n");
- exit(1);
- }
- XGetInputFocus(display, &winFocus, &revert);
- strncpy(serialport,argv[1],16);
- fd=serialboot(serialport, baudrate);
- fprintf(stdout,"Serial:%s\n",serialport);
- if( fd <= 0 )
- {
- DEBUG("Erro\nProcure saber se o dispositivo esta conectado!!\n");
- exit(EXIT_SUCCESS);
- }
- while(1)
- {
- memset(buf,0,16);
- // efetuamos leitura e deixamos em "buf"
- serialread(fd, buf, '\n',16);
- usleep(620000);
- puts(buf);
- if( strnlen(buf,9) > 3 )
- {
- fprintf(stdout,"\n string: %s \n tamanho: %d",buf,strlen(buf));
- // button number 1 , buf == E13DDA28 então...
- if( strstr(buf,"FF30CF") || strstr(buf,"E13DDA28") )
- {
- // se apertar o botão 1 no controle da samsung ou LG, usamos "espeak" para "falar" , exemplo pedindo comida
- execl ("/usr/bin/espeak"," ","\"I want food\"",NULL);
- }
- // button number 2
- if( strstr(buf,"FF18E7") || strstr(buf,"AD586662") )
- {
- // se apertar o botão 2 no controle da samsung ou LG... exemplo pedindo água
- execl ("/usr/bin/espeak"," ","\"I want water\"",NULL);
- }
- // button number 3
- if( strstr(buf,"FF7A85") || strstr(buf,"273009C4") )
- {
- // vai tocar um mp3
- execl ("/usr/bin/mpg123"," ","/home/fulano/mp3/acdc/*",NULL);
- }
- }
- }
- } else {
- DEBUG("\nErro\nentrada muito grande...\n");
- exit(1);
- }
- if( display )
- XCloseDisplay(display);
- if( fd )
- close(fd);
- exit(0);
- }
- void no_write_coredump (void)
- {
- struct rlimit rlim;
- rlim.rlim_cur = 0;
- rlim.rlim_max = 0;
- setrlimit(RLIMIT_CORE, &rlim);
- }
- // o que vai fazer precionar a tecla
- void type_key( unsigned c )
- {
- XTestFakeKeyEvent(display, XKeysymToKeycode(display, c), True, CurrentTime);
- XTestFakeKeyEvent(display, XKeysymToKeycode(display, c), False, CurrentTime);
- XFlush(display);
- }
- // leitura serial
- int serialread(int fd, char* buf, char until,int max)
- {
- char b[1];
- int i = 0;
- do {
- int n = read( fd, b, 1 );
- if( n==-1 )
- return -1;
- if( !n )
- {
- usleep(16000);
- continue;
- }
- buf[i] = b[0];
- i++;
- } while ( b[0]!=until && max != i );
- buf[i] = 0;
- return 0;
- }
- // open() inicial
- int serialboot(const char* serialport, int baud)
- {
- struct termios toptions;
- int fd;
- fd = open( serialport, O_RDWR | O_NOCTTY | O_NDELAY );
- if( fd == -1 )
- {
- DEBUG("serialboot: não foi possivel abrir a porta ");
- close(fd);
- exit(1);
- }
- if( tcgetattr(fd, &toptions) < 0)
- {
- DEBUG("serialboot: nao foi possivel pegar atributos do terminal");
- close(fd);
- exit(1);
- }
- speed_t brate = baud;
- cfsetispeed(&toptions, brate);
- cfsetospeed(&toptions, brate);
- // para default recv com termios.h
- toptions.c_cflag &= ~PARENB;
- toptions.c_cflag &= ~CSTOPB;
- toptions.c_cflag &= ~CSIZE;
- toptions.c_cflag |= CS8;
- toptions.c_cflag &= ~CRTSCTS;
- toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
- toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
- toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
- toptions.c_oflag &= ~OPOST;
- // olhe http://unixwiz.net/techtips/termios-vmin-vtime.html
- toptions.c_cc[VMIN] = 0;
- toptions.c_cc[VTIME] = 20;
- if( tcsetattr(fd, TCSANOW, &toptions) < 0)
- {
- DEBUG("serialboot: nao foi possivel adicionar atributos no term erro 1");
- return -1;
- }
- return fd;
- }
Advertisement
Add Comment
Please, Sign In to add comment