基于XFS5152CE语音模块I2C功能封装

[复制链接]
查看1197 | 回复5 | 2023-11-29 23:04:18 | 显示全部楼层 |阅读模式




                               
登录/注册后可看大图

XFS5152CE模块功能代码;
头文件:
  1. <blockquote>
复制代码
具体实现:
  1. #include "XFS.h"
  2. #include <Wire.h>
  3. #define DBG_TAG "MAIN"
  4. #include "log.h"

  5. #define XFS_DataHead (uint8_t)0xFD

  6. uint8_t I2C_Addr;
  7. uint8_t ChipStatus;
  8. struct XFS_Protocol_TypeDef DataPack;

  9. size_t foo( const char* restrict src, uint8_t* restrict dst, size_t dst_maxlen )
  10. {
  11.     size_t idx = 0;
  12.     for( ; src[idx] && dst_maxlen; ++idx )
  13.     {
  14.         if( idx%8 == 0 )
  15.             *dst = 0;
  16.         if( src[idx] != '0' )
  17.             *dst |= 1<<(7-idx%8);
  18.         if( idx%8 == 7 )
  19.             ++dst, --dst_maxlen;
  20.     }
  21.     return (idx+7)/8;
  22. }

  23. void XFS5152CE(uint8_t encodingFormat)
  24. {
  25.   DataPack.DataHead = XFS_DataHead;
  26.   DataPack.Length_HH = 0x00;
  27.   DataPack.Length_LL = 0x00;

  28.   DataPack.Commond = 0x00;
  29.   DataPack.EncodingFormat = encodingFormat;

  30.   ChipStatus = 0x00;
  31. }


  32. void Begin(uint8_t addr)
  33. {
  34.   I2C_Addr = addr;
  35.   XFS5152CE(GB2312);
  36.   begin();
  37. }

  38. uint8_t GetChipStatus()
  39. {
  40.   uint8_t AskState[4] = {0xFD,0x00,0x01,0x21};
  41.   beginTransmission(I2C_Addr);
  42.   write_len(AskState,4);
  43.   endTransmission();
  44.   bflb_mtimer_delay_ms(100);
  45.   requestFrom(I2C_Addr, 1);
  46.   while (available())
  47.   {
  48.     ChipStatus = readI2c();
  49.   }
  50.   
  51.   LOG_F("ChipStatus=%x\r\n", ChipStatus);
  52.   return ChipStatus;
  53. }


  54. bool IIC_WriteByte(uint8_t data)
  55. {
  56.   beginTransmission(I2C_Addr);
  57.   write_char(data);
  58.   endTransmission();
  59.   // if(endTransmission()!=0)            //发送结束信号
  60.   //  {
  61.   //         bflb_mtimer_delay_ms(10);
  62.   //         return false;
  63.   //   }
  64.     bflb_mtimer_delay_ms(10);
  65.     return true;  
  66. }

  67. void IIC_WriteBytes(uint8_t* buff, uint32_t size)
  68. {
  69.   for (uint32_t i = 0; i < size; i++)
  70.   {
  71.     IIC_WriteByte(buff[i]);
  72.   }
  73. }

  74. void StartSynthesis(const char* str)
  75. {
  76.   uint16_t size = strlen(str) + 2;
  77.   DataPack.Length_HH = highByte(size);
  78.   DataPack.Length_LL = lowByte(size);
  79.   DataPack.Commond = CMD_StartSynthesis;
  80.   DataPack.Text = str;

  81.   uint8_t dst[(strlen(DataPack.Text)-1+7)/8];
  82.   size_t len = foo(DataPack.Text, dst, sizeof(dst)/sizeof(*dst) );
  83.   IIC_WriteBytes((uint8_t*)&DataPack,5);
  84.   IIC_WriteBytes(DataPack.Text, strlen(str));
  85.   
  86. }


  87. // void StartSynthesis(String str)
  88. // {
  89. //   StartSynthesis((const char*)str.c_str());
  90. // }

  91. void SendCommond(CMD_Type cmd)
  92. {
  93.   DataPack.Length_HH = 0x00;
  94.   DataPack.Length_LL = 0x01;
  95.   DataPack.Commond = cmd;
  96.   
  97.   beginTransmission(I2C_Addr);
  98.   write_len((uint8_t*)&DataPack, 4);
  99.   endTransmission();
  100. }

  101. void StopSynthesis()
  102. {
  103.   SendCommond(CMD_StopSynthesis);
  104. }

  105. void PauseSynthesis()
  106. {
  107.   SendCommond(CMD_PauseSynthesis);
  108. }

  109. void RecoverySynthesis()
  110. {
  111.   SendCommond(CMD_RecoverySynthesis);
  112. }

  113. void TextCtrl(char c, int d)
  114. {
  115.   char str[10];
  116.   if (d != -1)
  117.     sprintf(str, "[%c%d]", c, d);
  118.   else
  119.     sprintf(str, "[%c]", c);
  120.   StartSynthesis(str);
  121. }

  122. void SetEncodingFormat(EncodingFormat_Type encodingFormat)
  123. {
  124.   DataPack.EncodingFormat = encodingFormat;
  125. }

  126. void SetStyle(Style_Type style)
  127. {
  128.   TextCtrl('f', style);
  129.   while(GetChipStatus() != ChipStatus_Idle)
  130.   {
  131.      bflb_mtimer_delay_ms(30);
  132.   }
  133. }

  134. void SetLanguage(Language_Type language)
  135. {
  136.   TextCtrl('g', language);
  137.   while(GetChipStatus() != ChipStatus_Idle)
  138.   {
  139.      bflb_mtimer_delay_ms(30);
  140.   }
  141. }

  142. void SetArticulation(Articulation_Type articulation)
  143. {
  144.   TextCtrl('h', articulation);
  145.   while(GetChipStatus() != ChipStatus_Idle)
  146.   {
  147.      bflb_mtimer_delay_ms(30);
  148.   }
  149. }

  150. void SetSpell(Spell_Type spell)
  151. {
  152.   TextCtrl('i', spell);
  153.   while(GetChipStatus() != ChipStatus_Idle)
  154.   {
  155.      bflb_mtimer_delay_ms(30);
  156.   }
  157. }

  158. void SetReader(Reader_Type reader)
  159. {
  160.   TextCtrl('m', reader);
  161.   while(GetChipStatus() != ChipStatus_Idle)
  162.   {
  163.      bflb_mtimer_delay_ms(30);
  164.   }
  165. }

  166. void SetNumberHandle(NumberHandle_Type numberHandle)
  167. {
  168.   TextCtrl('n', numberHandle);
  169.   while(GetChipStatus() != ChipStatus_Idle)
  170.   {
  171.      bflb_mtimer_delay_ms(30);
  172.   }
  173. }

  174. void SetZeroPronunciation(ZeroPronunciation_Type zeroPronunciation)
  175. {
  176.   TextCtrl('o', zeroPronunciation);
  177.   while(GetChipStatus() != ChipStatus_Idle)
  178.   {
  179.      bflb_mtimer_delay_ms(30);
  180.   }
  181. }


  182. void SetNamePronunciation(NamePronunciation_Type namePronunciation)
  183. {
  184.   TextCtrl('r', namePronunciation);
  185.   while(GetChipStatus() != ChipStatus_Idle)
  186.   {
  187.      bflb_mtimer_delay_ms(30);
  188.   }
  189. }

  190. void SetSpeed(int speed)
  191. {
  192.   // speed = constrain(speed, 0, 10);
  193.   TextCtrl('s', speed);
  194.   while(GetChipStatus() != ChipStatus_Idle)
  195.   {
  196.      bflb_mtimer_delay_ms(30);
  197.   }
  198. }

  199. void SetIntonation(int intonation)
  200. {
  201.   // intonation = constrain(intonation, 0, 10);
  202.   TextCtrl('t', intonation);
  203.   while(GetChipStatus() != ChipStatus_Idle)
  204.   {
  205.      bflb_mtimer_delay_ms(30);
  206.   }
  207. }

  208. void SetVolume(int volume)
  209. {
  210.   // volume = constrain(volume, 0, 10);
  211.   TextCtrl('v', volume);
  212.   while(GetChipStatus() != ChipStatus_Idle)
  213.   {
  214.      bflb_mtimer_delay_ms(30);
  215.   }
  216. }

  217. void SetPromptTone(PromptTone_Type promptTone)
  218. {
  219.   TextCtrl('x', promptTone);
  220.   while(GetChipStatus() != ChipStatus_Idle)
  221.   {
  222.      bflb_mtimer_delay_ms(30);
  223.   }
  224. }

  225. void SetOnePronunciation(OnePronunciation_Type onePronunciation)
  226. {
  227.   TextCtrl('y', onePronunciation);
  228.   while(GetChipStatus() != ChipStatus_Idle)
  229.   {
  230.      bflb_mtimer_delay_ms(30);
  231.   }
  232. }

  233. void SetRhythm(Rhythm_Type rhythm)
  234. {
  235.   TextCtrl('z', rhythm);
  236.   while(GetChipStatus() != ChipStatus_Idle)
  237.   {
  238.      bflb_mtimer_delay_ms(30);
  239.   }
  240. }


  241. void SetRestoreDefault()
  242. {
  243.   TextCtrl('d', -1);
  244.   while(GetChipStatus() != ChipStatus_Idle)
  245.   {
  246.      bflb_mtimer_delay_ms(30);
  247.   }
  248. }
复制代码


                               
登录/注册后可看大图

语音播报内容 文件头 speak.h,
该文件是为了方式播报内容乱码错乱。文件需要保存为 ANSI;

  1. char* hello[] = {"主人", "出门记得带手机和钥匙"};
复制代码





本帖被以下淘专辑推荐:

回复

使用道具 举报

lazy | 2023-11-30 09:30:24 | 显示全部楼层
挺好的
回复

使用道具 举报

干簧管 | 2023-11-30 09:54:16 | 显示全部楼层
虽然看不懂,但还是顶
回复 支持 反对

使用道具 举报

qwe2079282957 | 2023-11-30 11:24:31 | 显示全部楼层
学习
回复

使用道具 举报

496199544 | 2023-11-30 12:34:23 | 显示全部楼层
回复

使用道具 举报

干簧管 | 2023-12-4 15:41:09 | 显示全部楼层
HAO
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则