Android Open Source - ArduinoRGB-Controller Lamp Parser






From Project

Back to project page ArduinoRGB-Controller.

License

The source code is released under:

MIT License

If you think the Android project ArduinoRGB-Controller listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package dk.cmol.arduinorgb_controller;
//from   w  ww.j  av  a  2 s . c o  m
import java.util.ArrayList;

public class LampParser {
  
  // TODO:   There is a lot of duplication going on in this class,
  //       try to figure out an elegant solution to the problem.

  // Define operations
  private final int SET = 0;
  private final int FADE = 1;

  private final int ALL = 128;
  
  /* Right now all messages are split at the byte (00001010).
     Consider splitting at 254 (11111110) as this has a smaller
     implication on the messages, or creating a server that
     does not care about breaks and just puts out the bytes
     it receives. */
  private final int BREAK = 10;

  boolean allLamps;
  ArrayList<Integer> selectedLamps;

  public LampParser() {
  }

  private void init(boolean[] lamps) {
    // Check if the array is the wrong length
    // TODO: Make the number of lamps dynamic (1-4)
    if (lamps.length != 4) {
      throw new IllegalArgumentException();
    }

    // Check if the all lamps flag should be set
    allLamps = true;
    for (int i = 0; i < lamps.length; i++) {
      if (!lamps[i]) {
        allLamps = false;
        break;
      }
    }

    // Build array list with lamps
    if (!allLamps) {
      selectedLamps = new ArrayList<Integer>();
      for (int i = 0; i < lamps.length; i++) {
        if (lamps[i]) {
          selectedLamps.add(i);
        }
      }
    }

  }

  // Create control byte, use on a per lamp basis
  public byte ctrl(int lamp, int mode) {
    return (byte) ((lamp << 5) | mode);
  }

  // Change color values in the byte array
  public void addColor(byte[] packet, int offset, String colStr) {
    String[] colsStr = colStr.split("-");
    for (int i = 0; i < 3; i++) {
      int col = Integer.parseInt(colsStr[i]);
      // Add 1 to colors representing BREAK
      packet[offset + i] = (byte) (col == BREAK ? col+1 : col);
    }
  }

  // Create the fade packet
  public byte[] fade(boolean[] lamps, String cols, int fadeTime) {
    // (Re-)Initialize class
    init(lamps);
    byte[] ret;

    if (allLamps) {

      ret = new byte[5];
      ret[0] = (byte) (ALL | FADE); // CTRL byte

      // Set colors
      addColor(ret, 1, cols);

      // Set fadeTime, and escape BREAK
      ret[4] = (byte) (fadeTime == BREAK ? fadeTime+1 : fadeTime);
    } else {
      // Array pointer
      int pos = 0;

      // Initialize the array to
      ret = new byte[selectedLamps.size() * 5];
      for (int lamp : selectedLamps) {
        // Set control byte
        ret[pos] = ctrl(lamp, FADE);

        // Set colors
        addColor(ret, pos + 1, cols);

        // Set fade
        ret[pos + 4] = (byte) fadeTime;

        // Go 5 bytes in
        pos += 5;
      }
    }

    return ret;

  }

  // Create the set packet
  public byte[] set(boolean[] lamps, String cols) {
    // (Re-)Initialize class
    init(lamps);
    byte[] ret;

    if (allLamps) {

      ret = new byte[4];
      ret[0] = (byte) (ALL | SET); // CTRL byte

      // Set colors
      addColor(ret, 1, cols);
    } else {
      // Array pointer
      int pos = 0;

      // Initialize the array to
      ret = new byte[selectedLamps.size() * 4];
      for (int lamp : selectedLamps) {
        // Set control byte
        ret[pos] = ctrl(lamp, SET);

        // Set colors
        addColor(ret, pos + 1, cols);

        // Go 4 bytes in
        pos += 4;
      }
    }

    return ret;

  }

}




Java Source Code List

dk.cmol.arduinorgb_controller.ArduinoRGBActivity.java
dk.cmol.arduinorgb_controller.ArduinoSocket.java
dk.cmol.arduinorgb_controller.ColorDotsFragment.java
dk.cmol.arduinorgb_controller.FaderFragment.java
dk.cmol.arduinorgb_controller.GyroFragment.java
dk.cmol.arduinorgb_controller.LampParser.java
dk.cmol.arduinorgb_controller.MenuPagerAdapter.java
dk.cmol.arduinorgb_controller.SettingsActivity.java
dk.cmol.arduinorgb_controller.SettingsFragment.java
dk.cmol.arduinorgb_controller.SliderFragment.java