graycatlabs / PyBBIO

A Python library for Arduino-style hardware IO support on the Beaglebone
https://github.com/graycatlabs/PyBBIO/wiki
MIT License
251 stars 89 forks source link

analogWrite() not always sets the value #77

Closed n-epifanov closed 9 years ago

n-epifanov commented 9 years ago

Hi,

looking at the code I noticed pwm's analogWrite() just reuses previous period:

    period_ns = int(sysfs.kernelFileIO('%s/%s' % (helper_path, PWM_PERIOD)))

which means if e.g. pwmWrite() was used to set some low frequency, next analogWrite() won't set output correctly.

alexanderhiam commented 9 years ago

That's reading the period of the PWM waveform, i.e. the total time of one full cycle. The duty cycle is set by writing the duration of the high (or low if the polarity is inverted) pulse, so that has to be calculated based on the PWM period. In other words that should be correct as is.

n-epifanov commented 9 years ago

Oh, sorry, missed that the cycle can be asymmetric. You're right.

n-epifanov commented 9 years ago

On the other hand e.g.

duty_ns = int(value * (period_ns/resolution))

https://github.com/graycatlabs/PyBBIO/blob/master/bbio/platform/beaglebone/pwm.py#L37 looks like is using integer division, in python 2.7 "/" for ints returns int. Is it intentional?

from __future__ import division

would help.