diff --git a/devtimer.py b/devtimer.py index f1b9d81..60c489e 100755 --- a/devtimer.py +++ b/devtimer.py @@ -79,7 +79,6 @@ DIM_BY=0 # How much are we currently dimming FARENHEIGHT=0 # Will be populated with segment pattern for "F" OUTOFRANGE = False # Flag for compensating profiles when temp is out of range -RUNNING = False # Whether or not to run the timer ##### @@ -229,7 +228,6 @@ # Callback when footswitch is pressed def footsw_pressed(): - global RUNNING # Mask interrupts during debounce window @@ -239,11 +237,11 @@ # Reflect changed state and current time - RUNNING = not RUNNING + run_timer.RUNNING = not run_timer.RUNNING footsw_pressed.lastISR = time() if DEBUG: - print("Running State: %s" % RUNNING) + print("Running State: %s" % run_timer.RUNNING) # Check to see if footswitch got pressed @@ -341,6 +339,59 @@ ##### +# Core Timer Logic +##### + +def run_timer(): + + # Start timing, using the selected profile and measured temperature + + elapsed_time = 0 + compensation_factor = 1 + + while True: + + # Beep periodically + + if run_timer.RUNNING and not elapsed_time % BEEP_INTERVAL: + Thread(name="Beep", target=beep, args=[1, 0.8]).start() + + if DEBUG: + last = time() + + # If we're not running, don't update elapsed time + + if not run_timer.RUNNING: + elapsed_time = 0 + + # Update the time display + + Thread(name="Timer", target=show_elapsed, args=(time_led, elapsed_time)).start() + + # For temperatures in-range, look up the compensating factor + + OUTOFRANGE = False + if (CURRENT_PROFILE == REALTIME): + compensation_factor = 1 # Realtime requires no compensation + + elif TEMP_LOW <= CURRENT_TEMP <= TEMP_HIGH: + compensation_factor = compensate[CURRENT_PROFILE][CURRENT_TEMP-TEMP_LOW] + + # Temperature is out of range for our correction table + # This implictly uses the last known compensation factor so we can keep running + + else: + OUTOFRANGE = True + + sleep(compensation_factor - CALIBRATION_OFFSET) + elapsed_time += 1 + elapsed_time %= 6000 + + if DEBUG: + print("Elapsed Time: %s Current Temp: %s Factor: %s Inter-update Time: %s" % (elapsed_time, CURRENT_TEMP, compensation_factor, str(time()-last))) + + +##### # Program entry point ##### @@ -397,48 +448,7 @@ Thread(name="MonitorFootSW", target=footsw_monitor).start() - # Start timing, using the selected profile and measured temperature + # Start the timer thread - elapsed_time = 0 - compensation_factor = 1 - - while True: - - # Beep periodically - - if RUNNING and not elapsed_time % BEEP_INTERVAL: - Thread(name="Beep", target=beep, args=[1, 0.8]).start() - - if DEBUG: - last = time() - - # If we're not running, don't update elapsed time - - if not RUNNING: - elapsed_time = 0 - - # Update the time display - - Thread(name="Timer", target=show_elapsed, args=(time_led, elapsed_time)).start() - - # For temperatures in-range, look up the compensating factor - - OUTOFRANGE = False - if (CURRENT_PROFILE == REALTIME): - compensation_factor = 1 # Realtime requires no compensation - - elif TEMP_LOW <= CURRENT_TEMP <= TEMP_HIGH: - compensation_factor = compensate[CURRENT_PROFILE][CURRENT_TEMP-TEMP_LOW] - - # Temperature is out of range for our correction table - # This implictly uses the last known compensation factor so we can keep running - - else: - OUTOFRANGE = True - - sleep(compensation_factor - CALIBRATION_OFFSET) - elapsed_time += 1 - elapsed_time %= 6000 - - if DEBUG: - print("Current Temp: %s Factor: %s Inter-update Time: %s" % (CURRENT_TEMP, compensation_factor, str(time()-last))) + run_timer.RUNNING = False # Whether or not to run the timer + Thread(name="RunTimer", target=run_timer).start()