I have a fragmentactivity and a fragment class; I am trying to set up so that when a user hits a Switch in my fragment class, the oncheckchangedlistener will soft-reset the IOIO-OTG board in order to allow the user to dynamically change a pin on the IOIO from output to input or vice versa.
However I am not sure how to call the ioio_.softReset(); method from outside classes.
Here is some relevent code:
//sets the listener for the mode switches
for(int i = 0; i<digitalIOModeSwitchArray.length;i++){
digitalIOModeSwitchArray[i].setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
}
});
}
NEXT:
@Override
protected IOIOLooper createIOIOLooper() {
return new Looper();
}
public IOIOLooper globalLooperRetriever(){
return l1;
}
NEXT:
package com.example.ioiorun;
import ioio.lib.api.DigitalInput;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.IOIO;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.BaseIOIOLooper;
public class Looper extends BaseIOIOLooper {
digitalFragment digitalFragmentObject;
// The variable digitalIOs
private DigitalOutput digitalO0;
private DigitalInput digitalI0;
private DigitalOutput digitalO1;
private DigitalInput digitalI1;
private DigitalOutput digitalO2;
private DigitalInput digitalI2;
private DigitalOutput digitalO3;
private DigitalInput digitalI3;
private DigitalOutput digitalO4;
private DigitalInput digitalI4;
private DigitalOutput digitalO5;
private DigitalInput digitalI5;
private DigitalOutput digitalO6;
private DigitalInput digitalI6;
private DigitalOutput digitalO7;
private DigitalInput digitalI7;
private DigitalOutput digitalO8;
private DigitalInput digitalI8;
private DigitalOutput digitalO9;
private DigitalInput digitalI9;
// The strictly digital-inputs.
private DigitalInput digitalInput0;
private DigitalInput digitalInput1;
private DigitalInput digitalInput2;
private DigitalInput digitalInput3;
private DigitalInput digitalInput4;
private DigitalOutput[] digitalOArray = { digitalO0, digitalO1, digitalO2,
digitalO3, digitalO4, digitalO5, digitalO6, digitalO7, digitalO8,
digitalO9 };
private DigitalInput[] digitalIArray = { digitalI0, digitalI1, digitalI2,
digitalI3, digitalI4, digitalI5, digitalI6, digitalI7, digitalI8,
digitalI9 };
private DigitalInput[] digitalInputArray = { digitalInput0, digitalInput1,
digitalInput2, digitalInput3, digitalInput4 };
/**
* Called every time a connection with IOIO has been established. Typically
* used to open pins.
*
* @throws ConnectionLostException
* When IOIO connection is lost.
*
* @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#setup()
*/
@Override
protected void setup() throws ConnectionLostException {
for (int i = 0; i < digitalOArray.length; i++) {
if (digitalFragmentObject.getIOModeSwitch(i).isActivated()) {
digitalOArray[i] = ioio_.openDigitalOutput(i + 9);
} else {
digitalIArray[i] = ioio_.openDigitalInput(i + 9);
}
}
for (int i = 0; i < digitalInputArray.length; i++) {
digitalInputArray[i] = ioio_.openDigitalInput(i + 9);
}
// for (int i = 0; i < pinArray.length; i++) {
// pinArray[i] = ioio_.openDigitalOutput(i + 1, false);
// }
}
/**
* Called repetitively while the IOIO is connected.
*
* @throws ConnectionLostException
* When IOIO connection is lost.
*
* @see ioio.lib.util.AbstractIOIOActivity.IOIOThread#loop()
*/
@Override
public void loop() throws ConnectionLostException {
// led_.write(!button_.isChecked());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
// for (int i = 0; i < pinDigArray.length; i++) {
// if(chosePin1.getValue() == i + 1){
// pinDigArray[i].write(true);
// }
}
public IOIO getIOIOBoardInstance() {
return ioio_;
}
}
EDIT:
*CODE ON THE createIOIOLooper();
METHOD AND THE BASEIOIOLOOPER CLASS:*
package ioio.lib.util;
import ioio.lib.api.IOIO;
import ioio.lib.api.exception.ConnectionLostException;
/**
* A convenience implementation of {@link IOIOLooper}.
*
* This base class provides no-op implementations for all methods and provides
* the {@link #ioio_} field for subclasses.
*
*/
public class BaseIOIOLooper implements IOIOLooper {
protected IOIO ioio_;
@Override
public final void setup(IOIO ioio) throws ConnectionLostException,
InterruptedException {
ioio_ = ioio;
setup();
}
/**
* This method will be called as soon as connection to the IOIO has been
* established. Typically, this will include opening pins and modules using
* the openXXX() methods of the {@link #ioio_} field.
*
* @throws ConnectionLostException
* The connection to the IOIO has been lost.
* @throws InterruptedException
* The thread has been interrupted.
*/
protected void setup() throws ConnectionLostException, InterruptedException {
}
@Override
public void loop() throws ConnectionLostException, InterruptedException {
Thread.sleep(20);
}
@Override
public void disconnected() {
}
@Override
public void incompatible() {
}
}
THE IOIOLOOPER CLASS:
package ioio.lib.util;
import ioio.lib.api.IOIO;
import ioio.lib.api.exception.ConnectionLostException;
/**
* A handler implementing interaction with a single IOIO over a single
* connection period. The interface utilizes a basic workflow for working with a
* IOIO instance: as soon as a connection is established, {@link #setup(IOIO)}
* will be called. Then, the {@link #loop()} method will be called repeatedly as
* long as the connection is alive. Last, the {@link #disconnected()} method
* will be called upon losing the connection (as result of physical
* disconnection, closing the application, etc). In case a IOIO with an
* incompatible firmware is encountered, {@link #incompatible()} will be called
* instead of {@link #setup(IOIO)}, and the IOIO instance is entirely useless,
* until eventually {@link #disconnected()} gets called.
*
*/
public interface IOIOLooper {
/**
* Subclasses should override this method for performing operations to be
* done once as soon as IOIO communication is established.
*/
public abstract void setup(IOIO ioio) throws ConnectionLostException,
InterruptedException;
/**
* Subclasses should override this method for performing operations to be
* done repetitively as long as IOIO communication persists. Typically, this
* will be the main logic of the application, processing inputs and
* producing outputs.
*/
public abstract void loop() throws ConnectionLostException,
InterruptedException;
/**
* Subclasses should override this method for performing operations to be
* done once as soon as IOIO communication is lost or closed. Typically,
* this will include GUI changes corresponding to the change. This method
* will only be called if setup() has been called. The ioio argument passed
* to {@link #setup(IOIO)} must not be used from within this method - it is
* invalid. This method should not block for long, since it may cause an
* ANR.
*/
public abstract void disconnected();
/**
* Subclasses should override this method for performing operations to be
* done if an incompatible IOIO firmware is detected. The ioio argument
* passed to {@link #setup(IOIO)} must not be used from within this method -
* it is invalid. This method will only be called once, until a compatible
* IOIO is connected (i.e. {@link #setup(IOIO)} gets called).
*/
public abstract void incompatible();
}
RELEVENT LINK TO IOIO-OTG GITHUB AND WIKI PAGES ON CLASSES:
RELEVENT LINK TO IOIOUSERS PAGE ON THIS TOPIC: FORUM PAGE
EDIT 2: *RELEVANT INFORMATION ON softReset(); METHOD FROM THE IOIO.JAVA CLASS IN :*
public void softReset() throws ConnectionLostException;
/**
* Equivalent to disconnecting and reconnecting the board power supply.
* <p>
* The connection will be dropped and not reestablished. Full boot sequence will take place, so
* firmware upgrades can be performed. A connection must have been established prior to calling
* this method, by invoking {@link #waitForConnect()}.
*
* @throws ConnectionLostException
* Connection was lost before or during the execution of this method.
* @see #softReset()
*/
Assuming you already have a looper instance in your activity Here's what you need to do
Define an interface
public interface SoftResetListener
{
void softReset();
}
Implement that interface in your activity
public class MainActivity implements SoftResetListener
{
public void softReset()
{
//replace with your looper instance
looper.getIOIOBoardInstance().softReset();
}
}
Inside your fragment onCheckedChanged()
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Activity looperActivity= getActivity();
if(looperActivity instanceof SoftResetListener) {
((SoftResetListener)looperActivity).softReset();
}
}