Skip to main content
Associate III
March 9, 2024
Solved

Custom keyboard negative and float values

  • March 9, 2024
  • 4 replies
  • 3782 views

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! 

This topic has been closed for replies.
Best answer by GaetanGodart

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,

4 replies

GaetanGodart
GaetanGodartBest answer
Technical Moderator
March 11, 2024

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,

GaetanGodart
Technical Moderator
March 26, 2024

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,

Associate III
March 31, 2024

Hi! 

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

Thank you! 

GaetanGodart
Technical Moderator
April 2, 2024

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,

GaetanGodart
Technical Moderator
April 2, 2024

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,

Associate III
April 3, 2024

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
Technical Moderator
April 3, 2024

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,

Associate III
April 3, 2024

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

Thank you! 

Regards, 

Thomas

GaetanGodart
Technical Moderator
April 4, 2024

No problem! :smiling_face_with_smiling_eyes:

 

Regards,