diff options
Diffstat (limited to 'lib/buzzer/buzzer.cpp')
| -rw-r--r-- | lib/buzzer/buzzer.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/buzzer/buzzer.cpp b/lib/buzzer/buzzer.cpp new file mode 100644 index 0000000..a83ec22 --- /dev/null +++ b/lib/buzzer/buzzer.cpp @@ -0,0 +1,37 @@ +#include <Arduino.h> +#include "settings.h" +#include "buzzer.h" + +#define BUZZER_CHANNEL 3 +#define BUZZER_RESOLUTION 8 + +BUZZER::BUZZER() +{ + ledcSetup(BUZZER_CHANNEL, 2000, BUZZER_RESOLUTION); + ledcAttachPin(BUZZER_PIN, BUZZER_CHANNEL); +} + +void BUZZER::playTone(uint16_t frequency, uint32_t duration) +{ + ledcWriteTone(BUZZER_CHANNEL, frequency); + if (duration > 0) + { + delay(duration); + stop(); + } +} + +void BUZZER::playMelody(const uint16_t melody[][2], size_t length) +{ + + for (size_t i = 0; i < length; i++) + { + playTone(melody[i][0], melody[i][1]); + delay(50); + } +} + +void BUZZER::stop() +{ + ledcWriteTone(BUZZER_CHANNEL, 0); +}
\ No newline at end of file |