Converts actual time to vertical BCD digits. So actual time, 12:37:51 looks like:
......
...**.
.***..
*.****
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | #!/usr/bin/env python
# -*- coding: utf8 -*-
__version__ = '$Id: binclock_bcd_curses.py 780 2010-10-19 10:33:34Z mn $'
# binary clock, bcd version
# author: Michal Niklas
import sys
import time
import curses
def bin_old(n):
"""bin() that works with Python 2.4"""
if n < 1:
return '0'
result = []
while n:
if n % 2:
result.append('1')
else:
result.append('0')
n = n // 2
result.reverse()
return ''.join(result)
def bcd_digit(sn):
"""converts decimal digit char to 4 char binary 0 and 1 representation"""
n = int(sn)
try:
bin_nr = bin(n)[2:]
except NameError:
bin_nr = bin_old(n)
return ('0000' + bin_nr)[-4:]
def add_bcd(n, digits):
"""add n binary digits to digits"""
nn = "%02d" % (n)
digits.append(bcd_digit(nn[0]))
digits.append(bcd_digit(nn[1]))
def get_stars(digits):
"""changes digits to vertical picture of clock with stars and dots"""
digits_arr = []
for j in range(len(digits[0]) - 1, -1, -1):
digits2 = []
for i in range(len(digits)):
digits2.append(digits[i][j])
digits_arr.append(''.join(digits2))
digits_arr.reverse()
stars = "\n".join(digits_arr)
stars = stars.replace('0', '.')
stars = stars.replace('1', '*')
return stars
def main():
try:
try:
window = curses.initscr()
while 1:
digits = []
window.clear()
tm = time.localtime()
add_bcd(tm.tm_hour, digits)
add_bcd(tm.tm_min, digits)
add_bcd(tm.tm_sec, digits)
stars = get_stars(digits)
line_nr = 0
for line in stars.split('\n'):
window.addstr(line_nr, 0, line[:6])
line_nr += 1
window.refresh()
time.sleep(0.5)
except KeyboardInterrupt:
pass
finally:
# reset terminal
curses.nocbreak()
curses.echo()
curses.endwin()
if '--version' in sys.argv:
print(__version__)
elif __name__ == '__main__':
main()
|
Very nice.
Might I suggest an improvement ? Render the BCD to fill the entire 80x24 curses display window.
cheers James