Need an optimization for this code...

edited June 2014 in Android Mode

Hello everyone,

I would appreciate if someone could help me to optimize this code. This code takes reads from arduino code which is an array separated by , and then splits and use each number to color a rectangle. The screen has 32 rectangles. This code runs too slow on android phones\tablets and I don't know the problem. I've tried HTC One/Samsung S4/Tegra Note 7.

Here is the code:

    import android.content.Intent;
    import android.os.Bundle;
    import ketai.net.bluetooth.*;
    import ketai.ui.*;
    import ketai.net.*;

    PFont fontMy;
    boolean bReleased = true; //no permament sending when finger is tap
    KetaiBluetooth bt;
    boolean isConfiguring = true;
    String info = "";
    float num[] = new float[32];
    int i = 0;
    int x = 0;
    int y = 0;
    int Height = 0;
    int Width = 0;
    KetaiList klist;
    ArrayList devicesDiscovered = new ArrayList();

    //********************************************************************
    // The following code is required to enable bluetooth at startup.
    //********************************************************************

    void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      bt = new KetaiBluetooth(this);
    }

    void onActivityResult(int requestCode, int resultCode, Intent data) {
      bt.onActivityResult(requestCode, resultCode, data);
    }

    void setup() {
      size(displayWidth, displayHeight, JAVA2D);
      frameRate(30);
      orientation(LANDSCAPE);
      background(0);

      //start listening for BT connections
      bt.start();
      //at app start select device…
      isConfiguring = true;
      //font size
      fontMy = createFont("SansSerif", 40);
      textFont(fontMy);
      Height = (displayHeight - (displayHeight % 4));
      Width = (displayWidth - (displayWidth % 8));
    }

    void draw() {
      //at app start select device
      if (isConfiguring)
      {
        ArrayList names;
        background(0, 0, 0);
        klist = new KetaiList(this, bt.getPairedDeviceNames());
        isConfiguring = false;
      }
      else
      {
        //    background(0, 50, 0);
        //  if((mousePressed) && (bReleased == true))
        //  {
        // //send with BT
        //  byte[] data = {'s','w','i','t','c','h','\r'};
        //  bt.broadcast(data);
        // //first tap off to send next message
        //  bReleased = false;
        //  }
        //  if(mousePressed == false)
        //  {
        //  bReleased = true; //finger is up
        //  }
        //print received data
        //    fill(255);
        //      textAlign(LEFT);
        //     text(info.length(), 20, 104);
        //       text(w,20,104);
        //       text(h,20,130);
        num = float(split(info, ','));
        if (num.length == 32) {
          //      info = "";
          fill(255);
          noStroke();
          i = 0;
          for (int y=0; y<Height; y+=Height/4) {
            for (int x=0; x<Width; x+=Width/8) {
              fill(num[i]);
              rect(x, y, Width/8, Height/4);
              i++;
            }
          }
          num = new float [32];
        }
      }
    }

    void onKetaiListSelection(KetaiList klist) {
      String selection = klist.getSelection();
      bt.connectToDeviceByName(selection);
      //dispose of list for now
      klist = null;
    }

    //Call back method to manage data received
    void onBluetoothDataEvent(String who, byte[] data) {
      if (isConfiguring)
        return;
      //received

      if (info.length() >= 62)
        info = "";

      info += new String(data);
    }

    // Arduino+Bluetooth+Processing 
    // Arduino-Android Bluetooth communication

Thank you in advance :)

Sign In or Register to comment.