Android Open Source - SerialPlot Data Buffer






From Project

Back to project page SerialPlot.

License

The source code is released under:

MIT License

If you think the Android project SerialPlot listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.SerialPlot;
//from  w  w  w .  java  2 s  . com
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.os.Environment;
import android.os.Handler;

public class DataBuffer {
  
  private Context mContext;
  private Handler mHandler;
  
  private ArrayList<Number> mBuffer;
  
  // Message types sent to the main activity
    public static final int MESSAGE_NEW_VALUE = 100;
    
    //for debug
    Random r = new Random();
  
  public DataBuffer(Context context, Handler handler){
    mContext = context;
    mHandler = handler;
    
    mBuffer = new ArrayList<Number>();
  }
  
  public void add(String data, boolean notify){
    // try accounts for when crap is sent over serial
    //    - parseDouble will cause the app to crash
    //      in this case.
    Double d;
    try {
      d = Double.parseDouble(data);
      mBuffer.add(d);
      if (notify) notifyNewVal(d);
    } catch (Exception e) {}
  }
  
  public void add(Number val){
    mBuffer.add(val);
  }
  
  public void remove(int index){
    mBuffer.remove(index);
  }
  
  public void clear() {
    mBuffer.clear();
  }
  
  public ArrayList<Number> getBuffer(){
    return mBuffer;
  }
  
  public List<Number> getLastElements(int n){
    int start = mBuffer.size()-n;
    if (start < 0){
      return mBuffer;
    } else {
      return mBuffer.subList(start, mBuffer.size());
    }
  }
  
  public void setBuffer(ArrayList<Number> data) {
    mBuffer = data;
  }
  
  public void notifyNewVal(Double d){
    mHandler.obtainMessage(MESSAGE_NEW_VALUE, d).sendToTarget();
  }
  
  public int size() {
    return mBuffer.size();
  }
}




Java Source Code List

org.SerialPlot.BTcom.java
org.SerialPlot.DataBuffer.java
org.SerialPlot.DeviceListActivity.java
org.SerialPlot.FileArrayAdapter.java
org.SerialPlot.FileBrowserActivity.java
org.SerialPlot.Option.java
org.SerialPlot.SerialPlotActivity.java