Skip to content

Instantly share code, notes, and snippets.

@JarbasAI
Last active November 2, 2018 10:51
Show Gist options
  • Save JarbasAI/24c8e65bd47fa739e160d7490af35ea6 to your computer and use it in GitHub Desktop.
Save JarbasAI/24c8e65bd47fa739e160d7490af35ea6 to your computer and use it in GitHub Desktop.
mark1 faceplate pictures from .txt
def mouth_display_txt(txt_path, invert=True):
"""Converts a txt file into the appropriate encoding for the
Arduino Mark I enclosure.
Args:
txt_path (string): The absolute path of the txt
threshold (int): The value ranges from 0 to 255. The pixel will
draw on the faceplate it the value is below a
threshold
invert (bool): inverts the image being drawn.
x (int): x offset for image
y (int): y offset for image
"""
# to understand how this funtion works you need to understand how the
# Mark I arduino proprietary encoding works to display to the faceplate
width = 32
height = 8
encode = ""
# read and crop
with open(txt_path, "r") as f:
lines = f.readlines()
if len(lines) > height:
lines = lines[:height]
for i, line in enumerate(lines):
if len(line) > width:
lines[i] = line[:width]
# Each char value represents a width number starting with B=1
# then increment 1 for the next. ie C=2
width_codes = ['B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a']
height_codes = ['B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']
encode += width_codes[width - 1]
encode += height_codes[height - 1]
# Turn the image pixels into binary values 1's and 0's
# the Mark I face plate encoding uses binary values to
# binary_values returns a list of 1's and 0s'. ie ['1', '1', '0', ...]
binary_values = []
for i in range(width): # pixels
for j in range(height): #lines
pixels = lines[j]
if pixels[i] != " ":
if invert is False:
binary_values.append('1')
else:
binary_values.append('0')
else:
if invert is False:
binary_values.append('0')
else:
binary_values.append('1')
# these values are used to determine how binary values
# needs to be grouped together
number_of_bottom_pixel = 0
if height > 4:
number_of_top_pixel = 4
number_of_bottom_pixel = height - 4
else:
number_of_top_pixel = height
# this loop will group together the individual binary values
# ie. binary_list = ['1111', '001', '0101', '100']
binary_list = []
binary_code = ''
increment = 0
alternate = False
for val in binary_values:
binary_code += val
increment += 1
if increment == number_of_top_pixel and alternate is False:
# binary code is reversed for encoding
binary_list.append(binary_code[::-1])
increment = 0
binary_code = ''
alternate = True
elif increment == number_of_bottom_pixel and alternate is True:
binary_list.append(binary_code[::-1])
increment = 0
binary_code = ''
alternate = False
# Code to let the Makrk I arduino know where to place the
# pixels on the faceplate
pixel_codes = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P']
for binary_values in binary_list:
number = int(binary_values, 2)
pixel_code = pixel_codes[number]
encode += pixel_code
return encode
if __name__ == '__main__':
import sys
print mouth_display_txt(sys.argv[1])
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXX XXXXXXXXXXXXX
XXXXXXXXXXXXXX XXXXXXXXXXXXX
XXXXXXXXXXXXXX XXX XXXXXXXXXXXXX
XXXXXXXXXXXXXX XXX XXXXXXXXXXXXX
XXXXXXXXXXXXX XX XXXXXXXXXXXXX
XXXXXXXXXXXX X XXXXXXXXXXXXX
XXXXXXXXXXXXX XXX XXXXXXXXXXXXXX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment