1
1
# 来源:https://pypi.org/project/micropython-ssd1306/
2
2
# MicroPython SSD1306 OLED driver, I2C and SPI interfaces
3
-
4
- from micropython import const
3
+ import math
5
4
import framebuf
5
+ from micropython import const
6
6
7
7
# register definitions
8
8
SET_CONTRAST = const (0x81 )
@@ -119,6 +119,44 @@ def write_data(self, buf):
119
119
self .write_list [1 ] = buf
120
120
self .i2c .writevto (self .addr , self .write_list )
121
121
122
+ def circle (self , center , radius , c , section = 100 ):
123
+ """
124
+ 画圆
125
+
126
+ Args:
127
+ c: 颜色
128
+ center: 中心(x, y)
129
+ radius: 半径
130
+ section: 分段
131
+ """
132
+ arr = []
133
+ for m in range (section + 1 ):
134
+ x = round (radius * math .cos ((2 * math .pi / section ) * m - math .pi ) + center [0 ])
135
+ y = round (radius * math .sin ((2 * math .pi / section ) * m - math .pi ) + center [1 ])
136
+ arr .append ([x , y ])
137
+ for i in range (len (arr ) - 1 ):
138
+ self .line (* arr [i ], * arr [i + 1 ], c )
139
+
140
+ def fill_circle (self , center , radius , c ):
141
+ """
142
+ 画填充圆
143
+
144
+ Args:
145
+ c: 颜色
146
+ center: 中心(x, y)
147
+ radius: 半径
148
+ """
149
+ rsq = radius * radius
150
+ for x in range (radius ):
151
+ y = int (math .sqrt (rsq - x * x )) # 计算 y 坐标
152
+ y0 = center [1 ] - y
153
+ end_y = y0 + y * 2
154
+ y0 = max (0 , min (y0 , self .height )) # 将 y0 限制在画布的范围内
155
+ length = abs (end_y - y0 ) + 1
156
+ self .vline (center [0 ] + x , y0 , length , c ) # 绘制左右两侧的垂直线
157
+ self .vline (center [0 ] - x , y0 , length , c )
158
+
159
+
122
160
123
161
class SSD1306_SPI (SSD1306 ):
124
162
def __init__ (self , width , height , spi , dc , res , cs , external_vcc = False ):
@@ -154,3 +192,40 @@ def write_data(self, buf):
154
192
self .cs (0 )
155
193
self .spi .write (buf )
156
194
self .cs (1 )
195
+
196
+ def circle (self , center , radius , c , section = 100 ):
197
+ """
198
+ 画圆
199
+
200
+ Args:
201
+ c: 颜色
202
+ center: 中心(x, y)
203
+ radius: 半径
204
+ section: 分段
205
+ """
206
+ arr = []
207
+ for m in range (section + 1 ):
208
+ x = round (radius * math .cos ((2 * math .pi / section ) * m - math .pi ) + center [0 ])
209
+ y = round (radius * math .sin ((2 * math .pi / section ) * m - math .pi ) + center [1 ])
210
+ arr .append ([x , y ])
211
+ for i in range (len (arr ) - 1 ):
212
+ self .line (* arr [i ], * arr [i + 1 ], c )
213
+
214
+ def fill_circle (self , center , radius , c ):
215
+ """
216
+ 画填充圆
217
+
218
+ Args:
219
+ c: 颜色
220
+ center: 中心(x, y)
221
+ radius: 半径
222
+ """
223
+ rsq = radius * radius
224
+ for x in range (radius ):
225
+ y = int (math .sqrt (rsq - x * x )) # 计算 y 坐标
226
+ y0 = center [1 ] - y
227
+ end_y = y0 + y * 2
228
+ y0 = max (0 , min (y0 , self .height )) # 将 y0 限制在画布的范围内
229
+ length = abs (end_y - y0 ) + 1
230
+ self .vline (center [0 ] + x , y0 , length , c ) # 绘制左右两侧的垂直线
231
+ self .vline (center [0 ] - x , y0 , length , c )
0 commit comments