ESP32 搭配语音合成模块
之前介绍过SYN6288 模块【参考1】,这次配合 ESP32 实现随机生成一个时间,然后通过语音播放出来。硬件使用的是ESP-WROOM-32 ESP-32S模块,语音模块的 RX 连接到 ESP32 的GPIO17:
String DataBuffer={{"零"},{"一"},{"二"},{"三"},{"四"},{"五"},{"六"},{"七"},{"八"},{"九"},{"十"}};
//存放转化后的汉字 Unicode值
char character;
#include <HardwareSerial.h>
//根据字符串计算计算出来的送到串口的值
char output;
void setup() {
Serial.begin(115200);
Serial2.begin(9600);
delay(1000);
}
void loop() {
Serial.println("generate");
int hh=random(13),mm=random(60);
String timeStr="";
Serial.print("Generate time: ");
Serial.print(hh);Serial.print(":");Serial.println(mm);
if (hh>9) {timeStr+="十";
if (hh>10) {timeStr+=DataBuffer;}
}
else {timeStr+=DataBuffer;}
timeStr+="点";
if (mm==0) {timeStr+="整";}
else
if (mm<10) {timeStr+="零";timeStr+=DataBuffer;}
else if (mm%10==0) {timeStr+=DataBuffer; timeStr+="十";}
else {timeStr+=DataBuffer;timeStr+="十";timeStr+=DataBuffer;}
if(mm!=0) {timeStr+="分";}
//timeStr="十二点五十八分";
//首先输出一次Arduino 原始字符串 UTF8 的值
for (int i =0;i<timeStr.length()*3;i++) {
Serial.print(timeStr&0xFF,HEX);
Serial.print(' ');
}
Serial.println(' ');
//将 UTF8 转化为 Unicode
for (int i =0;i<timeStr.length()/3;i=i+1) {
character=((timeStr&0xF)<<4)+((timeStr>>2)&0xF);
character=((timeStr&0x3)<<6)+(timeStr&0x3F);
Serial.print(character&0xFF,HEX);
Serial.print(' ');
Serial.print(character&0xFF,HEX);
Serial.print(' ');
}
Serial.println("");
output=0xFD;
output=(timeStr.length()/3*2+3)>>8;
output=((timeStr.length()/3*2+3)&0xFF);
output=0x01;
output=0x03;
//把字符串定义搬过去
for (int i=0;i<timeStr.length()/3*2;i++) {
output=character;
}
//计算一个校验和
output=output;
for (int i=1;i<timeStr.length()/3*2+5;i++) {
output=output ^ output;
}
for (int i =0;i<timeStr.length()/3*2+6;i++) {
Serial.print(output&0xFF,HEX);
Serial.print(' ');
Serial2.write(output);
}
Serial.println(' ');
delay(3000);
}
参考:
1. http://www.lab-z.com/ttssyn/ TTS 真人发音 SYN6288 模块
页:
[1]