Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Binary Clock
#8
(Dec-31-2016, 10:17 PM)sparkz_alot Wrote: The one thing I notice is the lack of row 5 (0), so midnight 00:00:00 won't show up.
Midnight should work just fine.  It will be all zeros.  

Anyway, latest version.  Flickering fixed; update speed increased so we don't miss some seconds (as could happen before); deciseconds added; some overall simplifications:  
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import time
import cursor
import colorama as cr

from datetime import datetime


TAB = " " * 4
UPDATE_SPEED = 1 / 60.0
DISPLAY = cr.Style.BRIGHT + cr.Fore.RED
ON = cr.Style.BRIGHT + cr.Fore.GREEN
OFF = DISPLAY
COLUMN_POSITIONS = (5, 7, 13, 15, 21, 23, 29)
START_ROW = 3


def get_cursor_locations(column):
   return [cr.Cursor.POS(column, START_ROW+2*i) for i in range(4)]


def render(digit, cursors):
   binary = bin(digit)[2:].zfill(4)
   for bit, cursor in zip(binary, cursors):
       color = ON if int(bit) else OFF
       print("{}{}{}".format(color, cursor, bit))


def startup():
   cursor.hide()
   cr.init()


def cleanup(message):
   print(DISPLAY + "\n" * 8)
   print(message)
   cursor.show()
   cr.deinit()


def run(seconds):
   print(TAB.join([DISPLAY, "HRS", " MIN", " SEC"]))
   start_time = time.clock()
   while time.clock() - start_time < seconds:
       time_digits = map(int, datetime.now().strftime("%H%M%S%f"))      
       for digit, column in zip(time_digits, COLUMN_POSITIONS):
           render(digit, get_cursor_locations(column))
       time.sleep(UPDATE_SPEED)


def main():
   startup()
   seconds_to_run = 60
   try:
       run(seconds_to_run)
       cleanup("{} second timer expired.".format(seconds_to_run))
   except KeyboardInterrupt:
       cleanup("Program stopped.")

 
if __name__ == "__main__":
   main()
Reply


Messages In This Thread
Binary Clock - by sparkz_alot - Dec-30-2016, 08:07 PM
RE: Binary Clock - by wavic - Dec-30-2016, 10:55 PM
RE: Binary Clock - by Skaperen - Dec-31-2016, 03:42 AM
RE: Binary Clock - by Mekire - Dec-31-2016, 05:23 AM
RE: Binary Clock - by sparkz_alot - Dec-31-2016, 02:14 PM
RE: Binary Clock - by Mekire - Dec-31-2016, 04:51 PM
RE: Binary Clock - by sparkz_alot - Dec-31-2016, 10:17 PM
RE: Binary Clock - by Mekire - Jan-01-2017, 12:13 AM
RE: Binary Clock - by Skaperen - Jan-01-2017, 07:55 AM
RE: Binary Clock - by sparkz_alot - Jan-01-2017, 03:23 PM
RE: Binary Clock - by wavic - Jan-01-2017, 06:39 PM
RE: Binary Clock - by sparkz_alot - Jan-02-2017, 03:02 PM
RE: Binary Clock - by wavic - Jan-02-2017, 03:29 PM
RE: Binary Clock - by sparkz_alot - Jan-02-2017, 03:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Playing around with a clock's seconds in tkinter menator01 1 2,522 Feb-12-2022, 04:32 PM
Last Post: menator01
  Simple wxpython digital clock menator01 2 4,136 Feb-20-2021, 05:47 PM
Last Post: menator01
  My Attempt at an alarm clock menator01 0 2,486 May-15-2020, 06:28 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020