预备
orangePi,SU-03T语音模块,手机(已装抖音)
语音模块烧录sdk
进入语音模块官网http://www.smartpi.cn/#/,配置词条和识别后的串口输出指令,生成SDK(以下为主要部分配置)。
取下SDK,烧录语音模块
手机接入Pi
apt-get install adb -y
查看到手机接入的信息
dmeg
配置USB设备的热拔插,支持Udev机制
vim /etc/udev/rules.d/android.rules
SUBSYSTEM=="usb",ENV{DEVTYPE}=="usb_device",MODE="0666"
adb devices
手机打开开发者,文件传输模式(数据线支持信号传输),打开安全设置
测试
上划
adb shell input swipe 540 1300 540 500 100
下划
adb shell input swipe 540 500 540 1300 100
退出
adb shell input keyevent 26
双击
adb shell input tap 500 500 && adb shell input tap 500 500
SU-03T连接Pi
SU-03T Pi
vcc 5
gnd gnd
b7(T) 10(R)
代码编写
func.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int serial_open (const char *device, const int baud)
{
struct termios options ;
speed_t myBaud ;
int status, fd ;
switch (baud)
{
case 9600: myBaud = B9600 ; break ;
case 115200: myBaud = B115200 ; break ;
}
if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
return -1 ;
fcntl (fd, F_SETFL, O_RDWR) ;
tcgetattr (fd, &options) ;
cfmakeraw (&options) ;
cfsetispeed (&options, myBaud) ;
cfsetospeed (&options, myBaud) ;
options.c_cflag |= (CLOCAL | CREAD) ;
options.c_cflag &= ~PARENB ;
options.c_cflag &= ~CSTOPB ;
options.c_cflag &= ~CSIZE ;
options.c_cflag |= CS8 ;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
options.c_oflag &= ~OPOST ;
options.c_cc [VMIN] = 0 ;
options.c_cc [VTIME] = 100 ; // Ten seconds (100 deciseconds)
tcsetattr (fd, TCSANOW, &options) ;
ioctl (fd, TIOCMGET, &status);
status |= TIOCM_DTR ;
status |= TIOCM_RTS ;
ioctl (fd, TIOCMSET, &status);
usleep (10000) ; // 10mS
return fd ;
}
void serial_send (const int fd, const char *s)
{
int ret;
ret = write (fd, s, strlen (s));
if (ret < 0)
printf("Serial Puts Error\n");
}
char serial_get (const int fd)
{
char cmd;
if (read (fd, &cmd, 1) != 1)
return -1;
return cmd;
}
func.h
int serial_open (const char *device, const int baud);
void serial_send (const int fd, const char *s);
char serial_get (const int fd);
main_pro.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "func.h"
#include
int fd;
void *read_serial()
{
char buf;
while(1)
{
buf = serial_get(fd);
switch(buf)
{
case 'N' :
system("adb shell input swipe 540 1300 540 500 100");
break;
case 'P' :
system("adb shell input swipe 540 500 540 1300 100");
break;
case 'T' :
system("adb shell input tap 500 500 && adb shell input tap 500 500");
break;
case 'Q' :
system("adb shell input keyevent 26");
break;
}
}
}
void *send_serial()
{
char buf_send[32];
while(1)
{
memset(buf_send,'\0',sizeof(buf_send));
scanf("%s",buf_send);
serial_send(fd,buf_send);
}
}
int main()
{
pthread_t read_s;
pthread_t send_s;
if ((fd = serial_open ("/dev/ttyS5", 115200)) < 0)
{
printf("open serial fail\n");
exit(-1);
}
pthread_create(&read_s,NULL,read_serial,NULL);
pthread_create(&send_s,NULL,send_serial,NULL);
while(1){sleep(10);}
return 0;
}
编译
gcc func.c main_pro.c -o main_pro -lpthread