forked from elecfreaks/Octopus_MicroPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.py
77 lines (54 loc) · 1.89 KB
/
matrix.py
1
2
3
4
5
6
7
8
9
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from microbit import *
HT16K33_ADDRESS = 0x70
HT16K33_BLINK_CMD = 0x80
HT16K33_BLINK_DISPLAYON = 0x01
HT16K33_CMD_BRIGHTNESS = 0xE0
class MATRIX(object):
"""基本描述
8x16 点阵显示屏
"""
def __init__(self):
i2c.init()
self.__initmodule()
self.__matBuf = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
def __i2cwrite_matrix(self, addr, reg):
i2c.write(addr, bytearray([reg]))
def __initmodule(self):
self.__i2cwrite_matrix(HT16K33_ADDRESS, 0x21)
self.__i2cwrite_matrix(HT16K33_ADDRESS, HT16K33_BLINK_CMD | HT16K33_BLINK_DISPLAYON | (0 << 1))
self.__i2cwrite_matrix(HT16K33_ADDRESS, HT16K33_CMD_BRIGHTNESS | 0xF)
def __matrix_show(self):
self.__matBuf[0] = 0x00
i2c.write(HT16K33_ADDRESS, bytearray([
self.__matBuf[0], self.__matBuf[1], self.__matBuf[2], self.__matBuf[3], self.__matBuf[4], self.__matBuf[5],
self.__matBuf[6], self.__matBuf[7], self.__matBuf[8], self.__matBuf[9], self.__matBuf[10],
self.__matBuf[11],
self.__matBuf[12], self.__matBuf[13], self.__matBuf[14], self.__matBuf[15], self.__matBuf[16]]))
def set_matrix_clear(self):
"""
清空点阵显示屏
"""
for i in range(17):
self.__matBuf[i] = 0
self.__matrix_show()
def set_matrix_draw(self, x, y):
"""
点亮个像素点
Args:
x (number): X 轴 0-15
y (number): Y 轴 0-7
Returns:
NONE
"""
idx = int(y) * 2 + int(x) // 8
tmp = self.__matBuf[idx + 1]
tmp |= (1 << (x % 8))
self.__matBuf[idx + 1] = tmp
self.__matrix_show()
if __name__ == '__main__':
dis = MATRIX()
x, y = 0, 0
for y in range(8):
for x in range(16):
dis.set_matrix_draw(x, y)
dis.set_matrix_clear()