Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions pcduino/gpio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pinmap import PinMap
import os.path


__all__ = ['HIGH', 'LOW', 'INPUT', 'OUTPUT','digital_write', 'digital_read',
Expand Down Expand Up @@ -35,6 +36,14 @@ def digital_read(channel):

def pin_mode(channel, mode):
""" Set Mode of a GPIO channel """

# has to disable new sysfs pwm
pwmPath = '/sys/class/misc/pwmtimer/enable/'
ending = 'pwm' + str(channel)
if os.path.isfile(os.path.join(pwmPath ,ending)):
with open(os.path.join(pwmPath, ending), 'w+') as f:
f.write('0')

path = gpio_mode_pins.get_path(channel)
with open(path, 'w+') as f:
f.write('0' if mode == INPUT else '1')
30 changes: 21 additions & 9 deletions pcduino/pwm.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import os.path

from pinmap import PinMap


pins = PinMap('/sys/class/leds', 'pwm', 5)
from os import listdir

MAX_PWM_LEVEL = 255

Expand All @@ -13,14 +9,30 @@ def analog_write(pin, value):
value can be an number between 0 and 255.

"""
path = pins.get_path(pin)

with open(os.path.join(path, 'max_brightness')) as f:
path = '/sys/class/misc/pwmtimer/'

ending = 'pwm' + str(pin)

pins = listdir(os.path.join(path, 'enable'))
if not ending in pins:
raise ValueError("Pin not found, PWM only possible on " + " ".join(str(p) for p in pins) + ".")

with open(os.path.join(path, 'max_level',ending)) as f:
max_value = int(f.read())

if value < 0 or value > MAX_PWM_LEVEL:
raise ValueError("value must be between 0 and %s" % MAX_PWM_LEVEL)

map_level = (max_value * value) / MAX_PWM_LEVEL
with open(os.path.join(path, 'brightness'), 'w+') as f:
map_level = ((max_value-1) * value) // MAX_PWM_LEVEL
#-1 because if it puts max_value the duty cycle somehow becomes 0 (overflow)

#disable -> change level -> enable , as requested by documentation
with open(os.path.join(path, 'enable', ending), 'w+') as f:
f.write("0\n")

with open(os.path.join(path, 'level', ending), 'w+') as f:
f.write("%d\n" % map_level)

with open(os.path.join(path, 'enable', ending), 'w+') as f:
f.write("1\n")