Issue with TextMenu using Timeout

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

fredsiva
New User
Posts: 13
Joined: Thu Nov 09, 2017 7:24 am

Issue with TextMenu using Timeout

Postby fredsiva » Fri Dec 22, 2017 1:40 pm

Hello,

using the very useful TextMenu class (thanks!), I encountered an issue with the select() method using the timeout as second argument. The method() immediately exits, even with a timeout of 50000. I checked the code, and I tried a quick fix on a private copy.

I believe that the System.currentTimeMillis() needs to be formally casted to int, otherwise the subsctract with _startTime does not give expected results.

Here is proposed fix in red.

if (timeout > 0 && ((int) System.currentTimeMillis()) - _startTime >= timeout) {

in its context:

Code: Select all

   public int select(int selectedIndex, int timeout)
   {
      if (selectedIndex >= _length)
         //might result in -1
         selectedIndex = _length -1;
      if (selectedIndex < 0)
         selectedIndex = 0;
      
//      if (_length<_size) _size = _length;
      _quit = false;
      resetTimeout();
//      LCD.clear();
      if (_topIndex > selectedIndex)
         _topIndex = selectedIndex;
      if (_topIndex > _length - _height)
         _topIndex = _length - _height;         
      display(selectedIndex, _topIndex);
      while(true)
      {
         int button;
         do
         {            
            if (_quit)
               return -2; // quit by another thread
            
            if (timeout > 0 && ((int) System.currentTimeMillis()) - _startTime >= timeout) {
               return -3; // timeout
               
            }
            


Does it make sense?

User avatar
gloomyandy
leJOS Team Member
Posts: 6017
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Issue with TextMenu using Timeout

Postby gloomyandy » Fri Dec 22, 2017 5:43 pm

Did your fix make things work? I'm struggling to see why it would unless that value of currentTimeMillis is > 2^31 was that the case with your tests?

I don't really understand why you are seeing a problem to be honest. The leJOS menu uses very similar code and specifies a timeout and that always seems to work fine.
leJOS news https://lejosnews.wordpress.com/

fredsiva
New User
Posts: 13
Joined: Thu Nov 09, 2017 7:24 am

Re: Issue with TextMenu using Timeout

Postby fredsiva » Fri Dec 22, 2017 10:54 pm

Yes, the simple fix worked. Without it, the select() method immediately returns -3
The time of my EV3 is set to the current time (Dec 2017) , which is indeed a huge number, which is I guess why it is normally stored in a "long" variable.

Here are some traces, but first the code (Which is a copy of the TextMenu)

Code: Select all

            
if (timeout > 0 && ((int) System.currentTimeMillis()) - _startTime >= timeout) {
   System.out.println(" Current time =" + System.currentTimeMillis() );
   System.out.println(" Start time =" + _startTime );
   System.out.println(" TextMenu - time diff is " + (System.currentTimeMillis() - _startTime )+ " and timeout=" + timeout);
   System.out.println("Fixed TextMenu - time diff is " + (((int) System.currentTimeMillis()) - _startTime )+ " and timeout=" + timeout);
   
   return -3; // timeout
}


and the system output:
Current time =1513982585504
Start time =-2140875006
TextMenu - time diff is 1516123460569 and timeout=5000
FTextMenu - time diff is 5112 and timeout=5000

Not a big deal, I can of course work with my fix, but I just thought this fix would make lejos more robust (Or storing the timeout in a long)

User avatar
gloomyandy
leJOS Team Member
Posts: 6017
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Issue with TextMenu using Timeout

Postby gloomyandy » Sat Dec 23, 2017 7:07 am

Ah yes I suspect this is only a problem if your system time has been set by using a time server. On my network I do not allow the EV3 to connect out to one and so the time does not get set.

Probably a better fix would be to store the start time as a long, but yes it certainly is a bug at the moment, I'll upload a fix the next time I'm working on leJOS.
leJOS news https://lejosnews.wordpress.com/


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 3 guests