XFS5152CE模块功能代码; 头文件: 具体实现:
- #include "XFS.h"
- #include <Wire.h>
- #define DBG_TAG "MAIN"
- #include "log.h"
- #define XFS_DataHead (uint8_t)0xFD
- uint8_t I2C_Addr;
- uint8_t ChipStatus;
- struct XFS_Protocol_TypeDef DataPack;
- size_t foo( const char* restrict src, uint8_t* restrict dst, size_t dst_maxlen )
- {
- size_t idx = 0;
- for( ; src[idx] && dst_maxlen; ++idx )
- {
- if( idx%8 == 0 )
- *dst = 0;
- if( src[idx] != '0' )
- *dst |= 1<<(7-idx%8);
- if( idx%8 == 7 )
- ++dst, --dst_maxlen;
- }
- return (idx+7)/8;
- }
- void XFS5152CE(uint8_t encodingFormat)
- {
- DataPack.DataHead = XFS_DataHead;
- DataPack.Length_HH = 0x00;
- DataPack.Length_LL = 0x00;
- DataPack.Commond = 0x00;
- DataPack.EncodingFormat = encodingFormat;
- ChipStatus = 0x00;
- }
- void Begin(uint8_t addr)
- {
- I2C_Addr = addr;
- XFS5152CE(GB2312);
- begin();
- }
- uint8_t GetChipStatus()
- {
- uint8_t AskState[4] = {0xFD,0x00,0x01,0x21};
- beginTransmission(I2C_Addr);
- write_len(AskState,4);
- endTransmission();
- bflb_mtimer_delay_ms(100);
- requestFrom(I2C_Addr, 1);
- while (available())
- {
- ChipStatus = readI2c();
- }
-
- LOG_F("ChipStatus=%x\r\n", ChipStatus);
- return ChipStatus;
- }
- bool IIC_WriteByte(uint8_t data)
- {
- beginTransmission(I2C_Addr);
- write_char(data);
- endTransmission();
- // if(endTransmission()!=0) //发送结束信号
- // {
- // bflb_mtimer_delay_ms(10);
- // return false;
- // }
- bflb_mtimer_delay_ms(10);
- return true;
- }
- void IIC_WriteBytes(uint8_t* buff, uint32_t size)
- {
- for (uint32_t i = 0; i < size; i++)
- {
- IIC_WriteByte(buff[i]);
- }
- }
- void 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;
- uint8_t dst[(strlen(DataPack.Text)-1+7)/8];
- size_t len = foo(DataPack.Text, dst, sizeof(dst)/sizeof(*dst) );
- IIC_WriteBytes((uint8_t*)&DataPack,5);
- IIC_WriteBytes(DataPack.Text, strlen(str));
-
- }
- // void StartSynthesis(String str)
- // {
- // StartSynthesis((const char*)str.c_str());
- // }
- void SendCommond(CMD_Type cmd)
- {
- DataPack.Length_HH = 0x00;
- DataPack.Length_LL = 0x01;
- DataPack.Commond = cmd;
-
- beginTransmission(I2C_Addr);
- write_len((uint8_t*)&DataPack, 4);
- endTransmission();
- }
- void StopSynthesis()
- {
- SendCommond(CMD_StopSynthesis);
- }
- void PauseSynthesis()
- {
- SendCommond(CMD_PauseSynthesis);
- }
- void RecoverySynthesis()
- {
- SendCommond(CMD_RecoverySynthesis);
- }
- void 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 SetEncodingFormat(EncodingFormat_Type encodingFormat)
- {
- DataPack.EncodingFormat = encodingFormat;
- }
- void SetStyle(Style_Type style)
- {
- TextCtrl('f', style);
- while(GetChipStatus() != ChipStatus_Idle)
- {
- bflb_mtimer_delay_ms(30);
- }
- }
- void SetLanguage(Language_Type language)
- {
- TextCtrl('g', language);
- while(GetChipStatus() != ChipStatus_Idle)
- {
- bflb_mtimer_delay_ms(30);
- }
- }
- void SetArticulation(Articulation_Type articulation)
- {
- TextCtrl('h', articulation);
- while(GetChipStatus() != ChipStatus_Idle)
- {
- bflb_mtimer_delay_ms(30);
- }
- }
- void SetSpell(Spell_Type spell)
- {
- TextCtrl('i', spell);
- while(GetChipStatus() != ChipStatus_Idle)
- {
- bflb_mtimer_delay_ms(30);
- }
- }
- void SetReader(Reader_Type reader)
- {
- TextCtrl('m', reader);
- while(GetChipStatus() != ChipStatus_Idle)
- {
- bflb_mtimer_delay_ms(30);
- }
- }
- void SetNumberHandle(NumberHandle_Type numberHandle)
- {
- TextCtrl('n', numberHandle);
- while(GetChipStatus() != ChipStatus_Idle)
- {
- bflb_mtimer_delay_ms(30);
- }
- }
- void SetZeroPronunciation(ZeroPronunciation_Type zeroPronunciation)
- {
- TextCtrl('o', zeroPronunciation);
- while(GetChipStatus() != ChipStatus_Idle)
- {
- bflb_mtimer_delay_ms(30);
- }
- }
- void SetNamePronunciation(NamePronunciation_Type namePronunciation)
- {
- TextCtrl('r', namePronunciation);
- while(GetChipStatus() != ChipStatus_Idle)
- {
- bflb_mtimer_delay_ms(30);
- }
- }
- void SetSpeed(int speed)
- {
- // speed = constrain(speed, 0, 10);
- TextCtrl('s', speed);
- while(GetChipStatus() != ChipStatus_Idle)
- {
- bflb_mtimer_delay_ms(30);
- }
- }
- void SetIntonation(int intonation)
- {
- // intonation = constrain(intonation, 0, 10);
- TextCtrl('t', intonation);
- while(GetChipStatus() != ChipStatus_Idle)
- {
- bflb_mtimer_delay_ms(30);
- }
- }
- void SetVolume(int volume)
- {
- // volume = constrain(volume, 0, 10);
- TextCtrl('v', volume);
- while(GetChipStatus() != ChipStatus_Idle)
- {
- bflb_mtimer_delay_ms(30);
- }
- }
- void SetPromptTone(PromptTone_Type promptTone)
- {
- TextCtrl('x', promptTone);
- while(GetChipStatus() != ChipStatus_Idle)
- {
- bflb_mtimer_delay_ms(30);
- }
- }
- void SetOnePronunciation(OnePronunciation_Type onePronunciation)
- {
- TextCtrl('y', onePronunciation);
- while(GetChipStatus() != ChipStatus_Idle)
- {
- bflb_mtimer_delay_ms(30);
- }
- }
- void SetRhythm(Rhythm_Type rhythm)
- {
- TextCtrl('z', rhythm);
- while(GetChipStatus() != ChipStatus_Idle)
- {
- bflb_mtimer_delay_ms(30);
- }
- }
- void SetRestoreDefault()
- {
- TextCtrl('d', -1);
- while(GetChipStatus() != ChipStatus_Idle)
- {
- bflb_mtimer_delay_ms(30);
- }
- }
复制代码语音播报内容 文件头 speak.h,
该文件是为了方式播报内容乱码错乱。文件需要保存为 ANSI;
- char* hello[] = {"主人", "出门记得带手机和钥匙"};
复制代码
|