Adding another GPIO channels
Hi, I am a mbientlab metawear RG sensor user. I connected the board with 2 pressure sensors on GPIO Pin 2 and Pin 3. My project dependence is com.mbientlab:metawear:2.5.0. If I use one gpio, the graph can display normally. But if i add another gpio,the two gpio values will interfere with each other.My code shows as follows.I do not know why it happened.
---------------------------------------------------------------------------------------------------------------
private final AsyncOperation.CompletionHandler<RouteManager> GpioStreamSetup_2 = new AsyncOperation.CompletionHandler<RouteManager>() {
@Override
public void success(RouteManager result) {
streamRouteManager2 = result;
result.subscribe(streamKey2, new RouteManager.MessageHandler() {
@Override
public void process(Message message) {
final short gpioValue2 = message.getData(Short.class);
......
}
}
}
}private final AsyncOperation.CompletionHandler<RouteManager> GpioStreamSetup_3 = new AsyncOperation.CompletionHandler<RouteManager>() {
@Override
public void success(RouteManager result) {
streamRouteManager3 = result;
result.subscribe(streamKey3, new RouteManager.MessageHandler() {
@Override
public void process(Message message) {
final short gpioValue3 = message.getData(Short.class);
......
}
}
}
}-------------------------------------------------------------------------------------
protected void setup() {
gpioModule_2.routeData().fromAnalogIn(gpioPin2, Gpio.AnalogReadMode.ADC).stream(streamKey).commit()
.onComplete(GpioStreamSetup_2);
gpioModule_3.routeData().fromAnalogIn(gpioPin3, Gpio.AnalogReadMode.ADC).stream(streamKey_3).commit()
.onComplete(GpioStreamSetup_3);
timerModule_2.scheduleTask(new Timer.Task() {
@Override
public void commands() {
gpioModule_2.readAnalogIn(gpioPin2, Gpio.AnalogReadMode.ADC);
gpioModule_3.readAnalogIn(gpioPin3, Gpio.AnalogReadMode.ADC);
}
}, GPIO_SAMPLE_PERIOD, false).onComplete(new AsyncOperation.CompletionHandler<Timer.Controller>() {
@Override
public void success(Timer.Controller result) {
result.start();
}
});
}This discussion has been closed.
Comments
private final AsyncOperation.CompletionHandler<RouteManager> GpioStreamSetup_2 = new AsyncOperation.CompletionHandler<RouteManager>() {
----------------------------------------------------------------------@Override
public void success(RouteManager result) {
streamRouteManager = result;
result.subscribe(streamKey_2, new RouteManager.MessageHandler() {
@Override
public void process(Message message) {
final short gpioValue2 = message.getData(Short.class);
LineData data = chart.getData();
if (startTime == -1) {
data.addXValue("0");
startTime = System.currentTimeMillis();
} else {
data.addXValue(String.format(Locale.US,"%.2f", sampleCount_2 * samplingPeriod));
}
temp2 = (float) (gpioValue2 *0.056 -1.287);
if(temp2<0){
temp2=0;
}
data.addEntry(new Entry(temp2, sampleCount_2), 0);
Log.w("pressure", "temp2 is " + temp2);
savaPressure(temp2);
sampleCount_2++;
if (atomicBoolean.get()) {
UpdatePressure();
Log.w("gpiopressure", "send" + System.currentTimeMillis() + "ppppp" + PublicStatic.UploadFrequence);
atomicBoolean.compareAndSet(true, false);
}
}
});
}
};
timerModule_2.scheduleTask(new Timer.Task() {
@Override
public void commands() {
gpioModule_2.readAnalogIn(gpioPin2, Gpio.AnalogReadMode.ADC);
gpioModule_3.readAnalogIn(gpioPin3, Gpio.AnalogReadMode.ADC);
}
}, GPIO_SAMPLE_PERIOD, false).onComplete(new AsyncOperation.CompletionHandler<Timer.Controller>() {
@Override
public void success(Timer.Controller result) {
result.start();
}
});
private final AsyncOperation.CompletionHandler<RouteManager> GpioStreamSetup_3 = new AsyncOperation.CompletionHandler<RouteManager>() {@Override
public void success(RouteManager result3) {
streamRouteManager_3= result3;
result3.subscribe(streamKey_3, new RouteManager.MessageHandler() {
@Override
public void process(Message message) {
final short gpioValue3 = message.getData(Short.class);
LineData data2 = chart2.getData();
if (startTime1 == -1) {
data2.addXValue("0");
startTime1 = System.currentTimeMillis();
} else {
data2.addXValue(String.format("%.2f", sampleCount_3 * samplingPeriod));
}
temp3 = (float) (gpioValue3 *0.056 -1.287);
if(temp3<0){
temp3=0;
}
data2.addEntry(new Entry(temp3, sampleCount_3), 0);
Log.w("pressure", "temp3 is " + temp3);
//savaPressure(temp3);
sampleCount_3++;
if (atomicBoolean.get()) {
UpdatePressure();
atomicBoolean.compareAndSet(true, false);
}
}
});
}
};
01-20 11:07:45.222 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 621
01-20 11:07:45.273 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 1
01-20 11:07:45.273 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 618
01-20 11:07:45.317 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 1
01-20 11:07:45.320 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 1
01-20 11:07:45.389 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 618
01-20 11:07:45.424 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 622
01-20 11:07:45.463 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 1
01-20 11:07:45.465 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 620
01-20 11:07:45.524 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 621
01-20 11:07:45.524 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 1
01-20 11:07:45.524 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 619
01-20 11:07:45.560 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 1
01-20 11:07:45.562 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 619
01-20 11:07:45.624 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 1
01-20 11:07:45.630 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 621
01-20 11:07:45.633 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 621
01-20 11:07:45.724 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 620
01-20 11:07:45.758 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 623
01-20 11:07:45.759 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 1
01-20 11:07:45.765 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 613
01-20 11:07:45.805 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 618
01-20 11:07:45.859 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 1
01-20 11:07:45.902 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 622
01-20 11:07:45.999 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 1
01-20 11:07:46.006 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 626
01-20 11:07:46.059 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 627
public class GpioPressueFragment extends SingleDataSensorFragment {
private static java.util.Timer timer = new java.util.Timer(true);
private static AtomicBoolean atomicBoolean = new AtomicBoolean(true);
private TimerTask task;
private final String streamKey_2= "gpio_2_stream";
private final String streamKey_3= "gpio_3_stream";
private static final int GPIO_SAMPLE_PERIOD = 33;
private SharedPreferences mySharedPreferences;
private SharedPreferences userInfoSharePreferences;
private SharedPreferences.Editor editor;
public static float temp2;
public static float temp3;
private byte gpioPin2 = 2;
private byte gpioPin3 = 3;
private int readMode = 0;
private int chartSet = 0;
private Gpio gpioModule_2;
// private Gpio gpioModule_3;
private Timer timerModule_2;
// private Timer timerModule_3;
private long startTime = -1;
private long startTime1 = -1;
private OkHttpUtil httpUtil = new OkHttpUtil();
private float pressure_max, pressure_min, pressure_average;
private int count = 1;
private DecimalFormat df = new DecimalFormat("0.00");
private final AsyncOperation.CompletionHandler<RouteManager> GpioStreamSetup_2 = new AsyncOperation.CompletionHandler<RouteManager>() {
@Override
public void success(RouteManager result) {
streamRouteManager = result;
result.subscribe(streamKey_2, new RouteManager.MessageHandler() {
@Override
public void process(Message message) {
final short gpioValue2 = message.getData(Short.class);
LineData data = chart.getData();
if (startTime == -1) {
data.addXValue("0");
startTime = System.currentTimeMillis();
} else {
data.addXValue(String.format(Locale.US,"%.2f", sampleCount_2 * samplingPeriod));
}
Log.w("module", "gpioValue2 is " + gpioValue2);
temp2 = (float) (gpioValue2 *0.056 -1.287);
if(temp2<0){
temp2=0;
}
data.addEntry(new Entry(temp2, sampleCount_2), 0);
savaPressure(temp2);
sampleCount_2++;
if (atomicBoolean.get()) {
UpdatePressure();
Log.w("gpiopressure", "send" + System.currentTimeMillis() + "ppppp" + PublicStatic.UploadFrequence);
atomicBoolean.compareAndSet(true, false);
}
}
});
}
};
private final AsyncOperation.CompletionHandler<RouteManager> GpioStreamSetup_3 = new AsyncOperation.CompletionHandler<RouteManager>() {
@Override
public void success(RouteManager result) {
streamRouteManager_3= result;
result.subscribe(streamKey_3, new RouteManager.MessageHandler() {
@Override
public void process(Message message) {
final short gpioValue3 = message.getData(Short.class);
LineData data2 = chart2.getData();
if (startTime1 == -1) {
data2.addXValue("0");
startTime1 = System.currentTimeMillis();
} else {
data2.addXValue(String.format("%.2f", sampleCount_3 * samplingPeriod));
}
Log.w("module", "gpioValue3 is " + gpioValue3);
temp3 = (float) (gpioValue3 *0.056 -1.287);
if(temp3<0){
temp3=0;
}
data2.addEntry(new Entry(temp3, sampleCount_3), 0);
Log.w("pressure", "temp3 is " + temp3);
sampleCount_3++;
if (atomicBoolean.get()) {
UpdatePressure();
atomicBoolean.compareAndSet(true, false);
}
}
});
}
};
private void UpdatePressure() {
JSONObject json = new JSONObject();
try {
json.put("sensorData1", temp2);
json.put("sensorData2",temp3);
json.put("sensorData3", 0);
json.put("sensorData4", 0);
json.put("count", 0);
json.put("gait", 0);
json.put("time", System.currentTimeMillis());
json.put("patientMobile",userInfoSharePreferences.getString("id","0"));
} catch (JSONException e) {
e.printStackTrace();
}
httpUtil.postJson(HttpUrl.NLF_DATA, json.toString(), getActivity(), new OkHttpUtil.HttpCallBack() {
@Override
public void onSusscess(JSONObject data) {
return;
}
});
}
class AutomicSetBooleanTask extends TimerTask {
public void run() {
Log.w("send", "senddddd");
atomicBoolean.set(true);
}
}
public GpioPressueFragment() {
super(R.string.navigation_fragment_gpio, "adc", R.layout.fragment_gpio, GPIO_SAMPLE_PERIOD / 1000.f, 0, 1023);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
01-20 11:07:45.222 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 621
01-20 11:07:45.273 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 1
01-20 11:07:45.273 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 618
01-20 11:07:45.317 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 1
01-20 11:07:45.320 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 1
01-20 11:07:45.389 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 618
01-20 11:07:45.424 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 622
01-20 11:07:45.463 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 1
01-20 11:07:45.465 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 620
01-20 11:07:45.524 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 621
01-20 11:07:45.524 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 1
01-20 11:07:45.524 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 619
01-20 11:07:45.560 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 1
01-20 11:07:45.562 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 619
01-20 11:07:45.624 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 1
01-20 11:07:45.630 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 621
01-20 11:07:45.633 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 621
01-20 11:07:45.724 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 620
01-20 11:07:45.758 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 623
01-20 11:07:45.759 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 1
01-20 11:07:45.765 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 613
01-20 11:07:45.805 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 618
01-20 11:07:45.859 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 1
01-20 11:07:45.902 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 622
01-20 11:07:45.999 18253-18253/com.mbientlab.metawear.app W/module: gpioValue2 is 1
01-20 11:07:46.006 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 626
01-20 11:07:46.059 18253-18253/com.mbientlab.metawear.app W/module: gpioValue3 is 627
protected void setup() {
gpioModule_2.routeData().fromAnalogIn(gpioPin2, Gpio.AnalogReadMode.ADC).stream(streamKey_2).commit()
.onComplete(GpioStreamSetup_2);
gpioModule_2.routeData().fromAnalogIn(gpioPin3, Gpio.AnalogReadMode.ADC).stream(streamKey_3).commit()
.onComplete(GpioStreamSetup_3);
timerModule_2.scheduleTask(new Timer.Task() {
@Override
public void commands() {
gpioModule_2.readAnalogIn(gpioPin2, Gpio.AnalogReadMode.ADC);
// gpioModule_2.readAnalogIn(gpioPin3, Gpio.AnalogReadMode.ADC);
}
}, GPIO_SAMPLE_PERIOD, false).onComplete(new AsyncOperation.CompletionHandler<Timer.Controller>() {
@Override
public void success(Timer.Controller result) {
result.start();
}
});
}
Then gpioValue2 and gpioValue3 will not interfere with each other.But the gpioValue3 is digital,i need analog values.I again change my code like this :