# This file will use an external interrupt on another pin that "External Interrupt.py"
from machine import Pin
import time
from tm1637 import TM1637
#Variable declarations
global total_seconds # total_seconds is the countdown time in seconds
terminal_count=10 # Terminal count is when the count down clock ends, make 1 sec more thatn you want to show
dvd_pwr_offset =0 # This variable is the reduction from the full count to when we power on the DVD
dvd_start_offset =2 # This variable is when we issue a play command offset from the full count
dvd_stop_offset=15 # This variable is when we issue a stop command offset from the full count
dvd_pwrD_offset = 30 # This variable is when we power down the DVD offset from the full count
total_seconds=0
# GPIO definition
pps=Pin(15,Pin.IN, Pin.PULL_UP)
rst = Pin(16, Pin.IN,Pin.PULL_UP)
countdownP=Pin(17, Pin.IN, Pin.PULL_UP)
DVD_PWR=Pin(20, machine.Pin.OUT)
DVD_Start=Pin(21,machine.Pin.OUT)
DVD_STOP=Pin(22,machine.Pin.OUT)
#First trun off the DVD
DVD_PWR.value(1)
DVD_Start.value(1)
DVD_STOP.value(1)
#Set up the display
display=TM1637(dio=Pin(26),clk=Pin(27))
display.write([0, 0, 0, 0])
# there are 4 banks of switches that will determine the count down
# M10 is 10's of minutes
# M1 is 1's of minutes
# S10 is 10's of seconds
# S1 is ones of seconds
M10 = 0
M1 = 0
S10 = 1
S1 = 3
#turn this into negative time
M10=-M10
DVD_Count=0
def countdown(pps):
global total_seconds,DVD_Count,DVD_PWR
# Calculate the tens of minutes, minutes, tens of seconds, and seconds
tens_of_minutes = abs(total_seconds) // 600
minutes = (abs(total_seconds) % 600) // 60
tens_of_seconds = (abs(total_seconds) % 60) // 10
seconds = abs(total_seconds) % 10
# Format the time as a string
time_str = "{:01d}{:01d}{:01d}{:01d}".format(tens_of_minutes, minutes, tens_of_seconds, seconds)
# Add a minus sign if the time is negative
if total_seconds < 0:
time_str = "-" + time_str[1:]
if total_seconds > 0:
time_str=" "+time_str[1:]
# Display the time on the TM1637 displaydisplay.show
display.show(time_str, True)
#print(dvd_pwr_offset,DVD_Count)
if DVD_Count-dvd_pwr_offset ==0:
#print("DVD power on")
DVD_PWR.value(0)
time.sleep(.25)
DVD_PWR.value(1)
if DVD_Count - dvd_start_offset ==0:
#print ("DVD start")
DVD_Start.value(0)
time.sleep(.25)
DVD_Start.value(1)
if DVD_Count -dvd_stop_offset ==0:
#print("DVD stop")
DVD_STOP.value(0)
time.sleep(.25)
DVD_STOP.value(1)
DVD_Count +=1
# Decrement the total seconds
total_seconds +=1
#print(total_seconds,terminal_count)
if total_seconds ==terminal_count:
pps.irq(handler=None)
return
while True:
# This is the main loop. Reset has happened and we wait for the 1 PPS signal
# Check to see if the reset button was pushed
rstV=rst.value()
if rstV==0:
cdv=1
#start the DVD here
total_seconds = S1 + (S10 * 10) + (M1 * 60) + (M10 * 600)
# Calculate the minutes and seconds from total seconds
mins, secs = divmod(total_seconds, 60)
# Format the time string
time_format = f"{mins:02}:{secs:02}"
# Calculate tens of minutes
tens_of_minutes = total_seconds // 600
remaining_seconds = total_seconds % 600
# Calculate minutes
minutes = remaining_seconds // 60
remaining_seconds = remaining_seconds % 60
# Calculate tens of seconds
tens_of_seconds = remaining_seconds // 10
# Calculate seconds
seconds = remaining_seconds % 10
total_seconds=-total_seconds
# Format the time as a string
time_str = "{:01d}{:01d}{:01d}{:01d}".format(tens_of_minutes, minutes, tens_of_seconds, seconds)
#print(time_str)
# Add a minus sign if the time is negative
#if total_seconds < 0:
time_str = "-" + time_str[1:]
# Display the time on the TM1637 displaydisplay.show
display.show(time_str, True)
pps.irq(handler=None)
cdv=countdownP.value()
if cdv ==0:
DVD_Count =0
#print(DVD_Count)
pps.irq(trigger=Pin.IRQ_FALLING, handler=countdown)