What is it? |
L-1-AM Countdown |
A work colleague asked if I could create a visual countdown clock for his son who has SEN and struggles to understand time. Ingeniously they use a stack of seven Lego bricks to represent a week, as one day passes they remove a brick until the final day. However, they needed something more accurate for say, two hours or three days and 6 hours. The Raspberry Pi and Python was and obvious choice for the solution. I did some search and came across EzGraphics. This simple graphic module enables you to create shapes, edit colour and assign text to a customisable window.
Installation and setup is simple, basically you need:
|
|
1. Read the Event Date and TimeIn order to ensure that the time and date calculations are always up to date the user enters the date, month, and time details into a text file which is stored in the same folder as the main program. This is then read and the data extracted. The user can then remote into the Raspberry Pi and edit / update the text file.
file = open("event_time.txt","r") #opens file lines = file.read().splitlines() file.close() for line in lines: date_list.append(line) date_list =str(date_list) Then each piece of data is stripped from the list and stored in four separate variables. These are then used in the calculations of the final time remaining and the three 'bars' which display the remaining times date_of_event = int(date_list[2:4].strip(' ')) #date date_of_month = int(date_list[15:16].strip(' ')) #month hour_of_event = int(date_list[29:31].strip(' ')) #hour minute_of_event = int(date_list[42:44].strip(' ')) #minute 2. Calculate the Remaining Time in SecondsTo ensure that the countdown clock is always accurate and up to date even after the Raspberry Pi is unplugged or reset, the system time is read and stored in a variable called current_time and converted into seconds. The 'event' time is read from the text file and also converted into 'the number of seconds' until the event. This value is subtracted from the current time leaving the remaining seconds until the final event. For example, if the event is 180 seconds away and the current seconds is 120 then this means there are 60 left seconds until the event.
current_time = time.time() '''future date and time of event''' t = datetime.datetime(2017, date_of_month, date_of_event, hour_of_event, minute_of_event) future_time= time.mktime(t.timetuple()) '''time difference''' final_time = (future_time - current_time) 3. Checking for Time ErrorsOnce the remaining seconds have been calculated, the value is compare with to see if it is firstly, less than zero which means the event data has passed. Secondly it checks for the seconds being over 604800, which would mean that the event is over 7 days away and does not meet the time boundaries.
if final_time < 0: print ("TIME ERROR 1:") print("") print("The time you have entered has already passed.\nLoad the event_time.txt file and enter a valid date") elif final_time > 604800: print ("TIME ERROR 2:") print("") print("The time you have entered is too far into the furture.\nPlease select times for an event up to seven days.\nLoad the event_time.txt file and enter a valid date") In each of these outcomes a suitable error message is displayed asking the user to alter the date/time text file to meet the requirements of the countdown clock. If no errors are found then the 'seconds' value is within the boundaries and the main function the Countdown_Calculations() is triggered and the program begins. else: Countdown_Calculations() print("T I M E U P") #win = GraphicsWindow(750, 400) #canvas = win.canvas() 4. Set the Labels and BackgroundJust before the program calculates the remaining seconds and displays the various indicators it sets the background to black and adds the text labels to display the remain time values.
canvas.setColor("black") #set colour canvas.drawText(280, 40, str(days)) canvas.setColor("black") #set colour canvas.drawText(370, 40, str(hours)) canvas.setColor("black") #set colour canvas.drawText(480, 40, str(minutes)) |
5. Countdown ValuesWhen the main Countdown_Calculation() function begins it first takes the remaining seconds and converts them into days. This is simple, there are 86400 seconds in a day therefore divide the total number of seconds by 86400.
days = int(time_in_seconds/86400) print ("Days", days) To calculate the hours left basically subtract the days (in seconds) from the total time and divide by 3600 as this is the number of seconds in an hour. Note that each of these equations use the int feature to take the integer value from the calculation. For example 3.13 would mean 3 hours. hours = int((time_in_seconds - (days * 86400))/3600) print ("Hours", hours) You may be thinking why not calculate the values from the remainders, the issue with this is that say 3.13, the 13 is not 13 minutes as there are 60 minutes in an hour not 100. Soit is easier to calculate the time values from the seconds. minutes = int((time_in_seconds - ((hours * 3600) + (days * 86400)))/60) print ("Minutes", minutes) The minutes remaining is a similar equation except that both the hours and the days are subtracted from the remaining seconds, then the value is divided by 60 to give the number of minutes. 6. Displaying the TimeEach of the values, days, hours and seconds have their own rectangle which is used to display the remaining time. This is set using the values calculated in the previous step. For example, the day timer display is 602 pixels wide, this is because each day is represented by a width of 86 as there are 7 days in a week and 7 x 86 is 602.
canvas.setColor("green") #set colour canvas.drawRectangle(98, 100, 602, 50) '''add the hours''' canvas.setColor("red") #set colour canvas.drawRectangle(98, 100, (days * 86), 50) canvas.drawText(280, 40, str(days)) First the code draws a green rectangle, then the day colour, red, is set. Next the number of days is times by 86 to calculate the length of the red that needs to fill the green rectangle. In essence you are drawing two rectangles, as the time counts down the one at the bottom is revealed. The last line sets the labels. 6. Displaying the Time again.For the minutes the rectangle is set to 600 and each second is represented by 10. To calculate the remaining minutes the number of days and hours is added together and subtracted from the total seconds. This value is then divided by 60 to give the minutes. So if there is 30 minutes remaining then this is 30 x 10 which equals 300, so half of the rectangle is shaded blue to create the countdown.
canvas.drawRectangle(100, 300, 600, 50) canvas.setColor("blue") #set colour canvas.drawRectangle(100, 300, (minutes * 10), 50) canvas.drawText(480, 40, str(minutes)) Then the program subtracts 60 from the remaining seconds values and waits 60 seconds, a minute before carry out the same calculations from step one. The text file is read again, the values extracted. The current time is seconds is retrieved and the values subtracted to leave the remaining time in seconds. The values for the Days, Hours and Minutes are re-calculated and the rectangles drawn again. '''wait for 60 seconds before looping''' time_in_seconds = time_in_seconds - 60 time.sleep(60) Once the remain time hits a value of zero then the allotted time has passed and the countdown is complete. The program halts and displays a 'time's up' message. canvas.setBackground(255, 255, 255) canvas.setColor("white") #set colour canvas.drawText(340, 220, "TIME UP") Download the Code |