Skip to content

Instantly share code, notes, and snippets.

@lucaswinningham
Created August 7, 2023 12:35
Show Gist options
  • Save lucaswinningham/3b8c2375a6d22e53979144088c39d7c7 to your computer and use it in GitHub Desktop.
Save lucaswinningham/3b8c2375a6d22e53979144088c39d7c7 to your computer and use it in GitHub Desktop.
5x7 matrix display of essential ascii characters.
# https://www.ascii-code.com/
# https://fontstruct.com/fontstructions/show/847768/5x7_dot_matrix
'
1 2 3 4 5 6 7 8 9 0
a 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 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
'
# 00 01 02 03 04
# 05 06 07 08 09
# 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
module Display
class << self
def to_display(character)
set = SET_MAP[character] || SET_MAP['?']
full_display = []
ROWS.times.each do |row_number|
row_display = []
start = row_number * COLUMNS
COLUMNS.times.to_a.each do |column_number|
position = start + column_number
row_display << (set.include?(position) ? ON : OFF)
end
full_display << row_display
end
full_display
end
def to_message_display(string)
string.chars.map(&method(:to_display)).transpose
end
def print(string)
print_string = to_message_display(string).map { |row_displays|
row_displays.map(&:join).join(' ')
}.join("\n")
puts print_string
end
end
ON = '•'
OFF = '·'
## 5x5 (for use in super script / sub script)
# SET_MAP = {
# '0' => [1, 2, 3, 5, 8, 9, 10, 12, 14, 15, 16, 19, 21, 22, 23],
# '1' => [2, 6, 7, 12, 17, 21, 22, 23],
# '2' => [0, 1, 2, 3, 9, 11, 12, 13, 15, 20, 21, 22, 23, 24],
# '3' => [0, 1, 2, 3, 9, 11, 12, 13, 19, 20, 21, 22, 23],
# '4' => [0, 4, 5, 9, 10, 11, 12, 13, 14, 19, 24],
# '5' => [0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 19, 20, 21, 22, 23],
# '6' => [1, 2, 3, 5, 10, 11, 12, 13, 15, 19, 21, 22, 23],
# '7' => [0, 1, 2, 3, 4, 9, 13, 14, 19, 24],
# '8' => [1, 2, 3, 5, 9, 11, 12, 13, 15, 19, 21, 22, 23],
# '9' => [1, 2, 3, 5, 9, 11, 12, 13, 14, 19, 21, 22, 23],
# 'a' => [11, 12, 13, 16, 18, 21, 22, 23, 24],
# 'b' => [1, 6, 11, 12, 13, 16, 18, 21, 22, 23],
# 'c' => [11, 12, 13, 16, 21, 22, 23],
# 'd' => [3, 8, 11, 12, 13, 16, 18, 21, 22, 23],
# 'e' => [11, 12, 13, 16, 21, 22, 23],
# }
ROWS = 7
COLUMNS = 5
SET_MAP = {
' ' => [],
'!' => [2, 7, 12, 17, 22, 32],
'"' => [1, 3, 6, 8],
'#' => [1, 3, 6, 8, 10, 11, 12, 13, 14, 16, 18, 20, 21, 22, 23, 24, 26, 28, 31, 33],
'$' => [2, 6, 7, 8, 9, 10, 12, 16, 17, 18, 22, 24, 25, 26, 27, 28, 32],
'%' => [0, 1, 5, 6, 9, 13, 17, 21, 25, 28, 29, 33, 34],
'&' => [1, 2, 5, 8, 10, 12, 16, 20, 22, 24, 25, 28, 31, 32, 34],
"'" => [2, 7],
'(' => [3, 7, 11, 16, 21, 27, 33],
')' => [1, 7, 13, 18, 23, 27, 31],
'*' => [7, 10, 12, 14, 16, 17, 18, 20, 22, 24, 27],
'+' => [7, 12, 15, 16, 17, 18, 19, 22, 27],
',' => [21, 22, 27, 31],
'-' => [15, 16, 17, 18, 19],
'.' => [26, 27, 31, 32],
'/' => [9, 13, 17, 21, 25],
'0' => [1, 2, 3, 5, 9, 10, 13, 14, 15, 17, 19, 20, 21, 24, 25, 29, 31, 32, 33],
'1' => [2, 6, 7, 12, 17, 22, 27, 31, 32, 33],
'2' => [1, 2, 3, 5, 9, 14, 17, 18, 21, 25, 30, 31, 32, 33, 34],
'3' => [1, 2, 3, 5, 9, 14, 17, 18, 24, 25, 29, 31, 32, 33],
'4' => [3, 7, 8, 11, 13, 15, 18, 20, 21, 22, 23, 24, 28, 33],
'5' => [0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 19, 24, 25, 29, 31, 32, 33],
'6' => [2, 3, 6, 10, 15, 16, 17, 18, 20, 24, 25, 29, 31, 32, 33],
'7' => [0, 1, 2, 3, 4, 9, 13, 17, 21, 26, 31],
'8' => [1, 2, 3, 5, 9, 10, 14, 16, 17, 18, 20, 24, 25, 29, 31, 32, 33],
'9' => [1, 2, 3, 5, 9, 10, 14, 16, 17, 18, 19, 24, 28, 31, 32],
':' => [6, 7, 11, 12, 21, 22, 26, 27],
';' => [6, 7, 11, 12, 21, 22, 27, 31],
'<' => [3, 7, 11, 15, 21, 27, 33],
'=' => [10, 11, 12, 13, 14, 20, 21, 22, 23, 24],
'>' => [1, 7, 13, 19, 23, 27, 31],
'?' => [1, 2, 3, 5, 9, 14, 18, 22, 32],
'@' => [1, 2, 3, 5, 9, 14, 16, 17, 19, 20, 22, 24, 25, 27, 29, 31, 32, 33],
'A' => [2, 6, 8, 10, 14, 15, 19, 20, 21, 22, 23, 24, 30, 34],
'B' => [0, 1, 2, 3, 5, 9, 10, 14, 15, 16, 17, 18, 20, 24, 25, 29, 30, 31, 32, 33],
'C' => [1, 2, 3, 5, 9, 10, 15, 20, 25, 29, 31, 32, 33],
'D' => [0, 1, 2, 3, 5, 9, 10, 14, 15, 19, 20, 24, 25, 29, 30, 31, 32, 33],
'E' => [0, 1, 2, 3, 4, 5, 10, 15, 16, 17, 18, 20, 25, 30, 31, 32, 33, 34],
'F' => [0, 1, 2, 3, 4, 5, 10, 15, 16, 17, 18, 20, 25, 30],
'G' => [1, 2, 3, 5, 9, 10, 15, 18, 19, 20, 24, 25, 29, 31, 32, 33, 34],
'H' => [0, 4, 5, 9, 10, 14, 15, 16, 17, 18, 19, 20, 24, 25, 29, 30, 34],
'I' => [1, 2, 3, 7, 12, 17, 22, 27, 31, 32, 33],
'J' => [1, 2, 3, 4, 8, 13, 18, 23, 25, 28, 31, 32],
'K' => [0, 4, 5, 8, 10, 12, 15, 16, 20, 22, 25, 28, 30, 34],
'L' => [0, 5, 10, 15, 20, 25, 30, 31, 32, 33, 34],
'M' => [0, 4, 5, 6, 8, 9, 10, 12, 14, 15, 17, 19, 20, 24, 25, 29, 30, 34],
'N' => [0, 4, 5, 9, 10, 11, 14, 15, 17, 19, 20, 23, 24, 25, 29, 30, 34],
'O' => [1, 2, 3, 5, 9, 10, 14, 15, 19, 20, 24, 25, 29, 31, 32, 33],
'P' => [0, 1, 2, 3, 5, 9, 10, 14, 15, 16, 17, 18, 20, 25, 30],
'Q' => [1, 2, 3, 5, 9, 10, 14, 15, 19, 20, 22, 24, 25, 28, 31, 32, 34],
'R' => [0, 1, 2, 3, 5, 9, 10, 14, 15, 16, 17, 18, 20, 22, 25, 28, 30, 34],
'S' => [1, 2, 3, 5, 9, 10, 16, 17, 18, 24, 25, 29, 31, 32, 33],
'T' => [0, 1, 2, 3, 4, 7, 12, 17, 22, 27, 32],
'U' => [0, 4, 5, 9, 10, 14, 15, 19, 20, 24, 25, 29, 31, 32, 33],
'V' => [0, 4, 5, 9, 10, 14, 15, 19, 20, 24, 26, 28, 32],
'W' => [0, 4, 5, 9, 10, 14, 15, 17, 19, 20, 22, 24, 25, 27, 29, 31, 33],
'X' => [0, 4, 5, 9, 11, 13, 17, 21, 23, 25, 29, 30, 34],
'Y' => [0, 4, 5, 9, 10, 14, 16, 18, 22, 27, 32],
'Z' => [0, 1, 2, 3, 4, 9, 13, 17, 21, 25, 30, 31, 32, 33, 34],
'[' => [1, 2, 3, 6, 11, 16, 21, 26, 31, 32, 33],
'\\' => [5, 11, 17, 23, 29],
']' => [1, 2, 3, 8, 13, 18, 23, 28, 31, 32, 33],
'^' => [2, 6, 8, 10, 14],
'_' => [30, 31, 32, 33, 34],
'`' => [1, 7, 13],
'a' => [11, 12, 13, 19, 21, 22, 23, 24, 25, 29, 31, 32, 33, 34],
'b' => [0, 5, 10, 11, 12, 13, 15, 19, 20, 24, 25, 29, 30, 31, 32, 33],
'c' => [11, 12, 13, 14, 15, 20, 25, 31, 32, 33, 34],
'd' => [4, 9, 11, 12, 13, 14, 15, 19, 20, 24, 25, 29, 31, 32, 33, 34],
'e' => [11, 12, 13, 15, 19, 20, 21, 22, 23, 24, 25, 31, 32, 33],
'f' => [2, 3, 6, 9, 11, 15, 16, 17, 21, 26, 31],
'g' => [11, 12, 13, 14, 15, 19, 21, 22, 23, 24, 29, 31, 32, 33],
'h' => [0, 5, 10, 12, 13, 15, 16, 19, 20, 24, 25, 29, 30, 34],
'i' => [2, 12, 16, 17, 22, 27, 31, 32, 33],
'j' => [3, 12, 13, 18, 23, 25, 28, 31, 32],
'k' => [0, 5, 10, 13, 15, 17, 20, 21, 25, 27, 30, 33],
'l' => [1, 2, 7, 12, 17, 22, 27, 31, 32, 33],
'm' => [10, 11, 13, 15, 17, 19, 20, 22, 24, 25, 27, 29, 30, 32, 34],
'n' => [10, 12, 13, 15, 16, 19, 20, 24, 25, 29, 30, 34],
'o' => [11, 12, 13, 15, 19, 20, 24, 25, 29, 31, 32, 33],
'p' => [10, 11, 12, 13, 15, 19, 20, 21, 22, 23, 25, 30],
'q' => [11, 12, 13, 14, 15, 19, 21, 22, 23, 24, 29, 34],
'r' => [10, 12, 13, 15, 16, 19, 20, 25, 30],
's' => [11, 12, 13, 15, 21, 22, 23, 29, 31, 32, 33],
't' => [2, 7, 11, 12, 13, 17, 22, 27, 32],
'u' => [10, 14, 15, 19, 20, 24, 25, 29, 31, 32, 33],
'v' => [10, 14, 15, 19, 20, 24, 26, 28, 32],
'w' => [10, 14, 15, 19, 20, 22, 24, 25, 27, 29, 31, 33],
'x' => [10, 14, 16, 18, 22, 26, 28, 30, 34],
'y' => [10, 14, 15, 19, 21, 22, 23, 24, 29, 31, 32, 33],
'z' => [10, 11, 12, 13, 14, 18, 22, 26, 30, 31, 32, 33, 34],
'{' => [3, 7, 12, 16, 22, 27, 33],
'|' => [2, 7, 12, 17, 22, 27, 32],
'}' => [1, 7, 12, 18, 22, 27, 31],
'~' => [16, 17, 19, 20, 23],
}
end
Display.print(('0'..'9').to_a.join)
puts
Display.print(('a'..'z').to_a.join)
puts
Display.print(('A'..'Z').to_a.join)
puts
Display.print(' !"#$%&\'()*+,-./')
puts
Display.print(':;<=>?@[\\]^_`{|}~')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment