IOIO,有没有办法调用softreset();来自另一个班级?

时间:2014-02-28 18:53:53

标签: java android android-fragments ioio

我有片段活动和片段类;我正在尝试设置,以便当用户点击我的片段类中的Switch时,oncheckchangedlistener将软复位IOIO-OTG板,以便允许用户动态地将IOIO上的引脚从输出更改为输入或副反之亦然。

但是我不知道如何调用ioio_.softReset();来自外部课程的方法。

以下是一些相关代码:

//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_;
    }
}

修改 * createIOIOLooper();方法和BASIIOIOLOOPER类的代码: *

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() {
    }
}

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();

}

有关IOIO-OTG GITHUB和WIKI PAGES的相关链接:

IOIO WIKI

IOIOLIB FRAMEWORK PAGE

有关此主题的相关内容的相关链接:FORUM PAGE

编辑2: * 有关softReset()的相关信息;来自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()
 */

1 个答案:

答案 0 :(得分:2)

假设您的活动中已经有一个looper实例 这是你需要做的事情

  1. 定义界面

    public interface SoftResetListener
    {
     void softReset();
    }
    
  2. 在您的活动中实施该界面

    public class MainActivity implements SoftResetListener
    {
       public void softReset()
       {
          //replace with your looper instance
          looper.getIOIOBoardInstance().softReset();
       }
    
    }
    
  3. 片段内的onCheckedChanged()

    @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)     {
        Activity looperActivity= getActivity();
    
        if(looperActivity instanceof SoftResetListener) {
          ((SoftResetListener)looperActivity).softReset();
        }
    
     }