1 Commits

Author SHA1 Message Date
05762763e2 Allow controlling the buzzer from software 2022-10-06 10:51:32 +02:00
5 changed files with 88 additions and 0 deletions

View File

@ -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

72
main/buzzer.cpp Normal file
View File

@ -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 <esp_log.h>
#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);
}
}
}

7
main/buzzer.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
namespace buzzer
{
void init();
void set_frequency(int frequency);
} // namespace buzzer

View File

@ -15,6 +15,7 @@
#include <changevaluedisplay_string.h>
// 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':

View File

@ -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();