Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
attempting to fix #29
Browse files Browse the repository at this point in the history
  • Loading branch information
savageautomate committed Jan 8, 2013
1 parent b5a120c commit 9e727fb
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
19 changes: 19 additions & 0 deletions pi4j-core/src/main/java/com/pi4j/wiringpi/Spi.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@ private Spi() {
*/
public static native int wiringPiSPIDataRW(int channel, String data, int len);

/**
* <p>wiringPiSPIDataRW:</p>
*
* <p>
* Write and Read a block of data over the SPI bus. Note the data is being read into the
* transmit buffer, so will overwrite it! This is also a full-duplex operation.
* </p>
*
* <p>
* (ATTENTION: the 'data' argument can only be a maximum of 1024 characters.)
* </p>
*
* @param channel SPI channel</p>
* @param data
* @param len
* @return return -1 on error
*/
public static native int wiringPiSPIDataRW(int channel, byte[] data, int len);

/**
* <p>wiringPiSPISetup:</p>
*
Expand Down
2 changes: 1 addition & 1 deletion pi4j-native/src/main/native/com_pi4j_wiringpi_Serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ JNIEXPORT void JNICALL Java_com_pi4j_wiringpi_Serial_serialPuts
(JNIEnv *env, jobject obj, jint fd, jstring data)
{
char datachararr[2048];
int len = (*env)->GetStringLength(env, data);
int len = (*env)->GetStringUTFLength(env, data);
(*env)->GetStringUTFRegion(env, data, 0, len, datachararr);
serialPuts(fd, datachararr);
}
Expand Down
41 changes: 35 additions & 6 deletions pi4j-native/src/main/native/com_pi4j_wiringpi_Spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,48 @@ JNIEXPORT jint JNICALL Java_com_pi4j_wiringpi_Spi_wiringPiSPIGetFd
* Method: wiringPiSPIDataRW
* Signature: (ILjava/lang/String;I)I
*/
JNIEXPORT jint JNICALL Java_com_pi4j_wiringpi_Spi_wiringPiSPIDataRW
JNIEXPORT jint JNICALL Java_com_pi4j_wiringpi_Spi_wiringPiSPIDataRW__ILjava_lang_String_2I
(JNIEnv *env, jclass class, jint channel, jstring data, jint length)
{
char datachararr[2048];
int len = (*env)->GetStringLength(env, data);
(*env)->GetStringUTFRegion(env, data, 0, len, datachararr);
jint result = wiringPiSPIDataRW(channel, (unsigned char *)datachararr, length);
jstring returnString = (*env)->NewStringUTF(env, datachararr);
char buffer[2048];
int len = (*env)->GetStringUTFLength(env, data);
(*env)->GetStringUTFRegion(env, data, 0, len, buffer);
jint result = wiringPiSPIDataRW(channel, (unsigned char *)buffer, length);
jstring returnString = (*env)->NewStringUTF(env, buffer);
data = returnString;

return result;
}

/*
* Class: com_pi4j_wiringpi_Spi
* Method: wiringPiSPIDataRW
* Signature: (I[BI)I
*/
JNIEXPORT jint JNICALL Java_com_pi4j_wiringpi_Spi_wiringPiSPIDataRW__I_3BI
(JNIEnv *env, jclass class, jint channel, jbyteArray data, jint length)
{
int i;
unsigned char buffer[2048];

// copy the bytes from the data array argument into a native character buffer
jbyte *body = (*env)->GetByteArrayElements(env, data, 0);
for (i = 0; i < length; i++) {
buffer[i] = body[i + offset];
}

jint result = wiringPiSPIDataRW(channel, (unsigned char *)buffer, length);

// copy the resulting buffer bytes back into the data array argument
for (i = 0; i < length; i++) {
body[i] = buffer[i];
}
(*env)->ReleaseByteArrayElements(env, data, bodyReturn, 0);

return result;
}


/*
* Class: com_pi4j_wiringpi_Spi
* Method: wiringPiSPISetup
Expand Down
10 changes: 9 additions & 1 deletion pi4j-native/src/main/native/com_pi4j_wiringpi_Spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,17 @@ JNIEXPORT jint JNICALL Java_com_pi4j_wiringpi_Spi_wiringPiSPIGetFd
* Method: wiringPiSPIDataRW
* Signature: (ILjava/lang/String;I)I
*/
JNIEXPORT jint JNICALL Java_com_pi4j_wiringpi_Spi_wiringPiSPIDataRW
JNIEXPORT jint JNICALL Java_com_pi4j_wiringpi_Spi_wiringPiSPIDataRW__ILjava_lang_String_2I
(JNIEnv *, jclass, jint, jstring, jint);

/*
* Class: com_pi4j_wiringpi_Spi
* Method: wiringPiSPIDataRW
* Signature: (I[BI)I
*/
JNIEXPORT jint JNICALL Java_com_pi4j_wiringpi_Spi_wiringPiSPIDataRW__I_3BI
(JNIEnv *, jclass, jint, jbyteArray, jint);

/*
* Class: com_pi4j_wiringpi_Spi
* Method: wiringPiSPISetup
Expand Down

0 comments on commit 9e727fb

Please sign in to comment.