Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Code Block
languagejava
titleInterfacesInterfacesInc
linenumberstrue
collapsetrue
/** 
 * PLabOrientationEvent is the event object received when phone changes orientation 
 */ 
class PLabOrientationEvent { 
  /** 
   * alpha - magnetic direction (in degrees) 
   */ 
  float alpha; 
  /** 
   * beta - tilt front-to-back, front is positive (in degrees) 
   */ 
  float beta; 
  /** 
   * gamma - tilt left-to-right, right is positive(in degrees) 
   */ 
  float gamma; 
}

/** 
 * PLabAccelerationEvent is the event object received at regular intervals, telling what acceleration is 
 */ 
class PLabAccelerationEvent { 
  /** 
   * x - acceleration in x direction 
   */ 
  float x; 
  /** 
   * y - acceleration in y direction 
   */ 
  float y; 
  /** 
   * z - acceleration in z direction 
   */ 
  float z; 
  /** 
   * timestamp - when the acceleration was meassured 
   */ 
  float timestamp; 
}

/** 
 * 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 send (String string); 
  /** 
   * register callback function that will get the data sent from the connected device 
   */ 
  public void subscribeMessages (PLabRecv sub); 
  /** 
   * Disconnects the device and returns to main menu 
   */ 
  public void disconnect(); 
  /** 
   * hides the back button 
   */ 
  public void hideBackButton(); 
  /** 
   * display the back button 
   */ 
  public void showBackButton(); 
   
  /** 
   * make the device vibrate for given amount of time. On iOS time will be ignored. 
   */ 
  public void vibrate(int milliseconds); 
  /** 
   * Listen for device orientation changes 
   */ 
  public void addDeviceOrientationListener(PLabOrientationListener listener); 
  /** 
   * remove device orientation changes listener 
   */ 
  public void removeDeviceOrientationListener(PLabOrientationListener listener); 
  /** 
   * Listen for device acceleration changes 
   */ 
  public void addDeviceAccelerationListener(PLabAccelerationListener listener); 
  /** 
   * remove device acceleration changes listener 
   */ 
  public void removeDeviceAccelerationListener(PLabAccelerationListener listener); 
  /** 
   * Set milliseconds between each acceleration update 
   */ 
  public void setDeviceAccelerationUpdateInterval(int milliseconds); 
} 
 
/** 
 * A simple interface that defines the callback read function 
 */ 
interface PLabRecv { 
  /** 
   * The callback function. Will be called when the connected device sends something to this program. 
   */ 
  public void receive(String message); 
} 
 
/** 
 * PLabOrientationListener is the interface describing an orientation change listener 
 */ 
interface PLabOrientationListener { 
  public void deviceOrientation(PLabOrientationEvent event); 
} 
 
/** 
 * PLabAccelerationListener is the interface describing an acceleration event listener 
 */ 
interface PLabAccelerationListener { 
  public void deviceAcceleration(PLabAccelerationEvent event); 
}

...

Taben "Maskinvare" har linja "Standard Serial over Bluetooth link" som inneholder navnet på porten: 

Tilkobling fra kode, bruk AppReplacer

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 , og er fila AppReplacer. Bare kopier denne fila til prosjektet ditt, endre navn på kom port/ fjern kommentering på søkealgoritmen og 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.

...