Fixed timer, maybe. I have no way to try this

This commit is contained in:
Phew
2025-12-20 10:47:10 +01:00
parent b708b4c787
commit 91cb3ef098

View File

@@ -1,19 +1,19 @@
/* /*
* This creates sound tones by outputting a square wave on a DAC pin. The * This creates sound tones by outputting a square wave on a DAC pin. The
* volume of the tone is the level of the DAC pin. * volume of the tone is the level of the DAC pin.
* *
* The square wave is created by a timer. The timer runs at 2x the freq, because * The square wave is created by a timer. The timer runs at 2x the freq, because
* it needs to transition high and then low. * it needs to transition high and then low.
* *
* *
*/ */
#ifndef SOUND_H #ifndef SOUND_H
#define SOUND_H #define SOUND_H
#include "esp32-hal-timer.h"; #include "esp32-hal-timer.h";
#define ESP32_F_CPU 80000000 // the speed of the processor #define ESP32_F_CPU 1000000 // the speed of the processor
#define AUDIO_INTERRUPT_PRESCALER 80 #define AUDIO_INTERRUPT_PRESCALER 80
#define SOUND_TIMER_NO 0 #define SOUND_TIMER_NO 0
//#define AUDIO_PIN 25 //#define AUDIO_PIN 25
#define MIN_FREQ 20 #define MIN_FREQ 20
@@ -33,59 +33,69 @@ int dac_pin;
void IRAM_ATTR onSoundTimer() void IRAM_ATTR onSoundTimer()
{ {
if (sound_on) { if (sound_on) {
dacWrite(dac_pin, (sound_wave_high?sound_volume:0)); dacWrite(dac_pin, (sound_wave_high?sound_volume:0));
sound_wave_high = ! sound_wave_high; sound_wave_high = ! sound_wave_high;
}
} else
else dacWrite(dac_pin, 0);
dacWrite(dac_pin, 0);
} }
void sound_init(int pin){ // pin must be a DAC pin number !! (typically 25 or 26) void sound_init(int pin){ // pin must be a DAC pin number !! (typically 25 or 26)
dac_pin = pin; dac_pin = pin;
sound_on = false; sound_on = false;
pinMode(dac_pin, OUTPUT); pinMode(dac_pin, OUTPUT);
sound_volume = 0; sound_volume = 0;
sndTimer = timerBegin(SOUND_TIMER_NO, AUDIO_INTERRUPT_PRESCALER, true); #ifdef OLD_CODE
timerAttachInterrupt(sndTimer, &onSoundTimer, true); sndTimer = timerBegin(SOUND_TIMER_NO, AUDIO_INTERRUPT_PRESCALER, true);
timerAlarmWrite(sndTimer, ESP32_F_CPU/AUDIO_INTERRUPT_PRESCALER/MIN_FREQ, true); // lower timer freq timerAttachInterrupt(sndTimer, &onSoundTimer, true);
timerAlarmEnable(sndTimer); timerAlarmWrite(sndTimer, ESP32_F_CPU/AUDIO_INTERRUPT_PRESCALER/MIN_FREQ, true); // lower timer freq
timerAlarmEnable(sndTimer);
#else
//void RH_INTERRUPT_ATTR esp32_timer_interrupt_handler(); // Forward declaration
sndTimer = timerBegin(ESP32_F_CPU);
timerAttachInterrupt(sndTimer, &onSoundTimer);
timerAlarm(sndTimer, ESP32_F_CPU/AUDIO_INTERRUPT_PRESCALER/MIN_FREQ, true, 0);
#endif
} }
void sound_pause() // this prevents the interrupt from firing ... use during eeprom write void sound_pause() // this prevents the interrupt from firing ... use during eeprom write
{ {
if (sndTimer != NULL) if (sndTimer != NULL)
timerStop(sndTimer); timerStop(sndTimer);
} }
void sound_resume() // resume from pause ... after eeprom write void sound_resume() // resume from pause ... after eeprom write
{ {
if (sndTimer != NULL) if (sndTimer != NULL)
timerRestart(sndTimer); timerRestart(sndTimer);
} }
bool sound(uint16_t freq, uint8_t volume){ bool sound(uint16_t freq, uint8_t volume){
if (volume == 0) { if (volume == 0) {
soundOff(); soundOff();
return false; return false;
} }
if (freq < MIN_FREQ || freq > MAX_FREQ) { if (freq < MIN_FREQ || freq > MAX_FREQ) {
return false; return false;
} }
sound_on = true; sound_on = true;
sound_volume = volume; sound_volume = volume;
timerAlarmWrite(sndTimer, ESP32_F_CPU/AUDIO_INTERRUPT_PRESCALER/(freq * 2), true); //timerAlarmWrite(sndTimer, ESP32_F_CPU/AUDIO_INTERRUPT_PRESCALER/(freq * 2), true);
return true; timerAlarm(sndTimer, ESP32_F_CPU/AUDIO_INTERRUPT_PRESCALER/(freq * 2), true, 0);
return true;
} }
void soundOff(){ void soundOff(){
sound_on = false; sound_on = false;
sound_volume = 0; sound_volume = 0;
timerAlarmWrite(sndTimer, ESP32_F_CPU/AUDIO_INTERRUPT_PRESCALER/(MIN_FREQ), true); // lower timer freq //timerAlarmWrite(sndTimer, ESP32_F_CPU/AUDIO_INTERRUPT_PRESCALER/(MIN_FREQ), true); // lower timer freq
timerAlarm(sndTimer, ESP32_F_CPU/AUDIO_INTERRUPT_PRESCALER/(MIN_FREQ), true, 0);
//timerAlarm(hw_timer_t *timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count);
} }