Skip to content

Commit

Permalink
Merge pull request Pi4J#6 from Pi4J/working
Browse files Browse the repository at this point in the history
Working
  • Loading branch information
savageautomate committed Aug 26, 2019
2 parents 89dbc65 + 18c3ec3 commit 51ef603
Show file tree
Hide file tree
Showing 104 changed files with 8,874 additions and 1,396 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,14 @@ Thumbs.db
*.zip
*.deb
/nbactions.xml

# PlatformIO specific IDE files
.pioenvs
.piolibdeps
.pio

# Microsoft Visual Studio Code IDE files
.vscode
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import java.io.IOException;

public interface PiGpio extends PiGpio_I2C, PiGpio_GPIO, PiGpio_PWM {
public interface PiGpio extends PiGpio_I2C, PiGpio_GPIO, PiGpio_PWM, PiGpio_Serial {

/**
* Creates a PiGpio instance using TCP Socket communication for remote I/O access.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,24 @@ public int dataLength(){
public byte[] data(){
return this.data;
}
public PiGpioPacket data(byte[] data){

public PiGpioPacket data(CharSequence data){
return this.data(data.toString().getBytes(StandardCharsets.US_ASCII));
}

public PiGpioPacket data(byte[] data) {
return data(data, data.length);
}

public PiGpioPacket data(byte[] data, int length) {
return data(data, 0, data.length);
}

public PiGpioPacket data(byte[] data, int offset, int length){
// check for valid data
if(data != null && data.length > 0) {
this.p3 = data.length;
this.data = Arrays.copyOf(data, data.length);
if(data != null && data.length > 0 && length > 0) {
this.p3 = length;
this.data = Arrays.copyOfRange(data, offset, offset+length);
}
else{
this.p3 = 0;
Expand Down Expand Up @@ -208,10 +221,19 @@ public static PiGpioPacket decode(byte[] data) throws IOException {
.p3(p3); // set RAW P3 value

// apply any extra payload data (if available)
if(rx.hasRemaining()){
var temp = new byte[rx.remaining()];
rx.get(temp);
packet.data(temp);
int remaining = rx.remaining();

// bounds check remaining byte count
if(p3 < remaining) remaining = p3;

//System.out.println("HAS-REMAINING: " + remaining);
if(remaining > 0){
var temp = new byte[remaining];
rx.get(temp, 0, remaining);
//System.out.println("[REMAINING ]" + Arrays.toString(temp));
//packet.data(temp);
packet.data = Arrays.copyOf(temp, remaining);
//System.out.println("[DATA SIZE ]" + packet.dataLength());
}
return packet;
}
Expand All @@ -227,7 +249,7 @@ public static byte[] encode(PiGpioPacket packet){
buffer.putInt((packet.p1())); // <P1>
buffer.putInt((packet.p2())); // <P2>
buffer.putInt((packet.p3())); // <P3>
if(packet.hasData()) {
if(packet.data != null && packet.data.length > 0) {
buffer.put(packet.data()); // <DATA>
}

Expand All @@ -237,7 +259,8 @@ public static byte[] encode(PiGpioPacket packet){

@Override
public String toString(){
if(hasData())
//if(this.data != null && this.data.length > 0)
if(p3() > 0)
return String.format("CMD=%s(%d); P1=%d; P2=%d; P3=%d; PAYLOAD=[0x%s]", cmd().name(), cmd().value(), p1(), p2(), p3(), StringUtil.toHexString(data()));
else
return String.format("CMD=%s(%d); P1=%d; P2=%d; P3=%d", cmd().name(), cmd().value(), p1(), p2(), p3());
Expand Down

Large diffs are not rendered by default.

0 comments on commit 51ef603

Please sign in to comment.