Skip to content

Commit 76f65ac

Browse files
committed
Implement play/pause
.. and also incidentally fix a problem where a RawSample could only be looped 131070 times.
1 parent b0f7c7b commit 76f65ac

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

ports/nrf/common-hal/audiopwmio/PWMAudioOut.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,25 +125,21 @@ STATIC void fill_buffers(audiopwmio_pwmaudioout_obj_t *self, int buf) {
125125
if (self->loop && get_buffer_result == GET_BUFFER_DONE) {
126126
audiosample_reset_buffer(self->sample, false, 0);
127127
} else if(get_buffer_result == GET_BUFFER_DONE) {
128-
self->pwm->LOOP = 0;
128+
self->pwm->SHORTS = NRF_PWM_SHORT_SEQEND0_STOP_MASK | NRF_PWM_SHORT_SEQEND1_STOP_MASK;
129129
self->stopping = true;
130-
} else {
131-
self->pwm->LOOP = 0xffff;
132130
}
133131
}
134132

135133
STATIC void audiopwmout_background_obj(audiopwmio_pwmaudioout_obj_t *self) {
136134
if(!common_hal_audiopwmio_pwmaudioout_get_playing(self))
137135
return;
138-
if(self->loop && self->single_buffer) {
139-
self->pwm->LOOP = 0xffff;
140-
} else if(self->stopping) {
136+
if(self->stopping) {
141137
bool stopped =
142138
(self->pwm->EVENTS_SEQEND[0] || !self->pwm->EVENTS_SEQSTARTED[0]) &&
143139
(self->pwm->EVENTS_SEQEND[1] || !self->pwm->EVENTS_SEQSTARTED[1]);
144140
if(stopped)
145141
self->pwm->TASKS_STOP = 1;
146-
} else if(!self->single_buffer) {
142+
} else if(!self->paused && !self->single_buffer) {
147143
if(self->pwm->EVENTS_SEQSTARTED[0]) fill_buffers(self, 1);
148144
if(self->pwm->EVENTS_SEQSTARTED[1]) fill_buffers(self, 0);
149145
}
@@ -248,21 +244,17 @@ void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t* self,
248244
self->scale = top-1;
249245
self->pwm->COUNTERTOP = top;
250246

251-
if(!self->single_buffer)
252-
self->pwm->LOOP = 1;
253-
else if(self->loop)
254-
self->pwm->LOOP = 0xffff;
255-
else
256-
self->pwm->LOOP = 0;
247+
self->pwm->LOOP = 1;
257248
audiosample_reset_buffer(self->sample, false, 0);
258249
activate_audiopwmout_obj(self);
259250
fill_buffers(self, 0);
260251
self->pwm->SEQ[1].PTR = self->pwm->SEQ[0].PTR;
261252
self->pwm->SEQ[1].CNT = self->pwm->SEQ[0].CNT;
262253
self->pwm->EVENTS_SEQSTARTED[0] = 0;
263254
self->pwm->EVENTS_SEQSTARTED[1] = 0;
264-
self->pwm->TASKS_SEQSTART[0] = 1;
265255
self->pwm->EVENTS_STOPPED = 0;
256+
self->pwm->SHORTS = NRF_PWM_SHORT_LOOPSDONE_SEQSTART0_MASK;
257+
self->pwm->TASKS_SEQSTART[0] = 1;
266258
self->playing = true;
267259
self->stopping = false;
268260
self->paused = false;
@@ -282,7 +274,7 @@ void common_hal_audiopwmio_pwmaudioout_stop(audiopwmio_pwmaudioout_obj_t* self)
282274
}
283275

284276
bool common_hal_audiopwmio_pwmaudioout_get_playing(audiopwmio_pwmaudioout_obj_t* self) {
285-
if(self->pwm->EVENTS_STOPPED) {
277+
if(!self->paused && self->pwm->EVENTS_STOPPED) {
286278
self->playing = false;
287279
self->pwm->EVENTS_STOPPED = 0;
288280
}
@@ -307,10 +299,15 @@ bool common_hal_audiopwmio_pwmaudioout_get_playing(audiopwmio_pwmaudioout_obj_t*
307299
*/
308300
void common_hal_audiopwmio_pwmaudioout_pause(audiopwmio_pwmaudioout_obj_t* self) {
309301
self->paused = true;
302+
self->pwm->SHORTS = NRF_PWM_SHORT_SEQEND1_STOP_MASK;
303+
while(!self->pwm->EVENTS_STOPPED) { /* NOTHING */ }
310304
}
311305

312306
void common_hal_audiopwmio_pwmaudioout_resume(audiopwmio_pwmaudioout_obj_t* self) {
313307
self->paused = false;
308+
self->pwm->SHORTS = NRF_PWM_SHORT_LOOPSDONE_SEQSTART0_MASK;
309+
self->pwm->EVENTS_STOPPED = 0;
310+
self->pwm->TASKS_SEQSTART[0] = 1;
314311
}
315312

316313
bool common_hal_audiopwmio_pwmaudioout_get_paused(audiopwmio_pwmaudioout_obj_t* self) {

0 commit comments

Comments
 (0)