Commit 75b9be07 authored by Conor Anderson's avatar Conor Anderson

Add base 16, and use by default

parent b2400dd8
......@@ -2,11 +2,11 @@
# binclock
# written by Brian Gajdos <brian@linuxee.sk>, 2001
# modified by Conor Anderson <conor@conr.ca>, 2017
import curses
import _curses
import math
import os
import sys
import time
......@@ -15,484 +15,491 @@ pause = 0
show_help_screen = 0
def b10(s):
result = 0
for i in range(len(s)):
try:
add = int(s[i])
except ValueError:
add = int({'a': '10',
'b': '11',
'c': '12',
'd': '13',
'e': '14',
'f': '15'}.get(s[i]))
if (i != len(s)):
add = add * 16**(len(s)-(1+i))
result = result + add
return result
def b16(n):
result = ''
remainder = n
for i in range(len(str(n)) - 1, 0, -1):
digit = remainder / 16**i
digit = {'10': 'a',
'11': 'b',
'12': 'c',
'13': 'd',
'14': 'e',
'15': 'f'}.get(str(digit), str(digit))
result = result + digit
remainder = remainder % 16**i
if i == 1:
remainder = {'10': 'a',
'11': 'b',
'12': 'c',
'13': 'd',
'14': 'e',
'15': 'f'}.get(str(remainder), str(remainder))
result = result + remainder
return result
class BincScreen:
def __init__(self):
self.stdscr = None
self.stdscr = None
def initialize(self):
self.stdscr = curses.initscr()
curses.start_color()
curses.noecho()
curses.cbreak()
self.stdscr.keypad(1)
self.stdscr.nodelay(1)
curses.halfdelay(1)
curses.init_pair(10, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(11, curses.COLOR_BLACK, curses.COLOR_BLACK)
curses.init_pair(12, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(13, curses.COLOR_BLUE, curses.COLOR_BLACK)
self.stdscr = curses.initscr()
curses.start_color()
curses.noecho()
curses.cbreak()
self.stdscr.keypad(1)
self.stdscr.nodelay(1)
curses.halfdelay(1)
def erase(self):
self.stdscr.erase()
curses.init_pair(10, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(11, curses.COLOR_BLACK, curses.COLOR_BLACK)
curses.init_pair(12, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(13, curses.COLOR_BLUE, curses.COLOR_BLACK)
def erase(self):
self.stdscr.erase()
def refresh(self):
self.stdscr.refresh()
self.stdscr.refresh()
def getch(self):
return self.stdscr.getch()
return self.stdscr.getch()
def addtext(self, x, y, col, txt):
ccc = 0
if col == 1:
ccc = curses.A_BOLD
elif col == 100:
ccc = curses.A_NORMAL | curses.color_pair(10)
elif col == 101:
ccc = curses.A_BOLD | curses.color_pair(10)
elif col == 111:
ccc = curses.A_BOLD | curses.color_pair(11)
elif col == 120:
ccc = curses.A_NORMAL | curses.color_pair(12)
elif col == 121:
ccc = curses.A_BOLD | curses.color_pair(12)
elif col == 130:
ccc = curses.A_NORMAL | curses.color_pair(13)
elif col == 131:
ccc = curses.A_BOLD | curses.color_pair(13)
self.stdscr.addstr(y, x, txt, ccc)
ccc = 0
if col == 1:
ccc = curses.A_BOLD
elif col == 100:
ccc = curses.A_NORMAL | curses.color_pair(10)
elif col == 101:
ccc = curses.A_BOLD | curses.color_pair(10)
elif col == 111:
ccc = curses.A_BOLD | curses.color_pair(11)
elif col == 120:
ccc = curses.A_NORMAL | curses.color_pair(12)
elif col == 121:
ccc = curses.A_BOLD | curses.color_pair(12)
elif col == 130:
ccc = curses.A_NORMAL | curses.color_pair(13)
elif col == 131:
ccc = curses.A_BOLD | curses.color_pair(13)
self.stdscr.addstr(y, x, txt, ccc)
def fetch_screensize(self):
my_width = 80
my_height = 25
my_height, my_width = self.stdscr.getmaxyx()
return my_width, my_height
my_width = 80
my_height = 25
my_height, my_width = self.stdscr.getmaxyx()
return my_width, my_height
def done(self):
curses.echo()
curses.endwin()
curses.echo()
curses.endwin()
class BincLED:
def __init__(self, scr, x, y, co):
self.scr = scr
self.x = x
self.y = y
self.status = 0
self.co = co
self.oldstatus = 0
self.scr = scr
self.x = x
self.y = y
self.status = 0
self.co = co
self.oldstatus = 0
def move(self, x, y):
if self.x == x and self.y == y:
return
self.x = x
self.y = y
if self.x == x and self.y == y:
return
self.x = x
self.y = y
def switch_on(self):
self.status = 1
self.status = 1
def switch_off(self):
self.status = 0
self.status = 0
def draw(self):
if self.status:
if self.co == 2:
self.scr.addtext(self.x, self.y, 121, "*")
else:
self.scr.addtext(self.x, self.y, 101, "*")
else:
if self.oldstatus:
if self.co == 2:
self.scr.addtext(self.x, self.y, 120, "*")
else:
self.scr.addtext(self.x, self.y, 100, "*")
else:
self.scr.addtext(self.x, self.y, 111, "*")
self.oldstatus = self.status
if self.status:
if self.co == 2:
self.scr.addtext(self.x, self.y, 121, "*")
else:
self.scr.addtext(self.x, self.y, 101, "*")
else:
if self.oldstatus:
if self.co == 2:
self.scr.addtext(self.x, self.y, 120, "*")
else:
self.scr.addtext(self.x, self.y, 100, "*")
else:
self.scr.addtext(self.x, self.y, 111, "*")
self.oldstatus = self.status
class BincColumn:
def __init__(self, scr, x, y, co=1):
self.scr = scr
self.x = x
self.y = y
self.co = co
self.leds = []
for i in range(4):
self.leds.append(BincLED(scr, x, y + i, co))
self.scr = scr
self.x = x
self.y = y
self.co = co
self.leds = []
for i in range(4):
self.leds.append(BincLED(scr, x, y + i, co))
def move(self, x, y):
if self.x == x and self.y == y:
return
self.x = x
self.y = y
for i in range(4):
self.leds[i].move(x, y + i)
if self.x == x and self.y == y:
return
self.x = x
self.y = y
for i in range(4):
self.leds[i].move(x, y + i)
def setup(self, number):
if number & 1:
self.leds[3].switch_on()
else:
self.leds[3].switch_off()
if number & 2:
self.leds[2].switch_on()
else:
self.leds[2].switch_off()
if number & 4:
self.leds[1].switch_on()
else:
self.leds[1].switch_off()
if number & 8:
self.leds[0].switch_on()
else:
self.leds[0].switch_off()
if number & 1:
self.leds[3].switch_on()
else:
self.leds[3].switch_off()
if number & 2:
self.leds[2].switch_on()
else:
self.leds[2].switch_off()
if number & 4:
self.leds[1].switch_on()
else:
self.leds[1].switch_off()
if number & 8:
self.leds[0].switch_on()
else:
self.leds[0].switch_off()
def draw(self):
for i in range(4):
self.leds[i].draw()
for i in range(4):
self.leds[i].draw()
class BincDigital:
def __init__(self, scr, x1, y1, x2, y2, x3, y3, x4, y4):
self.scr = scr
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.x3 = x3
self.y3 = y3
self.x4 = x4
self.y4 = y4
self.hour = 0
self.minute = 0
self.second = 0
self.hds = 0
self.scr = scr
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.x3 = x3
self.y3 = y3
self.x4 = x4
self.y4 = y4
self.hour = 0
self.minute = 0
self.second = 0
self.hds = 0
def move(self, x1, y1, x2, y2, x3, y3, x4, y4):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.x3 = x3
self.y3 = y3
self.x4 = x4
self.y4 = y4
self.x12 = ((x1 + 2) + x2) / 2
self.x23 = ((x2 + 2) + x3) / 2
self.x34 = ((x3 + 2) + x4) / 2
self.y12 = (y1 + y2) / 2
self.y23 = (y2 + y3) / 2
self.y34 = (y3 + y4) / 2
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.x3 = x3
self.y3 = y3
self.x4 = x4
self.y4 = y4
self.x12 = ((x1 + 2) + x2) / 2
self.x23 = ((x2 + 2) + x3) / 2
self.x34 = ((x3 + 2) + x4) / 2
self.y12 = (y1 + y2) / 2
self.y23 = (y2 + y3) / 2
self.y34 = (y3 + y4) / 2
def setup(self, hour, minute, second, hds):
self.hour = hour
self.minute = minute
self.second = second
self.hds = hds
def draw(self, thds = 0):
self.scr.addtext(self.x1, self.y1, 131, "%02d" % self.hour)
self.scr.addtext(self.x12, self.y12, 130, ":")
self.scr.addtext(self.x2, self.y2, 131, "%02d" % self.minute)
self.scr.addtext(self.x23, self.y23, 130, ":")
self.scr.addtext(self.x3, self.y3, 131, "%02d" % self.second)
if thds:
if self.hds < 50:
self.scr.addtext(self.x34, self.y34, 130, ".")
else:
self.scr.addtext(self.x34, self.y34, 130, " ")
self.scr.addtext(self.x4, self.y4, 130, "%02d" % self.hds)
self.hour = hour
self.minute = minute
self.second = second
self.hds = hds
def draw(self, thds=0):
self.scr.addtext(self.x1, self.y1, 131, str(self.hour).zfill(2))
self.scr.addtext(self.x12, self.y12, 130, ":")
self.scr.addtext(self.x2, self.y2, 131, str(self.minute).zfill(2))
self.scr.addtext(self.x23, self.y23, 130, ":")
self.scr.addtext(self.x3, self.y3, 131, str(self.second).zfill(2))
if thds:
if ((type(self.hds) == int and self.hds < 50) or
(type(self.hds) == str and b10(self.hds) < 50)):
self.scr.addtext(self.x34, self.y34, 130, ".")
else:
self.scr.addtext(self.x34, self.y34, 130, " ")
self.scr.addtext(self.x4, self.y4, 130, str(self.hds).zfill(2))
class BincTitle:
def __init__(self, scr, x, y, txt=""):
self.scr = scr
self.x = x
self.y = y
self.txt = txt
self.scr = scr
self.x = x
self.y = y
self.txt = txt
def move(self, x, y):
if self.x == x and self.y == y:
return
self.x = x
self.y = y
if self.x == x and self.y == y:
return
self.x = x
self.y = y
def draw(self):
self.scr.addtext(self.x, self.y, 0, self.txt)
self.scr.addtext(self.x, self.y, 0, self.txt)
class BincHelpAdv:
def __init__(self, scr, width, height):
self.scr = scr
self.width = width
self.height = height
self.txt_x = 0
self.txt_y = 0
self.start_time = 0
self.help_text = "Press '?' to get help"
self.window_too_small = 1
self.scr = scr
self.width = width
self.height = height
self.txt_x = 0
self.txt_y = 0
self.start_time = 0
self.help_text = "Press '?' to get help"
self.window_too_small = 1
def resize(self, width, height):
self.width = width
self.height = height
self.window_too_small = 0
if (len(self.help_text) + 1 > width) or (height < 2):
self.window_too_small = 1
self.txt_x = (width - len(self.help_text)) / 2
self.txt_y = height - 1
self.width = width
self.height = height
self.window_too_small = 0
if (len(self.help_text) + 1 > width) or (height < 2):
self.window_too_small = 1
def start(self):
self.start_time = time.time()
self.txt_x = (width - len(self.help_text)) / 2
self.txt_y = height - 1
def start(self):
self.start_time = time.time()
def draw(self):
if self.window_too_small:
return
t = time.time() - self.start_time
if t < 0.15:
a = 0
elif t < 0.25:
a = 1
elif t < 4.0:
a = 0
else:
a = 111
self.scr.addtext(self.txt_x, self.txt_y, a, self.help_text)
if self.window_too_small:
return
t = time.time() - self.start_time
if t < 0.15:
a = 0
elif t < 0.25:
a = 1
elif t < 4.0:
a = 0
else:
a = 111
self.scr.addtext(self.txt_x, self.txt_y, a, self.help_text)
class BincWatch:
def __init__(self, scr):
self.scr = scr
self.exc_window_is_too_small = "window is too small"
self.window_too_small = 0
self.toggle_digital = 0
self.toggle_help = 0
self.toggle_hundreds = 1
self.toggle_titles = 1
self.title_hrs = BincTitle(scr, 0, 0, "hrs")
self.title_min = BincTitle(scr, 0, 0, "min")
self.title_sec = BincTitle(scr, 0, 0, "sec")
self.title_hds = BincTitle(scr, 0, 0, "hds")
self.bincols = [
BincColumn(scr, 0, 0),
BincColumn(scr, 0, 0),
BincColumn(scr, 0, 0),
BincColumn(scr, 0, 0),
BincColumn(scr, 0, 0),
BincColumn(scr, 0, 0),
BincColumn(scr, 0, 0, 2),
BincColumn(scr, 0, 0, 2)
]
self.bindigi = BincDigital(scr, 0, 0, 0, 0, 0, 0, 0, 0)
self.binhelpadv = BincHelpAdv(scr, 0, 0)
def setup_time(self, t):
cas2 = time.localtime(t)
year, month, day, hour, minute, second, weekday = cas2[:7]
hds = int(math.fmod(math.floor(t * 100), 100))
self.bincols[0].setup(hour / 10)
self.bincols[1].setup(hour % 10)
self.bincols[2].setup(minute / 10)
self.bincols[3].setup(minute % 10)
self.bincols[4].setup(second / 10)
self.bincols[5].setup(second % 10)
self.bincols[6].setup(hds / 10)
self.bincols[7].setup(hds % 10)
self.bindigi.setup(hour, minute, second, hds)
if self.toggle_help:
if time.time() - self.binhelpadv.start_time > 5:
self.toggle_help = 0
self.scr = scr
self.exc_window_is_too_small = "window is too small"
self.window_too_small = 0
self.toggle_digital = 1
self.toggle_help = 0
self.toggle_hundreds = 1
self.toggle_titles = 1
self.title_hrs = BincTitle(scr, 0, 0, "hrs")
self.title_min = BincTitle(scr, 0, 0, "min")
self.title_sec = BincTitle(scr, 0, 0, "sec")
self.title_hds = BincTitle(scr, 0, 0, "hds")
self.bincols = [
BincColumn(scr, 0, 0),
BincColumn(scr, 0, 0),
BincColumn(scr, 0, 0),
BincColumn(scr, 0, 0),
BincColumn(scr, 0, 0),
BincColumn(scr, 0, 0),
BincColumn(scr, 0, 0, 2),
BincColumn(scr, 0, 0, 2)
]
self.bindigi = BincDigital(scr, 0, 0, 0, 0, 0, 0, 0, 0)
self.binhelpadv = BincHelpAdv(scr, 0, 0)
def setup_time(self, t, b):
cas2 = time.localtime(t)
year, month, day, hour, minute, second, weekday = cas2[:7]
hds = int(math.fmod(math.floor(t * 100), 100))
self.bincols[0].setup(hour / b)
self.bincols[1].setup(hour % b)
self.bincols[2].setup(minute / b)
self.bincols[3].setup(minute % b)
self.bincols[4].setup(second / b)
self.bincols[5].setup(second % b)
self.bincols[6].setup(hds / b)
self.bincols[7].setup(hds % b)
if b == 10:
self.bindigi.setup(hour, minute, second, hds)
else:
self.bindigi.setup(b16(hour), b16(minute), b16(second), b16(hds))
if self.toggle_help and time.time() - self.binhelpadv.start_time > 5:
self.toggle_help = 0
def recalc_coords(self, width, height):
inner_width = 13
if self.toggle_hundreds:
inner_width = inner_width + 5
inner_height = 4
if self.toggle_titles:
inner_height = inner_height + 2
if self.toggle_digital:
inner_height = inner_height + 2
self.window_too_small = 0
if inner_width > width or inner_height > height:
self.window_too_small = 1
raise self.exc_window_is_too_small
# if self.toggle_help:
# if (len(self.help_text) + 1 > width) or (inner_height + 2 > height):
# self.window_too_small = 1
# raise "window is too small"
off_x = (width - inner_width) / 2
off_y = (height - inner_height) / 2
pos_hrs_x = off_x
pos_homi_x = off_x + 3
pos_min_x = off_x + 5
pos_mise_x = off_x + 8
pos_sec_x = off_x + 10
pos_seho_x = off_x + 13
pos_hds_x = off_x + 15
pos_hrs_y = pos_min_y = pos_sec_y = pos_hds_y = off_y
if self.toggle_titles:
pos_hrs_y = pos_min_y = pos_sec_y = pos_hds_y = off_y + 2
pos_tit_y = off_y
self.title_hrs.move(pos_hrs_x, pos_tit_y)
self.title_min.move(pos_min_x, pos_tit_y)
self.title_sec.move(pos_sec_x, pos_tit_y)
self.title_hds.move(pos_hds_x, pos_tit_y)
if self.toggle_digital:
pos_dig_y = pos_hrs_y + 5
self.bindigi.move(pos_hrs_x, pos_dig_y,
pos_min_x, pos_dig_y, pos_sec_x, pos_dig_y,
pos_hds_x, pos_dig_y)
# pos_help_x = (width - len(self.help_text)) / 2
# pos_help_y = height - 1
self.binhelpadv.resize(width, height)
self.bincols[0].move(pos_hrs_x, pos_hrs_y)
self.bincols[1].move(pos_hrs_x + 1, pos_hrs_y)
self.bincols[2].move(pos_min_x, pos_min_y)
self.bincols[3].move(pos_min_x + 1, pos_min_y)
self.bincols[4].move(pos_sec_x, pos_sec_y)
self.bincols[5].move(pos_sec_x + 1, pos_sec_y)
self.bincols[6].move(pos_hds_x, pos_sec_y)
self.bincols[7].move(pos_hds_x + 1, pos_sec_y)
inner_width = 13
def recalc_coords2(self, width, height):
self.window_too_small = 0
try:
self.recalc_coords(width, height)
except self.exc_window_is_too_small:
self.window_too_small = 1
if self.toggle_hundreds: