main/task_sound.c

Mon, 22 Oct 2018 21:43:45 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 22 Oct 2018 21:43:45 +0200
changeset 6
e84200edc852
parent 4
6d1f512cd074
child 38
537ffe280775
permissions
-rw-r--r--

Updated esp-ide. Removed VNC server corre encoding because no clients would use it. Enabled WiFi error logmessages. Write runtime record is now debug logging. Removed recipe.Record number, not usefull and was wrong too. Removed console print of json log data.

/**
 * @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)
{
    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);
    }
}

mercurial