All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lejos.remote.nxt.NXTCommDevice Maven / Gradle / Ivy

Go to download

leJOS (pronounced like the Spanish word "lejos" for "far") is a tiny Java Virtual Machine. In 2013 it was ported to the LEGO EV3 brick.

The newest version!
package lejos.remote.nxt;

import lejos.internal.io.SystemSettings;

/**
 * Base class for nxt communications devices. Provides a common address/name,
 * plus utility functions. 
 * @author andy
 */
abstract public class NXTCommDevice
{
    public static final int ADDRESS_LEN = 6;
    public static final int NAME_LEN = 16;
    public static final String SERIAL_NO = "lejos.usb_serno";
    public static final String NAME = "lejos.usb_name";

    static String devAddress = "123";
    static String devName = "xxx";

    static {
        loadSettings();
    }

    /**
     * Determine if a string contains a Bluetooth style address.
     * @param s String to test
     * @return true if the string is an address
     */
    public static boolean isAddress(String s)
    {
        if (s == null || s.length() < 2) return false;
        return s.charAt(0) == '0' && s.charAt(1) == '0';
    }


    /**
     * Convert a string version of a Bluetooth address into a byte array
     * address.
     * @param strAddress The string version of the address
     * @return a byte array version of the address
     */
    public static byte[] stringToAddress(String strAddress)
    {
        if (strAddress != null && strAddress.length() == (ADDRESS_LEN)*2)
        {
            // Convert to binary format
            byte[] addr = new byte[ADDRESS_LEN];
            int out = 0;
            for(int i = 0; i < strAddress.length(); i += 2)
            {
                char c = strAddress.charAt(i);
                byte val = (byte)((c > '9' ? c - 'A' + 10 : c - '0') << 4);
                c = strAddress.charAt(i+1);
                val |= (byte)(c > '9' ? c - 'A' + 10 : c - '0');
                addr[out++] = val;
            }
            return addr;
        }
        else
            return null;
    }

    /**
     * Convert the string version of a devName into a byte array.
     * @param strName string version of the devName
     * @return byte array containing the devName.
     */
    public static byte[] stringToName(String strName)
    {
        if (strName != null && strName.length() <= NAME_LEN)
        {
            byte[] nam = new byte[NAME_LEN];
            for(int i = 0; i < strName.length(); i++)
            {
                nam[i] = (byte)strName.charAt(i);
            }
            return nam;
        }
        else
            return null;
    }

	protected static final char[] cs = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

	/**
	 * Helper method to convert address byte array to String.
	 * @param addr A byte array of bytes containing the address.
	 * @return String representation of Bluetooth address.
	 */
	public static String addressToString(byte [] addr)
    {
        if (addr == null || addr.length < ADDRESS_LEN) return null;
		char[] caddr = new char[ADDRESS_LEN*2];

		int ci = 0;
		int addri = 0;

		for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy