-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Beeper class to easy control buzzer
- Loading branch information
Showing
9 changed files
with
123 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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,66 @@ | ||
#include "Beeper.h" | ||
|
||
#include "Tools.h" | ||
|
||
void Beeper::tick() { | ||
uint32_t currentTime = millis(); | ||
switch (m_state) { | ||
case State::off: | ||
break; | ||
case State::on: | ||
if (m_timer <= currentTime) { | ||
m_pinState = !m_pinState; | ||
if (m_pinState) | ||
m_timer = currentTime + 100; | ||
else | ||
m_timer = currentTime + 900; | ||
} | ||
break; | ||
case State::single: | ||
if (m_timer <= currentTime) { | ||
m_state = State::off; | ||
m_pinState = false; | ||
} | ||
break; | ||
} | ||
|
||
if (m_pinState) | ||
analogWrite(m_pin, gSettings.beepVolume); | ||
else | ||
analogWrite(m_pin, 0); | ||
} | ||
|
||
void Beeper::setup() { | ||
pinMode(BEEPER, OUTPUT); | ||
analogWrite(BEEPER, 0); | ||
} | ||
|
||
void Beeper::beep() { | ||
m_state = State::single; | ||
|
||
m_pinState = true; | ||
m_timer = millis() + 100; | ||
analogWrite(m_pinState, gSettings.beepVolume); | ||
} | ||
|
||
void Beeper::start(bool silentStart) { | ||
m_state = State::on; | ||
|
||
if (silentStart) { | ||
m_pinState = false; | ||
m_timer = millis() + 1000; | ||
} else { | ||
m_pinState = true; | ||
m_timer = millis() + 100; | ||
} | ||
|
||
if (m_pinState) | ||
analogWrite(m_pin, gSettings.beepVolume); | ||
else | ||
analogWrite(m_pin, 0); | ||
} | ||
|
||
void Beeper::stop() { | ||
m_state = State::off; | ||
analogWrite(m_pinState, 0); | ||
} |
This file contains 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,25 @@ | ||
#pragma once | ||
|
||
#include <stdint.h> | ||
|
||
class Beeper { | ||
public: | ||
Beeper(uint8_t pin) : m_pin(pin) {} | ||
|
||
enum class State { off, on, single }; | ||
|
||
void beep(); | ||
|
||
// start beep every second | ||
void start(bool silentStart = false); | ||
void stop(); | ||
|
||
void setup(); | ||
void tick(); | ||
|
||
private: | ||
bool m_pinState = false; | ||
State m_state = State::off; | ||
uint8_t m_pin; | ||
uint32_t m_timer; | ||
}; |
This file contains 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 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 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 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 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 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 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