r65emu/sound_dac.cpp

68 lines
1.2 KiB
C++
Raw Normal View History

2018-09-06 08:18:23 +00:00
#include <Arduino.h>
#include <hardware.h>
#include "timed.h"
2018-09-11 16:33:18 +00:00
#include "sound_dac.h"
2018-09-06 08:18:23 +00:00
2023-11-18 20:08:16 +00:00
#if defined(DAC_SOUND) && defined(ESP_PLATFORM)
2018-09-11 16:33:18 +00:00
static DAC *s;
2018-09-06 12:25:24 +00:00
2018-09-06 08:18:23 +00:00
#include <driver/dac.h>
static portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
static dac_channel_t channel;
void IRAM_ATTR timer_callback() {
2018-09-06 12:25:24 +00:00
s->on_tick();
}
2018-09-11 16:33:18 +00:00
void IRAM_ATTR DAC::on_tick() {
2018-09-06 08:18:23 +00:00
portENTER_CRITICAL_ISR(&mux);
if (_off < _size)
dac_output_voltage(channel, _bytes[_off++]);
2018-09-06 09:12:03 +00:00
else if (_bytes) {
2018-09-06 08:18:23 +00:00
_bytes = 0;
dac_output_disable(channel);
}
portEXIT_CRITICAL_ISR(&mux);
}
2018-09-11 16:33:18 +00:00
void DAC::begin(unsigned pin, unsigned freq) {
2018-09-06 08:18:23 +00:00
if (pin == 25)
channel = DAC_CHANNEL_1;
else if (pin == 26)
channel = DAC_CHANNEL_2;
2018-09-06 12:25:24 +00:00
s = this;
2018-09-06 09:12:03 +00:00
timer_create(freq, &timer_callback);
2018-09-06 08:18:23 +00:00
}
2018-09-11 16:33:18 +00:00
const uint8_t *DAC::play(const uint8_t *bytes, unsigned size) {
2018-09-06 08:18:23 +00:00
portENTER_CRITICAL_ISR(&mux);
const uint8_t *play = (const uint8_t *)_bytes;
if (_off == _size) {
_bytes = bytes;
_size = size;
_off = 0;
dac_output_enable(channel);
play = bytes;
}
portEXIT_CRITICAL_ISR(&mux);
return play;
}
#else
2018-11-25 13:21:05 +00:00
#pragma message "No DAC"
2018-09-06 08:18:23 +00:00
// does nothing by default
2018-09-11 16:33:18 +00:00
void DAC::begin(unsigned channel, unsigned freq) {
2018-09-06 08:18:23 +00:00
}
2018-09-11 16:33:18 +00:00
const uint8_t *DAC::play(const uint8_t *bytes, unsigned size) {
2018-09-06 08:18:23 +00:00
return 0;
}
#endif