Skip to content

A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol

rework pwm cmd

Geo8e4597f2b5c6
diff --git a/src/Controllers/DioController.cpp b/src/Controllers/DioController.cpp
index 0d7a7a1d..2610026b 100644
--- a/src/Controllers/DioController.cpp
+++ b/src/Controllers/DioController.cpp
@@ -221,9 +221,10 @@ void DioController::handlePwm(const TerminalCommand& cmd) {
return;
}
- terminalView.println("DIO PWM: Pin " + std::to_string(pin) +
+ terminalView.println("\nDIO PWM: Pin " + std::to_string(pin) +
" (" + std::to_string(freq) + "Hz, " +
- std::to_string(duty) + "% duty).");
+ std::to_string(duty) + "% duty)... Use 'reset " +
+ std::to_string(pin) + "' to stop.\n");
}
diff --git a/src/Services/PinService.cpp b/src/Services/PinService.cpp
index 60e13c89..24bbdc98 100644
--- a/src/Services/PinService.cpp
+++ b/src/Services/PinService.cpp
@@ -62,25 +62,21 @@ int PinService::readAnalog(uint8_t pin) {
bool PinService::setupPwm(uint8_t pin, uint32_t freq, uint8_t dutyPercent) {
if (dutyPercent > 100) dutyPercent = 100;
- int channel = pin % 8;
-
- // Find the highest compatible resolution
- int resolution = -1;
- for (int bits = 14; bits >= 1; --bits) {
- if (isPwmFeasible(freq, bits)) {
- if (ledcSetup(channel, freq, bits)) { // ok
- resolution = bits;
- break;
- }
- }
+ int channel = pin % 16;
+ const int resolution = 8;
+
+ ledcDetachPin(pin);
+
+ if (!ledcSetup(channel, freq, resolution)) {
+ return false;
}
- if (resolution < 0) return false;
ledcAttachPin(pin, channel);
uint32_t dutyMax = (1UL << resolution) - 1;
- uint32_t dutyVal = ((uint32_t)dutyPercent * dutyMax) / 100U;
+ uint32_t dutyVal = (dutyPercent * dutyMax) / 100;
ledcWrite(channel, dutyVal);
+
return true;
}
@@ -104,21 +100,6 @@ void PinService::setServoAngle(uint8_t pin, uint8_t angle) {
ledcWrite(channel, dutyVal);
}
-bool PinService::isPwmFeasible(uint32_t freq, uint8_t resolutionBits) {
- if (resolutionBits < 1 || resolutionBits > 14) return false;
-
- const uint32_t baseClkHz = 80000000UL; // 80 MHz
- const uint32_t maxDivParam = 0x3FFFF; // limite
- if (freq == 0) return false;
-
- // div_param = baseClk / (freq * 2^resolution)
- uint64_t denom = (uint64_t)freq * (1ULL << resolutionBits);
- if (denom == 0) return false;
-
- uint32_t divParam = (uint32_t)(baseClkHz / denom);
- return (divParam >= 1 && divParam <= maxDivParam);
-}
-
PinService::pullType PinService::getPullType(uint8_t pin){
return(pullState[pin]);
}
diff --git a/src/Services/PinService.h b/src/Services/PinService.h
index 91f5daf3..d0f278cc 100644
--- a/src/Services/PinService.h
+++ b/src/Services/PinService.h
@@ -22,6 +22,5 @@ public:
pullType getPullType(uint8_t pin);
std::vector<uint8_t> getConfiguredPullPins();
private:
- bool isPwmFeasible(uint32_t freq, uint8_t resolutionBits);
std::unordered_map<uint8_t, pullType> pullState;
};