Created
August 5, 2016 20:37
-
-
Save vpetrigo/cf86d8b11e6ffaed57adfb3feb8a5cad to your computer and use it in GitHub Desktop.
LCD like numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Возможный вывод | |
# x-------------------------------------------------x | |
# | -- -- -- -- -- -- -- -- | | |
# || | | | | | | | | | | | | || | |
# || | | | | | | | | | | | | || | |
# | -- -- -- -- -- -- -- | | |
# || | | | | | | | | | | | || | |
# || | | | | | | | | | | | || | |
# | -- -- -- -- -- -- -- | | |
# x-------------------------------------------------x | |
# рисует вертикальную черту из дефисов или возвращает пустую строку | |
# пример: dash_line(4) -> " -- " | |
# dash_line(4, False) -> " " | |
def dash_line(size, enable_dash=True): | |
line = " " | |
line += ("-" if enable_dash else " ") * (size - 2) | |
line += " " | |
return line | |
# рисует границы числа | |
# пример: horizontal_line(4) -> "| |" | |
# horizontal_line(4, False) -> " |" | |
# horizontal_line(4, True, False) -> "| " | |
def horizontal_line(size, left_border=True, right_border=True): | |
line = "" | |
line += "|" if left_border else " " | |
line += " " * (size - 2) | |
line += "|" if right_border else " " | |
return line | |
# рисование чисел | |
def draw_zero(line_no, v_size, h_size): | |
result = "" | |
if line_no == 0 or line_no == v_size - 1: | |
result = dash_line(h_size) | |
elif (0 < line_no < v_size // 2 or | |
v_size // 2 < line_no < v_size - 1): | |
result = horizontal_line(h_size) | |
else: | |
result = dash_line(h_size, enable_dash=False) | |
return result | |
def draw_one(line_no, v_size, h_size): | |
result = "" | |
if (line_no == 0 or | |
line_no == v_size // 2 or | |
line_no == v_size - 1): | |
result = dash_line(h_size, enable_dash=False) | |
else: | |
result = horizontal_line(h_size, left_border=False, right_border=True) | |
return result | |
def draw_two(line_no, v_size, h_size): | |
result = "" | |
if (line_no == 0 or | |
line_no == v_size // 2 or | |
line_no == v_size - 1): | |
result = dash_line(h_size, enable_dash=True) | |
elif 0 < line_no < v_size // 2: | |
result = horizontal_line(h_size, left_border=False, right_border=True) | |
else: | |
result = horizontal_line(h_size, left_border=True, right_border=False) | |
return result | |
def draw_three(line_no, v_size, h_size): | |
result = "" | |
if (line_no == 0 or | |
line_no == v_size // 2 or | |
line_no == v_size - 1): | |
result = dash_line(h_size, enable_dash=True) | |
else: | |
result = horizontal_line(h_size, left_border=False, right_border=True) | |
return result | |
def draw_four(line_no, v_size, h_size): | |
result = "" | |
if (line_no == 0 or | |
line_no == v_size - 1): | |
result = dash_line(h_size, enable_dash=False) | |
elif 0 < line_no < v_size // 2: | |
result = horizontal_line(h_size) | |
elif line_no == v_size // 2: | |
result = dash_line(h_size, enable_dash=True) | |
else: | |
result = horizontal_line(h_size, left_border=False, right_border=True) | |
return result | |
def draw_five(line_no, v_size, h_size): | |
result = "" | |
if (line_no == 0 or | |
line_no == v_size // 2 or | |
line_no == v_size - 1): | |
result = dash_line(h_size, enable_dash=True) | |
elif 0 < line_no < v_size // 2: | |
result = horizontal_line(h_size, left_border=True, right_border=False) | |
else: | |
result = horizontal_line(h_size, left_border=False, right_border=True) | |
return result | |
def draw_six(line_no, v_size, h_size): | |
result = "" | |
if (line_no == 0 or | |
line_no == v_size // 2 or | |
line_no == v_size - 1): | |
result = dash_line(h_size, enable_dash=True) | |
elif 0 < line_no < v_size // 2: | |
result = horizontal_line(h_size, left_border=True, right_border=False) | |
else: | |
result = horizontal_line(h_size) | |
return result | |
def draw_seven(line_no, v_size, h_size): | |
result = "" | |
if line_no == 0: | |
result = dash_line(h_size, enable_dash=True) | |
elif (0 < line_no < v_size // 2 or | |
v_size // 2 < line_no < v_size - 1): | |
result = horizontal_line(h_size, left_border=False) | |
else: | |
result = dash_line(h_size, enable_dash=False) | |
return result | |
def draw_eight(line_no, v_size, h_size): | |
result = "" | |
if (line_no == 0 or | |
line_no == v_size // 2 or | |
line_no == v_size - 1): | |
result = dash_line(h_size, enable_dash=True) | |
else: | |
result = horizontal_line(h_size) | |
return result | |
def draw_nine(line_no, v_size, h_size): | |
result = "" | |
if (line_no == 0 or | |
line_no == v_size // 2 or | |
line_no == v_size - 1): | |
result = dash_line(h_size, enable_dash=True) | |
elif 0 < line_no < v_size // 2: | |
result = horizontal_line(h_size) | |
else: | |
result = horizontal_line(h_size, left_border=False) | |
return result | |
# получение необходимой строки для определенного числа @number | |
# в зависимости от текущей строки. Как получить v_size (vertical size) | |
# и h_size (horizontal size) - догадайтесь сами | |
def get_lcd_num_repr(number, line_no, v_size, h_size): | |
NUMBER_DRAW_FUNC = [draw_zero, draw_one, draw_two, draw_three, | |
draw_four, draw_five, draw_six, draw_seven, | |
draw_eight, draw_nine] | |
return NUMBER_DRAW_FUNC[number](line_no, v_size, h_size) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment