diff -r 000000000000 -r b74b0e4902c3 main/task_sound.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/task_sound.c Sat Oct 20 13:23:15 2018 +0200 @@ -0,0 +1,139 @@ +/** + * @file task_sound.c + * @brief BrewBoard Sound driver. Plays WAVE audio, Microsoft PCM, 8 bit, mono 16000 Hz + * files from the /spiffs/sound/ directory. + */ + +#include "config.h" + + +#define PIEZO_BUZZER CONFIG_BUZZER_GPIO + +EventGroupHandle_t xEventGroupSound; + + +static const char *TAG = "task_sound"; + +const int SOUND_STARTUP = BIT0; ///< When set, play startup sound. +const int SOUND_PROMPT = BIT1; ///< When set, play prompt sound. +const int SOUND_TEMPREACHED = BIT2; ///< When set, play temperature reached sound. +const int SOUND_TIMEOUT = BIT3; ///< When set, play time out sound. +const int SOUND_ADDHOP = BIT4; ///< When set, play add hop sound. +const int SOUND_END = BIT5; ///< When set, play finished sound. +const int SOUND_WARN = BIT6; ///< When set, play warning sound. + + +const uint16_t _sound_Startup[] = { 50, 10, 10, 10, 10, 10, 10, 10, 10, 10, 200, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }; +const uint16_t _sound_Prompt[] = { 2, 50, 125 }; +const uint16_t _sound_TempReached[] = { 6, 250, 75, 250, 75, 250, 75}; +const uint16_t _sound_TimeOut[] = {10, 25, 975, 25, 975, 25, 975, 25, 975, 500, 500 }; // 5 seconds +const uint16_t _sound_AddHop[] = {18, 50, 50, 50, 50, 50, 225, 150, 50, 150, 50, 150, 225, 50, 50, 50 , 50 , 50, 50 }; +const uint16_t _sound_End[] = { 2, 1000, 75 }; +const uint16_t _sound_Warn[] = { 6, 100, 75, 100, 75, 100, 50 }; + + + +void BuzzerPlay(const uint16_t *sound) +{ + for (int i = 1; i <= sound[0]; i += 2) { + gpio_set_level(PIEZO_BUZZER, 1); + vTaskDelay(sound[i] / portTICK_PERIOD_MS); + gpio_set_level(PIEZO_BUZZER, 0); + vTaskDelay(sound[i+1] / portTICK_PERIOD_MS); + } + + gpio_set_level(PIEZO_BUZZER, 0); // Just make sure the sound is off. +} + + + +void SoundPlay(int id) +{ + switch (id) { + case SOUND_StartUp: xEventGroupSetBits(xEventGroupSound, SOUND_STARTUP); + break; + + case SOUND_Prompt: xEventGroupSetBits(xEventGroupSound, SOUND_PROMPT); + break; + + case SOUND_TempReached: xEventGroupSetBits(xEventGroupSound, SOUND_TEMPREACHED); + break; + + case SOUND_TimeOut: xEventGroupSetBits(xEventGroupSound, SOUND_TIMEOUT); + break; + + case SOUND_AddHop: xEventGroupSetBits(xEventGroupSound, SOUND_ADDHOP); + break; + + case SOUND_End: xEventGroupSetBits(xEventGroupSound, SOUND_END); + break; + + case SOUND_Warn: xEventGroupSetBits(xEventGroupSound, SOUND_WARN); + break; + } +} + + + +void task_sound(void *pvParameter) +{ + EventBits_t uxBits; + + ESP_LOGI(TAG, "Starting sound"); + + gpio_pad_select_gpio(PIEZO_BUZZER); + gpio_set_direction(PIEZO_BUZZER, GPIO_MODE_OUTPUT); + + xEventGroupSound = xEventGroupCreate(); + + while (1) { + uxBits = xEventGroupWaitBits(xEventGroupSound, + SOUND_STARTUP | SOUND_PROMPT | SOUND_TEMPREACHED | SOUND_TIMEOUT | SOUND_ADDHOP | SOUND_END | SOUND_WARN, + pdFALSE, pdFALSE, portMAX_DELAY ); + + if (uxBits & SOUND_STARTUP) { + BuzzerPlay(_sound_Startup); + xEventGroupClearBits(xEventGroupSound, SOUND_STARTUP); + } + if (uxBits & SOUND_PROMPT) { + VncSoundBell(); + BuzzerPlay(_sound_Prompt); + xEventGroupClearBits(xEventGroupSound, SOUND_PROMPT); + } + if (uxBits & SOUND_TEMPREACHED) { + VncSoundBell(); + BuzzerPlay(_sound_TempReached); + VncSoundBell(); + xEventGroupClearBits(xEventGroupSound, SOUND_TEMPREACHED); + } + if (uxBits & SOUND_TIMEOUT) { + BuzzerPlay(_sound_TimeOut); + xEventGroupClearBits(xEventGroupSound, SOUND_TIMEOUT); + } + if (uxBits & SOUND_ADDHOP) { + VncSoundBell(); + VncSoundBell(); + BuzzerPlay(_sound_AddHop); + VncSoundBell(); + VncSoundBell(); + xEventGroupClearBits(xEventGroupSound, SOUND_ADDHOP); + } + if (uxBits & SOUND_END) { + BuzzerPlay(_sound_End); + xEventGroupClearBits(xEventGroupSound, SOUND_END); + } + if (uxBits & SOUND_WARN) { + VncSoundBell(); + BuzzerPlay(_sound_Warn); + xEventGroupClearBits(xEventGroupSound, SOUND_WARN); + } + + vTaskDelay(10 / portTICK_PERIOD_MS); + } +} + +