Allow controlling the buzzer from software
This commit is contained in:
committed by
CommanderRedYT
parent
e6ddc39177
commit
05762763e2
@@ -48,6 +48,7 @@ set(headers
|
|||||||
bobbyschedulertask.h
|
bobbyschedulertask.h
|
||||||
bobbytypesafeenum.h
|
bobbytypesafeenum.h
|
||||||
buildserver.h
|
buildserver.h
|
||||||
|
buzzer.h
|
||||||
can.h
|
can.h
|
||||||
changevaluedisplay_bluetoothmode.h
|
changevaluedisplay_bluetoothmode.h
|
||||||
changevaluedisplay_bobbyquickactions.h
|
changevaluedisplay_bobbyquickactions.h
|
||||||
@@ -301,6 +302,7 @@ set(sources
|
|||||||
bobbyhupe.cpp
|
bobbyhupe.cpp
|
||||||
bobbyquickactions.cpp
|
bobbyquickactions.cpp
|
||||||
buildserver.cpp
|
buildserver.cpp
|
||||||
|
buzzer.cpp
|
||||||
can.cpp
|
can.cpp
|
||||||
changevaluedisplay_bluetoothmode.cpp
|
changevaluedisplay_bluetoothmode.cpp
|
||||||
changevaluedisplay_bobbyquickactions.cpp
|
changevaluedisplay_bobbyquickactions.cpp
|
||||||
|
72
main/buzzer.cpp
Normal file
72
main/buzzer.cpp
Normal 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
7
main/buzzer.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace buzzer
|
||||||
|
{
|
||||||
|
void init();
|
||||||
|
void set_frequency(int frequency);
|
||||||
|
} // namespace buzzer
|
@@ -15,6 +15,7 @@
|
|||||||
#include <changevaluedisplay_string.h>
|
#include <changevaluedisplay_string.h>
|
||||||
|
|
||||||
// local includes
|
// local includes
|
||||||
|
#include "buzzer.h"
|
||||||
#include "globals.h"
|
#include "globals.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "bobbybuttons.h"
|
#include "bobbybuttons.h"
|
||||||
@@ -22,6 +23,7 @@
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
constexpr const char * const TAG = "DEBUG";
|
constexpr const char * const TAG = "DEBUG";
|
||||||
|
constexpr int notes[] = {0, 523, 587, 659, 698, 784, 880, 988, 1047, 1175};
|
||||||
|
|
||||||
uint8_t consoleControlCharsReceived{};
|
uint8_t consoleControlCharsReceived{};
|
||||||
bool uart0Initialized{};
|
bool uart0Initialized{};
|
||||||
@@ -155,6 +157,8 @@ void handleNormalChar(char c)
|
|||||||
case '9':
|
case '9':
|
||||||
for (Controller &controller : controllers)
|
for (Controller &controller : controllers)
|
||||||
controller.command.buzzer.freq = c-'0';
|
controller.command.buzzer.freq = c-'0';
|
||||||
|
//buzzer::set_frequency(notes[c-'0']);
|
||||||
|
buzzer::set_frequency(500 * (c-'0'));
|
||||||
break;
|
break;
|
||||||
case 'z':
|
case 'z':
|
||||||
case 'Z':
|
case 'Z':
|
||||||
|
@@ -38,6 +38,7 @@ using namespace std::chrono_literals;
|
|||||||
#include "displays/statusdisplay.h"
|
#include "displays/statusdisplay.h"
|
||||||
#include "newsettings.h"
|
#include "newsettings.h"
|
||||||
#include "taskmanager.h"
|
#include "taskmanager.h"
|
||||||
|
#include "buzzer.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
espchrono::millis_clock::time_point lastStatsPush;
|
espchrono::millis_clock::time_point lastStatsPush;
|
||||||
@@ -155,6 +156,8 @@ extern "C" void app_main()
|
|||||||
esp_chip_info(&chip_info);
|
esp_chip_info(&chip_info);
|
||||||
esp_pm_get_configuration(&pm_config);
|
esp_pm_get_configuration(&pm_config);
|
||||||
|
|
||||||
|
buzzer::init();
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
const auto now = espchrono::millis_clock::now();
|
const auto now = espchrono::millis_clock::now();
|
||||||
|
Reference in New Issue
Block a user