forked from wled/WLED
-
-
Notifications
You must be signed in to change notification settings - Fork 134
New UserMod: Voice Control via DF2301Q #329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
troyhacks
wants to merge
6
commits into
MoonModules:mdev
Choose a base branch
from
troyhacks:DF2301Q_Voice_Control
base: mdev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+590
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9c5d475
Initial Commit
troyhacks 5d04c4c
I suppose I should make it compile into builds as a test.
troyhacks a743871
CodeRabbit Fixes
troyhacks 90bb119
Remove background task as there's no I2C concurrency protection in WL…
troyhacks 178b410
The delete voiceModule and voiceModule = nullptr statements have been…
troyhacks c4cf87a
Merge branch 'mdev' into pr/329
softhack007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| /*! | ||
| * @file DF2301Q.hpp | ||
| * @brief I2C interface for DF2301Q voice recognition module | ||
| * @note Uses Arduino Wire library for I2C communication | ||
| */ | ||
| #pragma once | ||
|
|
||
| #include <Wire.h> | ||
|
|
||
| #define DF2301Q_I2C_ADDR 0x64 | ||
| #define DF2301Q_I2C_REG_CMDID 0x02 | ||
| #define DF2301Q_I2C_REG_PLAY_CMDID 0x03 | ||
| #define DF2301Q_I2C_REG_SET_MUTE 0x04 | ||
| #define DF2301Q_I2C_REG_SET_VOLUME 0x05 | ||
| #define DF2301Q_I2C_REG_WAKE_TIME 0x06 | ||
|
|
||
| #define DF2301Q_POLL_INTERVAL_MS 100 | ||
|
|
||
| class DF2301Q { | ||
| public: | ||
| DF2301Q(uint8_t addr = DF2301Q_I2C_ADDR) | ||
| : _addr(addr), _detected(false), _lastCmd(0), _failCount(0) { } | ||
|
|
||
| // Check if device is present on I2C bus (with retry) | ||
| bool detect(uint8_t retries = 3, uint16_t delayMs = 100) { | ||
| for (uint8_t i = 0; i < retries; i++) { | ||
| Wire.beginTransmission(_addr); | ||
| uint8_t error = Wire.endTransmission(); | ||
|
|
||
| if (error == 0) { | ||
| _detected = true; | ||
| _failCount = 0; | ||
| return true; | ||
| } | ||
|
|
||
| if (i < retries - 1) { | ||
| delay(delayMs); | ||
| } | ||
| } | ||
| _detected = false; | ||
| return false; | ||
| } | ||
|
|
||
| // Quick check if module is still responding | ||
| bool ping() { | ||
| Wire.beginTransmission(_addr); | ||
| return (Wire.endTransmission() == 0); | ||
| } | ||
|
|
||
| // Mark module as lost (called when communication fails repeatedly) | ||
| void markLost() { | ||
| _detected = false; | ||
| } | ||
|
|
||
| bool isDetected() const { return _detected; } | ||
|
|
||
| // Poll for a voice command - call this from loop() | ||
| // Returns command ID if a new command was detected, 0 otherwise | ||
| uint8_t poll() { | ||
| if (!_detected) return 0; | ||
|
|
||
| uint8_t cmdID = 0; | ||
| if (readReg(DF2301Q_I2C_REG_CMDID, &cmdID)) { | ||
| _failCount = 0; // Reset on successful read | ||
|
|
||
| if (cmdID > 0 && cmdID != _lastCmd) { | ||
| _lastCmd = cmdID; | ||
| return cmdID; | ||
| } else if (cmdID == 0) { | ||
| // Reset lastCmd when no command pending, so same command can repeat | ||
| _lastCmd = 0; | ||
| } | ||
| } else { | ||
| // Track consecutive failures | ||
| _failCount++; | ||
| if (_failCount >= 10) { | ||
| _detected = false; // Mark as lost after 10 consecutive failures | ||
| } | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| uint8_t getFailCount() const { return _failCount; } | ||
|
|
||
| void playByCMDID(uint8_t cmdID) { | ||
| if (!_detected) return; | ||
| writeReg(DF2301Q_I2C_REG_PLAY_CMDID, cmdID); | ||
| } | ||
|
|
||
| uint8_t getWakeTime() { | ||
| if (!_detected) return 0; | ||
| uint8_t time = 0; | ||
| readReg(DF2301Q_I2C_REG_WAKE_TIME, &time); | ||
| return time; | ||
| } | ||
|
|
||
| void setWakeTime(uint8_t time) { | ||
| if (!_detected) return; | ||
| writeReg(DF2301Q_I2C_REG_WAKE_TIME, time); | ||
| } | ||
|
|
||
| void setVolume(uint8_t vol) { | ||
| if (!_detected) return; | ||
| writeReg(DF2301Q_I2C_REG_SET_VOLUME, vol); | ||
| } | ||
|
|
||
| void setMute(bool mute) { | ||
| if (!_detected) return; | ||
| writeReg(DF2301Q_I2C_REG_SET_MUTE, mute ? 1 : 0); | ||
| } | ||
|
|
||
| uint8_t getLastCommand() const { return _lastCmd; } | ||
|
|
||
| private: | ||
| bool writeReg(uint8_t reg, uint8_t value) { | ||
| Wire.beginTransmission(_addr); | ||
| Wire.write(reg); | ||
| Wire.write(value); | ||
| return (Wire.endTransmission() == 0); | ||
| } | ||
|
|
||
| bool readReg(uint8_t reg, uint8_t* value) { | ||
| Wire.beginTransmission(_addr); | ||
| Wire.write(reg); | ||
| if (Wire.endTransmission(false) != 0) { | ||
| return false; | ||
| } | ||
|
|
||
| if (Wire.requestFrom(_addr, (uint8_t)1) != 1) { | ||
| return false; | ||
| } | ||
|
|
||
| *value = Wire.read(); | ||
| return true; | ||
| } | ||
|
|
||
| uint8_t _addr; | ||
| bool _detected; | ||
| uint8_t _lastCmd; | ||
| uint8_t _failCount; | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.