Numpad demo

Place code snippets and demo code here
User avatar
karlkloss
Posts: 175
Joined: Fri Aug 18, 2023 12:21 pm
Has thanked: 25 times
Been thanked: 35 times

Numpad demo

Post by karlkloss »

Hi.
I programmed an example for a 320x240 Display (ESP32-2432S028), that shows two menu pages, with three numerical input fields each.
When you click on an input field, a numpad pops out, where you can input numbers.
Leading zeroes are suppressed, the < button deletes, the = button accepts the input.

Comments welcome, especially if somebody has an idea how to simplify this.

Code: [Local Link Removed for Guests]

' ********** MAIN **********
dim results(6) ' array that holds the values given back by the numpad
results(1)=0  ' Value 0.1, page 0
results(2)=0  ' Value 0.2, page 0
results(3)=0  ' Value 0.3, page 0
results(4)=0  ' Value 1.1, page 1
results(5)=0  ' Value 1.2, page 1
results(6)=0  ' Value 1.3, page 1
numpad_value=0
gosub Page0    ' show Page 0
gui.autorefresh 30,1
wait
' ********* END MAIN *******

' ********* PAGE 0 INIT *****
Page0:   
activepage = 0    ' set the number of the active page, so the numpad knows where to return to
numpadrunning = 0 ' Numpad is inactive
GUI.init 50, blue  ' Reserve enough elements, as the numpad uses the same page
txt_Title = GUI.textline(0,0,320,30,"Page 0",4,yellow,blue)
btn_next = gui.button(290,0,30,30,">",4)
gui.setevent btn_next, 1, Page1

txt_value1 = gui.textline(130,40, 100,40,"Value 0.1",4,yellow,blue,blue)
but_V1 = gui.button(20,40,100,40,STR$(results(1)),4,0,0,1,black,white,red,red)
gui.setevent but_V1,1,V1click

txt_value2 = gui.textline(130,90, 100,40,"Value 0.2",4,yellow,blue,blue)
but_V2 = gui.button(20,90,100,40,STR$(results(2)),4,0,0,1,black,white,red,red) 
gui.setevent but_V2,1,V2click

txt_value3 = gui.textline(130,140, 100,40,"Value 0.3",4,yellow,blue,blue)
but_V3 = gui.button(20,140,100,40,STR$(results(3)),4,0,0,1,black,white,red,red)
gui.setevent but_V3,1,V3click

Return
'********* PAGE 0 END *******

' ********* PAGE 1 INIT *****
Page1:   
activepage = 1    ' set the number of the active page, so the numpad knows where to return to
numpadrunning = 0
GUI.init 50, red
txt_Title = GUI.textline(0,0,320,30,"Page 1",4,yellow,red)
btn_prev = gui.button(0,0,30,30,"<",4,0,0,1,yellow,red,red,yellow)
gui.setevent btn_prev, 1, Page0

txt_value4 = gui.textline(130,40, 100,40,"Value 1.1",4,yellow,red,red)
but_V4 = gui.button(20,40,100,40,STR$(results(4)),4,0,0,1,black,white,red,red) 
gui.setevent but_V4,1,V4click

txt_value5 = gui.textline(130,90, 100,40,"Value 1.2",4,yellow,red,red)
but_V5 = gui.button(20,90,100,40,STR$(results(5)),4,0,0,1,black,white,red,red) 
gui.setevent but_V5,1,V5click

txt_value6 = gui.textline(130,140, 100,40,"Value 1.3",4,yellow,red,red)
but_V6 = gui.button(20,140,100,40,STR$(results(6)),4,0,0,1,black,white,red,red) 
gui.setevent but_V6,1,V6click

Return
'********* PAGE 0 END *******


' ******** V1CLICK **********
V1click: 
If NOT numpadrunning Then  ' activate numpad only when it isn't already running
   numpad_limit=6          ' set the limit to 6 digits
   numpad_x=150            ' set position of the numpad
   numpad_y=30
   numpad_target=but_V1    ' tell the numpad what input field to use
   resultsindex=1          ' tell the numpad where to store the end value
   Gosub numpad            ' show numpad
Endif
while gui.getvalue(but_V1)=1 ' prevent V1click from triggering multiple times
wend
Return
'******* V1CLICK END *********

'******* V2CLICK *************
V2click: 
If NOT numpadrunning Then
   numpad_limit=5
   numpad_x=150
   numpad_y=40
   numpad_target=but_V2
   resultsindex=2
   Gosub numpad
Endif
while gui.getvalue(but_V2)=1
wend
Return
'******* V2CLICK END **********

'******* V3CLICK *************
V3click: 
If NOT numpadrunning Then
   numpad_limit=4
   numpad_x=150
   numpad_y=90
   numpad_target=but_V3
   resultsindex=3
   Gosub numpad
Endif
while gui.getvalue(but_V3)=1
wend
Return
'******* V3CLICK END **********

'******* V4CLICK *************
V4click: 
If NOT numpadrunning Then
   numpad_limit=4
   numpad_x=150
   numpad_y=30
   numpad_target=but_V4
   resultsindex=4
   Gosub numpad
Endif
while gui.getvalue(but_V4)=1
wend
Return
'******* V4CLICK END **********

'******* V5CLICK *************
V5click: 
If NOT numpadrunning Then
   numpad_limit=5
   numpad_x=150
   numpad_y=40
   numpad_target=but_V5
   resultsindex=5
   Gosub numpad
Endif
while gui.getvalue(but_V5)=1
wend
Return
'******* V5CLICK END **********

'******* V6CLICK *************
V6click: 
If NOT numpadrunning Then
   numpad_limit=6
   numpad_x=150
   numpad_y=90
   numpad_target=but_V6
   resultsindex=6
   Gosub numpad
Endif
while gui.getvalue(but_V6)=1
wend
Return
'******* V6CLICK END **********

'******* NUMPAD INIT ************
numpad:
numpadrunning=1     ' numpad is active
numpad_value=0      ' initial value is 0 (could also be anything else)
numstr$=str$(numpad_value) ' the string holding the digits 
gui.settext numpad_target, numstr$ ' set the value of the input field
num_box = GUI.Box(numpad_x, numpad_y, 106, 140, white ,black, red)  ' draw box
num_7 = gui.button(numpad_x+4, numpad_y+4,30,30,"7",3,4,0,10,black,white,red,black) 'draw keys
num_8 = gui.button(numpad_x+38, numpad_y+4,30,30,"8",3,4,0,10,black,white,red,black)
num_9 = gui.button(numpad_x+72, numpad_y+4,30,30,"9",3,4,0,10,black,white,red,black)
num_4 = gui.button(numpad_x+4, numpad_y+38,30,30,"4",3,4,0,10,black,white,red,black)
num_5 = gui.button(numpad_x+38, numpad_y+38,30,30,"5",3,4,0,10,black,white,red,black)
num_6 = gui.button(numpad_x+72, numpad_y+38,30,30,"6",3,4,0,10,black,white,red,black)
num_1 = gui.button(numpad_x+4, numpad_y+72,30,30,"1",3,4,0,10,black,white,red,black)
num_2 = gui.button(numpad_x+38, numpad_y+72,30,30,"2",3,4,0,10,black,white,red,black)
num_3 = gui.button(numpad_x+72, numpad_y+72,30,30,"3",3,4,0,10,black,white,red,black)
num_0 = gui.button(numpad_x+4, numpad_y+106,30,30,"0",3,4,0,10,black,white,red,black)
num_del = gui.button(numpad_x+38, numpad_y+106,30,30,"<",3,4,0,10,black,white,red,black)
num_OK = gui.button(numpad_x+72, numpad_y+106,30,30,"=",3,4,0,10,black,white,red,black)

gui.setevent num_1, 1, numclick ' set a collective event for all buttons 
gui.setevent num_2, 1, numclick
gui.setevent num_3, 1, numclick
gui.setevent num_4, 1, numclick
gui.setevent num_5, 1, numclick
gui.setevent num_6, 1, numclick
gui.setevent num_7, 1, numclick
gui.setevent num_8, 1, numclick
gui.setevent num_9, 1, numclick
gui.setevent num_0, 1, numclick
gui.setevent num_del, 1, numclick
gui.setevent num_OK, 1, numclick
Return
'*********** NUMPAD END **********

'********** NUMCLICK *************
numclick:                        ' Event handler for keys
num_target = Gui.Target
Select Case num_target
  Case num_0: numstr$=numstr$+"0" ' set the digit for the key
  Case num_1: numstr$=numstr$+"1"    
  Case num_2: numstr$=numstr$+"2"
  Case num_3: numstr$=numstr$+"3"
  Case num_4: numstr$=numstr$+"4"    
  Case num_5: numstr$=numstr$+"5"
  Case num_6: numstr$=numstr$+"6"
  Case num_7: numstr$=numstr$+"7"    
  Case num_8: numstr$=numstr$+"8"
  Case num_9: numstr$=numstr$+"9"
  Case num_0: numstr$=numstr$+"0"    
  Case num_del: If len (numstr$)>0 Then numstr$=left$(numstr$,len(numstr$)-1) ' when key pressed is "<", delete the last digit
  Case num_OK:                   ' when key pressed is "=", accept the current value und close the keypad
     numpadrunning=0             ' numpad is inactive
     results(resultsindex)=numpad_value ' write end result back 
     If activepage = 0 Then Gosub Page0 ' Redraw the page where we came from
     If activepage = 1 Then Gosub Page1
End Select      

If numpadrunning Then            ' If numpad still active THEN
   If len(numstr$) > numpad_limit Then numstr$=left$(numstr$,len(numstr$)-1) ' If more digits than allowed, delete last one
   numpad_value = val(numstr$) ' convert string of digits to number
   numstr$=STR$(numpad_value)  ' convert back, so leading zeroes get deleted
   gui.settext numpad_target, numstr$ ' set the text of the input field that called numpad to the current value
   while gui.getvalue(num_target)=1 ' prevent double triggering
   wend
End If
Return
'******** NUMCLICK END ********

Last edited by karlkloss on Tue Dec 10, 2024 10:47 am, edited 2 times in total.
User avatar
karlkloss
Posts: 175
Joined: Fri Aug 18, 2023 12:21 pm
Has thanked: 25 times
Been thanked: 35 times

Re: Numpad demo

Post by karlkloss »

How it looks like:
grafik.png
You do not have the required permissions to view the files attached to this post.
User avatar
karlkloss
Posts: 175
Joined: Fri Aug 18, 2023 12:21 pm
Has thanked: 25 times
Been thanked: 35 times

Re: Numpad demo

Post by karlkloss »

p.s. I'd like to put this into a library, but I don't know how to do this in BASIC.
Everything is global.

And this is why global variables are bad:

grafik.png
You do not have the required permissions to view the files attached to this post.
RonS
Posts: 140
Joined: Thu Mar 02, 2023 10:15 pm
Location: germany
Has thanked: 79 times
Been thanked: 32 times

Re: Numpad demo

Post by RonS »

nice code... I'm sure there's a lot of work in it - I have noticed its run at a "standart" TFT too
[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Tue Dec 10, 2024 10:29 am Hi.
I programmed an example for a 320x240 Display (ESP32-2432S028), that shows two menu pages, with three numerical input fields each.
When you click on an input field, a numpad pops out, where you can input numbers.
Leading zeroes are suppressed, the < button deletes, the = button accepts the input.

Comments welcome, especially if somebody has an idea how to simplify this.

[
Modules : 3xESP32-Cam MB (Chip"DM ESP32 S" ),AI-Thinker Audio Kit (ES8388), ESP32 Dev Kit with Display
User avatar
cicciocb
Site Admin
Posts: 2793
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 593 times
Been thanked: 1998 times
Contact:

Re: Numpad demo

Post by cicciocb »

I think that something a little bit nicer should be possible :

User avatar
Dompie
Posts: 43
Joined: Tue Feb 09, 2021 12:59 pm
Has thanked: 58 times
Been thanked: 11 times

Re: Numpad demo

Post by Dompie »

LVGL is out of testing.that's an amazing result in such a short time!!!!
User avatar
karlkloss
Posts: 175
Joined: Fri Aug 18, 2023 12:21 pm
Has thanked: 25 times
Been thanked: 35 times

Re: Numpad demo

Post by karlkloss »

'twas just a quick hack, because I needed a numpad urgently.
I have a full keyboard in the making, but it's not finished yet, and also has low priority.
User avatar
cicciocb
Site Admin
Posts: 2793
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 593 times
Been thanked: 1998 times
Contact:

Re: Numpad demo

Post by cicciocb »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Tue Feb 04, 2025 10:43 pm LVGL is out of testing.that's an amazing result in such a short time!!!!
Yes, it works quite well but with a significative impact on the RAM and the size of the firmware as it do not fits inside the actual module partitioning.
The target is ESP32-S3 with PSRAM and flash > 8MB, it should also be OK for the ESP32 with PSRAM plus SDCARD.
WIth 4MB modules, the choice could also be the removal of the OTA partition to gain more space for the firmware.

Anyway, the big problem will be the documentation .... (around 100 new commands for the LVGL plus many more from the lovyanGFX) .
User avatar
karlkloss
Posts: 175
Joined: Fri Aug 18, 2023 12:21 pm
Has thanked: 25 times
Been thanked: 35 times

Re: Numpad demo

Post by karlkloss »

Real men don't read docs, they read the sourcecode.
User avatar
cicciocb
Site Admin
Posts: 2793
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 593 times
Been thanked: 1998 times
Contact:

Re: Numpad demo

Post by cicciocb »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Wed Feb 05, 2025 11:19 am Real men don't read docs, they read the sourcecode.
:D
And real C++ warriors read the Assembly!

I suppose that people capable of reading source code likely use the Arduino SDK or Espressif's IDF.

And even in this case, "Maybe reading docs wasn't such a bad idea..."

Seriously, in general, LVGL is very comprehensive and so powerful that it proves to be too complex for the audience Annex is targeting.
Post Reply