文章目录
语言控制刷抖音小项目
1.装逼小项目:语言控制刷抖音
1.1.语音模块配置
- 进入语音模块官网 http://www.smartpi.cn/#/,进行配置词条和识别后的串口输出指令
- 记录下相关指令以及上图的识别词条,方便固件烧写后的调试
-
固件烧写
串口接线:官方手册是VCC接串口VCC
B7 引脚 接 串口RXD
B6 引脚 接 串口 TXD
GND 接 串口 GND
- 下载SDK将解压文件放到没有中文路径下,打开烧写软件
- 烧写文件
- 烧写前先把固件电源拨到OFF,选择固件以后拨到ON开始烧入
-
固件先和电脑调试助手配合,验证数据
1.2.编程实现语音和开发板通信
开发板接线:
VCC接5V
B7 引脚 RXD //负责把接收到的结果发送给全志H616 Linux
GND 接 GND
- 将语音模块插入开发板并编程实现基础逻辑代码,添加串口读取一个字符的接口
头文件程序:
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);
函数程序
#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 ((int)x) & 0xFF ;
}
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 ;
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 Puts Error\n");
}
int serialGetstring (const int fd,char *buffer)//获得字符串
{
int n_read;
n_read = read(fd,buffer,32);
return n_read;
}
myserialGetchar();
串口语音测试主程序
#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);
switch(cmd){
case 'N':
printf("next\n");
break;
case 'P':
printf("pre\n");
break;
case 'Z':
printf("zan\n");
break;
case 'Q':
printf("qu\n");
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);}
}
1.3.手机接入Linux热拔插相关
a. 把手机接入开发板
b. 安装adb工具,在终端输入adb安装指令: sudo apt-get install adb
c. dmesg能查看到手机接入的信息,但是输入adb devices会出现提醒
dinsufficient permissions for device: user in plugdev group; are your udev
rules wrong?
d. 配置文件,以支持USB设备的热拔插,支持UDEV的机制
在/etc/udev/rules.d 文件夹下创建规则文件
cd /etc/udev/rules.d/
sudo vim 51-android.rules
在文件中添加内容 SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", MODE="0666"
e. 在手机开发者选项中,打开USB调试,重新拔插手机
f. 手机弹出调试提醒,点确认手机调试模式
1.3.1.常见问题及解决方法
工作中要是遇到问题先不要着急去问别人和问主管,这样会显得你很NO,先去问一下度娘,你遇到的这些问题别人曾经遇到过,写的博文
我遇到的问题1:设备未被识别
有时,执行adb devices后,设备列表为空,无法识别连接的Android设备。
解决方法:
- 确保USB调试已开启:在设备的开发者选项中,启用USB调试。
- 安装正确的驱动程序:确保在计算机上安装了正确的USB驱动程序,使其能够识别Android设备。
- 重新连接设备:尝试重新连接USB并执行adb devices。
1.4.用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 input keyevent 26 锁屏
1.5.最后主程序
#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);
switch(cmd){
case 'N':
printf("next\n");
system("adb shell input swipe 540 1300 540 500 100");
break;
case 'P':
printf("pre\n");
system("adb shell input swipe 540 500 540 1300 100");
break;
case 'Z':
printf("zan\n");
system("adb shell \"seq 3 | while read i;do input tap 350 1050 &
input tap 350 1050 & sleep 0.01;done;\"");
break;
case 'Q':
printf("qu\n");
system("adb shell input keyevent 26");
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);}
}