本站改版新增arduino频道
arduino 使用SNR9816TTS模块教程
//first you should install ESPSoftwareSerial and UTF8ToGB2312 liberary
#include#include "UTF8ToGB2312.h"
#define MYPORT_TX 0
#define MYPORT_RX 1
EspSoftwareSerial::UART myPort;
uint8_t voicedata[] = { 0xFD, 0x00, 0x06, 0x01, 0x01, 0x5B, 0x76, 0x31, 0x5D }; //voicedata[7] = 0x31 ~ 0x39
uint8_t mandata[] = { 0xFD, 0x00, 0x06, 0x01, 0x01, 0x5B, 0x6D, 0x31, 0x5D };
uint8_t wemandata[] = { 0xFD, 0x00, 0x06, 0x01, 0x01, 0x5B, 0x6D, 0x30, 0x5D };
void setup() {
Serial.begin(115200);
myPort.begin(115200, SWSERIAL_8N1, MYPORT_RX, MYPORT_TX, false);
if (!myPort) { // If the object did not initialize, then its configuration is invalid
Serial.println("Invalid EspSoftwareSerial pin configuration, check config");
while (1) { // Don't continue with invalid configuration
delay(1000);
}
}
speech("系统开机");
delay(1000);
for (int i = 0; i < sizeof(voicedata) / sizeof(voicedata[0]); i++) {
myPort.write(voicedata[i]);
}
delay(1000);
speech("系统音量以调小");
// delay(2000);
for (int i = 0; i < sizeof(mandata) / sizeof(mandata[0]); i++) {
myPort.write(mandata[i]);
}
delay(2000);
speech("系统初始化男声成功");
// delay(1000);
// voicedata[7] = 0x31;
for (int i = 0; i < sizeof(wemandata) / sizeof(wemandata[0]); i++) {
myPort.write(wemandata[i]);
}
delay(2500);
speech("系统初始化女声成功");
// delay(5000);
}
void loop() {
// speech(" 系统初始化成功,网络地址如下");
delay(1000);
}
void speech(String data) {
//0--空闲 1--繁忙 2--异常
while (workstate() > 0) {
if (workstate() == 1) Serial.println("tts busy");
if (workstate() == 2) Serial.println("tts wrong");
}
String utf8_str = data;
String gb2312_str = GB.get(utf8_str);
unsigned char head[gb2312_str.length() + 6];
// 定义无符号字符类型数组,将 GB2312 编码的字符串复制到数组中
unsigned char gb2312_data[gb2312_str.length() + 1];
memset(gb2312_data, 0, sizeof(gb2312_data));
strncpy((char *)gb2312_data, gb2312_str.c_str(), gb2312_str.length());
// head byte
head[0] = 0xFD;
// length bytes
unsigned int dat_len = gb2312_str.length() + 3;
head[1] = dat_len >> 8;
head[2] = dat_len;
// cmd byte
head[3] = 0x01;
// para byte
head[4] = 0x01;
// send each character individually
for (int i = 0; i < gb2312_str.length(); i++) {
head[i + 5] = gb2312_data[i];
}
// 计算异或值并添加到数组,额这个是syn那个芯片的例程,这个异或计算有没有用我懒得改了。反正代码能跑。
head[gb2312_str.length() + 5] = head[0];
for (int i = 1; i < gb2312_str.length() + 5; i++) {
head[gb2312_str.length() + 5] ^= head[i];
}
// 发送字符数组到串口
for (int j = 0; j < gb2312_str.length() + 6; j++) {
myPort.write(head[j]);
}
delay(gb2312_str.length() * 300);
Serial.println(data);
}
// 查询tts合成工作状态 返回1表示繁忙 0表示空闲
int workstate() {
unsigned char head[4] = { 0xFD, 0x00, 0x01, 0x21 };
// 发送字符数组到串口
for (int j = 0; j < 4; j++) {
myPort.write(head[j]);
}
// 等待myPort的返回
while (myPort.available() < 1) {
// 可以在这里加入一些延时,以防止过快地查询
delay(150);
}
// 读取并处理返回的数据
byte response = myPort.read();
// 返回相应的状态值
if (response == 0x4E) {
// 繁忙
return 1;
} else if (response == 0x4F) {
// 空闲
return 0;
} else {
// 未知状态,可以根据需要进行处理
return 2;
}
}
Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号
执行时间: 0.0097219944000244 seconds