Explorar o código

alarm: add configuration variables

Helmut Pozimski hai 1 ano
pai
achega
08253111ae
Modificáronse 3 ficheiros con 14 adicións e 13 borrados
  1. 6 11
      main/alarm.c
  2. 3 2
      main/alarm_task.c
  3. 5 0
      main/configuration.h

+ 6 - 11
main/alarm.c

@@ -9,22 +9,17 @@
 #include <freertos/FreeRTOS.h>
 #include <freertos/event_groups.h>
 
+#include "configuration.h"
+
 #define MODE LEDC_LOW_SPEED_MODE
 #define CHANNEL LEDC_CHANNEL_0
-#define DUTY 4095
-
-/* TODO: This code works reasonably well for low frequencies
- * but has nasty high-pitched interference for high frequencies.
- * It needs to be checked if this is caused by the code or the
- * circutry.
- */
 
 void alarm_init(uint32_t frequency) {
 	 ledc_timer_config_t ledc_timer = {
     	.speed_mode       = MODE,
       .timer_num        = LEDC_TIMER_0,
-      .duty_resolution  = LEDC_TIMER_13_BIT,
-      .freq_hz          = frequency,  // Set output frequency at 5 kHz
+      .duty_resolution  = LEDC_TIMER_8_BIT,
+      .freq_hz          = frequency,
       .clk_cfg          = LEDC_AUTO_CLK
     };
     ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
@@ -34,7 +29,7 @@ void alarm_init(uint32_t frequency) {
       .channel        = CHANNEL,
       .timer_sel      = LEDC_TIMER_0,
       .intr_type      = LEDC_INTR_DISABLE,
-      .gpio_num       = 5,
+      .gpio_num       = BUZZER_PIN,
       .duty           = 0,
       .hpoint         = 0
 	};
@@ -42,7 +37,7 @@ void alarm_init(uint32_t frequency) {
 }
 
 void beep(uint32_t length_ms) {
-	ESP_ERROR_CHECK(ledc_set_duty(MODE, CHANNEL, DUTY));
+	ESP_ERROR_CHECK(ledc_set_duty(MODE, CHANNEL, LEDC_DUTY));
 	ESP_ERROR_CHECK(ledc_update_duty(MODE, CHANNEL));
 	vTaskDelay(length_ms / portTICK_PERIOD_MS);
 	ESP_ERROR_CHECK(ledc_set_duty(MODE, CHANNEL, 0));

+ 3 - 2
main/alarm_task.c

@@ -17,6 +17,7 @@
 #include "alarm.h"
 #include "ds3231.h"
 #include "alarm_task.h"
+#include "configuration.h"
 
 #define SECONDS_PER_DAY 86400
 #define TAG "alarm-task"
@@ -76,7 +77,7 @@ static void assert_alarm(uint8_t* button_pressed_flag) {
 		vTaskDelay(100 / portTICK_PERIOD_MS);
 		beep(100);
 		vTaskDelay(2000 / portTICK_PERIOD_MS);
-		if (*button_pressed_flag) {
+		if (*button_pressed_flag == 1) {
 			break;		
 		}
 	}
@@ -87,7 +88,7 @@ void alarm_task(void *pvParameters) {
 	alarm_parameters* parameters = (alarm_parameters*) pvParameters;
 	*parameters->task_handle = xTaskGetCurrentTaskHandle();
 	struct tm current_time;
-	alarm_init(200);
+	alarm_init(ALARM_FREQUENCY);
 	uint8_t* button_pressed_flag = parameters->button_pressed_flag;
 	
 	while (1) {

+ 5 - 0
main/configuration.h

@@ -15,6 +15,11 @@
 #define TM1637_DIO_PIN
 
 #define BUTTON_INTERRUPT_PIN
+#define BUZZER_PIN
+
+#define LEDC_DUTY
+#define ALARM_FREQUENCY
+
 
 // Can be set from 0 to 7
 #define DISPLAY_BRIGHTNESS