ASCII制御文字を 7x5 Dot Matrix で表示

[Dot Matrix](https://ja.wikipedia.org/wiki/%E3%83%89%E3%83%83%E3%83%88%E3%83%9E%E3%83%88%E3%83%AA%E3%82%AF%E3%82%B9)とは，点（ドット）によるパターンのことで，電子デバイスでよく使われています．

この記事では，ドットでASCII制御文字を表示させるためのソースコードをメモします．

<img src="https://programresource.net/images/2020/03/P_20200304_222130_vHDR_Auto_HP-1024x577.jpg" width=50%>
引用: [Arduinoでドットマトリクス表示 Part1 基本表示](https://programresource.net/2020/03/05/3075.html)

# 出力画像

![DotMatrix.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/193401/6d8acfde-a586-da0c-88d5-69f453a62335.png)

# バイナリデータ

さしあたりpythonで扱える記述にしています．

ソースは[こちら](https://github.com/willh99/AVR-Scrolling-LED/blob/master/matrix.h)から拝借したものを加工しました．

```python
dot_matrix_list = [ 
 [0b0000000,0b0000000,0b0000000,0b0000000,0b0000000],  # space
 [0b0000000,0b0000000,0b1001111,0b0000000,0b0000000],  # !
 [0b0000000,0b0000111,0b0000000,0b0000111,0b0000000],  # "
 [0b0010100,0b1111111,0b0010100,0b1111111,0b0010100],  # #
 [0b0100100,0b0101010,0b1111111,0b0101010,0b0010010],  # $
 [0b0100011,0b0010011,0b0001000,0b1100100,0b1100010],  # %
 [0b0110110,0b1001001,0b1010101,0b0100010,0b1010000],  # &
 [0b0000000,0b0000101,0b0000011,0b0000000,0b0000000],  # '
 [0b0000000,0b0011100,0b0100010,0b1000001,0b0000000],  # (
 [0b0000000,0b1000001,0b0100010,0b0011100,0b0000000],  # )
 [0b0010100,0b0001000,0b0111110,0b0001000,0b0010100],  # *
 [0b0001000,0b0001000,0b0111110,0b0001000,0b0001000],  # +
 [0b0000000,0b1010000,0b0110000,0b0000000,0b0000000],  # ,
 [0b0001000,0b0001000,0b0001000,0b0001000,0b0001000],  # -
 [0b0000000,0b1100000,0b1100000,0b0000000,0b0000000],  # .
 [0b0100000,0b0010000,0b0001000,0b0000100,0b0000010],  # /
 [0b0111110,0b1010001,0b1001001,0b1000101,0b0111110],  # 0
 [0b0000000,0b1000010,0b1111111,0b1000000,0b0000000],  # 1
 [0b1000010,0b1100001,0b1010001,0b1001001,0b1000110],  # 2
 [0b0100001,0b1000001,0b1000101,0b1001011,0b0110001],  # 3
 [0b0011000,0b0010100,0b0010010,0b1111111,0b0010000],  # 4
 [0b0100111,0b1000101,0b1000101,0b1000101,0b0111001],  # 5 
 [0b0111100,0b1001010,0b1001001,0b1001001,0b0110000],  # 6
 [0b0000011,0b1110001,0b0001001,0b0000101,0b0000011],  # 7
 [0b0110110,0b1001001,0b1001001,0b1001001,0b0110110],  # 8
 [0b0000110,0b1001001,0b1001001,0b0101001,0b0011110],  # 9
 [0b0000000,0b1101100,0b1101100,0b0000000,0b0000000],  # :
 [0b0000000,0b1010110,0b0110110,0b0000000,0b0000000],  # ;
 [0b0001000,0b0010100,0b0100010,0b1000001,0b0000000],  # <
 [0b0010100,0b0010100,0b0010100,0b0010100,0b0010100],  # =
 [0b0000000,0b1000001,0b0100010,0b0010100,0b0001000],  # >
 [0b0000010,0b0000001,0b1010001,0b0001001,0b0000110],  # ?
 [0b0110010,0b1001001,0b1111001,0b1000001,0b0111110],  # @
 [0b1111110,0b0010001,0b0010001,0b0010001,0b1111110],  # A
 [0b1111111,0b1001001,0b1001001,0b1001001,0b0111110],  # B
 [0b0111110,0b1000001,0b1000001,0b1000001,0b0100010],  # C
 [0b1111111,0b1000001,0b1000001,0b1000001,0b0111110],  # D
 [0b1111111,0b1001001,0b1001001,0b1001001,0b1001001],  # E
 [0b1111111,0b0001001,0b0001001,0b0001001,0b0000001],  # F
 [0b0111110,0b1000001,0b1001001,0b1001001,0b0111010],  # G
 [0b1111111,0b0001000,0b0001000,0b0001000,0b1111111],  # H
 [0b1000001,0b1000001,0b1111111,0b1000001,0b1000001],  # I
 [0b0110000,0b1000001,0b1000001,0b0111111,0b0000001],  # J
 [0b1111111,0b0001000,0b0010100,0b0100010,0b1000001],  # K
 [0b1111111,0b1000000,0b1000000,0b1000000,0b1000000],  # L
 [0b1111111,0b0000010,0b0001100,0b0000010,0b1111111],  # M
 [0b1111111,0b0000100,0b0001000,0b0010000,0b1111111],  # N
 [0b0111110,0b1000001,0b1000001,0b1000001,0b0111110],  # O
 [0b1111111,0b0001001,0b0001001,0b0001001,0b0000110],  # P
 [0b0111110,0b1000001,0b1010001,0b0100001,0b1011110],  # Q
 [0b1111111,0b0001001,0b0001001,0b0011001,0b1100110],  # R
 [0b1000110,0b1001001,0b1001001,0b1001001,0b0110001],  # S 
 [0b0000001,0b0000001,0b1111111,0b0000001,0b0000001],  # T
 [0b0111111,0b1000000,0b1000000,0b1000000,0b0111111],  # U
 [0b0001111,0b0110000,0b1000000,0b0110000,0b0001111],  # V
 [0b0111111,0b1000000,0b0111000,0b1000000,0b0111111],  # W
 [0b1100011,0b0010100,0b0001000,0b0010100,0b1100011],  # X 
 [0b0000011,0b0000100,0b1111000,0b0000100,0b0000011],  # Y
 [0b1100001,0b1010001,0b1001001,0b1000101,0b1000011],  # Z
 [0b1111111,0b1000001,0b1000001,0b0000000,0b0000000],  # [
 [0b0000010,0b0000100,0b0001000,0b0010000,0b0100000],  # '\'
 [0b0000000,0b0000000,0b1000001,0b1000001,0b1111111],  # ]
 [0b0000100,0b0000010,0b0000001,0b0000010,0b0000100],  # ^
 [0b1000000,0b1000000,0b1000000,0b1000000,0b1000000],  # _
 [0b0000000,0b0000001,0b0000010,0b0000100,0b0000000],  # `
 [0b0100000,0b1010100,0b1010100,0b1010100,0b1111000],  # a
 [0b1111111,0b1001000,0b1000100,0b1000100,0b0111000],  # 0b
 [0b0111000,0b1000100,0b1000100,0b1000100,0b0100000],  # c
 [0b0111000,0b1000100,0b1000100,0b1001000,0b1111111],  # d
 [0b0111000,0b1010100,0b1010100,0b1010100,0b0011000],  # e
 [0b0001000,0b1111110,0b0001001,0b0000001,0b0000010],  # f
 [0b0001100,0b1010010,0b1010010,0b1010010,0b0111110],  # g
 [0b1111111,0b0001000,0b0000100,0b0000100,0b1111000],  # h
 [0b0000000,0b1000100,0b1111101,0b1000000,0b0000000],  # i
 [0b0100000,0b1000000,0b1000100,0b0111101,0b0000000],  # j
 [0b1111111,0b0010000,0b0101000,0b1000100,0b0000000],  # k
 [0b0000000,0b1000001,0b1111111,0b1000000,0b0000000],  # l
 [0b1111000,0b0000100,0b0001000,0b0000100,0b1111000],  # m
 [0b1111100,0b0001000,0b0000100,0b0000100,0b1111000],  # n
 [0b0111000,0b1000100,0b1000100,0b1000100,0b0111000],  # o
 [0b1111100,0b0010100,0b0010100,0b0010100,0b0001000],  # p
 [0b0001000,0b0010100,0b0010100,0b1111100,0b0000000],  # q
 [0b1111100,0b0001000,0b0000100,0b0000100,0b0001000],  # r
 [0b1001000,0b1010100,0b1010100,0b1010100,0b0100000],  # s
 [0b0000100,0b0111111,0b1000100,0b1000000,0b0100000],  # t
 [0b0111100,0b1000000,0b1000000,0b0100000,0b1111100],  # u
 [0b0011100,0b0100000,0b1000000,0b0100000,0b0011100],  # v
 [0b0111100,0b1000000,0b0110000,0b1000000,0b0111100],  # w
 [0b1000100,0b0101000,0b0010000,0b0101000,0b1000100],  # x
 [0b0001100,0b1010000,0b1010000,0b1010000,0b0111100],  # y
 [0b1000100,0b1100100,0b1010100,0b1001100,0b1000100],  # z          
 [0b0000000,0b0001000,0b0110110,0b1000001,0b0000000],  # [
 [0b0000000,0b0000000,0b1111111,0b0000000,0b0000000],  # |
 [0b0000000,0b1000001,0b0110110,0b0001000,0b0000000],  # ]
 [0b0001000,0b0000100,0b0000100,0b0001000,0b0000100]  # ~    
]  # characters[95]
```

# 描画コード

```python
import matplotlib.pyplot as plt
import numpy as np

def binary_to_int_list(binary):
    """2進数を01のリストに変換"""
    return [int(n_str) for n_str in list(format(binary, '07b'))]

int_matrix_list = [[binary_to_int_list(binary) 
                    for binary in dot_matrix]
                   for dot_matrix in dot_matrix_list]

# 描画
fig, axes = plt.subplots(nrows=19, ncols=5, figsize=(7,30))

for i, int_matrix in enumerate(int_matrix_list):
    row, col = i//5, i%5 
    axes[row,col].imshow(np.flipud(np.array(int_matrix).T), cmap='Greys', interpolation='nearest')
    axes[row,col].tick_params(labelbottom=False, labelleft=False, labelright=False, labeltop=False,
                             bottom=False, left=False, right=False, top=False)
plt.savefig("DotMatrix.png")
plt.show()
```



### 参照

- [willh99
/
AVR-Scrolling-LED](https://github.com/willh99/AVR-Scrolling-LED/blob/master/matrix.h)