Porting driver to Java

I finally got back to working on the java drivers for PivotPi. I noticed this chunk of code in PCA9685.py.

def set_pwm_freq(self, freq_hz):
    """Set the PWM frequency to the provided value in hertz."""
    prescaleval = 25000000.0    # 25MHz
    prescaleval /= 4096.0       # 12-bit
    prescaleval /= float(freq_hz)
    prescaleval -= 1.0

Am I missing something, why not just make it one line?


peter lin

I’m almost ready to unit test the driver. Hopefully I can find a couple hours this week.

I got the basic driver working. you can find the code here https://github.com/woolfel/PivotPi.

I created a pull request.

Hi @woolfel,

Well, indeed we could turn those couple of lines into one, but there would be one disadvantage.

By “compressing” all those mathematical operations into one, you “loose” information and therefore whoever reads the source code, no longer understands the relationship between freq_hz and prescaleval variables.

Since the Pi has enough processing power for handling it, we shouldn’t care too much about it - not in this case where we don’t do performance stuff.

Here’s a stackexchange answer that better explains this concept:

As for the Pull Request you’ve just created, please give us some time to test it (we already have some work on other PRs).
But I want to thank you for taking the time to write this Java library in such depth.
We might turn it into a tutorial like the following one:

Thank you!

I plan to add javadocs to it tomorrow or later this week. If I have time over the holiday, I may write a tutorial like the other ones I’ve written for BrickPi on my blog.

I tried to keep the Java driver similar to the python, so that’s why I asked the question. It wasn’t clear to me why 25Mhz and 12-bit are important. Usually in Java docs I try to explain why certain constants are used, since the user might not know. It’s good to have multiple lines, but it also helps to explain the significance of 25Mhz and 12-bit :slight_smile:

I added a quick readme and some javadocs

Hi @woolfel,

Thank you a lot for your contribution.
As soon as we have some spare time on our hands we will review it and give you a feedback - this generally means we will do a code review, test the code and then check for any inconsistencies.

I have added the pending label, which means it’s on our priority stack.

Edit: I’ve reviewed the PR - it’s okay.

Thank you!