0

I collect data from a loop here:

public void loop() throws ConnectionLostException, InterruptedException {

        led_.write(false);

        setCurrentAIVoltage0(AI0.getVoltage());
        setCurrentAIVoltage1(AI1.getVoltage());
        setCurrentAIVoltage2(AI2.getVoltage());
        setCurrentAIVoltage3(AI3.getVoltage());
        setCurrentAIVoltage4(AI4.getVoltage());
        setCurrentAIVoltage5(AI5.getVoltage());
        AIF.updateUI();
    }

Where currentAIVoltage0 through currentAIVoltage5 are all public float variables. and my updateUI method is a method in my Fragment, AIF.

AIF however tends to be slow using this approach; and the AI updates irregularly.

Here is my updateUI method in my class file for my object AIF:

    public void updateUI(){

         getActivity().runOnUiThread(new Runnable() 
            {
                 @Override
                public void run() {
                    // TODO Auto-generated method stub
                    AITVVOLT0.setText("Voltage is: " + getCurrentAIVoltage0());
                    AITVVOLT1.setText("Voltage is: " + getCurrentAIVoltage1());
                    AITVVOLT2.setText("Voltage is: " + getCurrentAIVoltage2());
                    AITVVOLT3.setText("Voltage is: " + getCurrentAIVoltage3());
                    AITVVOLT4.setText("Voltage is: " + getCurrentAIVoltage4());
                    AITVVOLT5.setText("Voltage is: " + getCurrentAIVoltage5());
                    AIVOLTBAR0.setProgress(Math.round(getCurrentAIVoltage0()*100));
                    AIVOLTBAR1.setProgress(Math.round(getCurrentAIVoltage1()*100));
                    AIVOLTBAR2.setProgress(Math.round(getCurrentAIVoltage2()*100));
                    AIVOLTBAR3.setProgress(Math.round(getCurrentAIVoltage3()*100));
                    AIVOLTBAR4.setProgress(Math.round(getCurrentAIVoltage4()*100));
                    AIVOLTBAR5.setProgress(Math.round(getCurrentAIVoltage5()*100));
                }
             }
           );  
        }

Full Fragment:

public static class AnalogInputFragment extends Fragment {
    public TextView AITVVOLT0;
    public TextView AITVVOLT1;
    public TextView AITVVOLT2;
    public TextView AITVVOLT3;
    public TextView AITVVOLT4;
    public TextView AITVVOLT5;

    public ProgressBar AIVOLTBAR0;
    public ProgressBar AIVOLTBAR1;
    public ProgressBar AIVOLTBAR2;
    public ProgressBar AIVOLTBAR3;
    public ProgressBar AIVOLTBAR4;
    public ProgressBar AIVOLTBAR5;

    public static AnalogInputFragment newInstance() {
        AnalogInputFragment fragment = new AnalogInputFragment();
        return fragment;
    }

    public AnalogInputFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.analogin_fragment,
                container, false);
        AITVVOLT0 = (TextView) rootView.findViewById(R.id.aintvvolt0);
        AITVVOLT1 = (TextView) rootView.findViewById(R.id.aintvvolt1);
        AITVVOLT2 = (TextView) rootView.findViewById(R.id.aintvvolt2);
        AITVVOLT3 = (TextView) rootView.findViewById(R.id.aintvvolt3);
        AITVVOLT4 = (TextView) rootView.findViewById(R.id.aintvvolt4);
        AITVVOLT5 = (TextView) rootView.findViewById(R.id.aintvvolt5);


        AIVOLTBAR0 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar0);
        AIVOLTBAR1 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar1);
        AIVOLTBAR2 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar2);
        AIVOLTBAR3 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar3);
        AIVOLTBAR4 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar4);
        AIVOLTBAR5 = (ProgressBar) rootView.findViewById(R.id.aintvvoltbar5);

        AITVVOLT0.setText("Voltage is: " + currentAIVoltage0);
        AITVVOLT1.setText("Voltage is: " + currentAIVoltage1);
        AITVVOLT2.setText("Voltage is: " + currentAIVoltage2);
        AITVVOLT3.setText("Voltage is: " + currentAIVoltage3);
        AITVVOLT4.setText("Voltage is: " + currentAIVoltage4);
        AITVVOLT5.setText("Voltage is: " + currentAIVoltage5);

        AIVOLTBAR0.setMax(330);
        AIVOLTBAR1.setMax(330);
        AIVOLTBAR2.setMax(330);
        AIVOLTBAR3.setMax(330);
        AIVOLTBAR4.setMax(330);
        AIVOLTBAR5.setMax(330);

        AIVOLTBAR0.setProgress(0);
        AIVOLTBAR1.setProgress(0);
        AIVOLTBAR2.setProgress(0);
        AIVOLTBAR3.setProgress(0);
        AIVOLTBAR4.setProgress(0);
        AIVOLTBAR5.setProgress(0);
        return rootView;
    }
    public void updateUI(){

         getActivity().runOnUiThread(new Runnable() 
            {
                 @Override
                public void run() {
                    // TODO Auto-generated method stub

                    AITVVOLT0.setText("Voltage is: " + getCurrentAIVoltage0());
                    AITVVOLT1.setText("Voltage is: " + getCurrentAIVoltage1());
                    AITVVOLT2.setText("Voltage is: " + getCurrentAIVoltage2());
                    AITVVOLT3.setText("Voltage is: " + getCurrentAIVoltage3());
                    AITVVOLT4.setText("Voltage is: " + getCurrentAIVoltage4());
                    AITVVOLT5.setText("Voltage is: " + getCurrentAIVoltage5());
                    AIVOLTBAR0.setProgress(Math.round(getCurrentAIVoltage0()*100));
                    AIVOLTBAR1.setProgress(Math.round(getCurrentAIVoltage1()*100));
                    AIVOLTBAR2.setProgress(Math.round(getCurrentAIVoltage2()*100));
                    AIVOLTBAR3.setProgress(Math.round(getCurrentAIVoltage3()*100));
                    AIVOLTBAR4.setProgress(Math.round(getCurrentAIVoltage4()*100));
                    AIVOLTBAR5.setProgress(Math.round(getCurrentAIVoltage5()*100));
                }
             }
           );  
        }
}
Timothy Frisch
  • 2,995
  • 2
  • 30
  • 64
  • Can you please be more specific about the problems you are having? And please show more of your code. – Xaver Kapeller Aug 20 '14 at 17:36
  • The problem is when running the program the data being displayed (which is setting up a series of progressbars and altering them based on voltage levels being read in on a separate thread) is super slow/choppy. – Timothy Frisch Aug 20 '14 at 17:39
  • Could you please show us the whole code of the `Fragment` and the code of wherever you get the data from you are displaying? – Xaver Kapeller Aug 20 '14 at 17:41
  • The code that gets the data is shown above with the `getVoltage` methods. I will add the full `Fragment` code. – Timothy Frisch Aug 20 '14 at 17:44
  • The code inside `getCurrentAIVoltage0()` is what is most likely causing the problems, I need to see that too. The Fragment is completely fine, although it seems you are calling `updateUI()` from somewhere outside the `Fragment`? This is kind of uncommon. `Fragments` are supposed to be self contained units, completely modular and not dependent on anything. They are supposed to be responsible for their own UI and nothing outside the `Fragment` should in any way have anything to do with something regarding the UI inside the `Fragment`. I think this misconception may be the source of your error. – Xaver Kapeller Aug 20 '14 at 17:47
  • The `getCurrentAIVoltage#()` methods are merely getters/setters from the MainActivity class, should this really matter? – Timothy Frisch Aug 20 '14 at 17:57
  • But where do you get your data from? I am telling you there is no way that the `Fragment` above could lag in any way. The error is somewhere else. And since you seem to be multi threading I assume that the error must have something to do with this. Hence show me where you get the data from. – Xaver Kapeller Aug 20 '14 at 18:00
  • How is `loop()` called? If you create too many `Runnable`s, the ui thread may become unresponsive and it could even lead to memory problems (if you create `Runnable`s faster than they are consumed) – fabian Aug 20 '14 at 18:02
  • what kind of update frequency are we talking about here? – njzk2 Aug 20 '14 at 18:08
  • The loop is called upon connection to an external device; I did not program the IOIO Loop(); API. – Timothy Frisch Aug 20 '14 at 18:13

0 Answers0