Useful Java ME 8 Embedded code snippets

Collection of ME 8 Embedded code snippets:

  • Using java.util.TimerTask and Timer – for scheduling periodic tasks (reading a sensor, checking a status etc eg
MyTimerTask myTimerTask = new MyTimerTask();
Timer timer = new Timer();
timer.schedule(myTimerTask, millisBeforeStarting, millisBetweenExecution);
  • Opening a GPIO pin:
this.pin = PeripheralManager.open(this.pinId); 
this.pin.setValue(true); // true = on, false = off
  • Adding an EventListener to a switch:
GPIOPinConfig config = new GPIOPinConfig(this.portId, this.pinId,
    GPIOPinConfig.DIR_INPUT_ONLY, PeripheralConfig.DEFAULT,
    GPIOPinConfig.TRIGGER_BOTH_EDGES,
this.pin = PeripheralManager.open(config);
this.pin.setInputListener(this);

… where this implements PinListener and valueChanged() method:

public void valueChanged(PinEvent event) {
    GPIOPin eventPin = event.getPeripheral();
    ...
}
  • Opening an I2C device for input/output:
I2CDeviceConfig config = new I2CDeviceConfig(i2cBus, address, addressSizeBits, serialClock);
myDevice = PeripheralManager.open(config);
  • Read from a UART device:
this.uart = PeripheralManager.open(UART_DEVICE_ID);
this.uart.setBaudRate(9600);

InputStream is = Channels.newInputStream(uart);
this.serialBufferedReader = new BufferedReader(new InputStreamReader(is));
  • Read using Generic Connection Framework:
CommConnection conn = (CommConnection)Connector.open("comm:/dev/ttyAMA0;baudrate=9600");
InputStream inputStream = conn.openInputStream();
this.serialBufferedReader = new BufferedReader(new
    InputStreamReader(inputStream));
  • Open and write to a record store:
this.store = RecordStore.openRecordStore(storeName, true);
byte[] dataBytes = data.getBytes();
recordNum = store.addRecord(dataBytes, 0, dataBytes.length);

Resizing your Debian root fs on Raspberry Pi

Once you’ve copied one of the Debian disk images to an SD card, you may have noticed that the disk image is only for 2GB and you have a lot of unused space on your SD card. The raspi-config util has an option (Expand Root FS) to resize your root partition for you. There’s also plenty of other utils to do the job. I used gparted from a gparted LiveCD booted on my Mac to resize my partition and it worked well.

Configuring a static IP on Debian Wheezy for Raspberry Pi

$ sudo nano /etc/network/interfaces

Change:

iface eth0 inet dhcp

to

iface eth0 inet static

Below this line enter the following, replacing x where necessary for your network config.

address 192.168.x.x
netmask 255.255.255.0
network 192.168.x.0
broadcast 192.168.x.255
gateway 192.168.x.x