Question

So those who are familiar with the IOIO-OTG know you can set a pin on the board to be either an input or an output.

So I have the following code:

package ioio.examples.hello;

import ioio.lib.api.DigitalInput;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * This is the main activity of the HelloIOIO example application.
 * 
 * It displays a toggle button on the screen, which enables control of the
 * on-board LED. This example shows a very simple usage of the IOIO, by using
 * the {@link IOIOActivity} class. For a more advanced use case, see the
 * HelloIOIOPower example.
 */
@SuppressLint("NewApi")
public class MainActivity extends IOIOActivity {
    private NumberPicker chosePin1;
    private ToggleButton button_;
    private ToggleButton modeButton_;
    private TextView testerReader;

    /**
     * Called when the activity is first created. Here we normally initialize
     * our GUI.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        testerReader = (TextView) findViewById(R.id.testRead);
        button_ = (ToggleButton) findViewById(R.id.button);

        modeButton_ = (ToggleButton) findViewById(R.id.toggleMode);

        chosePin1 = (NumberPicker) findViewById(R.id.chosePin1);
        chosePin1.setMaxValue(46);
        chosePin1.setMinValue(1);
    }

    /**
     * This is the thread on which all the IOIO activity happens. It will be run
     * every time the application is resumed and aborted when it is paused. The
     * method setup() will be called right after a connection with the IOIO has
     * been established (which might happen several times!). Then, loop() will
     * be called repetitively until the IOIO gets disconnected.
     */
    class Looper extends BaseIOIOLooper {
        /** The on-board LED. */
        private DigitalOutput led_;

        boolean initial;
        boolean changed;


        private DigitalOutput pinO9
        private DigitalInput pinI9;

        /**
         * 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 {
            led_ = ioio_.openDigitalOutput(0, true);
        }

        /**
         * 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) {
            }

            pinO9 = ioio_.openDigitalOutput(9, false);
            pinO9.write(modeButton_.isActivated());

            if (!modeButton_.isActivated()) {
                pinI9.close();
                pinO9 = ioio_.openDigitalOutput(9,false);
                pinO9.write(true);
            } else if (modeButton_.isActivated()) {
                pinO9.close();
                pinI9 = ioio_.openDigitalInput(9);
                try {
                    if (pinI9.read()) {
                        testerReader.setText("ON");
                    } else {
                        testerReader.setText("OFF");
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }

    /**
     * A method to create our IOIO thread.
     * 
     * @see ioio.lib.util.AbstractIOIOActivity#createIOIOThread()
     */
    @Override
    protected IOIOLooper createIOIOLooper() {
        return new Looper();
    }
}

My problem lies in that; I am curious if there is a way to live-switch a pin from pinO9 to pinI9; basically switching it from input to output and vice versa based on the state of a Switch on the main_layout.xml file.

You can see within my looper call I have a method that compares the state of a switch to open and close pins; which does not work as I thought it would?

For those unfamiliar with the IOIO board it is a controller board similar to Arduino.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top