Display both red and infrared channels
Turn on raw sensor feed
diff --git a/res/layout/activity_ara_plox.xml b/res/layout/activity_ara_plox.xml
index 0d0e8aa..4063dbb 100644
--- a/res/layout/activity_ara_plox.xml
+++ b/res/layout/activity_ara_plox.xml
@@ -3,12 +3,27 @@
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical" >
-
-    <LinearLayout android:id="@+id/chart"
+    <TextView
+        android:id="@+id/textView"
+        android:layout_width="fill_parent"
+        android:layout_height="0dip"
+        android:gravity="center"
+        android:layout_weight="1"
+        android:background="#FFFFFF"
+        android:fontFamily="sans-serif"
+        android:textSize="32sp"
+        android:text="@string/hello_world" />
+    <LinearLayout android:id="@+id/irChart"
                   android:orientation="horizontal"
                   android:layout_width="fill_parent"
                   android:layout_height="0dip"
-                  android:layout_weight="2"/>
+                  android:layout_weight="3"/>
+    <LinearLayout android:id="@+id/redChart"
+                  android:orientation="horizontal"
+                  android:layout_width="fill_parent"
+                  android:layout_height="0dip"
+                  android:layout_weight="3"/>
+
 
 <!--
     <TextView
diff --git a/res/values/strings.xml b/res/values/strings.xml
index d4f6f28..8c5ee37 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
     <string name="app_name">AraPloxIO</string>
-    <string name="hello_world">Hello world!</string>
+    <string name="hello_world">Pulse Monitor</string>
 </resources>
diff --git a/src/com/google/araploxio/AFE4400Thread.java b/src/com/google/araploxio/AFE4400Thread.java
index 84e949b..4193341 100644
--- a/src/com/google/araploxio/AFE4400Thread.java
+++ b/src/com/google/araploxio/AFE4400Thread.java
@@ -124,8 +124,7 @@
         while (!stopped) {
             Message msg;
             try {
-                msg = handler.obtainMessage(0, x++, ploxInputStream.readInt());
-                ploxInputStream.readInt();
+                msg = handler.obtainMessage(0, ploxInputStream.readInt(), ploxInputStream.readInt());
                 msg.sendToTarget();
             } catch (IOException e1) {
                 e1.printStackTrace();
@@ -170,10 +169,8 @@
             }
         }
 
-        int sample = 0;
         while (!isStopped()) {
             results = null;
-            boolean stop = true;
             byte[] data;
             int LED1VAL;
             int LED2VAL;
@@ -183,30 +180,45 @@
                                         0xff, 0xff, 0xff),    // 3 dummy bytes
                 I2cTransaction.newRead(4),
             };
+            I2cTransaction[] txns1 = {
+                I2cTransaction.newWrite(0x01,                 // Select SPI device 1
+                                        0x2f,                 // LED2-ALED2VAL
+                                        0xff, 0xff, 0xff),    // 3 dummy bytes
+                I2cTransaction.newRead(4),
+            };
+
 
             try {
-                if (stop) {
-                    for (I2cTransaction txn: txns0) {
-                        results = i2c.performTransactions(bus, address, txn);
-                    }
-                } else {
-                    results = i2c.performTransactions(bus, address, txns0);
+                for (I2cTransaction txn: txns0) {
+                    results = i2c.performTransactions(bus, address, txn);
                 }
             } catch (IOException e) {
                 setText("error while reading back: " + e);
                 return;
             }
 
-            if (stop)
-                data = results[0].data;
-            else
-                data = results[1].data;
+            data = results[0].data;
 
             LED1VAL = (((int)data[1]) << 16) |
                       (((int)data[2] & 0xFF) <<  8) |
                       (((int)data[3] & 0xFF));
 
-            Message msg = handler.obtainMessage(0, sample++, LED1VAL);
+            try {
+                for (I2cTransaction txn: txns1) {
+                    results = i2c.performTransactions(bus, address, txn);
+                }
+            } catch (IOException e) {
+                setText("error while reading back: " + e);
+                return;
+            }
+
+            data = results[0].data;
+
+            LED2VAL = (((int)data[1]) << 16) |
+                      (((int)data[2] & 0xFF) <<  8) |
+                      (((int)data[3] & 0xFF));
+
+            Message msg = handler.obtainMessage(0, LED1VAL, LED2VAL);
             msg.sendToTarget();
 
             try {
diff --git a/src/com/google/araploxio/AraPloxActivity.java b/src/com/google/araploxio/AraPloxActivity.java
index a0f8def..bc9a0af 100644
--- a/src/com/google/araploxio/AraPloxActivity.java
+++ b/src/com/google/araploxio/AraPloxActivity.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2014 Google, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 package com.google.araploxio;
 
 import org.achartengine.ChartFactory;
@@ -23,135 +39,154 @@
 
 public class AraPloxActivity extends Activity {
 	private static final String TAG = "AraPlox/Activity";
+    private static final int SCROLLBACK_SIZE = 200;
 
 	private static TextView bpmTextView;
 	private static Handler mHandler = null;
 	private static AFE4400Thread afe4400Thread = null;
-	private GraphicalView chart_view = null;
+    private static int sampleCount = 0;
 
-	protected void onCreate(Bundle savedInstanceState) {
-		super.onCreate(savedInstanceState);
-		Log.d(TAG, "onCreate()");
-		requestWindowFeature(Window.FEATURE_NO_TITLE);
-		setContentView(R.layout.activity_ara_plox);
-        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+    private AraPloxChart irPloxChart = new AraPloxChart(R.id.irChart);
+    private AraPloxChart redPloxChart = new AraPloxChart(R.id.redChart);
 
-		mHandler = new Handler() {
-			public void handleMessage(Message msg) {
-				handleAFE4400Message(msg);
-			}
-		};
-	}
+    private class AraPloxChart {
+        int id;
+        private XYMultipleSeriesRenderer renderer;
+        private XYMultipleSeriesDataset dataSet;
+        private XYSeriesRenderer xySeriesRenderer;
+        private XYSeries xySeries;
+        private GraphicalView chartView;
 
-	private void handleAFE4400Message(Message msg) {
-		//	Log.d(TAG, "Message received: arg1: " + msg.arg1 + " arg2: " + msg.arg2);
-		double x = msg.arg1;
-		double y = msg.arg2;
+        public AraPloxChart(int id) {
+            this.id = id;
+        }
 
-		xySeries.add(x, y);
-		// Scroll continuously after ~150 points
-		if (x > 200) {
-			xySeries.remove(0);
-		}
+        public void makeChart(int pointColor) {
+            dataSet = new XYMultipleSeriesDataset();
+            xySeries = new XYSeries("");
+            dataSet.addSeries(xySeries);
 
-        // Autoscale
-        double maxX = xySeries.getMaxX();
-        double minX = xySeries.getMinX();
-        double maxY = xySeries.getMaxY();
-        double minY = xySeries.getMinY();
+            int[] colors = new int[] {pointColor};
+            PointStyle[] styles = new PointStyle[] {PointStyle.CIRCLE};
 
-        maxY *= 1.02;
-        minY *= .98;
+            renderer = new XYMultipleSeriesRenderer();
 
-        renderer.setXAxisMax(maxX);
-        renderer.setXAxisMin(minX);
-        renderer.setYAxisMax(maxY);
-        renderer.setYAxisMin(minY);
+            int length = colors.length;
+            for (int i = 0; i < length; i++) {
+                xySeriesRenderer = new XYSeriesRenderer();
+                xySeriesRenderer.setFillPoints(true);
+                xySeriesRenderer.setLineWidth(10.0f);
+                xySeriesRenderer.setPointStrokeWidth(1);
+                xySeriesRenderer.setColor(colors[i]);
+                xySeriesRenderer.setPointStyle(styles[i]);
+                renderer.addSeriesRenderer(xySeriesRenderer);
+            }
 
-        chart_view.repaint();
+            renderer.setChartTitleTextSize(20);
+            renderer.setLabelsTextSize(15);
+            renderer.setLegendTextSize(15);
+            renderer.setPointSize(5f);
+            renderer.setMargins(new int[] { 50, 50, 50, 50 });
+            renderer.setXAxisMin(0);
+            renderer.setXAxisMax(10);
+            renderer.setYAxisMin(0);
+            renderer.setYAxisMax(100);
+            renderer.setAxesColor(Color.BLACK);
+            renderer.setLabelsColor(Color.BLACK);
+            renderer.setShowLegend(false);
+            renderer.setXLabels(10);
+            renderer.setYLabels(0);
+            renderer.setShowGrid(false);
+            renderer.setXLabelsAlign(Align.CENTER);
+            renderer.setApplyBackgroundColor(true);
+            renderer.setBackgroundColor(Color.WHITE);
+            renderer.setMarginsColor(Color.WHITE);
 
+            chartView = ChartFactory.getCubeLineChartView(getApplicationContext(), dataSet, renderer, 1.0f);
+
+            LinearLayout layout = (LinearLayout)findViewById(id);
+            layout.addView(chartView, new LayoutParams(LayoutParams.FILL_PARENT,
+                        LayoutParams.FILL_PARENT));
+            chartView.repaint();
+        }
+
+        public void removeChart() {
+            LinearLayout layout = (LinearLayout)findViewById(id);
+            layout.removeView(chartView);
+        }
+
+        public void handleNewSample(double x, double y) {
+            xySeries.add(x, y);
+
+            // Scroll continuously after SCROLLBACK_SIZE points
+            if (x > SCROLLBACK_SIZE) {
+                xySeries.remove(0);
+            }
+
+            // Autoscale
+            double maxX = xySeries.getMaxX();
+            double minX = xySeries.getMinX();
+            double maxY = xySeries.getMaxY();
+            double minY = xySeries.getMinY();
+
+            maxY *= 1.01;
+            minY *= .99;
+
+            renderer.setXAxisMax(maxX);
+            renderer.setXAxisMin(minX);
+            renderer.setYAxisMax(maxY);
+            renderer.setYAxisMin(minY);
+
+            chartView.repaint();
+
+            sampleCount++;
+        }
     }
 
-    private XYMultipleSeriesRenderer renderer;
-	private XYMultipleSeriesDataset dataSet;
-	private XYSeries xySeries;
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        Log.d(TAG, "onCreate()");
+        requestWindowFeature(Window.FEATURE_NO_TITLE);
+        setContentView(R.layout.activity_ara_plox);
+        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
 
-	private void makeChart() {
-		dataSet = new XYMultipleSeriesDataset();
-		xySeries = new XYSeries("");
-		dataSet.addSeries(xySeries);
+        mHandler = new Handler() {
+            public void handleMessage(Message msg) {
+                double irLedSample  = msg.arg1;
+                double redLedSample = msg.arg2;
 
-		int[] colors = new int[] {Color.RED};
-		PointStyle[] styles = new PointStyle[] {PointStyle.CIRCLE};
+                irPloxChart.handleNewSample(sampleCount, irLedSample);
+                redPloxChart.handleNewSample(sampleCount, redLedSample);
 
-		renderer = new XYMultipleSeriesRenderer();
+                sampleCount++;
+            }
+        };
+    }
 
-		int length = colors.length;
-		for (int i = 0; i < length; i++) {
-			XYSeriesRenderer r = new XYSeriesRenderer();
-			r.setFillPoints(true);
-			r.setLineWidth(10.0f);
-			r.setPointStrokeWidth(1);
-			r.setColor(colors[i]);
-			r.setPointStyle(styles[i]);
-			renderer.addSeriesRenderer(r);
-		}
+    protected void onResume() {
+        super.onResume();
+        Log.d(TAG, "onResume()");
+        if (afe4400Thread == null) {
+            afe4400Thread = new AFE4400Thread(getApplicationContext(), mHandler);
+            afe4400Thread.start();
+        }
+        irPloxChart.makeChart(Color.GREEN);
+        redPloxChart.makeChart(Color.RED);
+    }
 
-		renderer.setChartTitleTextSize(20);
-		renderer.setLabelsTextSize(15);
-		renderer.setLegendTextSize(15);
-		renderer.setPointSize(5f);
-		renderer.setMargins(new int[] { 100, 100, 100, 100 });
-		renderer.setXAxisMin(0);
-		renderer.setXAxisMax(10);
-		renderer.setYAxisMin(0);
-		renderer.setYAxisMax(100);
-		renderer.setAxesColor(Color.BLACK);
-		renderer.setLabelsColor(Color.BLACK);
-		renderer.setShowLegend(false);
-		renderer.setXLabels(10);
-		renderer.setYLabels(0);
-		renderer.setShowGrid(false);
-		renderer.setXLabelsAlign(Align.CENTER);
-		renderer.setApplyBackgroundColor(true);
-		renderer.setBackgroundColor(Color.WHITE);
-		renderer.setMarginsColor(Color.WHITE);
-		if (chart_view == null) {
-			LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
-			chart_view = ChartFactory.getCubeLineChartView(this, dataSet, renderer, 1.0f);
-			layout.addView(chart_view, new LayoutParams(LayoutParams.FILL_PARENT,
-					LayoutParams.FILL_PARENT));
-			chart_view.repaint();
-		}
-	}
+    protected void onPause() {
+        super.onPause();
+        Log.d(TAG, "onPause()");
+        if (afe4400Thread != null) {
+            afe4400Thread.requestStop();
+            afe4400Thread = null;
+        }
+        irPloxChart.removeChart();
+        redPloxChart.removeChart();
+    }
 
-	protected void onResume() {
-		super.onResume();
-		Log.d(TAG, "onResume()");
-		if (afe4400Thread == null) {
-			afe4400Thread = new AFE4400Thread(getApplicationContext(), mHandler);
-			afe4400Thread.start();
-		}
-		makeChart();
-	}
-
-	protected void onPause() {
-		super.onPause();
-		Log.d(TAG, "onPause()");
-		if (afe4400Thread != null) {
-			afe4400Thread.requestStop();
-			afe4400Thread = null;
-		}
-
-	}
-
-	protected void onStop() {
-		super.onStop();
-		Log.d(TAG, "onStop()");
-		if (chart_view != null) {
-			LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
-			layout.removeView(chart_view);
-			chart_view = null;
-		}
-	}
+    protected void onStop() {
+        super.onStop();
+        Log.d(TAG, "onStop()");
+    }
 }