aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gaudio/pwm/gaudio_play_pwm.c
blob: 57541470a0d3f8069d008d2df94fb04d09ab8e16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * This file is subject to the terms of the GFX License. If a copy of
 * the license was not distributed with this file, you can obtain one at:
 *
 *              http://ugfx.io/license.html
 */

#include "gfx.h"

#if GFX_USE_GAUDIO && GAUDIO_NEED_PLAY

/* Include the driver defines */
#include "../../../src/gaudio/gaudio_driver_play.h"

/* Forward definition */
static void gaudio_play_pwm_timer_callbackI(void);

/* Include the board interface */
#include "gaudio_play_board.h"

static GDataBuffer		*pplay;
static ArrayDataFormat	playfmt;
static size_t			playlen;
static uint8_t			*pdata;

static void gaudio_play_pwm_timer_callbackI(void) {
	if (pplay) {

		// Get the next value from the current data buffer
		if (gfxSampleFormatBits(playfmt) > 8) {
			gaudio_play_pwm_setI(*(uint16_t *)pdata);
			pdata += 2;
		} else {
			gaudio_play_pwm_setI(*pdata);
			pdata++;
		}

		// Are we done yet
		if (--playlen)
			return;
		gaudioPlayReleaseDataBlockI(pplay);

		// Get a new data buffer
		if (!(pplay = gaudioPlayGetDataBlockI())) {
			// All is done
			gaudioPlayDoneI();
			return;
		}

	} else {
		// Get a new data buffer
		if (!(pplay = gaudioPlayGetDataBlockI()))
			return;
	}

	// Set up ready for the new buffer
	playlen = pplay->len;
	if (gfxSampleFormatBits(playfmt) > 8)
		playlen >>= 1;
	pdata = (uint8_t *)(pplay+1);
}


/*===========================================================================*/
/* External declarations.                                                    */
/*===========================================================================*/

gBool gaudio_play_lld_init(uint16_t channel, uint32_t frequency, ArrayDataFormat format) {
	(void) channel;

	if (format != ARRAY_DATA_8BITUNSIGNED && format != ARRAY_DATA_10BITUNSIGNED)
		return gFalse;

	playfmt = format;
	return gaudio_play_pwm_setup(frequency, format);
}

gBool gaudio_play_lld_set_volume(uint8_t vol) {
	(void) vol;
	return gFalse;
}

void gaudio_play_lld_start(void) {

	gfxSystemLock();
	// Get a new data buffer
	if (pplay || !(pplay = gaudioPlayGetDataBlockI())) {
		gfxSystemUnlock();			// Nothing to do
		return;
	}

	// Set up ready for the new buffer
	playlen = pplay->len;
	if (gfxSampleFormatBits(playfmt) > 8)
		playlen >>= 1;
	pdata = (uint8_t *)(pplay+1);
	gfxSystemUnlock();

	// Start the playing
	gaudio_play_pwm_start();
}

void gaudio_play_lld_stop(void) {
	// Stop everything
	gaudio_play_pwm_stop();

	// We may need to clean up the remaining buffer.
	gfxSystemLock();
	if (pplay) {
		gaudioPlayReleaseDataBlockI(pplay);
		pplay = 0;
		gaudioPlayDoneI();
	}
	gfxSystemUnlock();
}

#endif /* GFX_USE_GAUDIO && GAUDIO_NEED_PLAY */