语音控制刷抖音项目
视频演示:
语音控制刷抖音
1.接线
2.语音模块的配置
配置B6引脚为RX,配置B7引脚TX

{
char cmd;
while(1){
cmd = myserialGetchar(fd);
printf("GET->0x%c\n",cmd);
switch(cmd){
case 'N':
printf("next\n");
break;
case 'P':
printf("shangyige\n");
break;
case 'Z':
printf("bangwodianzhan\n");
break;
case 'Q':
printf("bukanl\n");
break;
case 'F':
printf("fengkuangdianzhan\n");
break;
}
}
}
int main(int argc,char **argv)
{
char deviceName[32] = {'\0'};
pthread_t readt;
//pthread_t sendt;
if(argc < 2){
printf("uage:%s /dev/ttyS?\n",argv[0]);
return -1;
}
strcpy(deviceName,argv[1]);
if ((fd = myserialOpen(deviceName,115200)) == -1){
printf("open %s error\n",deviceName);
return -1;
}
pthread_create(&readt,NULL,readSerial,NULL);
while(1){
sleep(10);
}
}
uartTool.c文件
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "wiringSerial.h"
char myserialGetchar (const int fd)
{
char x ;
if (read (fd, &x, 1) != 1)
return -1 ;
return x ;
}
int myserialOpen (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) ;
// Get and modify current options:
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 serialSendstring (const int fd, const char *s)
{
int ret;
ret = write (fd, s, strlen(s)) ;
if (ret < 0)
printf("Serial Putchar Error\n");
}
int serialGetstring (const int fd,char *buffer)
{
int n_read;
n_read = read(fd,buffer,32);
return n_read;
}
uartTool.文件
int myserialOpen (const char *device, const int baud);
void serialSendstring (const int fd, const char *s);
int serialGetstring (const int fd,char *buffer);
char myserialGetchar (const int fd);
6.手机通过USB连接开发板
安装adb工具,在终端输入adb安装指令: sudo apt-get install adb
dmeg能查看到手机接入的信息
但是输入adb devices会出现提醒:dinsufficient permissions for device: user in plugdev group; are your udevrules wrong?
解决方法:
配置文件,以支持USB设备的热拔插,支持UDEV的机制
在/etc/udev/rules.d 文件夹下创建规则文件
cd /etc/udev/rules.d/
sudo vim xiaomi-android.rules //创建这个文件并编辑他
在文件中添加内容 SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", MODE="0666"//添加完成后保存退出
在手机开发者选项中,打开USB调试,重新拔插手机 ,手机弹出调试提醒,点确认手机调试模式
注意小米手机打开USB调试后还需要设置:开启USB调试、USB调试(安全设置),关闭启用MIUI系统优化,三者缺一不可。
7.用shell指令来操作手机屏幕,模拟手动滑屏
adb shell input swipe 540 1300 540 500 100 //向下滑动540是水平的,1300是竖直方向,下 是500
adb shell input swipe 540 500 540 1300 100 //向上滑动
adb shell "seq 3 | while read i;do input tap 350 1050 & input tap 350 1050 &sleep 0.01;done;"
//单击暂停
adb shell "for i in {1..3}; do input tap 350 1050 && sleep 0.15 && input tap 350 1050; sleep 1; done
//双击点赞
adb shell input keyevent 26 //锁屏
8.最后将指令写入代码通过语音控制手机操作
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pthread.h>
#include "uartTool.h"
int fd;
void* readSerial()
{
char cmd;
while(1){
cmd = myserialGetchar(fd);
printf("GET->0x%c\n",cmd);
switch(cmd){
case 'N':
printf("next\n");
system("adb shell input swipe 540 1300 540 500 100");
break;
case 'P':
printf("shangyige\n");
system("adb shell input swipe 540 500 540 1300 100");
break;
case 'Z':
printf("bangwodianzhan\n");
system("adb shell \"for i in {1..3}; do input tap 350 1050 && sleep 0.15 && input tap 350 1050; sleep 1; done\"");
break;
case 'Q':
printf("bukanl\n");
system("adb shell input keyevent 26");
break;
case 'F':
printf("fengkuangdianzhan\n");
system("adb shell \"for i in {1..3}; do input tap 350 1050 && sleep 0.15 && input tap 350 1050; sleep 1; done\"");
break;
}
}
}
int main(int argc,char **argv)
{
char deviceName[32] = {'\0'};
pthread_t readt;
if(argc < 2){
printf("uage:%s /dev/ttyS?\n",argv[0]);
return -1;
}
strcpy(deviceName,argv[1]);
if ((fd = myserialOpen(deviceName,115200)) == -1){
printf("open %s error\n",deviceName);
return -1;
}
pthread_create(&readt,NULL,readSerial,NULL);
while(1){
sleep(10);
}
}