main/task_sound.c

Sun, 18 Jul 2021 11:25:55 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 18 Jul 2021 11:25:55 +0200
changeset 109
72af8958b469
parent 88
7f02dbee58d0
permissions
-rw-r--r--

Version 0.3.17. Made the Hendi PWM change official.

/**
 * @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