Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turnouts implementation #8

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added doc/z21-lan-protocol-v1-10-en.pdf
Binary file not shown.
4 changes: 3 additions & 1 deletion src/main/java/z21Drive/Z21.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public class Z21 implements Runnable{
public static final Z21 instance = new Z21();
private static final String host = "192.168.0.111";
private static final String host = "192.168.0.90";
private static final int port = 21105;
private boolean exit = false;
private List<Z21ResponseListener> responseListeners = new ArrayList<Z21ResponseListener>();
Expand Down Expand Up @@ -236,6 +236,8 @@ else if (header1 == 0x40 && header2 == 0x00 && xHeader == 0x61 && (data[5] & 255
return new Z21BroadcastLanXProgrammingMode(newArray);
else if (header1 == 0x40 && header2 == 0x00 && xHeader == 0x61 && (data[5] & 255) == 0x08)
return new Z21BroadcastLanXShortCircuit(newArray);
else if (header1 == 0x40 && header2 == 0x00 && xHeader == 0x43)
return new Z21BroadcastLanXTurnoutsInfo(newArray);
else {
Logger.getLogger("Z21 Receiver").warning("Received unknown message. Array:");
for (byte b : newArray)
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/z21Drive/actions/Z21ActionGetLanXSetTurnout.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package z21Drive.actions;

import z21Drive.LocoAddressOutOfRangeException;

/**
* Used to retrieve loco status from z21.
* Supports loco addresses up to 128.
*/
public class Z21ActionGetLanXSetTurnout extends Z21Action{

/**
* @param turnoutAddress Address of the turnout to request info of.
* @param position is the position of the turnout
* @param active sets the decoder status
* @throws LocoAddressOutOfRangeException Thrown if loco address is out of supported range.
*/
public Z21ActionGetLanXSetTurnout(int turnoutAddress, byte position, boolean active) throws LocoAddressOutOfRangeException{
byteRepresentation.add(Byte.decode("0x40"));
byteRepresentation.add(Byte.decode("0x00"));
if (turnoutAddress < 1)
throw new LocoAddressOutOfRangeException(turnoutAddress);
addDataToByteRepresentation(new Object[]{turnoutAddress, position, active});
addLenByte();
}

@Override
public void addDataToByteRepresentation(Object[] objs) {
//Add all the data
byteRepresentation.add((byte)(int)Integer.decode("0x53"));

byte Adr_MSB = (byte) (((Integer)objs[0]) >> 8);
byte Adr_LSB = (byte) (((Integer)objs[0]) & 0b11111111);
if (Adr_MSB != 0){
Adr_MSB |= 0b11000000;
}

byteRepresentation.add(Adr_MSB);
byteRepresentation.add(Adr_LSB);

//data byte, with Q=1
byte db2 = (byte) 0xA0;

//add position
db2 |= (byte)((Byte)objs[1]);

//add activate
if(((Boolean)objs[2]))
db2 |= (byte)(0x08);

byteRepresentation.add(db2);
byteRepresentation.add((byte) (byteRepresentation.get(2) ^
byteRepresentation.get(3) ^
byteRepresentation.get(4) ^
byteRepresentation.get(5)));
}
}
9 changes: 7 additions & 2 deletions src/main/java/z21Drive/broadcasts/BroadcastTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
* Represents all messages that all clients get.
*/
public enum BroadcastTypes {
LAN_X_LOCO_INFO, LAN_X_UNKNOWN_COMMAND, LAN_X_TRACK_POWER_OFF, LAN_X_TRACK_POWER_ON, LAN_X_PROGRAMMING_MODE,
LAN_X_SHORT_CIRCUIT
LAN_X_LOCO_INFO,
LAN_X_UNKNOWN_COMMAND,
LAN_X_TRACK_POWER_OFF,
LAN_X_TRACK_POWER_ON,
LAN_X_PROGRAMMING_MODE,
LAN_X_SHORT_CIRCUIT,
LAN_X_GET_TURNOUT_INFO,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package z21Drive.broadcasts;

/**
* It represents the state of a turnout.
*/
public class Z21BroadcastLanXTurnoutsInfo extends Z21Broadcast{
private int turnoutAddres;
private int position;

public Z21BroadcastLanXTurnoutsInfo(byte[] initArray) {
super(initArray);
boundType = BroadcastTypes.LAN_X_GET_TURNOUT_INFO;
if (byteRepresentation != null)
populateFields();
}

private void populateFields(){

byte adr_MSB = byteRepresentation [5];
byte adr_LSB = byteRepresentation [6];

turnoutAddres = (adr_MSB & 0x3F) << 8 | adr_LSB;
position = byteRepresentation [7] & 0x03;
}

public int getTurnoutAddress()
{
return turnoutAddres;
}

public int getPosition()
{
return position;
}
}
73 changes: 73 additions & 0 deletions src/main/java/z21Drive/testing/Turnouts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package z21Drive.testing;

import z21Drive.LocoAddressOutOfRangeException;
import z21Drive.Z21;
import z21Drive.actions.Z21ActionGetLanXSetTurnout;
import z21Drive.broadcasts.BroadcastTypes;
import z21Drive.broadcasts.Z21Broadcast;
import z21Drive.broadcasts.Z21BroadcastLanXTurnoutsInfo;
import z21Drive.broadcasts.Z21BroadcastListener;

/**
* Sends request for info of loco #5 and then keeps printing any changes.
* @see z21Drive.Z21
*/
public class Turnouts implements Runnable{
boolean finished;
int address;

public static void main(String[] args) {
//Start things up
new Thread(new Turnouts(1)).start();
}

public Turnouts(int address) {
this.address = address;
}

public void run(){
final Z21 z21 = Z21.instance;
z21.addBroadcastListener(new Z21BroadcastListener() {
@Override
public void onBroadCast(BroadcastTypes type, Z21Broadcast broadcast) {
if (type == BroadcastTypes.LAN_X_GET_TURNOUT_INFO){


Z21BroadcastLanXTurnoutsInfo t = (Z21BroadcastLanXTurnoutsInfo) broadcast;
System.out.println("Turnout address: " + t.getTurnoutAddress());
System.out.println("Position: " + t.getPosition());
finished = true;
}
}

@Override
public BroadcastTypes[] getListenerTypes() {
return new BroadcastTypes[]{BroadcastTypes.LAN_X_GET_TURNOUT_INFO};
}
});


try {
z21.sendActionToZ21(new Z21ActionGetLanXSetTurnout(address, (byte) 1, true));
Thread.sleep(150);
z21.sendActionToZ21(new Z21ActionGetLanXSetTurnout(address, (byte) 1, false));

Thread.sleep(1500);


z21.sendActionToZ21(new Z21ActionGetLanXSetTurnout(address, (byte) 0, true));
Thread.sleep(150);
z21.sendActionToZ21(new Z21ActionGetLanXSetTurnout(address, (byte) 0, false));



} catch (LocoAddressOutOfRangeException | InterruptedException e) {
e.printStackTrace();
}

while (!finished){}

System.out.println("shuddown");
z21.shutdown();
}
}