Skip to content

Commit

Permalink
Debug flag
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.code.sf.net/p/lejos/code/trunk@6796 06c8f799-c779-4783-8af7-179afedb67c8
  • Loading branch information
lgriffiths committed Aug 21, 2012
1 parent baa838d commit 0075c63
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions rpi/rpijni/sensorport.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
#include "lejos_nxt_SensorPort.h"
#include "lejos_nxt_NXTEvent.h"

#define debug 0

int fd; // File description
char *fileName = "/dev/i2c-0"; // Name of the port we will be using

JNIEXPORT void JNICALL Java_lejos_nxt_SensorPort_i2cEnableById
(JNIEnv *env, jclass cls, jint id, jint mode)
{
printf("Enabling port %d in mode %d\n",id,mode);
if (debug) printf("Enabling port %d in mode %d\n",id,mode);
if ((fd = open(fileName, O_RDWR)) < 0) { // Open port for reading and writing
printf("Failed to open i2c port\n");
exit(1);
Expand All @@ -29,20 +31,20 @@ JNIEXPORT void JNICALL Java_lejos_nxt_SensorPort_i2cEnableById
JNIEXPORT void JNICALL Java_lejos_nxt_SensorPort_i2cDisableById
(JNIEnv *env, jclass cls, jint id)
{
printf("Disabling port %d\n",id);
if (debug) printf("Disabling port %d\n",id);
close(fd);
}

JNIEXPORT jint JNICALL Java_lejos_nxt_SensorPort_i2cStartById
(JNIEnv *env, jclass cls, jint id, jint addr, jbyteArray buf, jint offset, jint wlen, jint rlen)
{
printf("Writing %d bytes on port %d at address %d\n",wlen,id,addr);
if (debug)printf("Writing %d bytes on port %d at address %d\n",wlen,id,addr);
if (ioctl(fd, I2C_SLAVE, (addr >> 1)) < 0) { // Set the port options and set the address of the device we wish to speak to
printf("Unable to get bus access to talk to slave\n");
exit(1);
}
char *jb = (char *) (*env)->GetByteArrayElements(env, buf, 0);
printf("First byte is %d\n",*jb);
if (debug) printf("First byte is %d\n",*jb);
if ((write(fd, jb, wlen)) != wlen) { // Send the register to read from
printf("Error writing to i2c slave - %s\n",strerror(errno));
exit(1);
Expand All @@ -54,7 +56,7 @@ JNIEXPORT jint JNICALL Java_lejos_nxt_SensorPort_i2cStartById
JNIEXPORT jint JNICALL Java_lejos_nxt_SensorPort_i2cCompleteById
(JNIEnv *env, jclass cls, jint id, jbyteArray buf, jint offset, jint rlen)
{
printf("Reading %d bytes from port %d\n",rlen,id);
if (debug) printf("Reading %d bytes from port %d\n",rlen,id);
if (rlen > 0) {
char *jb = (char *) (*env)->GetByteArrayElements(env, buf, 0);
if (read(fd, jb, rlen) != rlen) { // Read back data into buf[]
Expand Down

0 comments on commit 0075c63

Please sign in to comment.