GPIO pins

Hi, i am trying to add in 2 more GPIO pins but couldnt get the data. Ive tried to change the code to read 2 more gpio pins and expand the save data, but i only get one line and it is not a straight line. It has many oscillations and it runs very fast. 

Save Data:
protected String saveData() {
final String CSV_HEADER = String.format("time,adc0,adc1,adc2%n");
String filename = String.format("%s_%tY%<tm%<td-%<tH%<tM%<tS%<tL.csv", getContext().getString(sensorResId), Calendar.getInstance());

try {
FileOutputStream fos = getActivity().openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(CSV_HEADER.getBytes());

LineData data = chart.getLineData();
LineDataSet gpio0DataSet = data.getDataSetByIndex(0), gpio1DataSet = data.getDataSetByIndex(1), gpio2DataSet = data.getDataSetByIndex(2);
for (int i = 0; i < data.getXValCount(); i++) {
fos.write(String.format("%.3f,%.3f,%.3f,%.3f%n", i * samplingPeriod,
gpio0DataSet.getEntryForXIndex(i).getVal(),
gpio1DataSet.getEntryForXIndex(i).getVal(),
gpio2DataSet.getEntryForXIndex(i)).getBytes());
}
fos.close();
return filename;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

Comments

  • read analog pins:
    private class GpioMessageHandler implements RouteManager.MessageHandler {
    private final ArrayList<Entry> dataEntries;
    private final int setIndex;

    public GpioMessageHandler(ArrayList<Entry> dataEntries, int setIndex) {
    this.dataEntries= dataEntries;
    this.setIndex= setIndex;
    }
    @Override
    public void process(Message message) {
    final Float gpioValue = message.getData(Float.class);

    LineData data = chart.getData();
    if (startTime == -1) {
    data.addXValue("0");
    startTime= System.currentTimeMillis();
    } else {
    data.addXValue(String.format("%.2f", sampleCount * samplingPeriod));
    }

    data.addEntry(new Entry(gpioValue, sampleCount), 0);

    sampleCount++;
    }
    }
    case READ_ADC:
    gpioModule.routeData().fromAnalogIn(gpio0Pin, AnalogReadMode.ADC).stream(STREAM0_KEY).commit()
    .onComplete(new AsyncOperation.CompletionHandler<RouteManager>() {
    @Override
    public void success(RouteManager result) {
    streamRouteManager= result;
    result.subscribe(STREAM0_KEY, new GpioMessageHandler(gpio0Data, 0));
    }
    });
    gpioModule.routeData().fromAnalogIn(gpio1Pin, AnalogReadMode.ADC).stream(STREAM1_KEY).commit()
    .onComplete(new AsyncOperation.CompletionHandler<RouteManager>() {
    @Override
    public void success(RouteManager result) {
    streamRouteManager= result;
    result.subscribe(STREAM1_KEY, new GpioMessageHandler(gpio1Data, 0));
    }
    });
    gpioModule.routeData().fromAnalogIn(gpio2Pin, AnalogReadMode.ADC).stream(STREAM2_KEY).commit()
    .onComplete(new AsyncOperation.CompletionHandler<RouteManager>() {
    @Override
    public void success(RouteManager result) {
    streamRouteManager= result;
    result.subscribe(STREAM2_KEY, new GpioMessageHandler(gpio2Data, 0));
    }
    });
    break;
  • You are instantiating three GpioMessageHandler objects with the same index.
  • So what should i change in order to read and stream 3 gpio lines on the same graph? Ive refered to the barometer fragment which have similar codes to what i am doing but i cant seem to get it.
  • edited June 2016
    Look at how the barometer fragment sets up the pressure and altitude message handler and compare that to your current code.  Also, the only thing changes that need to be made to the copied BarometerMessageHandler class is how the data is interpreted as gpio data is a short not a float. 
  • I change the codes already. I can show three gpio lines but once i start sampling, there is an error saying that "Unfortunately, MetaWear has stopped." Can help me check my codes? Thank you:)
  • private class GpioMessageHandler implements RouteManager.MessageHandler {
    private final ArrayList<Entry> dataEntries;
    private final int setIndex;

    public GpioMessageHandler(ArrayList<Entry> dataEntries, int setIndex) {
    this.dataEntries = dataEntries;
    this.setIndex = setIndex;
    }

    @Override
    public void process(Message message) {
    final Short gpioValue = message.getData(Short.class);

    LineData data = chart.getData();
    if (startTime == -1) {
    data.addXValue("0");
    startTime= System.currentTimeMillis();
    } else {
    data.addXValue(String.format("%.2f", sampleCount * samplingPeriod));
    sampleCount++;
    }

    data.addEntry(new Entry(gpioValue, sampleCount), setIndex);
    data.addEntry(new Entry(gpioValue, sampleCount), setIndex);
    data.addEntry(new Entry(gpioValue, sampleCount), setIndex);

    }

    }
  • case READ_ADC:
    gpioModule.routeData().fromAnalogIn(gpioPin0, AnalogReadMode.ADC).stream(STREAM0_KEY).commit()
    .onComplete(new AsyncOperation.CompletionHandler<RouteManager>() {
    @Override
    public void success(RouteManager result) {
    streamRouteManager = result;
    result.subscribe(STREAM0_KEY, new GpioMessageHandler(gpio0Data, 0));
    }
    });
    gpioModule.routeData().fromAnalogIn(gpioPin1, AnalogReadMode.ADC).stream(STREAM1_KEY).commit()
    .onComplete(new AsyncOperation.CompletionHandler<RouteManager>() {
    @Override
    public void success(RouteManager result) {
    streamRouteManager = result;
    result.subscribe(STREAM1_KEY, new GpioMessageHandler(gpio1Data, 1));
    }
    });
    gpioModule.routeData().fromAnalogIn(gpioPin2, AnalogReadMode.ADC).stream(STREAM2_KEY).commit()
    .onComplete(new AsyncOperation.CompletionHandler<RouteManager>() {
    @Override
    public void success(RouteManager result) {
    streamRouteManager = result;
    result.subscribe(STREAM2_KEY, new GpioMessageHandler(gpio2Data, 2));
    }
    });
    break;
  • edited June 2016
    And do i have to add in one output control for each GPIO output? 
    Or one output control is enough to set for all 3 GPIO pins?
  • edited June 2016
    Please post the stack trace of the crash and relevant pieces of code mentioned in the stack trace.  Without the stack trace, I don't know what I should be looking for.

    One UI control is sufficient to control all the GPIO channels.
  • hi, can you please advise on what a stack trace is?
  • See this Stack Overflow thread.  The stack trace will be dumped into Logcat in red text.
This discussion has been closed.