想要做一个语音播报温湿度和光照程序,用XFS5152CE,但没法播报数字变量,想请教大家怎么做。代码是这样的:
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#define PIN_AO 0
#define PIN_DO 4//湿度
OneWire ds(10);
int ledPin = 13;
int pirPin = 7;//人体
int pirValue;
int sec = 0;
int sensorPin = 2;//光照
int value = 0;
#include "XFS.h" //封装好的命令库
#include "TextTab.h" //中文需要放在该记事本中(因为编码不兼容)
/*实例化语音合成对象*/
XFS5152CE xfs;
/*超时设置,示例为30S*/
static uint32_t LastSpeakTime = 0;
#define SpeakTimeOut 10000
/**
@brief 初始化语音合成
@param 无
@retval 无
*/
uint8_t n = 1;
static void XFS_Init()
{
xfs.Begin(0x30);//设备i2c地址,地址为0x50
delay(n);
xfs.SetReader(XFS5152CE::Reader_XiaoYan); //设置发音人
delay(n);
xfs.SetEncodingFormat(XFS5152CE::GB2312); //文本的编码格式
delay(n);
xfs.SetLanguage(xfs.Language_Auto); //语种判断
delay(n);
xfs.SetStyle(XFS5152CE::Style_Continue); //合成风格设置
delay(n);
xfs.SetArticulation(XFS5152CE::Articulation_Letter); //设置单词的发音方式
delay(n);
xfs.SetSpeed(5); //设置语速1~10
delay(n);
xfs.SetIntonation(5); //设置语调1~10
delay(n);
xfs.SetVolume(5); //设置音量1~10
delay(n);
}
unsigned char result = 0xFF;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
digitalWrite(ledPin, LOW);
pinMode(PIN_AO, INPUT);
pinMode(PIN_DO, INPUT);
sensors.begin();
Serial.begin(115200); //串口波特率设置,打印数据时串口需要选择和这里一样的波特率
XFS_Init();
xfs.SetSpell(XFS5152CE::Spell_Enable);
xfs.StartSynthesis("kai1ji1");
while(xfs.GetChipStatus() != xfs.ChipStatus_Idle)
{
delay(30);
}
}
void loop()
{
sensors.requestTemperatures();
Serial.print("Temperature: ");
Serial.print(sensors.getTempCByIndex(0));
Serial.print((char)176);//shows degrees character
Serial.print("C | ");
value = analogRead(sensorPin);
Serial.println(value, DEC);
Serial.print("AO=");
Serial.print(analogRead(PIN_AO));
Serial.print(", DO=");
Serial.println(digitalRead(PIN_DO));
pirValue = digitalRead(pirPin);
digitalWrite(ledPin, pirValue);
sec += 1;
Serial.print("Second: ");
Serial.print(sec);
Serial.print("IR value: ");
Serial.print(pirValue);
Serial.print('\n');
delay(250);
if (pirValue==1){
xfs.SetSpell(XFS5152CE::Spell_Enable);
xfs.StartSynthesis("dang1qian2wen1du4");
while(xfs.GetChipStatus() != xfs.ChipStatus_Idle)
{
delay(30);
}
xfs.SetSpell(XFS5152CE::Spell_Enable);
xfs.StartSynthesis("dang1qian2shi1du4");
while(xfs.GetChipStatus() != xfs.ChipStatus_Idle)
{
delay(30);
}
xfs.SetSpell(XFS5152CE::Spell_Enable);
xfs.StartSynthesis("dang1qian2guang1zhao4");
while(xfs.GetChipStatus() != xfs.ChipStatus_Idle)
{
delay(30);
}
}
}
第二个
static const char* TextTab1[] = {
"����Dz����ܿƼ�","��ӭʹ���Dz����ܿƼ���������ģ��","hey yahboom"
};
static const char* TextTab2[] = {
"�������ѽ"
};
第三个
#include "XFS.h"
#include <Wire.h>
#define XFS_WIRE Wire
#define XFS_DataHead (uint8_t)0xFD
XFS5152CE::XFS5152CE(EncodingFormat_Type encodingFormat)
{
DataPack.DataHead = XFS_DataHead;
DataPack.Length_HH = 0x00;
DataPack.Length_LL = 0x00;
DataPack.Commond = 0x00;
DataPack.EncodingFormat = encodingFormat;
ChipStatus = 0x00;
}
void XFS5152CE::Begin(uint8_t addr)
{
I2C_Addr = addr;
XFS_WIRE.begin();
}
uint8_t XFS5152CE::GetChipStatus()
{
uint8_t AskState[4] = {0xFD,0x00,0x01,0x21};
XFS_WIRE.beginTransmission(I2C_Addr);
XFS_WIRE.write(AskState,4);
XFS_WIRE.endTransmission();
delay(100);
XFS_WIRE.requestFrom(I2C_Addr, 1);
while (XFS_WIRE.available())
{
ChipStatus = XFS_WIRE.read();
}
return ChipStatus;
}
bool XFS5152CE::IIC_WriteByte(uint8_t data)
{
Wire.beginTransmission(I2C_Addr);
Wire.write(data);
if(Wire.endTransmission()!=0) //发送结束信号
{
delay(10);
return false;
}
delay(10);
return true;
}
void XFS5152CE::IIC_WriteBytes(uint8_t* buff, uint32_t size)
{
for (uint32_t i = 0; i < size; i++)
{
IIC_WriteByte(buff[i]);
}
}
void XFS5152CE::StartSynthesis(const char* str)
{
uint16_t size = strlen(str) + 2;
DataPack.Length_HH = highByte(size);
DataPack.Length_LL = lowByte(size);
DataPack.Commond = CMD_StartSynthesis;
DataPack.Text = str;
IIC_WriteBytes((uint8_t*)&DataPack,5);
IIC_WriteBytes(DataPack.Text, strlen(str));
}
void XFS5152CE::StartSynthesis(String str)
{
StartSynthesis((const char*)str.c_str());
}
void XFS5152CE::SendCommond(CMD_Type cmd)
{
DataPack.Length_HH = 0x00;
DataPack.Length_LL = 0x01;
DataPack.Commond = cmd;
XFS_WIRE.beginTransmission(I2C_Addr);
XFS_WIRE.write((uint8_t*)&DataPack, 4);
XFS_WIRE.endTransmission();
}
void XFS5152CE::StopSynthesis()
{
SendCommond(CMD_StopSynthesis);
}
void XFS5152CE:auseSynthesis()
{
SendCommond(CMD_PauseSynthesis);
}
void XFS5152CE::RecoverySynthesis()
{
SendCommond(CMD_RecoverySynthesis);
}
void XFS5152CE::TextCtrl(char c, int d)
{
char str[10];
if (d != -1)
sprintf(str, "[%c%d]", c, d);
else
sprintf(str, "[%c]", c);
StartSynthesis(str);
}
void XFS5152CE::SetEncodingFormat(EncodingFormat_Type encodingFormat)
{
DataPack.EncodingFormat = encodingFormat;
}
void XFS5152CE::SetStyle(Style_Type style)
{
TextCtrl('f', style);
while(GetChipStatus() != ChipStatus_Idle)
{
delay(30);
}
}
void XFS5152CE::SetLanguage(Language_Type language)
{
TextCtrl('g', language);
while(GetChipStatus() != ChipStatus_Idle)
{
delay(30);
}
}
void XFS5152CE::SetArticulation(Articulation_Type articulation)
{
TextCtrl('h', articulation);
while(GetChipStatus() != ChipStatus_Idle)
{
delay(30);
}
}
void XFS5152CE::SetSpell(Spell_Type spell)
{
TextCtrl('i', spell);
while(GetChipStatus() != ChipStatus_Idle)
{
delay(30);
}
}
void XFS5152CE::SetReader(Reader_Type reader)
{
TextCtrl('m', reader);
while(GetChipStatus() != ChipStatus_Idle)
{
delay(30);
}
}
void XFS5152CE::SetNumberHandle(NumberHandle_Type numberHandle)
{
TextCtrl('n', numberHandle);
while(GetChipStatus() != ChipStatus_Idle)
{
delay(30);
}
}
void XFS5152CE::SetZeroPronunciation(ZeroPronunciation_Type zeroPronunciation)
{
TextCtrl('o', zeroPronunciation);
while(GetChipStatus() != ChipStatus_Idle)
{
delay(30);
}
}
void XFS5152CE::SetNamePronunciation(NamePronunciation_Type namePronunciation)
{
TextCtrl('r', namePronunciation);
while(GetChipStatus() != ChipStatus_Idle)
{
delay(30);
}
}
void XFS5152CE::SetSpeed(int speed)
{
speed = constrain(speed, 0, 10);
TextCtrl('s', speed);
while(GetChipStatus() != ChipStatus_Idle)
{
delay(30);
}
}
void XFS5152CE::SetIntonation(int intonation)
{
intonation = constrain(intonation, 0, 10);
TextCtrl('t', intonation);
while(GetChipStatus() != ChipStatus_Idle)
{
delay(30);
}
}
void XFS5152CE::SetVolume(int volume)
{
volume = constrain(volume, 0, 10);
TextCtrl('v', volume);
while(GetChipStatus() != ChipStatus_Idle)
{
delay(30);
}
}
void XFS5152CE::SetPromptTone(PromptTone_Type promptTone)
{
TextCtrl('x', promptTone);
while(GetChipStatus() != ChipStatus_Idle)
{
delay(30);
}
}
void XFS5152CE::SetOnePronunciation(OnePronunciation_Type onePronunciation)
{
TextCtrl('y', onePronunciation);
while(GetChipStatus() != ChipStatus_Idle)
{
delay(30);
}
}
void XFS5152CE::SetRhythm(Rhythm_Type rhythm)
{
TextCtrl('z', rhythm);
while(GetChipStatus() != ChipStatus_Idle)
{
delay(30);
}
}
void XFS5152CE::SetRestoreDefault()
{
TextCtrl('d', -1);
while(GetChipStatus() != ChipStatus_Idle)
{
delay(30);
}
}
第四个
#ifndef __XFS_H
#define __XFS_H
#include "Arduino.h"
class XFS5152CE
{
public:
typedef struct
{
uint8_t DataHead;
uint8_t Length_HH;
uint8_t Length_LL;
uint8_t Commond;
uint8_t EncodingFormat;
const char* Text;
} XFS_Protocol_TypeDef;
XFS_Protocol_TypeDef DataPack;
/*
*| 帧头(1Byte)| 数据区长度(2Byte)| 数据区(<4KByte) |
*| | 高字节 | 低字节 | 命令字 | 文本编码格式 | 待合成文本 |
*| 0xFD | 0xHH | 0xLL | 0x01 | 0x00~0x03 | ... ... |
*/
typedef enum
{
CMD_StartSynthesis = 0x01,//语音合成命令
CMD_StopSynthesis = 0x02,//停止合成命令,没有参数
CMD_PauseSynthesis = 0x03,//暂停合成命令,没有参数
CMD_RecoverySynthesis = 0x04,//恢复合成命令,没有参数
CMD_CheckChipStatus = 0x21,//芯片状态查询命令
CMD_PowerSavingMode = 0x88,//芯片进入省电模式
CMD_NormalMode = 0xFF//芯片从省电模式返回正常工作模式
} CMD_Type;//命令字
void StartSynthesis(const char* str);//开始合成
void StartSynthesis(String str);//开始合成
bool IIC_WriteByte(uint8_t data);
void IIC_WriteBytes(uint8_t* buff, uint32_t size);
void SendCommond(CMD_Type cmd);
void StopSynthesis();//停止合成
void PauseSynthesis();//暂停合成
void RecoverySynthesis();//恢复合成
typedef enum
{
GB2312 = 0x00,
GBK = 0x01,
BIG5 = 0x02,
UNICODE = 0x03
} EncodingFormat_Type;//文本的编码格式
void SetEncodingFormat(EncodingFormat_Type encodingFormat);
typedef enum
{
ChipStatus_InitSuccessful = 0x4A,//初始化成功回传
ChipStatus_CorrectCommand = 0x41,//收到正确的命令帧回传
ChipStatus_ErrorCommand = 0x45,//收到不能识别命令帧回传
ChipStatus_Busy = 0x4E,//芯片忙碌状态回传
ChipStatus_Idle = 0x4F//芯片空闲状态回传
} ChipStatus_Type;//芯片回传
uint8_t ChipStatus;
typedef enum
{
Style_Single,//?为 0,一字一顿的风格
Style_Continue//?为 1,正常合成
} Style_Type; //合成风格设置 [f?]
void SetStyle(Style_Type style);
typedef enum
{
Language_Auto,//? 为 0,自动判断语种
Language_Chinese,//? 为 1,阿拉伯数字、度量单位、特殊符号等合成为中文
Language_English//? 为 2,阿拉伯数字、度量单位、特殊符号等合成为英文
} Language_Type; //合成语种设置 [g?]
void SetLanguage(Language_Type language);
typedef enum
{
Articulation_Auto,//? 为 0,自动判断单词发音方式
Articulation_Letter,//? 为 1,字母发音方式
Articulation_Word//? 为 2,单词发音方式
} Articulation_Type; //设置单词的发音方式 [h?]
void SetArticulation(Articulation_Type articulation);
typedef enum
{
Spell_Disable,//? 为 0,不识别汉语拼音
Spell_Enable//? 为 1,将“拼音+1 位数字(声调)”识别为汉语拼音,例如: hao3
} Spell_Type; //设置对汉语拼音的识别 [i?]
void SetSpell(Spell_Type spell);
typedef enum
{
Reader_XiaoYan = 3,//? 为 3,设置发音人为小燕(女声, 推荐发音人)
Reader_XuJiu = 51,//? 为 51,设置发音人为许久(男声, 推荐发音人)
Reader_XuDuo = 52,//? 为 52,设置发音人为许多(男声)
Reader_XiaoPing = 53,//? 为 53,设置发音人为小萍(女声)
Reader_DonaldDuck = 54,//? 为 54,设置发音人为唐老鸭(效果器)
Reader_XuXiaoBao = 55//? 为 55,设置发音人为许小宝(女童声)
} Reader_Type;//选择发音人 [m?]
void SetReader(Reader_Type reader);
typedef enum
{
NumberHandle_Auto,//? 为 0,自动判断
NumberHandle_Number,//? 为 1,数字作号码处理
NumberHandle_Value//? 为 2,数字作数值处理
} NumberHandle_Type; //设置数字处理策略 [n?]
void SetNumberHandle(NumberHandle_Type numberHandle);
typedef enum
{
ZeroPronunciation_Zero,//? 为 0,读成“zero
ZeroPronunciation_O//? 为 1,读成“欧”音
} ZeroPronunciation_Type; //数字“0”在读 作英文、号码时 的读法 [o?]
void SetZeroPronunciation(ZeroPronunciation_Type zeroPronunciation);
typedef enum
{
NamePronunciation_Auto,//? 为 0,自动判断姓氏读音
NamePronunciation_Constraint//? 为 1,强制使用姓氏读音规则
} NamePronunciation_Type; //设置姓名读音 策略 [r?]
void SetNamePronunciation(NamePronunciation_Type namePronunciation);
void SetSpeed(int speed);//设置语速 [s?] ? 为语速值,取值:0~10
void SetIntonation(int intonation);//设置语调 [t?] ? 为语调值,取值:0~10
void SetVolume(int volume);//设置音量 [v?] ? 为音量值,取值:0~10
typedef enum
{
PromptTone_Disable,//? 为 0,不使用提示音
PromptTone_Enable//? 为 1,使用提示音
} PromptTone_Type; //设置提示音处理策略 [x?]
void SetPromptTone(PromptTone_Type promptTone);
typedef enum
{
OnePronunciation_Yao,//? 为 0,合成号码“1”时读成“幺
OnePronunciation_Yi//? 为 1,合成号码“1”时读成“一”
} OnePronunciation_Type; //设置号码中“1”的读法 [y?]
void SetOnePronunciation(OnePronunciation_Type onePronunciation);
typedef enum
{
Rhythm_Diasble,//? 为 0,“ *”和“#”读出符号
Rhythm_Enable//? 为 1,处理成韵律,“*”用于断词,“#”用于停顿
} Rhythm_Type; //是否使用韵律 标记“*”和“#” [z?]
void SetRhythm(Rhythm_Type rhythm);
void SetRestoreDefault();//恢复默认的合成参数 [d] 所有设置(除发音人设置、语种设置外)恢复为默认值
XFS5152CE(EncodingFormat_Type encodingFormat = GB2312);
void Begin(uint8_t addr = 0x50);
uint8_t GetChipStatus();
void TextCtrl(char c, int d);
private :
uint8_t I2C_Addr;
};
#endif
谢谢大家帮助
|