Emakefun Speech Recognizer 2.1
载入中...
搜索中...
未找到
speech_recognizer.h
1#ifndef EMAKEFUN_SPEECH_RECOGNIZER_H_
2#define EMAKEFUN_SPEECH_RECOGNIZER_H_
3
4#include <WString.h>
5#include <Wire.h>
6#include <stdint.h>
7
8namespace emakefun {
9
10/**
11 * @brief 语音识别模块类
12 */
14 public:
15 /**
16 * @brief 语音识别模块默认I2C地址
17 */
18 static constexpr uint8_t kDefaultI2cAddress = 0x30;
19
20 /**
21 * @brief 每条关键词最大字符数
22 */
23 static constexpr uint8_t kMaxKeywordDataBytes = 50;
24
25 /**
26 * @enum ErrorCode
27 * @brief 错误码
28 */
29 enum ErrorCode : uint32_t {
30 kOK = 0, /**< 0:成功 */
31 kI2cDataTooLongToFitInTransmitBuffer = 1, /**< 1:I2C数据太长,无法装入传输缓冲区 */
32 kI2cReceivedNackOnTransmitOfAddress = 2, /**< 2:在I2C发送地址时收到NACK */
33 kI2cReceivedNackOnTransmitOfData = 3, /**< 3:在I2C发送数据时收到NACK */
34 kI2cOtherError = 4, /**< 4:其他I2C错误 */
35 kI2cTimeout = 5, /**< 5:I2C通讯超时 */
36 kInvalidParameter = 6, /**< 6:参数错误 */
37 kUnknownError = 7, /**< 7: 未知错误*/
38 };
39
40 /**
41 * @brief 识别模式
42 */
43 enum RecognitionMode : uint8_t {
44 kRecognitionAuto, ///< 自动识别模式
45 kButtonTrigger, ///< 按键触发识别模式
46 kKeywordTrigger, ///< 关键词触发识别模式
47 kKeywordOrButtonTrigger, ///< 按键或关键词触发识别模式
48 };
49
50 /**
51 * @brief 事件类型
52 */
53
54 enum Event : uint8_t {
55 kEventNone = 0, ///< 无事件
56 kEventStartWaitingForTrigger, ///< 开始等待触发
57 kEventButtonTriggered, ///< 被按键触发
58 kEventKeywordTriggered, ///< 被关键词触发
59 kEventStartRecognizing, ///< 开始识别
60 kEventSpeechRecognized, ///< 识别成功
62 };
63
64 /**
65 * @brief 构造函数
66 * @param i2c_address 语音识别模块I2C地址,默认为0x30
67 */
68 explicit SpeechRecognizer(TwoWire& wire = Wire, const uint8_t i2c_address = kDefaultI2cAddress);
69
70 /**
71 * @brief 初始化函数
72 * @param[in] wire Wire对象,用于I2C通讯,可选,默认使用Arduino标准的Wire对象进行I2C通讯
73 * @return 初始化结果
74 * @retval true 成功
75 * @retval false 失败,如I2C无法与语音识别模块通讯
76 */
78
79 /**
80 * @brief 设置识别模式
81 * @param[in] recognition_mode 识别模式,参考枚举: @ref RecognitionMode
82 */
83 void SetRecognitionMode(const RecognitionMode recognition_mode);
84
85 /**
86 * @brief 设置识别超时时间, 对自动触发模式( @ref kRecognitionAuto )无效
87 * @param[in] timeout_ms 超时时间,单位毫秒
88 */
89 void SetTimeout(const uint32_t timeout_ms);
90
91 /**
92 * @brief 添加语音识别关键词
93 * @details 最多可以添加50个词,每个词最大长度为50个字节
94 * @param[in] index 索引,范围 0 ~ 255,多个词可以共用一个索引,当在 [关键字触发识别模式]( @ref kKeywordTrigger
95 * ) 或 [按键或关键词触发识别模式]( @ref kKeywordOrButtonTrigger ) 下,索引 0 会被当做关键词
96 * @param[in] keyword 关键词,String类型,最大长度为50 ( 参考定义: @ref kMaxKeywordDataBytes ) 个字节
97 */
98 void AddKeyword(const uint8_t index, const String& keyword);
99
100 /**
101 * @brief 进行语音识别
102 * @details 在loop函数中<b>循环调用</b>该函数以推进语音识别模块的工作,
103 * 调用该函数后可以获取识别结果,可以通过( @ref GetEvent )获取事件
104 * @return 关键词的索引值
105 * @retval <0 未识别到结果
106 * @retval >=0 识别到关键词的索引值,对应 @ref AddKeyword 时设置的关键词索引
107 */
108 int16_t Recognize();
109
110 /**
111 * @brief 获取当前事件
112 * @return 事件类型,参考枚举: @ref Event
113 */
114 Event GetEvent();
115
116 private:
117 SpeechRecognizer(const SpeechRecognizer&) = delete;
118 SpeechRecognizer& operator=(const SpeechRecognizer&) = delete;
119 ErrorCode WaitUntilIdle();
120
121 TwoWire& wire_ = Wire;
122 const uint8_t i2c_address_ = kDefaultI2cAddress;
123};
124} // namespace emakefun
125
126#endif
语音识别模块类
Definition speech_recognizer.h:13
static constexpr uint8_t kDefaultI2cAddress
语音识别模块默认I2C地址
Definition speech_recognizer.h:18
Event GetEvent()
获取当前事件
Definition speech_recognizer.cpp:111
RecognitionMode
识别模式
Definition speech_recognizer.h:43
@ kButtonTrigger
按键触发识别模式
Definition speech_recognizer.h:45
@ kKeywordTrigger
关键词触发识别模式
Definition speech_recognizer.h:46
@ kRecognitionAuto
自动识别模式
Definition speech_recognizer.h:44
@ kKeywordOrButtonTrigger
按键或关键词触发识别模式
Definition speech_recognizer.h:47
static constexpr uint8_t kMaxKeywordDataBytes
每条关键词最大字符数
Definition speech_recognizer.h:23
int16_t Recognize()
进行语音识别
Definition speech_recognizer.cpp:94
ErrorCode Initialize()
初始化函数
Definition speech_recognizer.cpp:44
void AddKeyword(const uint8_t index, const String &keyword)
添加语音识别关键词
Definition speech_recognizer.cpp:71
ErrorCode
错误码
Definition speech_recognizer.h:29
@ kI2cReceivedNackOnTransmitOfAddress
Definition speech_recognizer.h:32
@ kI2cOtherError
Definition speech_recognizer.h:34
@ kInvalidParameter
Definition speech_recognizer.h:36
@ kOK
Definition speech_recognizer.h:30
@ kI2cDataTooLongToFitInTransmitBuffer
Definition speech_recognizer.h:31
@ kUnknownError
Definition speech_recognizer.h:37
@ kI2cReceivedNackOnTransmitOfData
Definition speech_recognizer.h:33
@ kI2cTimeout
Definition speech_recognizer.h:35
void SetRecognitionMode(const RecognitionMode recognition_mode)
设置识别模式
Definition speech_recognizer.cpp:55
void SetTimeout(const uint32_t timeout_ms)
设置识别超时时间, 对自动触发模式( kRecognitionAuto )无效
Definition speech_recognizer.cpp:63
Event
事件类型
Definition speech_recognizer.h:54
@ kEventNone
无事件
Definition speech_recognizer.h:55
@ kEventStartWaitingForTrigger
开始等待触发
Definition speech_recognizer.h:56
@ kEventStartRecognizing
开始识别
Definition speech_recognizer.h:59
@ kEventSpeechRecognized
识别成功
Definition speech_recognizer.h:60
@ kEventSpeechRecognitionTimedOut
识别超时
Definition speech_recognizer.h:61
@ kEventButtonTriggered
被按键触发
Definition speech_recognizer.h:57
@ kEventKeywordTriggered
被关键词触发
Definition speech_recognizer.h:58