From 05762763e2b54b4d2f640575063942886bd2a46e Mon Sep 17 00:00:00 2001 From: Michael Ehrenreich Date: Fri, 24 Jun 2022 03:40:15 +0200 Subject: [PATCH] Allow controlling the buzzer from software --- main/CMakeLists.txt | 2 ++ main/buzzer.cpp | 72 ++++++++++++++++++++++++++++++++++++++ main/buzzer.h | 7 ++++ main/debuginputhandler.cpp | 4 +++ main/main.cpp | 3 ++ 5 files changed, 88 insertions(+) create mode 100644 main/buzzer.cpp create mode 100644 main/buzzer.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 5227b44..87fed7f 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -48,6 +48,7 @@ set(headers bobbyschedulertask.h bobbytypesafeenum.h buildserver.h + buzzer.h can.h changevaluedisplay_bluetoothmode.h changevaluedisplay_bobbyquickactions.h @@ -301,6 +302,7 @@ set(sources bobbyhupe.cpp bobbyquickactions.cpp buildserver.cpp + buzzer.cpp can.cpp changevaluedisplay_bluetoothmode.cpp changevaluedisplay_bobbyquickactions.cpp diff --git a/main/buzzer.cpp b/main/buzzer.cpp new file mode 100644 index 0000000..ef3b0cc --- /dev/null +++ b/main/buzzer.cpp @@ -0,0 +1,72 @@ +/* LEDC (LED Controller) basic example + This example code is in the Public Domain (or CC0 licensed, at your option.) + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ + +// esp-idf includes +#include +#include "driver/ledc.h" +#include "esp_err.h" + +#include "buzzer.h" + +namespace buzzer +{ +constexpr const char TAG[] = "BUZZER"; + +#define LEDC_TIMER LEDC_TIMER_0 +#define LEDC_MODE LEDC_LOW_SPEED_MODE +#define LEDC_OUTPUT_IO (0) // Define the output GPIO +#define LEDC_CHANNEL LEDC_CHANNEL_0 +#define LEDC_DUTY_RES LEDC_TIMER_13_BIT // Set duty resolution to 13 bits +#define LEDC_DUTY (4095) // Set duty to 50%. ((2 ** 13) - 1) * 50% = 4095 +#define LEDC_FREQUENCY (4000) // Frequency in Hertz. Set frequency at 4 kHz + +void init() +{ + // Prepare and then apply the LEDC PWM timer configuration + ledc_timer_config_t ledc_timer = { + .speed_mode = LEDC_MODE, + .duty_resolution = LEDC_DUTY_RES, + .timer_num = LEDC_TIMER, + .freq_hz = LEDC_FREQUENCY, // Set output frequency at 4 kHz + .clk_cfg = LEDC_AUTO_CLK + }; + if (auto result = ledc_timer_config(&ledc_timer) != ESP_OK) { + ESP_LOGW(TAG, "ledc_timer_config() failed with %s", esp_err_to_name(result)); + return; + } + + // Prepare and then apply the LEDC PWM channel configuration + ledc_channel_config_t ledc_channel = { + .gpio_num = LEDC_OUTPUT_IO, + .speed_mode = LEDC_MODE, + .channel = LEDC_CHANNEL, + .intr_type = LEDC_INTR_DISABLE, + .timer_sel = LEDC_TIMER, + .duty = 0, // Set duty to 0% + .hpoint = 0 + }; + if (auto result = ledc_channel_config(&ledc_channel) != ESP_OK) { + ESP_LOGW(TAG, "ledc_channel_config() failed with %s", esp_err_to_name(result)); + return; + } +} + +void set_frequency(int frequency) +{ + if (frequency != 0) + { + ledc_set_freq(LEDC_MODE, LEDC_TIMER, frequency); + ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, LEDC_DUTY); + ledc_update_duty(LEDC_MODE, LEDC_CHANNEL); + } + else + { + ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, 0); + ledc_update_duty(LEDC_MODE, LEDC_CHANNEL); + } +} +} diff --git a/main/buzzer.h b/main/buzzer.h new file mode 100644 index 0000000..c19310e --- /dev/null +++ b/main/buzzer.h @@ -0,0 +1,7 @@ +#pragma once + +namespace buzzer +{ +void init(); +void set_frequency(int frequency); +} // namespace buzzer diff --git a/main/debuginputhandler.cpp b/main/debuginputhandler.cpp index d82c104..79044fd 100644 --- a/main/debuginputhandler.cpp +++ b/main/debuginputhandler.cpp @@ -15,6 +15,7 @@ #include // local includes +#include "buzzer.h" #include "globals.h" #include "utils.h" #include "bobbybuttons.h" @@ -22,6 +23,7 @@ namespace { constexpr const char * const TAG = "DEBUG"; +constexpr int notes[] = {0, 523, 587, 659, 698, 784, 880, 988, 1047, 1175}; uint8_t consoleControlCharsReceived{}; bool uart0Initialized{}; @@ -155,6 +157,8 @@ void handleNormalChar(char c) case '9': for (Controller &controller : controllers) controller.command.buzzer.freq = c-'0'; + //buzzer::set_frequency(notes[c-'0']); + buzzer::set_frequency(500 * (c-'0')); break; case 'z': case 'Z': diff --git a/main/main.cpp b/main/main.cpp index ed1272a..028bd5b 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -38,6 +38,7 @@ using namespace std::chrono_literals; #include "displays/statusdisplay.h" #include "newsettings.h" #include "taskmanager.h" +#include "buzzer.h" namespace { espchrono::millis_clock::time_point lastStatsPush; @@ -155,6 +156,8 @@ extern "C" void app_main() esp_chip_info(&chip_info); esp_pm_get_configuration(&pm_config); + buzzer::init(); + while (true) { const auto now = espchrono::millis_clock::now();