RobotUI Text Example

This guide assumes you have already performed the setup and the development guide

Create a new Arduino example project

Within the Arduino IDE start a new project by using the menu File->New. Add the code shown below. Make sure to change the BT_BAUDRATE value if you are no longer using the default baudrate for your HC-06 module. This code will create a text box and nine word buttons to manipulate the contents of the text box.
#include <RobotUI.h> #define BT_BAUDRATE 9600 #define NUM_WORDS 9 RobotUI robotUI; RobotUIText textBox; RobotUIButton wordButtons[NUM_WORDS]; RobotUIButton clearButton; char const * const words[NUM_WORDS] = {"Peter", "Jane", "dog", "ball", "the", "and", "here", "is", "likes"}; void LayoutDef() { robotUI.StartLayout(); robotUI.Add(textBox, 1, 25); robotUI.StartHorizontal(1); robotUI.Add(wordButtons[0]); robotUI.Add(wordButtons[1]); robotUI.Add(wordButtons[2]); robotUI.EndHorizontal(); robotUI.StartHorizontal(1); robotUI.Add(wordButtons[3]); robotUI.Add(wordButtons[4]); robotUI.Add(wordButtons[5]); robotUI.EndHorizontal(); robotUI.StartHorizontal(1); robotUI.Add(wordButtons[6]); robotUI.Add(wordButtons[7]); robotUI.Add(wordButtons[8]); robotUI.EndHorizontal(); robotUI.Add(clearButton); robotUI.EndLayout(); } void RecoverStatus() { textBox.SetText(""); for(int i=0;i<NUM_WORDS;++i) { wordButtons[i].SetName(words[i]); } clearButton.SetName("Clear sentence"); } void setup() { robotUI.Init(ROBOTUI_PIN_RX, ROBOTUI_PIN_TX, BT_BAUDRATE, DEFAULT_COMMAND_BUFFER_SIZE, LayoutDef, RecoverStatus); } void loop() { unsigned long ms = robotUI.Update(); for(int i=0;i<NUM_WORDS;++i) { if(wordButtons[i].IsPressed()) { wordButtons[i].ConsumeIsPressed(); textBox.AppendText(" "); textBox.AppendText(words[i]); } } if(clearButton.IsPressed()) { clearButton.ConsumeIsPressed(); textBox.SetText(""); } }
Below you can see the RobotUI app running when connected to the above example code.



LayoutDef function

Here we declare an array of nine word buttons, a clear sentence button and a text box. The LayoutDef function defines the layout of those widgets. First the text box, followed by three rows of three buttons. Finally a clear button is added. This example shows that the text box in the android app can show a large amount of data in it. This is perfect for logging data that comes from the Arduino project.
RobotUI robotUI; RobotUIText textBox; RobotUIButton wordButtons[NUM_WORDS]; RobotUIButton clearButton; void LayoutDef() { robotUI.StartLayout(); robotUI.Add(textBox, 1, 25); robotUI.StartHorizontal(1); robotUI.Add(wordButtons[0]); robotUI.Add(wordButtons[1]); robotUI.Add(wordButtons[2]); robotUI.EndHorizontal(); robotUI.StartHorizontal(1); robotUI.Add(wordButtons[3]); robotUI.Add(wordButtons[4]); robotUI.Add(wordButtons[5]); robotUI.EndHorizontal(); robotUI.StartHorizontal(1); robotUI.Add(wordButtons[6]); robotUI.Add(wordButtons[7]); robotUI.Add(wordButtons[8]); robotUI.EndHorizontal(); robotUI.Add(clearButton); robotUI.EndLayout(); }

RecoverStatus function

The RecoverStatus function is where the Arduino project informs the RobotUI app of its current status. This particular example does not store any state, so all we have to do is set the names of the buttons. (Check the other examples at the bottom of the page if you want an example that DOES recover internal state). Our RecoverStatus function clears the text box with textBox.SetText(""). The for loop will iterate through the words array and set the name of the associated nine buttons. Finally the name of the clearButton object is set using clearButton.SetName("Clear sentence").
char const * const words[NUM_WORDS] = {"Peter", "Jane", "dog", "ball", "the", "and", "here", "is", "likes"}; void RecoverStatus() { textBox.SetText(""); for(int i=0;i<NUM_WORDS;++i) { wordButtons[i].SetName(words[i]); } clearButton.SetName("Clear sentence"); }

The main loop

The loop function is where we monitor the nine wordButton objects and the clearButton object to change the contents of the textBox object. First we call robotUI.Update() every frame so it can manage communications with the app. Then we iterate through the wordButtons and check if any have been pressed by using wordButtons[i].IsPressed() If it returns true then the contents of the if statement will be executed. So the first thing we do is to consume the button event with wordButtons[i].ConsumeIsPressed(), then we append the associated word to the textBox using textBox.AppendText(words[i]). We append a space before the word using textBox.AppendText(" "). After the for loop we check the clearButton object in the same manner to determine if the user wants to clear the sentence they have constructed. We clear the textBox object by using textBox.SetText("").
void loop() { unsigned long ms = robotUI.Update(); for(int i=0;i<NUM_WORDS;++i) { if(wordButtons[i].IsPressed()) { wordButtons[i].ConsumeIsPressed(); textBox.AppendText(" "); textBox.AppendText(words[i]); } } if(clearButton.IsPressed()) { clearButton.ConsumeIsPressed(); textBox.SetText(""); } }

Other Examples

Here are some other examples for various widgets

button example
check box example
seek bar example
spinner example
switch example
text example
toggle button example