-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patholed.py
163 lines (143 loc) · 4.53 KB
/
oled.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from microbit import *
cmd = [
[0xAE], # SSD1306_DISPLAYOFF
[0xA4], # SSD1306_DISPLAYALLON_RESUME
[0xD5, 0xF0], # SSD1306_SETDISPLAYCLOCKDIV
[0xA8, 0x3F], # SSD1306_SETMULTIPLEX
[0xD3, 0x00], # SSD1306_SETDISPLAYOFFSET
[0 | 0x0], # line #SSD1306_SETSTARTLINE
[0x8D, 0x14], # SSD1306_CHARGEPUMP
[0x20, 0x00], # SSD1306_MEMORYMODE
[0x21, 0, 127], # SSD1306_COLUMNADDR
[0x22, 0, 63], # SSD1306_PAGEADDR
[0xA0 | 0x1], # SSD1306_SEGREMAP
[0xc8], # SSD1306_COMSCANDEC
[0xDA, 0x12], # SSD1306_SETCOMPINS
[0x81, 0xCF], # SSD1306_SETCONTRAST
[0xD9, 0xF1], # SSD1306_SETPRECHARGE
[0xDB, 0x40], # SSD1306_SETVCOMDETECT
[0xA6], # SSD1306_NORMALDISPLAY
[0xD6, 0], # zoom set_power_off
[0xAF] # SSD1306_DISPLAYON
]
ADDR = 0x3C
screen = bytearray(1025) # send byte plus pixels
screen[0] = 0x40
zoom = 1
class OLED1306(object):
"""基本描述
OLED1306显示屏
"""
def __init__(self):
for c in cmd:
self.__command(c)
def __command(self, c):
i2c.write(ADDR, b'\x00' + bytearray(c))
def __set_pos(self, col=0, page=0):
self.__command([0xb0 | page]) # page number
# take upper and lower value of col * 2
c = col
c1, c2 = c & 0x0F, c >> 4
self.__command([0x00 | c1]) # lower start column address
self.__command([0x10 | c2]) # upper start column address
def set_zoom(self, v):
global zoom
if zoom != v:
self.__command([0xd6, v]) # zoom on/off
self.__command([0xa7 - v]) # inverted display
zoom = v
def set_pixel(self, x, y, color=1):
"""
点亮或熄灭一个像素点
Args:
x (number): X 轴 0-127
y (number): Y 轴 0-63
color (number): 1 点亮 0 熄灭
Returns:
NONE
"""
page, shift_page = divmod(y, 8)
ind = x + page * 128
b = screen[ind] | (1 << shift_page) if color else screen[ind] & ~ (1 << shift_page)
screen[ind] = b
self.__set_pos(x, page)
i2c.write(ADDR, bytearray([0x40, b]))
def set_clear(self, c=0):
"""
删除所有显示信息,清屏
"""
global screen
for i in range(1, 1025):
screen[i] = 0
self.set_refresh()
def set_power_on(self):
"""
开启显示屏,默认开启
"""
self.__command([0xAF])
def set_power_off(self):
"""
关闭显示屏,黑屏
"""
self.__command([0xAE])
def set_refresh(self):
"""
刷新显示
"""
self.__set_pos()
i2c.write(ADDR, screen)
def set_text(self, x, y, text, draw=1):
"""
显示一行文本
Args:
x (number): X 轴坐标 0-127
y (number): Y 轴坐标 0-63
text (str): 只接受字符串或字符类型参数
Returns:
NONE
"""
for i in range(0, min(len(text), 12 - x)):
for c in range(0, 5):
col = 0
for r in range(1, 6):
p = Image(text[i]).get_pixel(c, r - 1)
col = col | (1 << r) if (p != 0) else col
ind = x * 5 + y * 128 + i * 5 + c + 1
screen[ind] = col
if draw == 1:
self.set_zoom(1)
self.__set_pos(x * 5, y)
ind0 = x * 10 + y * 128 + 1
i2c.write(ADDR, b'\x40' + screen[ind0:ind + 1])
def draw_row(self, x, y, l, c=1):
"""
画一横行
Args:
x (number): X 轴起始坐标 0-127
y (number): Y 轴起始坐标 0-63
l (number): 线段长度
c (number): 1: 显示线段 2: 消除线段
"""
d = 1 if l > 0 else -1
for i in range(x, x + l, d):
self.set_pixel(i, y, c)
def draw_col(self, x, y, l, c=1):
"""
画一竖列
Args:
x (number): X 轴起始坐标 0-127
y (number): Y 轴起始坐标 0-63
l (number): 线段长度
c (number): 1: 显示线段 2: 消除线段
"""
d = 1 if l > 0 else -1
for i in range(y, y + l, d):
self.set_pixel(x, i, c)
if __name__ == '__main__':
display = OLED1306()
display.set_clear()
display.set_text(0, 0, "HELLO,2021")
display.draw_row(10,15,10,1)
display.draw_row(10,25,10,1)
display.draw_col(10,15,10,1)
display.draw_col(20,15,11,1)