Working with a register - GideonLeGrange/panstamp-java GitHub Wiki
The Register
class represents a register in a panStamp device. Instances of Register
are obtained by retrieving them from a PanStamp
instance or by receiving them from an event, as described [here.](Working with a PanStamp device)
Each register has an integer register ID and a name, which can be retrieved like this, assuming we have a register named reg
:
int id = reg.getId();
String name = reg.getName();
We can also check if a register is a "standard" register - one that forms part of the SWAP specification and that is not specific to the sketch running on the panStamp:
boolean sr = reg.isStandard();
We can test if a register has a valued defined (received from the network) like this:
if (reg.hasValue()) {
// do something if we have a value
}
If a register has a value, we can retrieve it by calling getValue()
:
byte val[] = reg.getValue();
We can also set a register value by calling setValue()
. This will send a value to the register on the panStamp:
reg.setValue(new byte[]{0,1});
You can request the latest value for a register from a panStamp. This will generate a SWAP Query message, and if the panStamp is listenening for queries or updates (isn't in sleep mode), it will respond with a register value. This update will generate a valueReceived
event (see below).
reg.requestValue();
Most often, working with register byte[]
values is not the best way to access panStamp data. Typically a register is mapped to one or more endpoint, which offers a friendlier way of reading and setting data.
The endpoints available for a register can be retrieved like this:
List<Endpoint> endpoints = reg.getEndpoints();
If you know the name of the endpoint needed, it can be access by calling getEndpoint()
:
java
Endpoint ep = reg.getEndpoint("Temperature");
We can also check the existence of a specific endpoint by name:
```java
if (reg.hasEndpoint("Temperature")) {
// ...
}
We discuss endpoints in a lot more detail [here](Working with endpoints).
It is possible to attach a listener interface to a Register
to receive notification of register events. The register will call it's event handlers for in the following cases:
- A value is received from the network for the register
- The register value is set by the application
- An endpoint is added to the register
- A parameter is added to the register
The following examples shows how to implement RegisterListener to receive the above events:
reg.addListener(new RegisterListener() {
@Override
public void valueReceived(Register reg, byte[] value) {
System.out.printf("Value received form register %d\n", reg.getId());
}
@Override
public void valueSet(Register reg, byte[] value) {
System.out.printf("Value set for register %d\n", reg.getId());
}
@Override
public void endpointAdded(Register reg, Endpoint ep) {
System.out.printf("Endpoint '%s added to register %d\n", ep.getName(), reg.getId());
}
@Override
public void parameterAdded(Register reg, Parameter par) {
System.out.printf("Parameter '%s added to register %d\n", par.getName(), reg.getId());
}
});
The relevant listener methods will be called by the Register class to notify the interface of events.
Developers most likely will want to implement valueReceived()
. The other methods on RegisterListener
will mostly support tool creators, while application developers will not need them.
Because of this, we provide a simple convenience class implementation of RegisterListener
that can be extended. The user can then only override the method they're interested in. Simply subclass AbstractRegisterListener
and override for example valueReceived()
:
reg.addListener(new AbstractRegisterListener() {
@Override
public void valueReceived(Register reg, byte[] value) {
// handle reception of value in some way
}
});