Skip to content

Commit c377c86

Browse files
authored
Merge pull request #9 from JohannesAck/master
Fix of pwm.py to work with the new sysfs on V3b
2 parents 27833d4 + 1f7b1a5 commit c377c86

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

‎pcduino/gpio.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pinmap import PinMap
2+
import os.path
23

34

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

3637
def pin_mode(channel, mode):
3738
""" Set Mode of a GPIO channel """
39+
40+
# has to disable new sysfs pwm
41+
pwmPath = '/sys/class/misc/pwmtimer/enable/'
42+
ending = 'pwm' + str(channel)
43+
if os.path.isfile(os.path.join(pwmPath ,ending)):
44+
with open(os.path.join(pwmPath, ending), 'w+') as f:
45+
f.write('0')
46+
3847
path = gpio_mode_pins.get_path(channel)
3948
with open(path, 'w+') as f:
4049
f.write('0' if mode == INPUT else '1')

‎pcduino/pwm.py‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import os.path
2-
3-
from pinmap import PinMap
4-
5-
6-
pins = PinMap('/sys/class/leds', 'pwm', 5)
2+
from os import listdir
73

84
MAX_PWM_LEVEL = 255
95

@@ -13,14 +9,30 @@ def analog_write(pin, value):
139
value can be an number between 0 and 255.
1410
1511
"""
16-
path = pins.get_path(pin)
1712

18-
with open(os.path.join(path, 'max_brightness')) as f:
13+
path = '/sys/class/misc/pwmtimer/'
14+
15+
ending = 'pwm' + str(pin)
16+
17+
pins = listdir(os.path.join(path, 'enable'))
18+
if not ending in pins:
19+
raise ValueError("Pin not found, PWM only possible on " + " ".join(str(p) for p in pins) + ".")
20+
21+
with open(os.path.join(path, 'max_level',ending)) as f:
1922
max_value = int(f.read())
2023

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

24-
map_level = (max_value * value) / MAX_PWM_LEVEL
25-
with open(os.path.join(path, 'brightness'), 'w+') as f:
27+
map_level = ((max_value-1) * value) // MAX_PWM_LEVEL
28+
#-1 because if it puts max_value the duty cycle somehow becomes 0 (overflow)
29+
30+
#disable -> change level -> enable , as requested by documentation
31+
with open(os.path.join(path, 'enable', ending), 'w+') as f:
32+
f.write("0\n")
33+
34+
with open(os.path.join(path, 'level', ending), 'w+') as f:
2635
f.write("%d\n" % map_level)
36+
37+
with open(os.path.join(path, 'enable', ending), 'w+') as f:
38+
f.write("1\n")

0 commit comments

Comments
 (0)