cancel
Showing results for 
Search instead for 
Did you mean: 

Simulator - Using key presses as inputs?

AMars.4
Associate III

Hello,

I noticed there is a way to print to a console from the simulator using touchgfx_printf, I'm wondering if it's possible to read in characters also?

I noticed the comment in Utils.hpp:

The console window is used to print to using touchgfx_printf(), printf() or std::cout,

 * and read from using e.g. fgets() or std::cin. Alternatively, instead of using

 * printf(), simply use touchgfx_printf() which will ensure there is a console to write

 * to. After the first call to touchgfx_printf(), it will also be possible to read from

 * stdin.

This would be useful to replace parts of the code where I am waiting on a message queue, instead I could do the following, pseudocode:

if SIMULATOR

wait on character input x from console

do action based on key input

else

wait on OS message queue

do action based on message

My understanding is that FreeRTOS is not used for the simulator, so this would be a useful way to imitate other system tasks sending messages to the GUI. This would allow me to still use the simulator and keep the build simple.

If it is possible, then it would make a great addition to the already very good TouchGFX documentation.

Thanks in advance,

Anthony

1 ACCEPTED SOLUTION

Accepted Solutions
Osman SOYKURT
ST Employee

Hello Anthony,

There's another way to read any character from your keyboard, just by using Interactions in TouchGFX Designer. Indeed, a new interaction with "hardware button is clicked" as the trigger will allows you to use your keyboard.

For instance, this configuration :

0693W00000QNswxQAD.pngmeans that if you click on the letter "a" on you keyboard, you will change the screen to screen2.

I'm attaching a project you can use if you need. It's 2 screens that can be switched by pressing on the keyboard.

/Osman

Osman SOYKURT
ST Software Developer | TouchGFX

View solution in original post

5 REPLIES 5
AMars.4
Associate III

To sort of answer my own question, yes you can read in from the simulator console, so something being triggered from the UI could wait for user input as a response (to mimic hardware).

Trickier however is just waiting on user input as the console and UI run on the same thread, if it was possible to wait on a user keyboard input in a non-blocking manner that would be really useful!

My crude example, just done in the Model:

#include <gui/model/Model.hpp>
#include <gui/model/ModelListener.hpp>
#include <touchgfx/Utils.hpp>
 
#include<iostream>
 
Model::Model() : modelListener(0), firstRun(true)
{	
	
}
 
void Model::tick()
{
    char inputCh;
 
    if(firstRun)
    {
        touchgfx_printf("Application is running through simulator! \n");
	firstRun = false;
    }
 
    touchgfx_printf("Waiting for command character > \n");
    std::cin>>inputCh;
    touchgfx_printf("You entered: %c \n", inputCh);
}

AMars.4
Associate III

To follow up, what would be required in this case is a way to read a character in a non-blocking manner... which when using GCC is not obvious to me how to do this.

Osman SOYKURT
ST Employee

Hello Anthony,

There's another way to read any character from your keyboard, just by using Interactions in TouchGFX Designer. Indeed, a new interaction with "hardware button is clicked" as the trigger will allows you to use your keyboard.

For instance, this configuration :

0693W00000QNswxQAD.pngmeans that if you click on the letter "a" on you keyboard, you will change the screen to screen2.

I'm attaching a project you can use if you need. It's 2 screens that can be switched by pressing on the keyboard.

/Osman

Osman SOYKURT
ST Software Developer | TouchGFX
AMars.4
Associate III

Hi Osman,

Appreciated, I was looking for a way to do this in code so didn't think to check in the designer.

So this way I can keep my application simpler, checking on RTOS message queues in the model tick for the hardware, and waiting on a keypress for the simulator (to call a virtual function most likely).

I will look at the example you sent, thanks again.

Anthony

You're welcome :)

Osman SOYKURT
ST Software Developer | TouchGFX