fix crash after lowering period in config file
This commit is contained in:
parent
06e6069b53
commit
6a713c5b7e
1 changed files with 8 additions and 1 deletions
9
fand.py
9
fand.py
|
@ -36,6 +36,7 @@ class PWM:
|
||||||
self.chip_base = "/sys/class/pwm/pwmchip%d/" %(chip)
|
self.chip_base = "/sys/class/pwm/pwmchip%d/" %(chip)
|
||||||
self.ch_base = "/sys/class/pwm/pwmchip%d/pwm%d/" %(chip, ch)
|
self.ch_base = "/sys/class/pwm/pwmchip%d/pwm%d/" %(chip, ch)
|
||||||
self.period = period
|
self.period = period
|
||||||
|
self.duty_cycle = 0
|
||||||
|
|
||||||
# export the channel to sysfs if necessary
|
# export the channel to sysfs if necessary
|
||||||
if not os.path.isdir(self.ch_base):
|
if not os.path.isdir(self.ch_base):
|
||||||
|
@ -45,6 +46,9 @@ class PWM:
|
||||||
# disable channel
|
# disable channel
|
||||||
self.set_enabled(False)
|
self.set_enabled(False)
|
||||||
|
|
||||||
|
# set 0 duty cycle so period can be freely set
|
||||||
|
self.set_duty_cycle(0)
|
||||||
|
|
||||||
# set period & polarity
|
# set period & polarity
|
||||||
with open(self.ch_base + "period", "w") as f:
|
with open(self.ch_base + "period", "w") as f:
|
||||||
f.write(str(period))
|
f.write(str(period))
|
||||||
|
@ -71,7 +75,10 @@ class PWM:
|
||||||
f.write(requested)
|
f.write(requested)
|
||||||
|
|
||||||
def set_percent(self, pc):
|
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.seek(0)
|
||||||
self.f_dc.write(str(self.duty_cycle))
|
self.f_dc.write(str(self.duty_cycle))
|
||||||
self.f_dc.flush()
|
self.f_dc.flush()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue