11# 来源:https://pypi.org/project/micropython-ssd1306/
22# MicroPython SSD1306 OLED driver, I2C and SPI interfaces
3-
4- from micropython import const
3+ import math
54import framebuf
5+ from micropython import const
66
77# register definitions
88SET_CONTRAST = const (0x81 )
@@ -119,6 +119,44 @@ def write_data(self, buf):
119119 self .write_list [1 ] = buf
120120 self .i2c .writevto (self .addr , self .write_list )
121121
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+
122160
123161class SSD1306_SPI (SSD1306 ):
124162 def __init__ (self , width , height , spi , dc , res , cs , external_vcc = False ):
@@ -154,3 +192,40 @@ def write_data(self, buf):
154192 self .cs (0 )
155193 self .spi .write (buf )
156194 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