main/task_sound.c

Fri, 28 Jun 2024 15:33:24 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 28 Jun 2024 15:33:24 +0200
branch
idf 5.1
changeset 137
e0f50087c909
parent 136
89fc3c57282e
child 142
1f7069278fe7
permissions
-rw-r--r--

Fixed changing runtime datarecord size during switching between IDF 4.2 and 5.1. Fixed wiping the /spiffs filesystem. The directory listing from the SD card doesn't overwrite parts of the screen anymore. Solved the slow speed issue with the SD card. Try to force the SD card to operate at 20 MHz. More project settings changed to improve performance and memory usage.

/**
 * @file task_sound.c
 * @brief BrewBoard Sound driver. Uses a simple piezo buzzer.
 */

#include "config.h"


#define	PIEZO_BUZZER	CONFIG_BUZZER_GPIO	///< GPIO pin with the piezo buzzer.

EventGroupHandle_t	xEventGroupSound;	///< Event handle.


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.


/* Sound arrays. The first value is the length, followed by the on and off times. */
static 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 };
static const uint16_t		_sound_Prompt[]		= {  2, 50, 125 };
static const uint16_t		_sound_TempReached[]	= {  6, 250, 75, 250, 75, 250, 75};
static const uint16_t		_sound_TimeOut[]	= { 10, 25, 975, 25, 975, 25, 975, 25, 975, 500, 500 }; // 5 seconds
static const uint16_t		_sound_AddHop[]		= { 18, 50, 50, 50, 50, 50, 225, 150, 50, 150, 50, 150, 225, 50, 50, 50 , 50 , 50, 50 };
static const uint16_t		_sound_End[]		= {  2, 1000, 75 };
static const uint16_t		_sound_Warn[]		= {  6, 100, 75, 100, 75, 100, 50 };



/**
 * @brief Play sounds on the piezo buzzer.
 * @param sound The sound array.
 */
void BuzzerPlay(const uint16_t *sound)
{
    char	msg[16];

    for (int i = 1; i <= sound[0]; i += 2) {
	if (sound != _sound_Startup) {
	    snprintf(msg, 15, "{\"beep\":\"1\"}");
    	    ws_server_send_text_clients((char *)"/ws", msg, strlen(msg));
	}
	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, "Start 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) {
	    BuzzerPlay(_sound_Prompt);
	    xEventGroupClearBits(xEventGroupSound, SOUND_PROMPT);
	}
	if (uxBits & SOUND_TEMPREACHED) {
	    BuzzerPlay(_sound_TempReached);
	    xEventGroupClearBits(xEventGroupSound, SOUND_TEMPREACHED);
	}
	if (uxBits & SOUND_TIMEOUT) {
	    BuzzerPlay(_sound_TimeOut);
	    xEventGroupClearBits(xEventGroupSound, SOUND_TIMEOUT);
	}
	if (uxBits & SOUND_ADDHOP) {
	    BuzzerPlay(_sound_AddHop);
	    xEventGroupClearBits(xEventGroupSound, SOUND_ADDHOP);
	}
	if (uxBits & SOUND_END) {
	    BuzzerPlay(_sound_End);
	    xEventGroupClearBits(xEventGroupSound, SOUND_END);
	}
	if (uxBits & SOUND_WARN) {
	    BuzzerPlay(_sound_Warn);
	    xEventGroupClearBits(xEventGroupSound, SOUND_WARN);
	}

	vTaskDelay(10 / portTICK_PERIOD_MS);
    }
}

mercurial