首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IOIO,有没有一种方法可以从另一个类调用softreset()?

IOIO,有没有一种方法可以从另一个类调用softreset()?
EN

Stack Overflow用户
提问于 2014-02-28 18:53:53
回答 1查看 448关注 0票数 1

我有一个碎片活动和一个片段类;我试图设置这样的设置,以便当用户单击我的片段类中的一个开关时,oncheckchangedlistener将软重置IOIO板,以便允许用户动态地将IOIO上的一个引脚从输出更改为输入,反之亦然。

但是,我不知道如何从外部类调用ioio_.softReset();方法。

以下是一些相关代码:

代码语言:javascript
复制
//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

        }
    });
}

下一步:

代码语言:javascript
复制
@Override
    protected IOIOLooper createIOIOLooper() {
        return new Looper();
    }

    public  IOIOLooper globalLooperRetriever(){
        return l1;
    }

下一步:

代码语言:javascript
复制
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();方法和BASEIOIOLOOPER类的代码:*

代码语言:javascript
复制
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类:

代码语言:javascript
复制
    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和WIKI页面:

IOIO WIKI

IOIOLIB框架页面

关于此主题的相关链接到IOIOUSERS页面: 论坛页

编辑2: *关于softReset()的相关信息;来自IOIO.JAVA类IN :*的方法

代码语言:javascript
复制
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()
 */
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-28 20:17:57

假设您的活动中已经有一个活套实例,下面是您需要做的事情

  1. 定义接口 公共接口SoftResetListener { void softReset();}
  2. 在您的活动中实现该接口 公共类MainActivity实现SoftResetListener { public void softReset() {//替换为您的活套实例looper.getIOIOBoardInstance().softReset();}
  3. 在片段onCheckedChanged()中 @Override (CompoundButton buttonView,boolean ){ActivitylooperActivity= getActivity();if(looperActivity instanceof SoftResetListener) {isChecked}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22103519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档