alarm.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Helmut Pozimski <helmut@pozimski.eu>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0-only
  5. */
  6. #include <driver/ledc.h>
  7. #include <esp_err.h>
  8. #include <freertos/FreeRTOS.h>
  9. #include <freertos/event_groups.h>
  10. #include "configuration.h"
  11. #define MODE LEDC_LOW_SPEED_MODE
  12. #define CHANNEL LEDC_CHANNEL_0
  13. void alarm_init(uint32_t frequency) {
  14. ledc_timer_config_t ledc_timer = {
  15. .speed_mode = MODE,
  16. .timer_num = LEDC_TIMER_0,
  17. .duty_resolution = LEDC_TIMER_8_BIT,
  18. .freq_hz = frequency,
  19. .clk_cfg = LEDC_AUTO_CLK
  20. };
  21. ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
  22. ledc_channel_config_t ledc_channel = {
  23. .speed_mode = MODE,
  24. .channel = CHANNEL,
  25. .timer_sel = LEDC_TIMER_0,
  26. .intr_type = LEDC_INTR_DISABLE,
  27. .gpio_num = BUZZER_PIN,
  28. .duty = 0,
  29. .hpoint = 0
  30. };
  31. ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
  32. }
  33. void beep(uint32_t length_ms) {
  34. ESP_ERROR_CHECK(ledc_set_duty(MODE, CHANNEL, LEDC_DUTY));
  35. ESP_ERROR_CHECK(ledc_update_duty(MODE, CHANNEL));
  36. vTaskDelay(length_ms / portTICK_PERIOD_MS);
  37. ESP_ERROR_CHECK(ledc_set_duty(MODE, CHANNEL, 0));
  38. ESP_ERROR_CHECK(ledc_update_duty(MODE, CHANNEL));
  39. }