diff --git a/fand.py b/fand.py index 718ade8..a1aef52 100755 --- a/fand.py +++ b/fand.py @@ -36,6 +36,7 @@ class PWM: self.chip_base = "/sys/class/pwm/pwmchip%d/" %(chip) self.ch_base = "/sys/class/pwm/pwmchip%d/pwm%d/" %(chip, ch) self.period = period + self.duty_cycle = 0 # export the channel to sysfs if necessary if not os.path.isdir(self.ch_base): @@ -45,6 +46,9 @@ class PWM: # disable channel self.set_enabled(False) + # set 0 duty cycle so period can be freely set + self.set_duty_cycle(0) + # set period & polarity with open(self.ch_base + "period", "w") as f: f.write(str(period)) @@ -71,7 +75,10 @@ class PWM: f.write(requested) def set_percent(self, pc): - self.duty_cycle = int(self.period * pc/100) + self.set_duty_cycle(self.period * pc/100) + + def set_duty_cycle(self, dc): + self.duty_cycle = int(dc) self.f_dc.seek(0) self.f_dc.write(str(self.duty_cycle)) self.f_dc.flush()