Java: converting a float to a byte array and back again

Many physical variables are best represented as floats and sometimes it is necessary to pass these variables across a network link. Floats are very standardized and can be safely passed around between different architectures and operating systems but need to be converted to a byte stream first (something like JSON can send floats as strings but this is pretty inefficient in time and space). In C or C++ this is pretty easy but Java is strongly typed and doesn’t make it easy to convert a float value to a byte stream and vice versa. However it can be done…

public void convertFloatToByteArray(float f, byte[] b, int offset) {
   ByteBuffer.wrap(b, offset, 4).order(ByteOrder.LITTLE_ENDIAN).putFloat(f);
}

public float convertByteArrayToFloat(byte[] b, int offset) {
   return ByteBuffer.wrap(b, offset, 4).order(ByteOrder.LITTLE_ENDIAN).getFloat();
}

The elimination of non-compete agreements – it can’t happen too soon

Interesting piece here about the different situations in different US states regarding the enforceability of non-compete agreements. Once again, California leads the way. And also once again, Florida is one of the worst. We can only hope that these are outlawed everywhere in the very near future. Enforcement of non-compete agreements is the last refuge of the incompetent (which apparently is an out of context quote borrowed from Isaac Asimov :-)).

Raspberry Pi Compute Module

The Raspberry Pi Compute Module is a great step forward. It provides an easy way of using Raspberry Pi technology in commercial products which often require a customized physical format and I/O. This has been a problem in the past with other similar systems where they really were never intended for integration in real products. Now the Raspberry Pi provides a straightforward route to powerful commercial products with minimal NRE and risk compared to a full custom hardware design. Excellent!

Raspberry Pi I2C udev rules

Even when the I2C ports on the Raspberry Pi are enabled as described here, they are only accessible when running in supervisor mode, which is not terribly convenient. To fix that, it’s possible to add a udev rule to change the mode of the devices. The description here is borrowed from my friend at Jumpnow incidentally.

Create a file /etc/udev/rules.d/90-i2c.rules and add the line:

KERNEL=="i2c-[0-7]",MODE="0666"

Reboot the Pi and then the I2C ports will be accessible without having to run stuff with sudo.

Speeding up JPEG capture using the Raspberry Pi’s camera module and raspistill

PiCam

The Raspberry Pi with camera module is nice way of generating image and video data. However, I don’t know if this is a problem for everyone but I was only getting 2fps max out of raspistill in timelapse mode, even with a much smaller interval set. After some investigation, it seemed to be related to shutter delay. There is a way of speeding this up using the code below:

    {
        MMAL_PARAMETER_ZEROSHUTTERLAG_T shutter_config =
        {
            { MMAL_PARAMETER_ZERO_SHUTTER_LAG, sizeof(shutter_config) },
            .zero_shutter_lag_mode = 1,
            .concurrent_capture = 1
        };
        mmal_port_parameter_set(camera->control, &shutter_config.hdr);
    }

Add this code segment into the create_camera_component() function. The result is more like 8fps with my moderately overclocked Pi. Better than 2fps at least!

Using the scp command to copy files between Linux systems

scp is very handy for moving files between Linux systems, especially from desktops to embedded systems and vice versa. However, the syntax is always a challenge. For example, to copy a file from a remote system:

scp <username>@<machine>:<full path to file> .

would copy the specified file to the local directory. So for example:

scp pi@192.168.5.26:/home/pi/file fred

would copy some file called file into the local directory as a file called fred. The order can be reversed to send the file the other way.

It’s the ‘:’ that causes all the trouble…