How to interact with microphone input within states?

27 views
Skip to first unread message

Cheng

unread,
Mar 7, 2015, 6:35:46 PM3/7/15
to squirrel-st...@googlegroups.com
Hi all,

Suppose that I need to get two user confirmations within StateA so that I can move to StateB. Since user input is gathered from mic, there can be long delay. Thus I use one while loop (the inner loop) to gather user input.

Also, since I need to get two confirmation (yes or no should be viewed as confirmation), I use another loop (the while(true) loop) to make sure two confirmations are received. 

I plan to use the similar code inside the transitionFromAToBOnAny method:

boolean[] confirmed=new boolean[2];

while(true){

 for(int i=0; i<2; i++){

    String userInput;
    while((user=getUserInputFromMicrophone())!=null){
         if("Yes".equals(userInput){
             confirmed[i]=true;
         }else{
             confirmed[i]=false;
         }
         break;   //exit loop 
    }
 }

 if(confirmed[0]!=null&&confirmed[1]!=null){
      break; //exit loop
 }

}

fire(MoveToBEvent);

My questions:

1. Should the above code reside in the transitionFromAToBOnAny method or should it reside outside state machine completely?

2. Let us assume that gathering mic input can take 2-5 minutes. The state machine may handle other states like StateC due to some scheduler program, while the getUserInputFromMicrophone() is hanging. After the user input is gathered, I need to fire event inside StateC to activate StateA. When control finally falls in the transitionFromAToBOnAny method, how can I know if the user input is the first one or the second one? Will the state machine remember where the last line of code was executed upon the transition from StateA to StateB given the scheduler program.

Thanks,
Cheng

On Wed, Feb 11, 2015 at 7:03 PM, Cheng <zhouch...@gmail.com> wrote:
Thanks a lot.

On Wed, Feb 11, 2015 at 7:26 PM, vishal gaurav <vishals...@gmail.com> wrote:
Yes you can. There are numerous ways you can do that. Simplest is use string based events or lookup able event by some name (user input string).

On Monday, 9 February 2015 20:04:28 UTC+5:30, Jonathan Zhou wrote:
Hi,

I have two questions for help. 

1. Suppose that I need to monitor user input from console. When the input is "Love", the current state moves to the Love state. When it is Hate, the current state moves to the Hate state. My question is: How to link the user input to the transition? 

I would like to use Context to represent the user input. But how can I set up a listener to monitor the input from console? Please note that it isn't a Java Swing project.

2. Suppose that the current state machine is performing a function (as below), which can generate a random double. Depending on the double value, the next state varies.

              protected void ontoQuestion(FSMState from, FSMState to, FSMEvent event, String context) {

double move = new RandomBetween(0, 1);

if (move>0.5) {

                       //     need to move to State A

} else {

                       //     need to move to State B
}
}

My question is how to move to A or B but the supposed state to.

Thanks!

--
You received this message because you are subscribed to a topic in the Google Groups "squirrel state machine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/squirrel-state-machine/h_wOsvbpjws/unsubscribe.
To unsubscribe from this group and all its topics, send an email to squirrel-state-ma...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Reply all
Reply to author
Forward
0 new messages