Skip to content

Commit

Permalink
Merge pull request #8994 from hello-world-dot-c/development
Browse files Browse the repository at this point in the history
Adding frequency output mode for buzzer
  • Loading branch information
arendst authored Sep 15, 2020
2 parents 119e657 + 51688c1 commit 42ff135
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion tasmota/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ typedef union { // Restricted by MISRA-C Rule 18.4 bu
uint32_t teleinfo_rawdata : 1; // bit 26 (v8.4.0.2) - SetOption108 - enable Teleinfo + Tasmota Energy device (0) or Teleinfo raw data only (1)
uint32_t alexa_gen_1 : 1; // bit 27 (v8.4.0.3) - SetOption109 - Alexa gen1 mode - if you only have Echo Dot 2nd gen devices
uint32_t zb_disable_autobind : 1; // bit 28 (v8.5.0.1) - SetOption110 - disable Zigbee auto-config when pairing new devices
uint32_t spare29 : 1; // bit 29
uint32_t buzzer_freq_mode : 1; // bit 29 (v8.5.0.1) - SetOption111 - Use frequency output for buzzer pin instead of on/off signal
uint32_t spare30 : 1; // bit 30
uint32_t spare31 : 1; // bit 31
};
Expand Down
42 changes: 35 additions & 7 deletions tasmota/xdrv_24_buzzer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,36 @@ struct BUZZER {
uint8_t inverted = 0; // Buzzer inverted flag (1 = (0 = On, 1 = Off))
uint8_t count = 0; // Number of buzzes
uint8_t mode = 0; // Buzzer mode (0 = regular, 1 = infinite, 2 = follow LED)
uint8_t freq_mode = 0; // Output mode (0 = regular, 1 = using frequency output)
uint8_t set[2];
uint8_t duration;
uint8_t state = 0;
} Buzzer;

/*********************************************************************************************/

void BuzzerOff(void)
void BuzzerSet(uint8_t state)
{
DigitalWrite(GPIO_BUZZER, 0, Buzzer.inverted); // Buzzer Off
if (Buzzer.inverted) {
state = !state;
}

if (Buzzer.freq_mode == 1) {
static uint8_t last_state = 0;
if (last_state != state) {
if (state) {
analogWrite(Pin(GPIO_BUZZER, 0), Settings.pwm_range / 2); // set 50% duty cycle for frequency output
}
else {
analogWrite(Pin(GPIO_BUZZER, 0), 0); // set 0% (or 100% for inverted PWM) duty cycle which turns off frequency output either way
}
last_state = state;
}
}
else {
DigitalWrite(GPIO_BUZZER, 0, state); // Buzzer On/Off
}

}

//void BuzzerBeep(uint32_t count = 1, uint32_t on = 1, uint32_t off = 1, uint32_t tune = 0, uint32_t mode = 0);
Expand Down Expand Up @@ -69,19 +89,27 @@ void BuzzerBeep(uint32_t count, uint32_t on, uint32_t off, uint32_t tune, uint32
}
Buzzer.count = count * 2; // Start buzzer

AddLog_P2(LOG_LEVEL_DEBUG, PSTR("BUZ: %d(%d),%d,%d,0x%08X(0x%08X)"), count, Buzzer.count, on, off, tune, Buzzer.tune);
// We can use PWM mode for buzzer output if enabled.
if (Settings.flag4.buzzer_freq_mode) { // SetOption111 - Enable frequency output mode for buzzer
Buzzer.freq_mode = 1;
}
else {
Buzzer.freq_mode = 0;
}

AddLog_P2(LOG_LEVEL_DEBUG, PSTR("BUZ: %d(%d),%d,%d,0x%08X(0x%08X),%d"), count, Buzzer.count, on, off, tune, Buzzer.tune, Buzzer.freq_mode);

Buzzer.enable = (Buzzer.count > 0);
if (!Buzzer.enable) {
BuzzerOff();
BuzzerSet(0);
}
}

void BuzzerSetStateToLed(uint32_t state)
{
if (Buzzer.enable && (2 == Buzzer.mode)) {
Buzzer.state = (state != 0);
DigitalWrite(GPIO_BUZZER, 0, (Buzzer.inverted) ? !Buzzer.state : Buzzer.state);
BuzzerSet(Buzzer.state);
}
}

Expand Down Expand Up @@ -113,7 +141,7 @@ void BuzzerInit(void)
{
if (PinUsed(GPIO_BUZZER)) {
pinMode(Pin(GPIO_BUZZER), OUTPUT);
BuzzerOff();
BuzzerSet(0);
} else {
Buzzer.active = false;
}
Expand All @@ -140,7 +168,7 @@ void BuzzerEvery100mSec(void)
Buzzer.duration = Buzzer.set[Buzzer.state];
}
}
DigitalWrite(GPIO_BUZZER, 0, (Buzzer.inverted) ? !Buzzer.state : Buzzer.state);
BuzzerSet(Buzzer.state);
} else {
Buzzer.enable = false;
}
Expand Down

0 comments on commit 42ff135

Please sign in to comment.