фиксы до работы с перемычкой вместо оптики
This commit is contained in:
@@ -1,12 +1,18 @@
|
||||
#include "Pwm.h"
|
||||
#include "Config.h"
|
||||
#include "Core.h"
|
||||
|
||||
void PwmGenerator::begin() { pinMode(GPIO_PWM, OUTPUT); stop(); }
|
||||
void PwmGenerator::begin() {
|
||||
// Match LEDC_SOURCE_CLOCK_HZ and make the timer calculation deterministic.
|
||||
ledcSetClockSource(LEDC_USE_XTAL_CLK);
|
||||
pinMode(GPIO_PWM, OUTPUT);
|
||||
stop();
|
||||
}
|
||||
|
||||
bool PwmGenerator::start(uint32_t hz, uint8_t dutyPct, ActualPwm &a) {
|
||||
stop();
|
||||
uint8_t bits = LEDC_MAX_BITS;
|
||||
while (bits > 1 && static_cast<uint64_t>(hz) * (1ULL << bits) > 80000000ULL) --bits;
|
||||
const uint8_t bits = choosePwmResolution(hz, LEDC_SOURCE_CLOCK_HZ, LEDC_MAX_BITS);
|
||||
if (!bits) return false;
|
||||
if (!ledcAttachChannel(GPIO_PWM, hz, bits, LEDC_CHANNEL)) return false;
|
||||
const uint32_t top = (1UL << bits) - 1UL;
|
||||
const uint32_t duty = (static_cast<uint64_t>(top) * dutyPct + 50U) / 100U;
|
||||
@@ -18,20 +24,6 @@ bool PwmGenerator::start(uint32_t hz, uint8_t dutyPct, ActualPwm &a) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PwmGenerator::preview(uint32_t hz, uint8_t dutyPct, ActualPwm &a) {
|
||||
stop();
|
||||
uint8_t bits = LEDC_MAX_BITS;
|
||||
while (bits > 1 && static_cast<uint64_t>(hz) * (1ULL << bits) > 80000000ULL) --bits;
|
||||
if (!ledcAttachChannel(GPIO_PWM, hz, bits, LEDC_CHANNEL)) return false;
|
||||
ledcWriteChannel(LEDC_CHANNEL, 0); // query hardware without emitting test pulses
|
||||
const uint32_t actualHz = ledcReadFreq(GPIO_PWM);
|
||||
const uint32_t top = (1UL << bits) - 1UL;
|
||||
const uint32_t duty = (static_cast<uint64_t>(top) * dutyPct + 50U) / 100U;
|
||||
a = {hz, actualHz, 100.0f * duty / top, bits};
|
||||
ledcDetach(GPIO_PWM); pinMode(GPIO_PWM, OUTPUT); digitalWrite(GPIO_PWM, PWM_SAFE_LEVEL);
|
||||
return actualHz != 0;
|
||||
}
|
||||
|
||||
void PwmGenerator::stop() {
|
||||
if (running_) ledcDetach(GPIO_PWM);
|
||||
pinMode(GPIO_PWM, OUTPUT); digitalWrite(GPIO_PWM, PWM_SAFE_LEVEL);
|
||||
|
||||
Reference in New Issue
Block a user