Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
titleInterfaces
linenumberstrue
/**
 * PLabBridge is the interface we have for communicating with the plab app.
 */
interface PLabBridge {
  /**
   * gets the width we have available to draw on
   */
  public int getWidth ();
  /**
   * gets the height we have available to draw on
   */
  public int getHeight ();
  /**
   * sends a string to a connected bluetooth device. Can not send more than 20 characters.
   */
  public void write (String string);
  /**
   * register callback function that will get the data sent from the connected device
   */
  public void subscribeRead (PLabRead sub);
  /**
   * register callback function that will get error messages if any from the app
   */
  public void subscribeError (PLabRead sub);
  /**
   * Disconnects the device and returns to main menu
   */
  public void disconnect();
}

/**
 * A simple interface that defines the callback read function
 */
interface PLabRead {
  /**
   * The callback function. Will be called when the connected device sends something to this program.
   */
  public void read(String string);
}

/**
 * A variable to hold the injected bridge to the plab app
 */
private PLabBridge plabBridge;

/**
 * Injection function. Needed to get the functionality provided by our app. Use as setup.
 */
void bindPLabBridge (PLabBridge bridge) {
  plabBridge = bridge;
}

Her har vi to interfaces, PLabBridge og PLabRead. PLabRead er callback funksjon når vi mottar data fra bluetooth enheten.

Javascript objektet injiseres ved at bindPLabBridge kalles. Her kan du registrere lyttere ved hjelp av callback funksjoner. Den som er relevant er subscribeRead(). subscribeError() vil ved enkelte feil internt i appen bli kalt, noe som i all hovedsak har vært brukt i løpet av utviklinga av appen og egentlig ikke trengs lenger.

Vi vil også anbefale dere å kalle size(bridge.getWidth (), bridge.getHeight ()); i løpet av bindPLabBridge(). Da får dere muligheten til å tegne på hele skjermen til den mobile enheten. 

Processing på datamaskina og bluetooth

...

Etter paring på mac er det litt enklere å koble til enn på Windows, da enheten får navn etter hva den faktisk heter. Standard for dere er PLabXX (XX er her gruppenummer). Vi har lagd kode som dere kan bare klippe ut og lime inn som skal lete etter den første enheten som er paret som starter med PLab, og velge denne.

Når du har paret en enhet kan du bruke datamaskina som kontroll på samme måte som du kan bruke appen vår. Vi har lagd kopier-og-lim-inn kode som skal hjelpe deg med det, og gjøre det rett fram å overføre koden du har skrevet til mobil enhet. Koden ligger som del av ArduinoMobileIntegrationExamples/MinimalBTExample/Processing-computer/MinimalBTExample. Koden du kan klippe ut og lime inn er den som er rett under. Endre på linje 46-56 for å tilpasse til din maskin/os. Kall setupSerial() fra den vanlige setup() metoden for å få det hele til å virke.

Code Block
languagejava
titleProcessingComputerCode
linenumberstrue
/*
 * ---------- Serial communication for computer area ---------------------
 */
import processing.serial.*;

class BridgeReplacer implements PLabBridge {
  Serial port;
  PLabRead subscribedRead = null;
  
  BridgeReplacer(Serial serialPort) {
    port = serialPort;
    port.clear();
    
    // Buffer data until a newline character is reached
    port.bufferUntil('\n');
  } 
  public int getWidth () {
    return width;
  }
  public int getHeight () {
    return height;
  }
  public void write (String string) {
    port.write(string + "\n");
    println("Sent: " + string);
  }
  public void subscribeRead (PLabRead sub) {
    subscribedRead = sub;
  }
  public void subscribeError (PLabRead sub) { /* We do not care about this while developing */ }
  public void disconnect() { /* We do not care about this while developing */ }
}

BridgeReplacer bridgeReplacer;

void setupSerial() {
  // Get and display all installed serial ports on computer
  String[] serials = Serial.list();
  println(serials);
  // We need to connect to a port.
  // ---------------------- THIS IS INDIVIDUAL TO EACH COMPUTER!!! ----------------
  // On Windows computer, which one can be seen by name of the port and setting of paired device.
  // On a Mac/unix system, it should be the one containing the name of the device preceeded by tty
  String portName;
  // The com port here is found since I know which one in the list is the paired device, and I am on a Windows computer
  portName = "COM12";
  // ----- Basic search algorithm. Uncomment to use on Mac
  /*
  for (int i = 0; i < serials.length; i++) {
    portName = serials[i];
    // The name of the handed out bt devices contains the phrase "PLab". The first element containing this phrase should be the correct port.
    if (portName != null && portName.contains("PLab")) {
      break;
    }
  }
  */
   
  // Init a new serial connection
  Serial port = new Serial(this, portName, 9600);
  // Set up our replacement for the plab bridge
  bridgeReplacer = new BridgeReplacer(port);
  // "bind" the replacer instead of the javascript used in mobile devices (replace javascript injection)
  bindPLabBridge(bridgeReplacer);
}

// Event handler when something happens to the serial port
void serialEvent(Serial p) {
  // Ensure the was event was fired from the correct port
  if (p == bridgeReplacer.port) {
    // Try to read until a newline is found
    String msg = p.readStringUntil('\n');
    // Check the event was fired because a newline was received, and that we have a receiver
    if (msg != null && bridgeReplacer.subscribedRead != null) {
      // Send message to the one listening
      bridgeReplacer.subscribedRead.read(msg);
    }
  }
}

Den forutsetter at interfacene beskrevet i Interfaces er tilstede, og vi legger dem ved for helhetens skyld.

Code Block
languagejava
titleInterfaces - gjentatt
linenumberstrue
/*
 * ---------- Interfaces that make conversion to mobile simpler ----------
 */
// Interfaces documented in InterfacesExample
interface PLabBridge {
  public int getWidth ();
  public int getHeight ();
  public void write (String string);
  public void subscribeRead (PLabRead sub);
  public void subscribeError (PLabRead sub);
  public void disconnect();
}
interface PLabRead {
  public void read(String string);
}

For at det skal virke, må dere kalle setupSerial() fra setup(). Ellers er det bare å kjøre vanlig Processing utvikling og kode.