cancel
Showing results for 
Search instead for 
Did you mean: 

Custom keyboard negative and float values

Thomas8607
Senior

Hi all! 

 

I'm looking for a custom numerical keypad, with negative sign, comma(for float vaues) , clear and enter.

How can i create? I found only querty, and numerical without comma and sign.

 

Thank you! 

1 ACCEPTED SOLUTION

Accepted Solutions
GaetanGodart
ST Employee

Hello @Thomas8607 ,

 

To create a custom keyboard you could get inspired by the ones that you already saw.

Download the custom keyboard (file with tpkg extension) , create a new TouchGFX project from Designer, click "Edit" -> "Import" -> "Custom container", select the tpkg file you downloaded.

Then you can click on files on the bottom left corner and check the files used for the custom container under YourProject\TouchGFX\gui\src\containers and YourProject\TouchGFX\gui\include\containers.

 

I am not an expert in calculator creation but there must be a lot of examples online.

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)

View solution in original post

16 REPLIES 16
GaetanGodart
ST Employee

Hello @Thomas8607 ,

 

To create a custom keyboard you could get inspired by the ones that you already saw.

Download the custom keyboard (file with tpkg extension) , create a new TouchGFX project from Designer, click "Edit" -> "Import" -> "Custom container", select the tpkg file you downloaded.

Then you can click on files on the bottom left corner and check the files used for the custom container under YourProject\TouchGFX\gui\src\containers and YourProject\TouchGFX\gui\include\containers.

 

I am not an expert in calculator creation but there must be a lot of examples online.

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)
GaetanGodart
ST Employee

Hello @Thomas8607 ,

 

Were you able to achieve your desired result?

If my previous comments was helpful, I invite you to select it as "Best answer" to increase its visibility and help other people with the same issue as you.

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)

Hi! 

I started to convert a keyboard, but it is quite difficult.

Thank you! 

Hello @Thomas8607 ,

 

Replying to :

"

Hi Gaetan!

I modified the T9 keyboard a bit, what is finished is what it should be, but I can't supplement it with the following:

- a negative sign can only be placed in the first place (I entered a condition, but it doesn't work as I thought)
- if the buffer reaches the set limit, the keys cannot be pressed (numbers and "-", "."). If the size of the buffer is 10, I can still write into it and the simulator crashes
- only one decimal point can be entered and not in the first place.

 

If there is a negative signal in the buffer, then the output is int32_t, if there is not, then uint32_t. If there is a decimal point, it should be a float.
I don't know if this can be solved.

 

How can the widget be packaged so that I don't have to attach the whole project?

I would like to ask for help with these, if possible.

Thank you!

Regards,
Thomas Soronics

"

 

From what I have tried, I am able to put the minus sign on the 2 first characters. This is because the position starts at -1 and your condition is "position <= 0" which is true for values -1 and 0.
Simply change it to "position < 0".

 

There is also an error about the buffer size.
You store the characters in the buffer that you create, however, in your text area wildcard, you used a buffer of size 10 inside Designer:

GaetanGodart_0-1712056382001.png

The buffer in Designer have to have one more element to it which is the end of string character. So to display 10 characters, you need to set the buffer to 11.
Finally, you set the condition as "positionText <= 10", however, since the buffer initial position is -1 you actually get every value in [-1 ; 10] which is 12.
What you actually want is values between [-1; 10[ (1 included to 10 excluded) to get 10 characters.
So you should change "positionText <= 10" to "positionText < 9".

 

Last thing about the decimal point.
I see that you have set the "." as a regular character.
You should have instead made the point as a special character just like you did with the minus sign.
However, you can still make it work as a regular character by adding conditional statement in the functionWriteButtonCharacter function.
Here is what I have changed to make it work :

void containerNumKeyboard::functionWriteButtonCharacter(uint8_t value)
{

		if (positionText < 9){ // string length limit
            if(value == '.')
            {
                if( (positionText < 0) || isDot )   // either already have a dot or is at first position
                {
                    return;                         // so it is wrong call to point character so don't do anything.
                }
            }
            if(value == '.') isDot = true;
			positionText++;
			buffer[positionText] = value; //adds the character to the string char (buffer)
			Unicode::strncpy(textAreaKeyboardBuffer, buffer, TEXTAREAKEYBOARD_SIZE);// assign the string to the Text
			textAreaKeyboard.resizeHeightToCurrentText();
			textAreaKeyboard.invalidate();
		}
		else
		{
			touchgfx_printf("Buffer is full\n");
		}
}

 

I think it is better if we keep the discussion about this here and only use the widget discussion to publish widgets.

 

I hope this helps you!

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)

Thanks for the help

What else came to my mind and I'll try to solve it:
The zero button.
What if there is more than one zero in a row, but only if there is no decimal point.
Will the conversion function handle this (char to int or char to float)?


After I wrote it, I remembered that I should have written it here.
Sorry.

GaetanGodart
ST Employee

No problem @Thomas8607 !

 

As you said I will let you think for yourself on this one.

You could look at some other examples online of calculators.
I found a lot of examples using Javascript.
Even though Javascript is a high level language, this would give you the logic behind it.
Here is the example form FreeCodeCamp : FreeCodeCamp calculator JS .

Other than that, I have to say that the T9 keyboard might not the best base for a calculator where you do not change the keys that are displayed.
Instead you could use simple buttons or flex buttons.

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)

I wrote the conditions for the point and the zero as well.
If I checked correctly, everything works.


All that remains is to convert the contents of the buffer into a number, depending on whether it is negative/positive and whether there is a dot or not. 

GaetanGodart
ST Employee

Hello @Thomas8607 ,

 

I don't think you need to care about whether it is positive, negative or float. Instead just use a float type so it encompass all cases.

You could use the stof function to turn a string to a float : https://cplusplus.com/reference/string/stof/ .

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)

You're right, I don't know why I'm making it complicated.

Thank you! 

Regards, 

Thomas