GUI to Set Date Time

Place code snippets and demo code here
Post Reply
MarioL
Posts: 27
Joined: Sun Mar 21, 2021 8:38 am
Has thanked: 321 times
Been thanked: 40 times

GUI to Set Date Time

Post by MarioL »

Hi,
Below I share my solution for setting date and time via GUI of the touch TFT 320X240.
I hope it saves someone time.

Condivido di seguito la mia soluzione per settare data e ora tramite GUI del TFT 320X240 touch.
Spero faccia risparmiare tempo a qualcuno.




Code: [Local Link Removed for Guests]

' *************************************************************************************
' *****                                 File Info                                 *****
' *************************************************************************************
'    Filename: /GUI_SetDateTime
'    Date    : 28/02/2024
'    Version : 0
'    Edit. by: MarioL.
'    Function: example to set date and time from TFT GUI and chenge brightness
'    Firmware: Annex32 CAN 1.52.1 LFS
'    Hardware: ESP32-2432S028 (ESP32 2.8" 320X240 TFT ILI9341 with Touch R)
'    Doc.Ref.: Annex32 WIFI RDS Help Version 1.50.8
'    Note    :
' ***************************************************************************************

TFT.BRIGHTNESS 20
gosub lblPage_0
wait


'MAIN utility page
lblPage_0: '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
n_page = 0
'timer1 0 'disable timer for update device status
timer1 30000, lblUpdateDateTime  'enable timer for update date time in this page
gui.init 10, green

'Txt1 =GUI.Textline(x, y, width, height, "text" [,font] [,color_text] [,color_back] [,color_frame] [,alignment] [,margin] )
TxtGuiPage = gui.textline 50,2,220,48, "UTILITY PAGE", 4, red , white, red, ALIGN_MID_MID

'But = GUI.Button(x, y, width, height, "text" [,font] [,radius] [,toggle] [,group] [,color_text] [,color_pressed] [,color_released] [,color_frame] )
ButDatTimSet = GUI.Button(0, 60, 159, 48, "Date & Time", 4, 10, 0, 1, blue, yellow, green, black)    'cALL PING page
GUI.SETEVENT ButDatTimSet, 1, lblDateTimeSetPage

ButBrightness = GUI.Button(162, 60, 159, 48, "Brightness", 4, 10, 0, 1, blue, yellow, green, black) 'CALLbrightnes page control
GUI.SETEVENT ButBrightness, 1, lblBrightnessPage

TxtGuiDateTime = gui.textline 2,200,318,40, Date$ + "  "+left$(time$,5), 5, blue , white, blue, ALIGN_MID_MID  'testo data ora
gui.autorefresh 50, 1
RETURN


lblUpdateDateTime:
if dateunix(date$)>1704063600 then 'check if current data < 31/12/23
  gui.settext TxtGuiDateTime, Date$ + "  "+left$(time$,5) 'date an time text if date$ > 1/1/24
else
  gui.settext TxtGuiDateTime, "SET DATE REQUIRED" 'date an time text require to set data
end if
RETURN

'000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000


'vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
lblBrightnessPage:
gui.init 3, white
'Sld1 = GUI.Slider(x, y, width, height, value [,orientation] [,color_cursor] [,color_back] )
Sld1 = GUI.Slider(5, 50, 310, 50, 20)
GUI.SETEVENT Sld1, change, lblSetBright
ButOKbrigh = GUI.Button(50, 180, 220, 48, "OK", 4, 10, 0, 1, blue, yellow, green, black)
GUI.SETEVENT ButOKbrigh, 1, lblOKbright
gui.autorefresh 50, 1
RETURN


lblSetBright:   'CALLED IF SLIDER VALUE CHANGES
Value = GUI.GetValue(Sld1)
TFT.BRIGHTNESS value
RETURN


lblOKbright:  'CALLED WHEN OK BUTTON PRESSED
'goto lblPage_0
GOSUB lblPage_0      'THANKS PeterN
RETURN
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


'vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
lblDateTimeSetPage:
timer1 0                        'stop timer1
pos = 0                          'initialize digit position  to edit in  DateTime$
DateTime$  = "DD/MM/YY  HH:MM"   'DD/MM/YY  HH:MM  format
gui.init 15, black 'WHITE

for R = 0 to 1                   'draw 2 rows of C buttons in 320X240 pixel resolution
  for C = 0 to 4                 'draw 5 buttons
    x = (62 * C) + 5             'x position= x spacing * row + xofset
    y = (52 * R) + 90            'y position= y spacing * row + yofset
    i = (R * 5) + C              'index of object
    ButN = gui.Button(x, y, 60, 50, str$(i), 4, 10)
    GUI.SETEVENT ButN, touch, lblBtnDigitDataTime
  next C
next R

'But = GUI.Button(x, y, width, height, "text" [,font] [,radius] [,toggle] [,group] [,color_text] [,color_pressed] [,color_released] [,color_frame] )
ButN = GUI.Button(7, 195, 90, 40, "UNDO", 4, 10, 0, 1, blue, yellow, red, black)
GUI.SETEVENT ButN, 1, lblBtnDigitDataTime
ButN = GUI.Button(107, 195, 90, 40, "EXIT", 4, 10, 0, 1, blue, yellow, red, black)
GUI.SETEVENT ButN, 1, lblBtnDigitDataTime
ButN = GUI.Button(220, 195, 90, 40, "SAVE", 4, 10, 0, 1, red, yellow, red, black)
GUI.SETEVENT ButN, 1, lblBtnDigitDataTime
'drawing text objects after button object bacause  returned ID   must be coherent with button number
'''Txt1 =        GUI.Textline(x, y, w, h, "text" [,font] [,color_text] [,color_back] [,color_frame] [,alignment] [,margin] )
TxtGuiAdvice  = gui.textline 2,1, 318,35, "Digit Date and Time", 4, red, white,, ALIGN_MID_MID  'advice label
TxtGuiDateTime = gui.textline 2,37,318,35, DateTime$, 5, white, blue, blue, ALIGN_MID_MID  'text date time "DD/MM/YY  HH:MM"
GUI.AUTOREFRESH 50, 1  'Refresh GUI at 50ms interval
RETURN


lblBtnDigitDataTime:
Id = GUI.Target             'return id  of button pressed (same button number)

SELECT CASE Id
  CASE 0 TO 9  'number 0-9 buttons tapped
    InChr$ = str$(Id)        'return inputted number from Id object
    pos = pos+1              'increase digit position
    if pos = 3 then pos = 4   'jump position slash
    if pos = 6 then pos = 7   'jump position slash
    if pos = 9 then pos = 11  'jump position space
    if pos = 10 then pos = 11 'jump position space
    if pos = 13 then pos = 14 'jump position double point
    if pos > 15 then  goto lblJump 'jump over DateTime$ manage
    DateTime$ = LEFT$(DateTime$, pos-1) + InChr$ + RIGHT$(DateTime$, 15-pos) 'replade character in DateTime$
    GUI.SETTEXT TxtGuiDateTime, DateTime$  'update text in GUI
    LblJump:
  case 10  'undo button
    pause 200
    gosub lblDateTimeSetPage
    
  case 11  'exit button
    gosub lblPage_0
    
  case 12   'save button
    DA = VAL(MID$(DateTime$, 1, 2))   'numeric day from DateTime$  = "DD/MM/YY  HH:MM"
    MO = VAL(MID$(DateTime$, 4, 2))   'numeric month from DateTime$  = "DD/MM/YY  HH:MM"
    YE = VAL(MID$(DateTime$, 7, 2))   'numeric year from DateTime$  = "DD/MM/YY  HH:MM"
    HO = VAL(MID$(DateTime$, 11, 2))  'numeric hour from DateTime$  = "DD/MM/YY  HH:MM"
    MI = VAL(MID$(DateTime$, 14, 2))  'numeric year from DateTime$  = "DD/MM/YY  HH:MM"
    IF (DA>0) AND (DA<32) AND (MO>0) AND (MO<13) AND (HO<24) AND (MI<60) THEN   'CHECK RANGE VALUES
      SETTIME YE, MO, DA, HO, MI, 00  'Set the internal timekeeper SECONDS SET TO 00
      GUI.SETTEXT TxtGuiAdvice ,"DATE & TIME SAVED"  'update text in GUI
      pause 1500 
      gosub lblPage_0   'return to main page
    ELSE
      pos = 0  'reset digit position  in  DateTime$
      DateTime$  = "DD/MM/YY  HH:MM"   'DD/MM/YY  HH:MM  format
      GUI.SETTEXT TxtGuiDateTime, DateTime$  'update text in GUI
      GUI.SETTEXT TxtGuiAdvice ,"REDIGIT!  values out range"  'update text in GUI
    END IF
    
  case else
    wlog  "Not managed ID ", Id
end select

RETURN
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


You do not have the required permissions to view the files attached to this post.
Last edited by MarioL on Fri Sep 13, 2024 5:25 pm, edited 1 time in total.
User avatar
cicciocb
Site Admin
Posts: 3050
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 627 times
Been thanked: 2178 times
Contact:

Re: GUI to Set Date Time

Post by cicciocb »

Grazie mille, penso sia molto utile anche come un valido esempio da cui ispirarsi.
Hgs2000de
Posts: 11
Joined: Tue Aug 24, 2021 10:20 am
Location: München, Deutschland
Has thanked: 8 times

Re: GUI to Set Date Time

Post by Hgs2000de »

Super Arbeit. Bitte mehr davon.
Vielen Dank
Hgs2000de
Posts: 11
Joined: Tue Aug 24, 2021 10:20 am
Location: München, Deutschland
Has thanked: 8 times

Re: GUI to Set Date Time

Post by Hgs2000de »

Hallo MarioL,
Kannst du vielleicht dein Programm so umschreiben, dass man es als Unterprogramm benutzen kann.
Ich habe vor, eine Zeitschaltuhr damit zu programmieren.
Ich dachte, dass ich auf einen Button drücke, der die Zeit im Format hh:mm anzeigt, dann dein Programm aufgerufen wird und mir
die Zeit im Format hh:mm im Button angezeigt wird.
Ich habe es selber versucht, aber ich komme mit den ganzen Gosub‘s in deinem Code nicht zurecht.

Gruß
Günter
MarioL
Posts: 27
Joined: Sun Mar 21, 2021 8:38 am
Has thanked: 321 times
Been thanked: 40 times

Re: GUI to Set Date Time

Post by MarioL »

Si può fare, dammi un pò di tempo, durante il giorno l'orto richiede il mio lavoro e di sera ci sono le sagre in paese.
Per la prossima settimana modificherò il programma in modo da impostare solo HH:MM in una string "stringHHMM$".
Quindi, se ho capito bene, devo togliere la parte di impostazione della data DD/MM/YY lasciando solo l'impostazione dell'ora e dei minuti?
In effetti è un lavoro che avrei dovuto fare per il mio progetto del "timer di irrigazione".


It can be done, give me some time, during the day the garden requires my work and in the evening there are festivals in the village.
For next week I will modify the program to only set HH:MM to a "stringHHMM$" string.
So, if I understand correctly, I have to remove the DD/MM/YY date setting part, making only the hour and minute setting work.
It's actually something I should have done for my "watering timer" project.
User avatar
PeterN
Posts: 770
Joined: Mon Feb 08, 2021 7:56 pm
Location: Krefeld, Germany
Has thanked: 353 times
Been thanked: 435 times
Contact:

Re: GUI to Set Date Time

Post by PeterN »

Hello Mario,

if you look at your programme again ... there is something I'm not sure if it might be a problem in the "old" code:

Code: [Local Link Removed for Guests]

 lblOKbright:  'CALLED WHEN OK BUTTON PRESSED
goto lblPage_0
RETURN
Shouldn‘t there be a GOSUB instead of a GOTO? Otherwise there will probably not be a RETURN to the sub.

Good luck in your garden and have a lot of fun at the festival!
Hgs2000de
Posts: 11
Joined: Tue Aug 24, 2021 10:20 am
Location: München, Deutschland
Has thanked: 8 times

Re: GUI to Set Date Time

Post by Hgs2000de »

Hallo PeterN,

genau das meine ich.
Wenn ich goto lblPage_0 ausführe komme ich ja niemals zum Return
Das geht vielleicht ein paar hundert mal gut aber irgendwann läuft mir doch der Stack über.
Und deshalb hätte ich den Aufruf zur Uhrzeiteingabe gerne als Unterprogrammaufruf, weil ich ja auch nicht immer von Page 0
komme.
Ich möchte ja auch von anderen Seiten die Uhrzeiteingabe aufrufen.

Gruß Günter
User avatar
PeterN
Posts: 770
Joined: Mon Feb 08, 2021 7:56 pm
Location: Krefeld, Germany
Has thanked: 353 times
Been thanked: 435 times
Contact:

Re: GUI to Set Date Time

Post by PeterN »

Please, let's write in English in this forum, Günther

As I see, Mario has already created a very modular code.
My comment only referred to a small problem when you press the OK button and the sub is called.

And I see he has promised to come back soon to help you :-)
I think he perhaps will need more precise information about which functions you want to isolate.
MarioL
Posts: 27
Joined: Sun Mar 21, 2021 8:38 am
Has thanked: 321 times
Been thanked: 40 times

Re: GUI to Set Date Time

Post by MarioL »

GUI TO SET ONLY TIME (H:MM)

Di seguito il codice modificato per editare tramite GUI il valore della variabile EditedTime$ nel formato HH:MM.
Dopo l'editazione del valore tempo è possibile tornare a qualsiasi "Branch Label" il cui nome è stato preventivamenete memorizzato nella variabile ReturnLabelPageName$.
Spero di essere stato di aiuto.


Below is the modified code to edit the value of the EditedTime$ variable in the HH:MM format via GUI.
After editing the time value, it is possible to return to any "Branch Label" whose name has been previously stored in the ReturnLabelPageName$ variable.
I hope it is useful.

Code: [Local Link Removed for Guests]


' *************************************************************************************
' *****                                 File Info                                 *****
' *************************************************************************************
'    Filename: /GUI_SetTime
'    Date    : 13/09/2024
'    Version : 0
'    Edit. by: MarioL.
'    Function: example to edit time (HH:M) from TFT GUI 
'    Firmware: Annex32 CAN 1.60.3 LFS
'    Hardware: ESP32-2432S028 TFT ST7789#5 (ESP32 2.8" 320X240  With Touch R)
'    Doc.Ref.: Annex32 WIFI RDS Help Version  1.60.3 
'    options : Module = ESP32-2432S028, TFT Display = ST7789#5, TFT Orientation = LANDSCAPE
'    Note    : perform TOUCH.CALIB  command in immediate command window before use
' ***************************************************************************************


EditedTime$ = "HH:MM"    'EDITED TIME FROM GUI AFTER OK BUTTON PRESSED. IF EXIT PRESSED "HH:MM" SETTED!
ReturnLabelPageName$ = "lblPage_0"   'branch label  name to return after page Time imput are used
TFT.BRIGHTNESS 20
gosub ReturnLabelPageName$
wait


'MAIN page '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
lblPage_0:
ReturnLabelPageName$ = "lblPage_0" 'memorize label name to teturn after input OK or EXIT <<<<<<<<<<<<<<<<<<<

wlog "Edited time: " + EditedTime$ + "   Brnch Lbl to return: " + ReturnLabelPageName$ 
gui.init 10, green
'Txt1 =GUI.Textline(x, y, width, height, "text" [,font] [,color_text] [,color_back] [,color_frame] [,alignment] [,margin] )
TxtGuiPage = gui.textline 50,2,220,48, "UTILITY PAGE", 4, red , white, red, ALIGN_MID_MID
'But = GUI.Button(x, y, width, height, "text" [,font] [,radius] [,toggle] [,group] [,color_text] [,color_pressed] [,color_released] [,color_frame] )
ButTimeInput = GUI.Button(0, 60, 159, 48, "Time Input", 4, 10, 0, 1, blue, yellow, green, black)    'CALL EDIT TIME page
GUI.SETEVENT ButTimeInput, 1, lblEditTimePage
TxtEditedTime = gui.textline 2,200,318,40, "Last In Time:" +EditedTime$, 5, blue , white, blue, ALIGN_MID_MID  'testo  ora
gui.autorefresh 50, 1
RETURN


'00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000





'vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
'GOSUB TO THIS BRANCH LABEL TO EDIT TIME IN HH:MM FORMAT. 
'Obtain time values in EditedTime$ variable.
'remenber to set previously  ReturnLabelPageName$ var.(where to return after pressessed OK or EXIT)
lblEditTimePage:  'DRAW EDIT TIME PAGE
pos = 0                          'initialize digit position  to edit in  EditedTime$
EditingTime$  = "HH:MM"           'Time in editing mode, HH:MM  format
gui.init 15, black 'WHITE

for R = 0 to 1                   'draw 2 rows of C buttons in 320X240 pixel resolution
  for C = 0 to 4                 'draw 5 buttons
    x = (62 * C) + 5             'x position= x spacing * row + xofset
    y = (52 * R) + 90            'y position= y spacing * row + yofset
    i = (R * 5) + C              'index of object
    ButN = gui.Button(x, y, 60, 50, str$(i), 4, 10)
    GUI.SETEVENT ButN, touch, lblManageBtnTime
  next C
next R

'But = GUI.Button(x, y, width, height, "text" [,font] [,radius] [,toggle] [,group] [,color_text] [,color_pressed] [,color_released] [,color_frame] )
ButN = GUI.Button(7, 195, 90, 40, "UNDO", 4, 10, 0, 1, blue, yellow, red, black)
GUI.SETEVENT ButN, 1, lblManageBtnTime
ButN = GUI.Button(107, 195, 90, 40, "EXIT", 4, 10, 0, 1, blue, yellow, red, black)
GUI.SETEVENT ButN, 1, lblManageBtnTime
ButN = GUI.Button(220, 195, 90, 40, "OK", 4, 10, 0, 1, red, yellow, red, black)
GUI.SETEVENT ButN, 1, lblManageBtnTime
'drawing text objects after button object bacause  returned ID   must be coherent with button number
'''Txt1 =        GUI.Textline(x, y, w, h, "text" [,font] [,color_text] [,color_back] [,color_frame] [,alignment] [,margin] )
TxtGuiAdvice  = gui.textline 2,1, 318,35, "Edit Time", 4, red, white,, ALIGN_MID_MID  'advice label
TxtEditedTime = gui.textline 2,37,318,35, EditingTime$, 5, white, blue, blue, ALIGN_MID_MID  'text date time   "HH:MM"
GUI.AUTOREFRESH 50, 1  'Refresh GUI at 50ms interval
RETURN


lblManageBtnTime:  'MANAGE TAPPED BUTTONS IN  EDIT TIME PAGE
Id = GUI.Target             'return id  of button pressed (same button number)

SELECT CASE Id
  CASE 0 TO 9  'number 0-9 buttons tapped
    InChr$ = str$(Id)        'return inputted number from Id object
    pos = pos+1              'increase digit position
    if pos = 3 then pos = 4   'jump position slash
    if pos > 5 then  goto lblJump 'jump over EditingTime$ manage
    EditingTime$ = LEFT$(EditingTime$, pos-1) + InChr$ + RIGHT$(EditingTime$, 5-pos) 'replade character in EditingTime$
    GUI.SETTEXT TxtEditedTime, EditingTime$  'update text in GUI
    LblJump:
  CASE 10  'undo button
    pause 200
    gosub lblEditTimePage
    
  CASE 11  'exit button
    gosub ReturnLabelPageName$
    'gosub lblPage_0
    
  CASE 12   'OK button
    HO = VAL(MID$(EditingTime$, 1, 2))  'check numeric hour from EditingTime$  = "HH:MM"
    MI = VAL(MID$(EditingTime$, 4, 2))  'check numeric minutes from EditingTime$  = "HH:MM"
    IF  (HO<24) AND (MI<60) THEN         'CHECK RANGE VALUES
      'SETTIME YE, MO, DA, HO, MI, 00  'Set the internal timekeeper SECONDS SET TO 00
      GUI.SETTEXT TxtGuiAdvice ,"IMPUTTED TIME:  "+ EditingTime$ 'update advice text in GUI
      EditedTime$ = EditingTime$        'update EditedTime$ as inputted
      pause 1500
      GOSUB ReturnLabelPageName$         'return to previously stored branch label
      'gosub lblPage_0   'return to main page
    ELSE
      pos = 0                           'reset digit position  in  EditingTime$
      EditingTime$  = "HH:MM"          ' HH:MM  format
      GUI.SETTEXT TxtEditedTime, EditingTime$  'update text in GUI
      GUI.SETTEXT TxtGuiAdvice ,"REDIGIT!  values out range"  'update text in GUI
    END IF
    
  case else
   ' wlog  "Not managed ID ", Id
end select

RETURN
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^



Post Reply