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