Stoppuhr - Verbesserungsvorschläge

  • Hallo zusammen,

    da ich momentan von zwei CAD-Programmen die Geschwindigkeitsunterschiede messen soll, habe ich mir mit AutoIt eine kleine Stoppuhr gebastelt.

    Nun wollte ich Euch fragen, was man am Code verbessern könnte. (Besserer Programmierstiel, einfachere Funktionen....)
    Natürlich ist mir bewusst, dass jeder einen eigenen Programmierstiel hat. Doch ich finde meinen nicht wirklich gut im Moment.
    Gut, ich fange erst an mit AutoIt und ich bin kein Programmierer!

    Auch finde ich in diesem Beispiel unschön, dass ich Strings und Integer vermische.
    Aber ich weiss nicht, wie ich sonst die Millisekunden auf HH : MM : SS . hh aufteilen könnte.

    Wenn jemand konstruktive Ideen hat, dann nur her damit!

    Danke!
    Veronesi

    Spoiler anzeigen
    [autoit]

    #include <WindowsConstants.au3>
    #include <Date.au3>
    Opt("GUIOnEventMode",1) ;Enable Interrupts for GUI

    [/autoit] [autoit][/autoit] [autoit]

    $GUI = GUICreate("Stoppuhr", 325, 126, 214, 139, 0x00080000, 0x00000008) ;Create GUI with Topmost ID
    $Stoppuhr = GUICtrlCreateLabel("Stoppuhr", 8, 8, 135, 41)
    GUICtrlSetFont(-1, 26, 400, 0, "Arial")
    $Zeit = GUICtrlCreateLabel("00:00:00.00", 8, 56, 180, 41)
    GUICtrlSetFont(-1, 26, 400, 0, "Arial")
    GUICtrlSetColor(-1, 0xFF0000)
    $StartStopp = GUICtrlCreateButton("Start", 225, 8, 89, 41, $WS_GROUP)
    GUICtrlSetFont(-1, 16, 400, 0, "Arial")
    $Reset = GUICtrlCreateButton("Reset", 225, 56, 89, 41, $WS_GROUP)
    GUICtrlSetFont(-1, 16, 400, 0, "Arial")

    [/autoit] [autoit][/autoit] [autoit]

    GUICtrlSetOnEvent($StartStopp, "StartStopp") ;Create events for Buttons
    GUICtrlSetOnEvent($Reset, "Reset")
    GUISetOnEvent(-3, "End") ;Create Event for {ESC} and Close
    GUISetState(@SW_SHOW) ;Show GUI

    [/autoit] [autoit][/autoit] [autoit]

    Dim $Start = False, $Timer = 0, $tmpTimer = 0, $LastTimer = 0
    Dim $iHours, $iMins, $iSecs, $iHsecs

    [/autoit] [autoit][/autoit] [autoit]

    While Sleep(50) ;Main Loop
    If $Start Then ;Only if start is pressed....
    $tmpTimer = (TimerDiff($Timer)) + $LastTimer
    Convert() ;Convert ticks to time and splitt it into HH:MM:SS.hh
    GUICtrlSetData($Zeit, $iHours & ":" & $iMins & ":" &$iSecs & "." & $iHsecs) ;Update GUI
    EndIf
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func StartStopp() ;Interrupt for start/stop buttons
    If $Start Then ;Stop button pressed
    GUICtrlSetData($StartStopp, "Start") ;Change button text
    $LastTimer = $tmpTimer
    $Start = False
    Convert() ;Convert ticks to time and splitt it into HH:MM:SS.hh
    GUICtrlSetData($Zeit, $iHours & ":" & $iMins & ":" &$iSecs & "." & $iHsecs) ;Update GUI
    Else
    GUICtrlSetData($StartStopp, "Stopp") ;Start button pressed
    $Start = True
    $Timer = TimerInit()
    EndIf
    EndFunc ;==>StartStopp

    [/autoit] [autoit][/autoit] [autoit]

    Func Reset() ;Resets GUI
    GUICtrlSetData($Zeit, "00:00:00.00")
    $LastTimer = 0
    $Timer = TimerInit()
    EndFunc ;==>Reset

    [/autoit] [autoit][/autoit] [autoit]

    Func Convert() ;Convert ticks to time and splitt it into HH:MM:SS.hh
    _TicksToTime($tmpTimer, $iHours, $iMins, $iSecs)
    $iHsecs = StringRight($tmpTimer, 3)
    $iHsecs = Round($iHsecs / 10)
    If $iHours < 10 Then $iHours = "0" & $iHours
    If $iMins < 10 Then $iMins = "0" & $iMins
    If $iSecs < 10 Then $iSecs = "0" & $iSecs
    If $iHsecs < 10 Then $iHsecs = "0" & $iHsecs
    EndFunc ;==>Convert

    [/autoit] [autoit][/autoit] [autoit]

    Func End() ;Exit
    Exit
    EndFunc ;==>End

    [/autoit]

    Einmal editiert, zuletzt von veronesi (15. Juni 2010 um 13:37)

  • Erst mal vielen Dank für das Lob ;)

    Ich denke, man könnte schon noch einige Dinge verbessern....
    Übrigens funktionieren die Hundersttel Sekunden nicht korrekt.... Hmm muss ich noch schauen.

    Stringformat? Kenne ich nicht. Muss gleich mal die Hilfe durchlesen!
    Danke!

  • Also so ganz steige ich nicht, wie ich aus meinen Millisekunden mittels Stringformat ein Format mit HH : MM : SS.hh machen könnte.
    Mindestens den _TicksToTime() brauche ich ja doch noch, oder?

    Edit: So, nun funktionieren auch die Millisekunden

    Spoiler anzeigen
    [autoit][/autoit] [autoit][/autoit] [autoit]

    #include <WindowsConstants.au3>
    #include <Date.au3>

    [/autoit] [autoit][/autoit] [autoit]

    Dim $Start = False, $Timer = 0, $tmpTimer = 0, $LastTimer = 0
    Dim $iHours = 0, $iMins = 0, $iSecs = 0, $iHsecs = 0

    [/autoit] [autoit][/autoit] [autoit]

    Opt("GUIOnEventMode",1) ;Enable Interrupts for GUI
    $GUI = GUICreate("Stoppuhr", 325, 126, 214, 139, 0x00080000, 0x00000008) ;Create GUI with Topmost ID
    $Stoppuhr = GUICtrlCreateLabel("Stoppuhr", 8, 8, 135, 41)
    GUICtrlSetFont(-1, 26, 400, 0, "Arial")
    $Zeit = GUICtrlCreateLabel("00:00:00.00", 8, 56, 178, 41)
    GUICtrlSetFont(-1, 26, 400, 0, "Arial")
    GUICtrlSetColor(-1, 0xFF0000)
    $StartStopp = GUICtrlCreateButton("Start", 225, 8, 89, 41, $WS_GROUP)
    GUICtrlSetFont(-1, 16, 400, 0, "Arial")
    $Reset = GUICtrlCreateButton("Reset", 225, 56, 89, 41, $WS_GROUP)
    GUICtrlSetFont(-1, 16, 400, 0, "Arial")

    [/autoit] [autoit][/autoit] [autoit]

    GUICtrlSetOnEvent($StartStopp, "StartStopp") ;Create events for Buttons
    GUICtrlSetOnEvent($Reset, "Reset")
    GUISetOnEvent(-3, "End") ;Create Event for {ESC} and Close
    GUISetState(@SW_SHOW) ;Show GUI

    [/autoit] [autoit][/autoit] [autoit]

    While Sleep(50) ;Main Loop
    If $Start Then ;Only if start is pressed....
    $tmpTimer = (TimerDiff($Timer)) + $LastTimer
    Convert() ;Convert ticks to time and splitt it into HH:MM:SS.hh
    GUICtrlSetData($Zeit, $iHours & ":" & $iMins & ":" &$iSecs & "." & $iHsecs) ;Update GUI
    EndIf
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func StartStopp() ;Interrupt for start/stop buttons
    If $Start Then ;Stop button pressed
    GUICtrlSetData($StartStopp, "Start") ;Change button text
    $tmpTimer = (TimerDiff($Timer)) + $LastTimer
    $LastTimer = $tmpTimer
    $Start = False
    Convert() ;Convert ticks to time and splitt it into HH:MM:SS.hh
    GUICtrlSetData($Zeit, $iHours & ":" & $iMins & ":" &$iSecs & "." & $iHsecs) ;Update GUI
    Else
    GUICtrlSetData($StartStopp, "Stopp") ;Start button pressed
    $Start = True
    $Timer = TimerInit()
    EndIf
    EndFunc ;==>StartStopp

    [/autoit] [autoit][/autoit] [autoit]

    Func Reset() ;Resets GUI
    GUICtrlSetData($Zeit, "00:00:00.00")
    $LastTimer = 0
    $Timer = TimerInit()
    EndFunc ;==>Reset

    [/autoit] [autoit][/autoit] [autoit]

    Func Convert() ;Convert ticks to time and splitt it into HH:MM:SS.hh
    _TicksToTime($tmpTimer, $iHours, $iMins, $iSecs)
    $iHsecs = Round(($tmpTimer - (($iHours * 3600000) + ($iMins * 60000) + ($iSecs * 1000))) / 10)
    If $iHours < 10 Then $iHours = "0" & $iHours
    If $iMins < 10 Then $iMins = "0" & $iMins
    If $iSecs < 10 Then $iSecs = "0" & $iSecs
    If $iHsecs < 10 Then $iHsecs = "0" & $iHsecs
    EndFunc ;==>Convert

    [/autoit] [autoit][/autoit] [autoit]

    Func End() ;Exit
    Exit
    EndFunc ;==>End

    [/autoit]

    3 Mal editiert, zuletzt von veronesi (15. Juni 2010 um 10:43)

    • Offizieller Beitrag

    Die Convert-Funktion würde ich lieber so schreiben:

    [autoit]


    Func _NewTicksToTime($iTicks)
    Local $iHours, $iMins, $iSecs = Int($iTicks / 1000), $iMSecs = $iTicks - $iSecs * 1000
    $iHours = Int($iSecs / 3600)
    $iSecs = Mod($iSecs, 3600)
    $iMins = Int($iSecs / 60)
    $iSecs = Mod($iSecs, 60)
    Return StringFormat('%02i:%02i:%02i.%03i', $iHours, $iMins, $iSecs, $iMSecs)
    EndFunc ;==>_NewTicksToTime

    [/autoit]


    Ist aber letztendlich der eigene Programmierstil.

  • Hallo Oscar!

    Vielen Dank!
    Nun habe ich auch die Funktionen Int und Mod kennen gelernt! Super!

    Auch das StringFormat habe ich nun (mindestens im Ansatz) begriffen!
    Danke!

    Gruss
    Veronesi