Repository navigation
feat(button): Add AC frequency detection switch support (Moes MS-104B) #2658
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
igorufox
wants to merge
3
commits into
xoseperez:dev
Choose a base branch
from
igorufox:ac-frequency-switch
base: dev
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.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
|
|
||
| AC FREQUENCY DETECTION SWITCH MODULE | ||
|
|
||
| */ | ||
|
|
||
| #include "ac_switch.h" | ||
| #include "espurna.h" | ||
|
|
||
| namespace espurna { | ||
|
|
||
| AcSwitch::AcSwitch(uint8_t pin, unsigned long timeout_ms, unsigned long min_pulses, int frequency) : | ||
| _pin(pin), | ||
| _timeout_ms(timeout_ms), | ||
| _min_pulses(min_pulses) | ||
| { | ||
| if (frequency == 50) { | ||
| // 10000us period for 50Hz with CHANGE interrupt. Allow 3.5% tolerance. | ||
| _min_filter_period = 9650; | ||
| _max_filter_period = 10350; | ||
| } else if (frequency == 60) { | ||
| // ~8333us period for 60Hz. Allow 3.5% tolerance. | ||
| _min_filter_period = 8041; | ||
| _max_filter_period = 8625; | ||
| } | ||
|
|
||
| // INPUT_PULLUP is usually safer if the optocoupler pulls to ground. | ||
| // If the schematic is different, INPUT might be required. | ||
| pinMode(_pin, INPUT_PULLUP); | ||
| attachInterrupt(_pin, [this]() { this->_isr(); }, CHANGE); | ||
| } | ||
|
|
||
| AcSwitch::~AcSwitch() { | ||
| detachInterrupt(_pin); | ||
| } | ||
|
|
||
| bool AcSwitch::loop() { | ||
| const bool debug_enabled = getSetting("btnAcDbg", false); | ||
| unsigned long now = millis(); | ||
| if (now - _last_check < _timeout_ms) { | ||
| return false; | ||
| } | ||
|
|
||
| noInterrupts(); | ||
| unsigned long count = _pulses; | ||
| _pulses = 0; | ||
| interrupts(); | ||
|
|
||
| // Debug output every 5 seconds | ||
| if (debug_enabled && (now - _last_debug > 5000)) { | ||
| noInterrupts(); | ||
| unsigned long d_count = _debug_pulse_count; | ||
| unsigned long d_valid = _debug_valid_pulse_count; | ||
| unsigned long d_min = _min_period; | ||
| unsigned long d_max = _max_period; | ||
| unsigned long d_sum = _sum_period; | ||
| unsigned long d_p_count = _period_count; | ||
|
|
||
| // Reset stats | ||
| _debug_pulse_count = 0; | ||
| _debug_valid_pulse_count = 0; | ||
| _min_period = -1UL; | ||
| _max_period = 0; | ||
| _sum_period = 0; | ||
| _period_count = 0; | ||
| interrupts(); | ||
|
|
||
| unsigned long avg = d_p_count ? (d_sum / d_p_count) : 0; | ||
| DEBUG_MSG_P(PSTR("[AC] Pin %u: pulses=%lu valid=%lu period(us) min=%lu max=%lu avg=%lu\n"), | ||
| _pin, d_count, d_valid, (d_min == -1UL ? 0 : d_min), d_max, avg); | ||
|
|
||
| _last_debug = now; | ||
| } | ||
|
|
||
| bool changed = false; | ||
| bool new_state = (count >= _min_pulses); | ||
|
|
||
| if (new_state == _candidate_state) { | ||
| if (_latch < 10) _latch++; | ||
| } else { | ||
| _candidate_state = new_state; | ||
| _latch = 0; | ||
| } | ||
|
|
||
| if (_latch >= 1 && _candidate_state != _state) { | ||
| if (debug_enabled) { | ||
| DEBUG_MSG_P(PSTR("[AC] Pin %u: State changed %s -> %s (count: %lu, min: %lu)\n"), | ||
| _pin, _state ? "ON" : "OFF", _candidate_state ? "ON" : "OFF", count, _min_pulses); | ||
| } | ||
| _state = _candidate_state; | ||
| changed = true; | ||
| } | ||
|
|
||
| _last_check = now; | ||
| return changed; | ||
| } | ||
|
|
||
| bool AcSwitch::state() const { | ||
| return _state; | ||
| } | ||
|
|
||
| void IRAM_ATTR AcSwitch::_isr() { | ||
| unsigned long now = micros(); | ||
| unsigned long delta = now - _last_isr_time; | ||
|
|
||
| // Filter out noise (pulses shorter than 5ms) | ||
| if (_last_isr_time != 0 && delta < 5000) { | ||
| return; | ||
| } | ||
|
|
||
| if (_last_isr_time != 0) { | ||
| if (delta < _min_period) _min_period = delta; | ||
| if (delta > _max_period) _max_period = delta; | ||
| _sum_period += delta; | ||
| _period_count++; | ||
| } | ||
| _last_isr_time = now; | ||
| ++_debug_pulse_count; | ||
|
|
||
| // If frequency filter is disabled, count all non-noise pulses | ||
| if (_min_filter_period == 0) { | ||
| ++_pulses; | ||
| ++_debug_valid_pulse_count; | ||
| return; | ||
| } | ||
|
|
||
| // Count only pulses that are within the expected frequency range | ||
| if ((delta >= _min_filter_period) && (delta <= _max_filter_period)) { | ||
| ++_pulses; | ||
| ++_debug_valid_pulse_count; | ||
| } | ||
| } | ||
|
|
||
| } // namespace espurna | ||
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,52 @@ | ||
| /* | ||
|
|
||
| AC FREQUENCY DETECTION SWITCH MODULE | ||
|
|
||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <Arduino.h> | ||
| #include <FunctionalInterrupt.h> | ||
|
|
||
| namespace espurna { | ||
|
|
||
| class AcSwitch { | ||
| public: | ||
| // pin: GPIO pin | ||
| // timeout_ms: time window for pulse counting (default 100 ms) | ||
| // min_pulses: minimum number of pulses for ON state (default 5) | ||
| // frequency: target AC frequency (50 or 60). 0 to disable frequency filter. | ||
| AcSwitch(uint8_t pin, unsigned long timeout_ms = 100, unsigned long min_pulses = 5, int frequency = 0); | ||
| ~AcSwitch(); | ||
|
|
||
| bool loop(); | ||
| bool state() const; | ||
|
|
||
| private: | ||
| void IRAM_ATTR _isr(); | ||
|
|
||
| uint8_t _pin; | ||
| unsigned long _timeout_ms; | ||
| unsigned long _min_pulses; | ||
| uint16_t _min_filter_period { 0 }; | ||
| uint16_t _max_filter_period { 0 }; | ||
|
|
||
| volatile unsigned long _pulses{0}; | ||
| unsigned long _last_check{0}; | ||
| bool _state{false}; | ||
| bool _candidate_state{false}; | ||
| unsigned int _latch{0}; | ||
|
|
||
| // Statistics for debugging | ||
| unsigned long _last_debug{0}; | ||
| volatile unsigned long _last_isr_time{0}; | ||
| volatile unsigned long _min_period{-1UL}; | ||
| volatile unsigned long _max_period{0}; | ||
| volatile unsigned long _sum_period{0}; | ||
| volatile unsigned long _period_count{0}; | ||
| volatile unsigned long _debug_pulse_count{0}; | ||
| volatile unsigned long _debug_valid_pulse_count{0}; | ||
| }; | ||
|
|
||
| } // namespace espurna |
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
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.