예제 #1
0
  public static void main(String[] args) throws Exception {

    IPConnection ip = new IPConnection("localhost", 4223);

    BrickletLinearPoti poti = new BrickletLinearPoti("bxs");
    final BrickletLCD20x4 lcd = new BrickletLCD20x4("bfL");
    final BrickletDistanceIR dist = new BrickletDistanceIR("aUM");

    ip.addDevice(lcd);
    ip.addDevice(poti);
    ip.addDevice(dist);

    poti.setPositionCallbackPeriod(500L);
    lcd.clearDisplay();
    lcd.backlightOn();
    lcd.writeLine((short) 0, (short) 0, "Tinkering /w LinPoti");

    poti.addListener(
        new BrickletLinearPoti.PositionListener() {

          @Override
          public void position(int position) {
            System.out.println("Position: " + position);
            lcd.writeLine((short) 1, (short) 0, "Position: " + position + "   ");
          }
        });

    dist.setDistanceCallbackPeriod(2000);
    dist.addListener(
        new BrickletDistanceIR.DistanceListener() {
          @Override
          public void distance(int distance) {
            System.out.println("Distance: " + distance);
            lcd.writeLine((short) 2, (short) 0, "Distance: " + distance + "   ");
          }
        });

    System.in.read();

    ip.destroy();
  }
  @Override
  protected Node[] createNodesForKey(DeviceIdentifier model) {
    ArrayList<Node> nodes = new ArrayList<>();
    try {
      Device device = null;
      DeviceIdentifier tmpDM = model;
      String cutted = tmpDM.getName().substring(0, tmpDM.getName().trim().length() - 4).trim();
      switch (cutted) {
        case "Ambient Light Bricklet":
          device = new BrickletAmbientLight(tmpDM.getUid());
          nodes.add(new AmbientLightNode(device));
          break;
        case "AnalogIn Bricklet":
          device = new BrickletAnalogIn(tmpDM.getUid());
          break;
        case "AnalogOut Bricklet":
          device = new BrickletAnalogOut(tmpDM.getUid());
          break;
        case "Buzzer Bricklet":
          device = new BrickletPiezoBuzzer(tmpDM.getUid());
          break;
        case "Current12 Bricklet":
          device = new BrickletCurrent12(tmpDM.getUid());
          break;
        case "Current25 Bricklet":
          device = new BrickletCurrent25(tmpDM.getUid());
          break;
        case "DC Brick":
          device = new BrickDC(tmpDM.getUid());
          break;
        case "Distance IR Bricklet":
          device = new BrickletDistanceIR(tmpDM.getUid());
          nodes.add(new DistanceIRNode(device));
          break;
        case "Humidity Bricklet":
          device = new BrickletHumidity(tmpDM.getUid());
          break;
        case "IMU Brick":
          device = new BrickIMU(tmpDM.getUid());
          break;
        case "IO-16 Bricklet":
          BrickletIO16 io16 = new BrickletIO16(tmpDM.getUid());
          device = io16;
          nodes.add(new IO16Node(io16));
          break;
        case "IO-4 Bricklet":
          device = new BrickletIO4(tmpDM.getUid());
          break;
        case "Joystick Bricklet":
          device = new BrickletJoystick(tmpDM.getUid());
          break;
        case "LCD 16x2 Bricklet":
          device = new BrickletLCD16x2(tmpDM.getUid());
          break;
        case "LCD 20x4 Bricklet":
          device = new BrickletLCD20x4(tmpDM.getUid());
          nodes.add(new LCD2044Node(device));
          break;
        case "Master Brick":
          BrickMaster bm = new BrickMaster(tmpDM.getUid());
          device = bm;
          nodes.add(new BrickMasterNode(bm));
          break;
        case "Linear Poti Bricklet":
          BrickletLinearPoti blp = new BrickletLinearPoti(tmpDM.getUid());
          device = blp;
          nodes.add(new LinearPotiNode(blp));
          break;
        case "Rotary Poti Bricklet":
          device = new BrickletRotaryPoti(tmpDM.getUid());
          break;
        case "Relay Dual Bricklet":
          device = new BrickletDualRelay(tmpDM.getUid());
          break;
        case "Servo Brick":
          device = new BrickServo(tmpDM.getUid());
          break;
        case "Stepper Brick":
          device = new BrickStepper(tmpDM.getUid());
          break;
        case "Temperature AmbientBricklet":
          device = new BrickletTemperature(tmpDM.getUid());
          break;
        case "Temperature IR Bricklet":
          device = new BrickletTemperatureIR(tmpDM.getUid());
          break;
        case "Voltage Bricklet":
          device = new BrickletVoltage(tmpDM.getUid());
          break;
        case "Chibi Extension":
        case "StepDown Brick":
        case "RS485 Extension":
      }

      try {
        if (device != null) {
          ipc.addDevice(device);
        }
      } catch (TimeoutException ex) {
        Exceptions.printStackTrace(ex);
      }

      model.setDeviceType(device);
    } catch (IntrospectionException ex) {
      Exceptions.printStackTrace(ex);
    }
    return (nodes.toArray(new Node[nodes.size()]));
  }