Pi4J / pi4j-v1

DEPRECATED Java I/O library for Raspberry Pi (GPIO, I2C, SPI, UART)
http://www.pi4j.com
Apache License 2.0
1.3k stars 446 forks source link

SPI (wiringPiSPIDataRW) needs byte array argument #29

Closed savageautomate closed 11 years ago

savageautomate commented 11 years ago

@rlsutton1

For when you get around to implementing it... I've been using the Spi class, I currently see a significant issue with the exposed method Spi.wiringPiSPIDataRW in that it takes a string as the data to send.

The device I'm using needs to receive binary data, converting a byte array to a string doesn't really work in java - unicode and all. Also I don't think too many devices will take kindly to receiving unicode strings.

After much testing, the unicode issue is a big problem - a single byte becomes 1 or more bytes depending on the value. I've tried various encoding techniques to get around this without any luck. Also it's not possible to pass 0's as they are either being omitted or are terminating the string (not sure which).

savageautomate commented 11 years ago

I have attempted to fix this in the latest 0.0.5-SNAPSHOT. Please download the latest build and test this out.
(Note: I don't have an SPI device on hand to test with, so it may be a bit of a shot in the dark.)

https://github.com/Pi4J/pi4j/blob/c5fa25e7d91099827b7953f8566e3347a1e487e4/pi4j-core/src/main/java/com/pi4j/wiringpi/Spi.java#L140

rlsutton1 commented 11 years ago

Working beautifully.

On a side note, I bought a tlc5940 to provide 16pwm outputs which uses SPI. I've decided not to use it as it requires level shifters, ties up a lot of gpio pins as well as using clock cycles to reset it's counter 1000 times a second ( the clock resetting could also be achieved by sacrificing one of the tlc5940 outputs and using a pi4j interrupt - still uses clock cycles though).

The code I wrote includes a (possibly) generically useful piece of code to produce a byte array representing n x 12 bit values MSB first as required by the tlc5490.

The question is should I just throw the code I wrote for it or would you be interested in me wrapping up the code in a "Provider" to be included in pi4j?

Adafruit's PCA9685 board is so much a better option for pwm, as it's i2c and has it's own clock. - Which is perhaps a good reason not to implement code for the tlc5490.

If your interested let me know.

anyway good work on the fix for the JNI SPI Byte[].

savageautomate commented 11 years ago

If you think your code could benefit others, then by all means a GPIO provider impl class would be awesome. The project does not currently have any GPIO providers for PWM expansion.

savageautomate commented 11 years ago

Fixed in 0.0.5-SNAPSHOT, issue closed.