RELEASED: NXC UserInteraction Library

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
elendurwen
Posts: 16
Joined: 31 Jan 2012, 19:44

RELEASED: NXC UserInteraction Library

Post by elendurwen »

The UserInteraction (UIn) Library helps you create standard user interfaces (multi-line aligned texts, buttons, menus, etc.) and capture user actions on the NXT Mindstorms brick. The library works with the intelligent brick buttons and keeps all your sensor ports free for your program to use.

Image Image Image

UIn Library download and documentation: http://lenkaspace.net/lab/legoMindstorm ... ionLibrary
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: RELEASED: NXC UserInteraction Library

Post by HaWe »

what I'm doing is simply this:

Code: Select all

/******************************************************************************/
inline bool btnhit(){
/******************************************************************************/
   return ( ButtonPressed(BTN1, false) || ButtonPressed(BTN2, false)
         || ButtonPressed(BTN3, false) || ButtonPressed(BTN4, false));
}

/******************************************************************************/
inline char btnin() {  // read btn continuously (like cin <iostream>)
/******************************************************************************/
    char result = -1;
    if (ButtonPressed(BTNCENTER, false))
      result = BTNCENTER;
    else if (ButtonPressed(BTNEXIT, false))
      result = BTNEXIT;
    else if (ButtonPressed(BTNLEFT, false))
      result = BTNLEFT;
    else if (ButtonPressed(BTNRIGHT, false))
      result = BTNRIGHT;
    return result;
}

/******************************************************************************/
int Menu (string text, string choice) {
/******************************************************************************/
   ClearScreen();
   TextOut(6*(16-strlen(text))/2,16,text);
   TextOut(6*(16-strlen(choice))/2, 8,choice);
   TextOut(0, 0,"< L  ok/ESC   R >", DRAW_OPT_INVERT);
   while(!btnhit());
   ClearScreen();
   return btnin();
}  

//...
  char buf;
  buf=Menu("Calibrate ?", "/9  /ALL  /no");
  if(buf==BTNLEFT)   Calibrate_9_Squares();
  else
  if(buf==BTNCENTER) CalibrateAllSquares();
elendurwen
Posts: 16
Joined: 31 Jan 2012, 19:44

Re: RELEASED: NXC UserInteraction Library

Post by elendurwen »

The reason why UserInteraction Library doesn't use the ButtonPressed method is that if you alternate between tasks, ButtonPressed may return true even if it was pressed in a previous task (button is still pressed when tasks are switched). This happens both when you set the boolean to reset count to true or false (ButtonPressed(BTNCENTER, true) vs ButtonPressed(BTNCENTER, false)).

For example, try this one using your Menu method:

Code: Select all


task temp1();
task temp2();

/* ... your Menu, btnin and btnhit methods ... */
/**
 * Startup task
 */
task main() {
    ExitTo(temp1);
}

task temp2() {
    char buf;
    buf=Menu("TEMP2", "/9  /ALL  /no");
    if(buf==BTNCENTER) {
        ExitTo(temp1);
    } 
}
task temp1() {
    char buf;
    buf=Menu("TEMP1", "/9  /ALL  /no");
    if(buf==BTNCENTER) {
        ExitTo(temp2);
    } 
    
}
You will find that while temp1->temp2 executes ok, hitting the middle button in temp2 does not always correctly point back to temp1. This is because while temp1 is ran, the test for ButtonPressed returns true imediatelly and switches back to temp2. UIn solves this by comparing the original ButtonCount at the beginning of a menu (or other UI) function with ButtonCount in a while loop.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: RELEASED: NXC UserInteraction Library

Post by HaWe »

ah, I see, thanks for explanation, that's really a big improvement!
elendurwen
Posts: 16
Joined: 31 Jan 2012, 19:44

Re: RELEASED: NXC UserInteraction Library

Post by elendurwen »

No problem, let me know if you find any difficulties if you are going to use my library
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: RELEASED: NXC UserInteraction Library

Post by mcsummation »

I'm using your UI library in a remote NXT to generate a script for my Odin robot. You posted this right at the time I was starting to deal with a UI for this program. Thank you.

I've found one issue - the line across the screen created by createQuestionDialog is one line higher than I would like. I want it immediately above the answers. So, I am going to change it to put the line on LCD_LINE6 instead of line5. That gives me 5 lines for question text. I also shortened the vertical lines from 10 pixels to 8.

One "wish item" - it would sure be nice if the code could split the text when a string is too long for the screen width. Or, at least display what it can, so it would be easier to see where to put in the line break characters.
elendurwen
Posts: 16
Joined: 31 Jan 2012, 19:44

Re: RELEASED: NXC UserInteraction Library

Post by elendurwen »

The line changes sound reasonable, I think the library should figure out the length of your text and put the line immediately below or one line below it in the future. This would give you more flexibility and keep the look as neat as possible, what do you think? Or did you specifically want to put the line just above the answers?

You are right about splitting of the text - it's annoying that you have to put in the line breaks and that text that is too long doesn't display at all (unfortunately this is directly caused by the TextOut function). Automatic splitting of lines is on my list for the next release.
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: RELEASED: NXC UserInteraction Library

Post by mcsummation »

I think it looks best if the line stays in the same place. I feel that it is associated with the answer area, not the text area. I've now moved it to position 9 so there is one pixel space between it and the top of the answer letters. I also increased the vertical lines to be 9 pixels high.

On one of the panels, I wanted a blank line so put two # signs together (##). It caused the text below that to not display. To get a proper display I had to put "# #". That's not necessarily a problem, just thought I would tell you.
elendurwen
Posts: 16
Joined: 31 Jan 2012, 19:44

Re: RELEASED: NXC UserInteraction Library

Post by elendurwen »

Ok, thanks for letting me know about that.
By the way, are building Odin robot as in from Starcraft II?
mcsummation
Posts: 220
Joined: 23 Jan 2012, 17:07
Location: Round Rock, TX

Re: RELEASED: NXC UserInteraction Library

Post by mcsummation »

The Odin I am doing is from http://www.philohome.com/odin/odin.htm.

When I started doing the "instruction" panels at the beginning of the dialog, I decided I needed another function, one that will handle multiple panels with one call and I didn't find one in your library. So,

Code: Select all

//=============== Multipanel display
/**
  * Dispaly multiple panels controlled by the NXT arrow keys.
  * @param panelItems_[] Array of strings that are the panels.
  * exits when user select "Next" on last panel.
  */
void createMultiPanel(string& panelItems_[]) {
    // State machine controls which item is displayed.
    int iState = 1;             // Display first panel.
    string sLeft, sRight;       // Values for buttons.
    until (0 == iState) {
        sLeft = "Previous";
        if (1 == iState)        // If at the first panel.
            sLeft = "";         // No "previous" panel to go to.
        if (ArrayLen(panelItems_) == iState)
            sRight = "Exit";
        else
            sRight = "Next";
        if (0 == createQuestionDialog(panelItems_[iState-1],sLeft,sRight)) {
            if (1 != iState)                        // If not at panel 1, move left.
                iState--;
        }
        else
            if (ArrayLen(panelItems_) == iState)    // If at the end, get out.
                iState = 0;
            else
                iState++;
    }
}
It is used thusly:

Code: Select all

        string sHelp[5] = {
                "1. Use the light#gray buttons#to answer a#question.",
                "2. Use them also#to select values#in the boxes.",
                "3. Use the#orange button#to accept#a value.",
                "4. You will be#asked to#choose a unit#of measure.",
                "5. You will#tell how far#each direction#to go.",
                          };
        createMultiPanel(sHelp);
If you already have this function, what is its name? If not, you are welcome to use this code.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 10 guests