GUI to Set Date Time

Place code snippets and demo code here
Post Reply
MarioL
Posts: 21
Joined: Sun Mar 21, 2021 8:38 am
Has thanked: 262 times
Been thanked: 35 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
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.
User avatar
cicciocb
Site Admin
Posts: 2168
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 468 times
Been thanked: 1443 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: 3
Joined: Tue Aug 24, 2021 10:20 am
Has thanked: 3 times

Re: GUI to Set Date Time

Post by Hgs2000de »

Super Arbeit. Bitte mehr davon.
Vielen Dank
Post Reply