diff --git a/pcduino/gpio.py b/pcduino/gpio.py index e192f5d..71f0693 100644 --- a/pcduino/gpio.py +++ b/pcduino/gpio.py @@ -1,4 +1,5 @@ from pinmap import PinMap +import os.path __all__ = ['HIGH', 'LOW', 'INPUT', 'OUTPUT','digital_write', 'digital_read', @@ -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') diff --git a/pcduino/pwm.py b/pcduino/pwm.py index 6cc27c7..8036a55 100755 --- a/pcduino/pwm.py +++ b/pcduino/pwm.py @@ -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 @@ -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")