From 6a713c5b7e15c975a0ba815ef282784d7ba776e5 Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Thu, 29 Jul 2021 22:49:26 +0200 Subject: [PATCH] fix crash after lowering period in config file --- fand.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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()