まず第一に、私はJavaプログラミング言語に非常に慣れていないので、欲しいものを探してみましたが、具体的なものは何もありませんでした. 次に、Bluetooth デバイスから動的データをプロットしようとしています。
データはアプリケーション (オープン ソース アプリ) を使用して簡単にプロットできますがSensorgraph
、プロットはあまり明確ではありません (ズームインまたはズームアウトできず、x 軸または y 軸がありません...)。したがって、データをより適切に表示するには、別の方法でデータをプロットする必要があります。SensorGraph
コードは次のとおりです。
public class SensorGraph extends Activity {
Button Start, Stop, Pause;
private static final String TAG = "SensorGraph";
// change this to your Bluetooth device address
private static final String DEVICE_ADDRESS = "00:06:66:4B:E2:B7"; //"00:06:66:03:73:7B";
private GraphView mGraph;
private TextView mValueTV;
private ArduinoReceiver arduinoReceiver = new ArduinoReceiver();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get handles to Views defined in our layout file
mGraph = (GraphView)findViewById(R.id.graph);
mValueTV = (TextView) findViewById(R.id.value);
mGraph.setMaxValue(1024);
}
@Override
protected void onStart() {
super.onStart();
// in order to receive broadcasted intents we need to register our receiver
registerReceiver(arduinoReceiver, new IntentFilter(AmarinoIntent.ACTION_RECEIVED));
// this is how you tell Amarino to connect to a specific BT device from within your own code
Amarino.connect(this, DEVICE_ADDRESS);
}
@Override
protected void onStop() {
super.onStop();
// if you connect in onStart() you must not forget to disconnect when your app is closed
Amarino.disconnect(this, DEVICE_ADDRESS);
// do never forget to unregister a registered receiver
unregisterReceiver(arduinoReceiver);
}
/**
* ArduinoReceiver is responsible for catching broadcasted Amarino
* events.
*
* It extracts data from the intent and updates the graph accordingly.
*/
public class ArduinoReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String data = null;
// the device address from which the data was sent, we don't need it here but to demonstrate how you retrieve it
final String address = intent.getStringExtra(AmarinoIntent.EXTRA_DEVICE_ADDRESS);
// the type of data which is added to the intent
final int dataType = intent.getIntExtra(AmarinoIntent.EXTRA_DATA_TYPE, -1);
// we only expect String data though, but it is better to check if really string was sent
// later Amarino will support differnt data types, so far data comes always as string and
// you have to parse the data to the type you have sent from Arduino, like it is shown below
if (dataType == AmarinoIntent.STRING_EXTRA){
data = intent.getStringExtra(AmarinoIntent.EXTRA_DATA);
if (data != null){
mValueTV.setText(data);
try {
// since we know that our string value is an int number we can parse it to an integer
final int sensorReading = Integer.parseInt(data);
mGraph.addDataPoint(sensorReading);
}
catch (NumberFormatException e) { /* oh data was not an integer */ }
}
}
}
}
}
グラフビューのコードは次のとおりです。
public class GraphView extends View {
private Bitmap mBitmap;
private Paint mPaint = new Paint();
private Canvas mCanvas = new Canvas();
private float mSpeed = 1.0f;
private float mLastX;
private float mScale;
private float mLastValue;
private float mYOffset;
private int mColor;
private float mWidth;
private float maxValue = 1024f;
public GraphView(Context context) {
super(context);
init();
}
public GraphView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init(){
mColor = Color.argb(192, 64, 128, 64);
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
}
public void addDataPoint(float value){
final Paint paint = mPaint;
float newX = mLastX + mSpeed;
final float v = mYOffset + value * mScale;
paint.setColor(mColor);
mCanvas.drawLine(mLastX, mLastValue, newX, v, paint);
mLastValue = v;
mLastX += mSpeed;
invalidate();
}
public void setMaxValue(int max){
maxValue = max;
mScale = - (mYOffset * (1.0f / maxValue));
}
public void setSpeed(float speed){
mSpeed = speed;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
mCanvas.setBitmap(mBitmap);
mCanvas.drawColor(0xFFFFFFFF);
mYOffset = h;
mScale = - (mYOffset * (1.0f / maxValue));
mWidth = w;
mLastX = mWidth;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
synchronized (this) {
if (mBitmap != null) {
if (mLastX >= mWidth) {
mLastX = 0;
final Canvas cavas = mCanvas;
cavas.drawColor(0xFFFFFFFF);
mPaint.setColor(0xFF777777);
cavas.drawLine(0, mYOffset, mWidth, mYOffset, mPaint);
}
canvas.drawBitmap(mBitmap, 0, 0, null);
}
}
}
}
変更する必要があるのは、主にセンサーの読み取り値であり、別のグラフにプロットする必要があります。どうもありがとうございました。ご不明な点がございましたら、お気軽にお問い合わせください。