mirror of
https://github.com/jscrane/r65emu.git
synced 2024-09-30 02:58:36 +00:00
just use tone()/noTone() to support PWM sound now
This commit is contained in:
parent
f4dbc8ae26
commit
524f1664c4
@ -1,4 +1,3 @@
|
|||||||
// sound: dac and pwm
|
// sound: dac and pwm
|
||||||
#define DAC_SOUND 25
|
#define DAC_SOUND 25
|
||||||
#define PWM_SOUND 25
|
#define PWM_SOUND 25
|
||||||
#define PWM_DUTY 20 // 20/1024 -> volume
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
// sound
|
// sound
|
||||||
#define PWM_SOUND D2
|
#define PWM_SOUND D2
|
||||||
#define PWM_DUTY 20 // 20/1024 -> volume
|
|
||||||
#define PWM_TOP 1024
|
#define PWM_TOP 1024
|
||||||
|
@ -10,11 +10,7 @@
|
|||||||
|
|
||||||
// sound: dac and pwm
|
// sound: dac and pwm
|
||||||
#define DAC_SOUND 25
|
#define DAC_SOUND 25
|
||||||
|
#define PWM_SOUND 25
|
||||||
// PWM doesn't work
|
|
||||||
// "assert failed: ledc_clk_cfg_to_global_clk ledc.c:443 (false)"
|
|
||||||
//#define PWM_SOUND 25
|
|
||||||
//#define PWM_DUTY 20 // 20/1024 -> volume
|
|
||||||
|
|
||||||
// "tape" storage...
|
// "tape" storage...
|
||||||
#undef USE_SD
|
#undef USE_SD
|
||||||
|
@ -1,82 +1,16 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "sound_pwm.h"
|
#include "sound_pwm.h"
|
||||||
#include "hardware.h"
|
|
||||||
|
|
||||||
#if defined(PWM_SOUND) && defined(ESP32)
|
static unsigned gpio;
|
||||||
#include <driver/ledc.h>
|
|
||||||
|
|
||||||
#define CHANNEL LEDC_CHANNEL_0
|
|
||||||
#define TIMER LEDC_TIMER_0
|
|
||||||
#define SPEED_MODE LEDC_HIGH_SPEED_MODE
|
|
||||||
|
|
||||||
void PWM::begin(unsigned gpio) {
|
|
||||||
ledc_timer_config_t timer_conf;
|
|
||||||
timer_conf.duty_resolution = LEDC_TIMER_10_BIT;
|
|
||||||
timer_conf.freq_hz = 440;
|
|
||||||
timer_conf.speed_mode = SPEED_MODE;
|
|
||||||
timer_conf.timer_num = TIMER;
|
|
||||||
ESP_ERROR_CHECK(::ledc_timer_config(&timer_conf));
|
|
||||||
|
|
||||||
ledc_channel_config_t ledc_conf;
|
|
||||||
ledc_conf.channel = CHANNEL;
|
|
||||||
ledc_conf.duty = 0;
|
|
||||||
ledc_conf.gpio_num = gpio;
|
|
||||||
ledc_conf.intr_type = LEDC_INTR_DISABLE;
|
|
||||||
ledc_conf.speed_mode = SPEED_MODE;
|
|
||||||
ledc_conf.timer_sel = TIMER;
|
|
||||||
ESP_ERROR_CHECK(::ledc_channel_config(&ledc_conf));
|
|
||||||
}
|
|
||||||
|
|
||||||
void PWM::set_duty(unsigned duty) {
|
|
||||||
ESP_ERROR_CHECK(::ledc_set_duty(SPEED_MODE, CHANNEL, duty));
|
|
||||||
ESP_ERROR_CHECK(::ledc_update_duty(SPEED_MODE, CHANNEL));
|
|
||||||
}
|
|
||||||
|
|
||||||
void PWM::stop() {
|
|
||||||
set_duty(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void PWM::set_freq(unsigned freq) {
|
|
||||||
ESP_ERROR_CHECK(::ledc_set_freq(SPEED_MODE, TIMER, freq));
|
|
||||||
}
|
|
||||||
|
|
||||||
#elif defined(PWM_SOUND) && defined(ESP8266)
|
|
||||||
#include <core_esp8266_waveform.h>
|
|
||||||
|
|
||||||
static unsigned gpio, duty;
|
|
||||||
const unsigned period = 1024;
|
|
||||||
|
|
||||||
void PWM::begin(unsigned gpio) {
|
void PWM::begin(unsigned gpio) {
|
||||||
::gpio = gpio;
|
::gpio = gpio;
|
||||||
pinMode(gpio, OUTPUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void PWM::set_duty(unsigned duty) {
|
|
||||||
::duty = duty;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PWM::stop() {
|
void PWM::stop() {
|
||||||
stopWaveform(gpio);
|
noTone(gpio);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PWM::set_freq(unsigned freq) {
|
void PWM::start(unsigned freq) {
|
||||||
uint32_t t = 1000000 / freq;
|
tone(gpio, freq);
|
||||||
uint32_t h = duty * t / period;
|
|
||||||
startWaveform(gpio, h, t-h, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
|
||||||
#pragma message "No PWM"
|
|
||||||
|
|
||||||
void PWM::begin(unsigned gpio) {
|
|
||||||
}
|
|
||||||
|
|
||||||
void PWM::set_duty(unsigned duty) {
|
|
||||||
}
|
|
||||||
|
|
||||||
void PWM::stop() {
|
|
||||||
}
|
|
||||||
|
|
||||||
void PWM::set_freq(unsigned freq) {
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
@ -4,9 +4,8 @@
|
|||||||
class PWM {
|
class PWM {
|
||||||
public:
|
public:
|
||||||
void begin(unsigned gpio);
|
void begin(unsigned gpio);
|
||||||
void set_duty(unsigned duty);
|
|
||||||
void stop();
|
void stop();
|
||||||
void set_freq(unsigned freq);
|
void start(unsigned freq);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user