Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PWM bug when pin already exported #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions OPi/GPIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,15 +725,9 @@ def __init__(self, chip, pin, frequency, duty_cycle_percent, invert_polarity=Fal
self.duty_cycle_percent = duty_cycle_percent
self.invert_polarity = invert_polarity

# export pwm
try:
sysfs.PWM_Export(chip, pin) # creates the pwm sysfs object
if invert_polarity is True:
sysfs.PWM_Polarity(chip, pin, invert=True) # invert pwm i.e the duty cycle tells you how long the cycle is off
else:
sysfs.PWM_Polarity(chip, pin, invert=False) # don't invert the pwm signal. This is the normal way its used.
sysfs.PWM_Enable(chip, pin)
return sysfs.PWM_Frequency(chip, pin, frequency)

except (OSError, IOError) as e:
if e.errno == 16: # Device or resource busy
warnings.warn("Pin {0} is already in use, continuing anyway.".format(pin), stacklevel=2)
Expand All @@ -742,6 +736,16 @@ def __init__(self, chip, pin, frequency, duty_cycle_percent, invert_polarity=Fal
else:
raise e

# invert polarity if needed
if invert_polarity is True:
sysfs.PWM_Polarity(chip, pin, invert=True) # invert pwm i.e the duty cycle tells you how long the cycle is off
else:
sysfs.PWM_Polarity(chip, pin, invert=False) # don't invert the pwm signal. This is the normal way its used.

# enable pwm
sysfs.PWM_Enable(chip, pin)
sysfs.PWM_Frequency(chip, pin, frequency)

def start_pwm(self): # turn on pwm by setting the duty cycle to what the user specified
"""
Start PWM Signal.
Expand All @@ -755,7 +759,7 @@ def stop_pwm(self): # turn on pwm by setting the duty cycle to 0
return sysfs.PWM_Duty_Cycle_Percent(self.chip, self.pin, 0) # duty cycle at 0 is the equivilant of off

def change_frequency(self, new_frequency):
# Order of opperations:
# Order of operations:
# 1. convert to period
# 2. check if period is increasing or decreasing
# 3. If increasing update pwm period and then update the duty cycle period
Expand Down