#include "notes.h"
#define BEEP_PWM_CHANNEL 2
#define BEEP_PIN 33
#define MAX_NOTES_AMOUNT 10
struct MELODY {
uint8_t notes_amount;
uint16_t note_freq[MAX_NOTES_AMOUNT];
uint16_t note_duration[MAX_NOTES_AMOUNT];
};
struct SOUND {
bool sound_on;
struct MELODY* playing_melody_ptr;
} sound;
struct MELODY test_beep;
TaskHandle_t musicHandle = NULL;
void musicPlayerTaskSetup() {
sound.playing_melody_ptr = &test_beep;
xTaskCreatePinnedToCore(
musicPlayerTask, /* Function to implement the task */
"musicHandlerTask ", /* Name of the task */
4000, /* Stack size in words */
NULL, /* Task input parameter */
7, /* Priority of the task */
&musicHandle, /* Task handle. */
0); /* Core where the task should run */
}
void musicPlayerTask(void* pvParameters) {
uint8_t note_idx = 0;
struct MELODY* playing_melody = sound.playing_melody_ptr;
Serial.println("task");
if (playing_melody == 0) {
Serial.println("no melody");
vTaskDelete(NULL);
}
while (note_idx < playing_melody->notes_amount) {
uint8_t pwm_duty = 0;
if (playing_melody->note_freq[note_idx]) {
pwm_duty = 127;
ledcSetup(BEEP_PWM_CHANNEL, playing_melody->note_freq[note_idx], 8);
}
ledcWrite(BEEP_PWM_CHANNEL, pwm_duty);
vTaskDelay(playing_melody->note_duration[note_idx]);
note_idx++;
}
vTaskDelete(NULL);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
Serial.print("c test");
Serial.println(C(0));
test_beep.notes_amount = 4;
test_beep.note_freq[0] = 1000;
test_beep.note_freq[1] = 0;
test_beep.note_freq[2] = 1000;
test_beep.note_freq[3] = 0;
test_beep.note_duration[0] = 500;
test_beep.note_duration[1] = 500;
test_beep.note_duration[2] = 500;
test_beep.note_duration[3] = 500;
ledcSetup(BEEP_PWM_CHANNEL, 1000, 8);
ledcAttachPin(BEEP_PIN, BEEP_PWM_CHANNEL);
}
void loop() {
// ledcWrite(BEEP_PWM_CHANNEL, 127);
// delay(500);
// ledcWrite(BEEP_PWM_CHANNEL, 0);
// delay(500);
playMelody();
delay(500);
}
void playMelody() {
muteBeep();
musicPlayerTaskSetup();
}
void muteBeep() {
if (musicHandle != NULL) {
vTaskSuspend(musicHandle);
vTaskDelete(musicHandle);
}
ledcWrite(BEEP_PWM_CHANNEL, 0);
}