1
0
mirror of https://github.com/jscrane/r65emu.git synced 2024-10-31 13:05:08 +00:00
r65emu/sound_pwm.cpp

83 lines
1.7 KiB
C++
Raw Normal View History

2018-11-09 07:25:13 +00:00
#include <Arduino.h>
2018-09-11 15:06:52 +00:00
#include "sound_pwm.h"
#include "hardware.h"
2018-11-25 13:21:05 +00:00
#if defined(PWM_SOUND) && defined(ESP32)
2018-09-11 15:06:52 +00:00
#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));
}
2018-11-25 13:21:05 +00:00
#elif defined(PWM_SOUND) && defined(ESP8266)
2018-11-18 11:57:51 +00:00
#include <core_esp8266_waveform.h>
static unsigned gpio, duty;
const unsigned period = 1024;
2018-11-09 07:25:13 +00:00
2018-09-11 15:06:52 +00:00
void PWM::begin(unsigned gpio) {
2018-11-18 11:57:51 +00:00
::gpio = gpio;
pinMode(gpio, OUTPUT);
2018-09-11 15:06:52 +00:00
}
2018-09-14 13:19:07 +00:00
void PWM::set_duty(unsigned duty) {
2018-11-18 11:57:51 +00:00
::duty = duty;
2018-09-11 15:06:52 +00:00
}
void PWM::stop() {
2018-11-18 11:57:51 +00:00
stopWaveform(gpio);
2018-09-11 15:06:52 +00:00
}
void PWM::set_freq(unsigned freq) {
2018-11-18 11:57:51 +00:00
uint32_t t = 1000000 / freq;
uint32_t h = duty * t / period;
startWaveform(gpio, h, t-h, 0);
2018-09-11 15:06:52 +00:00
}
2018-11-11 11:59:43 +00:00
#else
2018-11-25 13:21:05 +00:00
#pragma message "No PWM"
2018-11-11 11:59:43 +00:00
void PWM::begin(unsigned gpio) {
}
void PWM::set_duty(unsigned duty) {
}
void PWM::stop() {
}
void PWM::set_freq(unsigned freq) {
}
2018-09-11 15:06:52 +00:00
#endif