From 1f7bf6935e766ef95569c06befebbad384058f1f Mon Sep 17 00:00:00 2001 From: Stephen Crane Date: Thu, 6 Sep 2018 10:12:03 +0100 Subject: [PATCH] sound uses timed --- sound.cpp | 9 ++------- timed.cpp | 19 +++++++------------ timed.h | 7 +------ 3 files changed, 10 insertions(+), 25 deletions(-) diff --git a/sound.cpp b/sound.cpp index 36c0a80..87840c2 100644 --- a/sound.cpp +++ b/sound.cpp @@ -12,17 +12,15 @@ static dac_channel_t channel; static volatile const uint8_t *_bytes; static volatile unsigned _size, _off; -static hw_timer_t *timer; void IRAM_ATTR timer_callback() { portENTER_CRITICAL_ISR(&mux); if (_off < _size) dac_output_voltage(channel, _bytes[_off++]); - else { + else if (_bytes) { _bytes = 0; dac_output_disable(channel); - timerAlarmDisable(timer); } portEXIT_CRITICAL_ISR(&mux); @@ -34,9 +32,7 @@ void Sound::begin(unsigned pin, unsigned freq) { else if (pin == 26) channel = DAC_CHANNEL_2; - timer = timerBegin(1, 80, true); - timerAttachInterrupt(timer, &timer_callback, true); - timerAlarmWrite(timer, 1000000 / freq, true); + timer_create(freq, &timer_callback); } const uint8_t *Sound::play(const uint8_t *bytes, unsigned size) { @@ -48,7 +44,6 @@ const uint8_t *Sound::play(const uint8_t *bytes, unsigned size) { _size = size; _off = 0; dac_output_enable(channel); - timerAlarmEnable(timer); play = bytes; } diff --git a/timed.cpp b/timed.cpp index 9bdd7ba..cae7409 100644 --- a/timed.cpp +++ b/timed.cpp @@ -9,17 +9,16 @@ #include "timed.h" -static Timed *t; - -// FIXME: disable timer when tick() returns false #if defined(__LM4F120H5QR__) +static void (*client_handler)(void); + static void timer0isr(void) { ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT); - t->tick(); + client_handler(); } -void timer_create(unsigned freq, Timed *client) { - t = client; +void timer_create(unsigned freq, void (*handler)(void)) { + client_handler = handler; ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC); TimerIntRegister(TIMER0_BASE, TIMER_A, timer0isr); @@ -30,13 +29,9 @@ void timer_create(unsigned freq, Timed *client) { } #elif defined(ESP_PLATFORM) -void IRAM_ATTR onTimer() { - t->tick(); -} - -void timer_create(unsigned freq, Timed *client) { +void timer_create(unsigned freq, void (*handler)(void)) { hw_timer_t *timer = timerBegin(3, 80, true); // prescaler of 80 - timerAttachInterrupt(timer, &onTimer, true); + timerAttachInterrupt(timer, handler, true); timerAlarmWrite(timer, 1000000 / freq, true); timerAlarmEnable(timer); } diff --git a/timed.h b/timed.h index 2644af5..7059e4d 100644 --- a/timed.h +++ b/timed.h @@ -1,11 +1,6 @@ #ifndef __TIMED_H__ #define __TIMED_H__ -class Timed { -public: - virtual bool tick() = 0; -}; - -void timer_create(unsigned freq, Timed *client); +void timer_create(unsigned freq, void (*handler)(void)); #endif