Skip to content

Commit 175d9fd

Browse files
committed
Fixed a bug that duty was not set correctly when PWM#duty was called directly.
1 parent a4ee58e commit 175d9fd

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

mrblib/pwm.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def initialize(pin = nil, **kwargs)
1919

2020
def frequency(freq)
2121
self.f = freq
22-
print(self.f)
2322
ESP32::LEDC.set_freq(self.group, self.timer, self.f)
2423
end
2524

@@ -28,12 +27,12 @@ def period_us(micro_sec)
2827
end
2928

3029
def duty(percent)
31-
self.d = Rational(percent, 100).to_i
32-
ESP32::LEDC.set_duty(self.group, self.channel, self.d)
30+
self.d = Rational(percent, 100)
31+
ESP32::LEDC.set_duty(self.group, self.channel, (self.d * (2 ** self.resolution)).to_i)
3332
end
3433

3534
def pulse_width_us(micro_sec)
36-
us_per_bit = Rational(1_000_000, self.f) / (2 ** self.resolution)
35+
us_per_bit = Rational(1_000_000, self.f)
3736
duty((Rational(micro_sec, us_per_bit) * 100).to_i)
3837
end
3938
end

0 commit comments

Comments
 (0)