/**
   * an Arduino does not need to know about a shield but a shield must know about a Arduino Arduino
   * owns the script, but a Shield needs additional support Shields are specific - but plug into a
   * generalized Arduino Arduino shields can not be plugged into other uCs
   *
   * <p>TODO - Program Version & Type injection - with feedback + query to load
   */
  @Override
  public boolean attach(Arduino inArduino) {

    if (inArduino == null) {
      error("can't attach - arduino is invalid");
      return false;
    }

    this.arduino = inArduino;

    // arduinoName; FIXME - get clear on diction Program Script or Sketch
    StringBuffer newProgram = new StringBuffer();
    newProgram.append(arduino.getSketch());

    // modify the program
    int insertPoint = newProgram.indexOf(Arduino.VENDOR_DEFINES_BEGIN);

    if (insertPoint > 0) {
      newProgram.insert(Arduino.VENDOR_DEFINES_BEGIN.length() + insertPoint, ADAFRUIT_DEFINES);
    } else {
      error("could not find insert point in MRLComm.ino");
      // get info back to user
      return false;
    }

    insertPoint = newProgram.indexOf(Arduino.VENDOR_SETUP_BEGIN);

    if (insertPoint > 0) {
      newProgram.insert(Arduino.VENDOR_SETUP_BEGIN.length() + insertPoint, ADAFRUIT_SETUP);
    } else {
      error("could not find insert point in MRLComm.ino");
      // get info back to user
      return false;
    }

    insertPoint = newProgram.indexOf(Arduino.VENDOR_CODE_BEGIN);

    if (insertPoint > 0) {
      newProgram.insert(Arduino.VENDOR_CODE_BEGIN.length() + insertPoint, ADAFRUIT_CODE);
    } else {
      error("could not find insert point in MRLComm.ino");
      // get info back to user
      return false;
    }

    // set the program
    Sketch sketch = new Sketch("AdafruitMotorShield", newProgram.toString());
    arduino.setSketch(sketch);
    // broadcast the arduino state - ArduinoGUI should subscribe to
    // setProgram
    broadcastState(); // state has changed let everyone know

    // servo9.attach(arduinoName, 9); // FIXME ??? - createServo(Integer i)
    // servo10.attach(arduinoName, 10);

    // error(String.format("couldn't find %s", arduinoName));
    return true;
  }
Exemple #2
0
  public static void main(String[] args) throws InterruptedException {

    LoggingFactory.getInstance().configure();
    LoggingFactory.getInstance().setLevel(Level.DEBUG);

    Servo right = new Servo("servo01");

    right.map(5, 180, 5, 180);
    log.info("{}", right.calc(1));
    log.info("{}", right.calc(3));
    right.map(180, 0, 0, 180);
    log.info("{}", right.calc(3));
    right.map(0, 180, 5, 178);
    log.info("{}", right.calc(0));

    right.startService();
    right.test();

    // FIXME - routing of servo.attach("arduino", 3);

    Arduino arduino = (Arduino) Runtime.createAndStart("arduino", "Arduino");

    arduino.connect("COM4");

    arduino.servoAttach(right.getName(), 13);

    right.test();

    Runtime.createAndStart("gui", "GUIService");

    // right.attach(serviceName)
    /*
     * Servo left = new Servo("left"); left.startService();
     *
     * //Servo neck = new Servo("neck"); //neck.startService();
     *
     * for (int i = 0; i < 30; ++i) {
     *
     * right.attach("arduino", 2); left.attach("arduino", 3);
     *
     * right.moveTo(120); // 70 back left.moveTo(70); // 118 back
     *
     * Thread.sleep(10000);
     *
     * right.moveTo(90); left.moveTo(90);
     *
     * //right.detach(); //left.detach(); }
     */

  }
Exemple #3
0
  public void addLocalSensor(String type, int pinNumber, String tags) {
    boolean notAdded = true;

    if (type.equals("analog")) {
      for (int i = 0; i < totalAnalogSensors; i++) {
        if (analogSensorPin[i] == pinNumber) notAdded = false;
      }
      if (notAdded) {
        localSensor[totalLocalSensors] =
            new LocalSensor(
                ANALOG, pinNumber, tags + ",analogRead" + pinNumber, totalLocalSensors++);
      }
    } else {

      for (int i = 2; i < totalDigitalSensors; i++) {
        if (digitalSensorPin[i] == pinNumber) notAdded = false;
      }

      if (notAdded) {
        localSensor[totalLocalSensors] =
            new LocalSensor(
                DIGITAL, pinNumber, tags + ",digitalRead" + pinNumber, totalLocalSensors++);
        a.pinMode(pinNumber, Arduino.INPUT);
      }
    }
    localSensorsAdded = true;
  }
Exemple #4
0
  public Pachuino(PApplet parent, String device, int speed) {
    System.out.println(
        "\n---------------------\nPachuino version: " + VERSION + "\n---------------------");
    pApplet = parent;

    for (int i = 0; i < Arduino.list().length; i++) {
      System.out.println(Arduino.list()[i]);
    }
    a = new Arduino(parent, device, speed);

    for (int i = 0; i < TOTAL_DIGITAL_IN; i++) {
      a.pinMode(i, Arduino.OUTPUT);
    }

    analogSensorPin = new int[TOTAL_ANALOG_IN];
    digitalSensorPin = new int[TOTAL_DIGITAL_IN];

    remoteFeed = new int[MAX_REMOTE_FEEDS];
    dIn = new DataIn[MAX_REMOTE_FEEDS];

    localSensor = new LocalSensor[TOTAL_ANALOG_IN + TOTAL_DIGITAL_IN];
    remoteSensor = new RemoteSensor[MAX_REMOTE_SENSORS];
    pApplet.registerPre(this);
  }
 @Override
 public void startService() {
   super.startService();
   arduino.startService();
   attach(arduino);
 }
 public void setSpeed(Integer motorPortNumber, Integer speed) {
   arduino.sendMsg(AF_DCMOTOR_SET_SPEED, motorPortNumber - 1, speed);
 }
 public void run(Integer motorPortNumber, Integer command) {
   arduino.sendMsg(AF_DCMOTOR_RUN_COMMAND, motorPortNumber - 1, command);
 }
 public boolean connect(String port) {
   return arduino.connect(port);
 }
Exemple #9
0
 public void digitalWrite(int pin, int val) {
   int tempValue = Arduino.HIGH;
   if (val == 0) tempValue = Arduino.LOW;
   a.digitalWrite(pin, tempValue);
 }
Exemple #10
0
 public void analogWrite(int pin, float val) {
   a.analogWrite(pin, (int) (val));
 }
Exemple #11
0
 public void analogWrite(int pin, int val) {
   a.analogWrite(pin, val);
 }
Exemple #12
0
 public int digitalRead(int pin) {
   return a.digitalRead(pin);
 }
Exemple #13
0
 public int analogRead(int pin) {
   return a.analogRead(pin);
 }