Working with a PanStamp device - GideonLeGrange/panstamp-java GitHub Wiki

The PanStamp class represents a panStamp wireless device. Instances of PanStamp objects are obtained by retrieving them from a Network instance or by receiving them from an event, as described [here.](Working with a Network)

Read and set device information

The most useful things to do with the PanStamp device object is to retrieve it's registers and to receive device events from it. However, for the developer's convenience, some of the more important device registers are unpacked and made available as get and set methods.

The device's name (which is set from the device definition) can also be retrieved.

This example shows how to read the panStamp's name, address, manufacturer ID and product ID, assuming the PanStamp object instance is called ps:

System.out.printf("PanStamp name: %s\n", ps.getName());
System.out.printf("PanStamp address: %d\n", ps.getAddress());
System.out.printf("PanStamp manufacturer ID: %d\n", ps.getManufacturerId());
System.out.printf("PanStamp product ID: %d\n", ps.getProductId());

Some of these settings can also be changed. Here is an example of changing the device's address:

ps.setAddress(11);

Other settings are manipulated in a similar fashion, and reading the API JavaDoc should be sufficient to understand those.

Find device registers

The panStamp device's registers are represented by the Register class. All the registers known for a device can be retrieved by using it's getRegisters() method. Assuming we have a PanStamp instance called dev, this will print out the registers for it:

List<Register> registers = dev.getRegisters();
for (Register reg : registers) {
    System.out.printf("Register #%d is %s\n", reg.getId(), reg.getName());
}

If we want to know if a register for a specific register ID is defined for a device, we can query it using hasRegister():

boolean hasIt = dev.hasRegister(0); // check if we have register 0 defined, will always be true 

We can retrieve a register for a specific register ID from a device with getRegister():

Register reg = dev.getRegister(0); 

Handling device events

It is possible to attach a listener interface to a PanStamp to receive notification of device events. The device will call it's event handlers for in the following cases:

  • The product code for the device changes
  • The synchronization state of the device changes
  • A new register is detected for the device
  • The device needs to go into one of the synchronization states that allows it to receive updates.

The following examples shows how to implement PanStampListener to receive the above events:

dev.addListener(new PanStampListener() {
    @Override
    public void productCodeChange(PanStamp dev, int manufacturerId, int productId) {
        System.out.printf("Manufacturer ID/Product ID for device #%d updated to %d/%d\n", dev.getAddress(), manufacturerId, productId);
    }

    @Override
    public void syncStateChange(PanStamp dev, int syncState) {
        System.out.printf("Sync state for device #%d changed to %d\n", dev.getAddress(), syncState);
    }

    @Override
    public void registerDetected(PanStamp dev, Register reg) {
        System.out.printf("Register %d detected for device #%d\n", reg.getId(), dev.getAddress());
    }

    @Override
    public void syncRequired(PanStamp dev) {
        System.out.printf("Sync required for device #%d\n", dev.getAddress());
    }
});

The relevant listener methods will be called by the PanStamp class to notify the interface of events.

Convenience class

Because the it is quite possible that developers may only be interested in implementing one of the available interface methods, we provide a simple convenience class implementation of PanStampListener that can be extended. The user can then only override the method they're interested in. Simply subclass AbstractPanStampListener and override for example syncRequired():

   dev.addListener(new AbstractPanStampListener() {

    @Override
    public void syncRequired(PanStamp dev) {
		// show popup dialog to tell user to press the sync button
		new syncButtonDialog(dev).show();
    }        
	
    });
⚠️ **GitHub.com Fallback** ⚠️