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

pwmFrequency int division vs float division #103

Open mgeorgiadis opened 8 years ago

mgeorgiadis commented 8 years ago

Hi,

In the PWM module, I've been having some slight issues with the pwmFrequency command. Each time I change the frequency using pwmFrequency, the duty cycle keeps getting set to 0. I took the liberty of inserting some print statements into the source code and found that in line 65 of pwm.py, the duty_percent is calculated with: duty_percent = old_duty_ns / old_period_ns Both of those numbers are integers (in nanoseconds) and in python2 this will return an integer result. Since this result is a fraction, it gets set to 0 (confirmed by printing the duty percent). I found that replacing line 65 with: duty_percent = float(old_duty_ns) / float(old_period_ns) solved the problem for me.