diff --git a/devtimer.py b/devtimer.py index e114548..c557ba8 100755 --- a/devtimer.py +++ b/devtimer.py @@ -48,7 +48,6 @@ CALIBRATION_OFFSET = 0.003 # Compensate for program overhead in master loop DEBOUNCE_TIME = 1.5 # In seconds MINUS = 16 # Index of minus segment table lookup -TEMP_SENTINEL = 999 # Briefly appears at start, if it stays, temp measurement isn't working # Range of compensated timing @@ -63,25 +62,6 @@ ##### -# Globals -##### - -# These get updated by the threads that read the switches and -# thermocouple. On a slow machine like the Pi Zero, we want to avoid -# unnecessary function calls, so we make these globally RW. -# So, shoot me ... - -CURRENT_PROFILE = REALTIME - - -# Operating globals - -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 - - -##### # Lookup Table For Compensating Factors ##### @@ -163,19 +143,19 @@ # display an outrageous temperature. If it stays on, it means # the temperature measurement process isn't working. - if monitor_temps.current_temp == TEMP_SENTINEL: - monitor_temps.current_temp = temp + if monitor_temps.CURRENT_TEMP == monitor_temps.SENTINEL: + monitor_temps.CURRENT_TEMP = temp - elif abs(temp - monitor_temps.current_temp) <= 10: - monitor_temps.current_temp = temp + elif abs(temp - monitor_temps.CURRENT_TEMP) <= 10: + monitor_temps.CURRENT_TEMP = temp probe.close() - Thread(name="Temp", target=show_temp, args=(temp_led, monitor_temps.current_temp)).start() + Thread(name="Temp", target=show_temp, args=(temp_led, monitor_temps.CURRENT_TEMP)).start() if DEBUG: - sys.stdout.write("Temp: %sF\n" % monitor_temps.current_temp) + sys.stdout.write("Temp: %sF\n" % monitor_temps.CURRENT_TEMP) sleep(1) @@ -191,30 +171,30 @@ def monitor_profile_sw(): - global CURRENT_PROFILE, DIM_BY, temp_led, time_led + global temp_led, time_led while True: if not wiringpi.digitalRead(PROFILE_SW_FILM): - CURRENT_PROFILE = FILM + monitor_profile_sw.CURRENT_PROFILE = FILM elif not wiringpi.digitalRead(PROFILE_SW_PAPER): - CURRENT_PROFILE = PAPER + monitor_profile_sw.CURRENT_PROFILE = PAPER else: - CURRENT_PROFILE = REALTIME + monitor_profile_sw.CURRENT_PROFILE = REALTIME # Dim displays in film mode - DIM_BY = 0 - if CURRENT_PROFILE == FILM: - DIM_BY = 2 + monitor_profile_sw.DIM_BY = 0 + if monitor_profile_sw.CURRENT_PROFILE == FILM: + monitor_profile_sw.DIM_BY = 2 - temp_led.brightness=DEFAULT_BRIGHTNESS - DIM_BY - time_led.brightness=DEFAULT_BRIGHTNESS - DIM_BY + temp_led.brightness=DEFAULT_BRIGHTNESS - monitor_profile_sw.DIM_BY + time_led.brightness=DEFAULT_BRIGHTNESS - monitor_profile_sw.DIM_BY if DEBUG: - print("Selected Profile: %s" % CURRENT_PROFILE) + print("Selected Profile: %s" % monitor_profile_sw.CURRENT_PROFILE) sleep(1) @@ -297,8 +277,6 @@ def show_temp(temp_led, temp): - global OUTOFRANGE - minus = hun = ten = one = 0 # Tells display to show nothing if temp < 0: # The leading digit is a minus sign @@ -325,15 +303,15 @@ # Update the temperature LED - temp_led.set_segments([hun, ten, one, FARENHEIGHT]) + temp_led.set_segments([hun, ten, one, show_temp.farenheight]) sleep(0.3) # If we're in a compensating profile and out of # temperature range, blink the time display - if OUTOFRANGE: + if show_temp.OUTOFRANGE: temp_led.brightness=0 - temp_led.set_segments([hun, ten, one, FARENHEIGHT]) + temp_led.set_segments([hun, ten, one, show_temp.farenheight]) ##### @@ -368,25 +346,25 @@ # For temperatures in-range, look up the compensating factor - OUTOFRANGE = False - if (CURRENT_PROFILE == REALTIME): + show_temp.OUTOFRANGE = False + if (monitor_profile_sw.CURRENT_PROFILE == REALTIME): compensation_factor = 1 # Realtime requires no compensation - elif TEMP_LOW <= monitor_temps.current_temp <= TEMP_HIGH: - compensation_factor = compensate[CURRENT_PROFILE][monitor_temps.current_temp-TEMP_LOW] + elif TEMP_LOW <= monitor_temps.CURRENT_TEMP <= TEMP_HIGH: + compensation_factor = compensate[monitor_profile_sw.CURRENT_PROFILE][monitor_temps.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 + show_temp.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, monitor_temps.current_temp, compensation_factor, str(time()-last))) + print("Elapsed Time: %s Current Temp: %s Factor: %s Inter-update Time: %s" % (elapsed_time, monitor_temps.CURRENT_TEMP, compensation_factor, str(time()-last))) ##### @@ -416,8 +394,9 @@ wiringpi.pinMode(PROFILE_SW_FILM, wiringpi.GPIO.PUD_UP) wiringpi.pinMode(PROFILE_SW_PAPER, wiringpi.GPIO.PUD_UP) - time_led = TM1637(TIME_CLK, TIME_DIO, DEFAULT_BRIGHTNESS - DIM_BY) - temp_led = TM1637(TEMP_CLK, TEMP_DIO, DEFAULT_BRIGHTNESS - DIM_BY) + monitor_profile_sw.DIM_BY=0 # How much are we currently dimming + time_led = TM1637(TIME_CLK, TIME_DIO, DEFAULT_BRIGHTNESS - monitor_profile_sw.DIM_BY) + temp_led = TM1637(TEMP_CLK, TEMP_DIO, DEFAULT_BRIGHTNESS - monitor_profile_sw.DIM_BY) # Issue audible start alert @@ -426,17 +405,20 @@ # Start monitoring the profile selection switch. # This allows the user to changes profiles during timer runs. + monitor_profile_sw.CURRENT_PROFILE = REALTIME Thread(name="MonitorProfileSW", target=monitor_profile_sw).start() # Get segment pattern for "F" - only need to do this once, not on every update - FARENHEIGHT = temp_led.digit_to_segment[0x0f] + show_temp.farenheight = temp_led.digit_to_segment[0x0f] # Initialize the time display Thread(name="InitTimeDisplay", target=show_elapsed, args=[time_led, 0]).start() # Show the initial temp sentinel - monitor_temps.current_temp = TEMP_SENTINEL - show_temp(temp_led, monitor_temps.current_temp) + + show_temp.OUTOFRANGE = False # Flag for compensating profiles when temp is out of range + monitor_temps.SENTINEL = monitor_temps.CURRENT_TEMP = 999 + show_temp(temp_led, monitor_temps.CURRENT_TEMP) # Start measuring temperature