|
@@ -1,269 +1,269 @@
|
1
|
|
-# https://www.waveshare.com/wiki/Pico-LCD-1.3
|
2
|
|
-# https://thepihut.com/blogs/raspberry-pi-tutorials/coding-graphics-with-micropython-on-raspberry-pi-pico-displays
|
3
|
|
-# https://api.arcade.academy/en/latest/_modules/arcade/draw_commands.html#draw_arc_filled
|
4
|
|
-
|
5
|
|
-from machine import Pin, SPI, PWM
|
6
|
|
-from array import array
|
7
|
|
-import framebuf
|
8
|
|
-import time
|
9
|
|
-import os
|
10
|
|
-import math
|
11
|
|
-import gc
|
12
|
|
-
|
13
|
|
-class KeyCheck:
|
14
|
|
- def __init__(self, new, old):
|
15
|
|
- self.new = new
|
16
|
|
- self.old = old
|
17
|
|
-
|
18
|
|
- def once(self, k):
|
19
|
|
- return self.new[k] and not self.old[k]
|
20
|
|
-
|
21
|
|
-class LCD(framebuf.FrameBuffer):
|
22
|
|
- def __init__(self):
|
23
|
|
- self.width = 240
|
24
|
|
- self.height = 240
|
25
|
|
-
|
26
|
|
- self.cs = Pin(9, Pin.OUT)
|
27
|
|
- self.rst = Pin(12, Pin.OUT)
|
28
|
|
-
|
29
|
|
- self.cs(1)
|
30
|
|
-
|
31
|
|
- #self.spi = SPI(1)
|
32
|
|
- #self.spi = SPI(1, 1_000_000)
|
33
|
|
- self.spi = SPI(1, 100_000_000, polarity=0, phase=0, sck=Pin(10), mosi=Pin(11), miso=None)
|
34
|
|
-
|
35
|
|
- self.dc = Pin(8, Pin.OUT)
|
36
|
|
- self.dc(1)
|
37
|
|
-
|
38
|
|
- gc.collect()
|
39
|
|
- self.buffer = bytearray(self.height * self.width * 2)
|
40
|
|
-
|
41
|
|
- super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
|
42
|
|
- self.init_display()
|
43
|
|
-
|
44
|
|
- self.red = self.color(0xFF, 0x00, 0x00)
|
45
|
|
- self.green = self.color(0x00, 0xFF, 0x00)
|
46
|
|
- self.blue = self.color(0x00, 0x00, 0xFF)
|
47
|
|
- self.yellow = self.color(0xFF, 0xFF, 0x00)
|
48
|
|
- self.white = self.color(0xFF, 0xFF, 0xFF)
|
49
|
|
- self.black = self.color(0x00, 0x00, 0x00)
|
50
|
|
-
|
51
|
|
- self.fill(self.black)
|
52
|
|
- self.show()
|
53
|
|
-
|
54
|
|
- self.pwm = PWM(Pin(13))
|
55
|
|
- self.pwm.freq(1000)
|
56
|
|
- self.brightness(0.0)
|
57
|
|
-
|
58
|
|
- self.keyA = Pin(15, Pin.IN, Pin.PULL_UP)
|
59
|
|
- self.keyB = Pin(17, Pin.IN, Pin.PULL_UP)
|
60
|
|
- self.keyX = Pin(19, Pin.IN, Pin.PULL_UP)
|
61
|
|
- self.keyY = Pin(21, Pin.IN, Pin.PULL_UP)
|
62
|
|
- self.up = Pin( 2, Pin.IN, Pin.PULL_UP)
|
63
|
|
- self.down = Pin(18, Pin.IN, Pin.PULL_UP)
|
64
|
|
- self.left = Pin(16, Pin.IN, Pin.PULL_UP)
|
65
|
|
- self.right = Pin(20, Pin.IN, Pin.PULL_UP)
|
66
|
|
- self.ctrl = Pin( 3, Pin.IN, Pin.PULL_UP)
|
67
|
|
-
|
68
|
|
- self.keys_old = {
|
69
|
|
- "a": False,
|
70
|
|
- "b": False,
|
71
|
|
- "x": False,
|
72
|
|
- "y": False,
|
73
|
|
- "up": False,
|
74
|
|
- "down": False,
|
75
|
|
- "left": False,
|
76
|
|
- "right": False,
|
77
|
|
- "enter": False,
|
78
|
|
- }
|
79
|
|
-
|
80
|
|
- def buttons(self):
|
81
|
|
- keys = {
|
82
|
|
- "a": self.keyA.value() == 0,
|
83
|
|
- "b": self.keyB.value() == 0,
|
84
|
|
- "x": self.keyX.value() == 0,
|
85
|
|
- "y": self.keyY.value() == 0,
|
86
|
|
- "up": self.up.value() == 0,
|
87
|
|
- "down": self.down.value() == 0,
|
88
|
|
- "left": self.left.value() == 0,
|
89
|
|
- "right": self.right.value() == 0,
|
90
|
|
- "enter": self.ctrl.value() == 0,
|
91
|
|
- }
|
92
|
|
- kc = KeyCheck(keys, self.keys_old)
|
93
|
|
- self.keys_old = keys.copy()
|
94
|
|
- return kc
|
95
|
|
-
|
96
|
|
- # Convert RGB888 to RGB565
|
97
|
|
- def color(self, R, G, B):
|
98
|
|
- return (((G & 0b00011100) << 3) + ((B & 0b11111000) >> 3) << 8) + (R & 0b11111000) + ((G & 0b11100000) >> 5)
|
99
|
|
-
|
100
|
|
- def brightness(self, v):
|
101
|
|
- self.pwm.duty_u16(int(v * 65535))
|
102
|
|
-
|
103
|
|
- def write_cmd(self, cmd):
|
104
|
|
- self.cs(1)
|
105
|
|
- self.dc(0)
|
106
|
|
- self.cs(0)
|
107
|
|
- self.spi.write(bytearray([cmd]))
|
108
|
|
- self.cs(1)
|
109
|
|
-
|
110
|
|
- def write_data(self, buf):
|
111
|
|
- self.cs(1)
|
112
|
|
- self.dc(1)
|
113
|
|
- self.cs(0)
|
114
|
|
- self.spi.write(bytearray([buf]))
|
115
|
|
- self.cs(1)
|
116
|
|
-
|
117
|
|
- def init_display(self):
|
118
|
|
- self.rst(1)
|
119
|
|
- self.rst(0)
|
120
|
|
- self.rst(1)
|
121
|
|
- self.write_cmd(0x36)
|
122
|
|
- self.write_data(0x70)
|
123
|
|
- self.write_cmd(0x3A)
|
124
|
|
- self.write_data(0x05)
|
125
|
|
- self.write_cmd(0xB2)
|
126
|
|
- self.write_data(0x0C)
|
127
|
|
- self.write_data(0x0C)
|
128
|
|
- self.write_data(0x00)
|
129
|
|
- self.write_data(0x33)
|
130
|
|
- self.write_data(0x33)
|
131
|
|
- self.write_cmd(0xB7)
|
132
|
|
- self.write_data(0x35)
|
133
|
|
- self.write_cmd(0xBB)
|
134
|
|
- self.write_data(0x19)
|
135
|
|
- self.write_cmd(0xC0)
|
136
|
|
- self.write_data(0x2C)
|
137
|
|
- self.write_cmd(0xC2)
|
138
|
|
- self.write_data(0x01)
|
139
|
|
- self.write_cmd(0xC3)
|
140
|
|
- self.write_data(0x12)
|
141
|
|
- self.write_cmd(0xC4)
|
142
|
|
- self.write_data(0x20)
|
143
|
|
- self.write_cmd(0xC6)
|
144
|
|
- self.write_data(0x0F)
|
145
|
|
- self.write_cmd(0xD0)
|
146
|
|
- self.write_data(0xA4)
|
147
|
|
- self.write_data(0xA1)
|
148
|
|
- self.write_cmd(0xE0)
|
149
|
|
- self.write_data(0xD0)
|
150
|
|
- self.write_data(0x04)
|
151
|
|
- self.write_data(0x0D)
|
152
|
|
- self.write_data(0x11)
|
153
|
|
- self.write_data(0x13)
|
154
|
|
- self.write_data(0x2B)
|
155
|
|
- self.write_data(0x3F)
|
156
|
|
- self.write_data(0x54)
|
157
|
|
- self.write_data(0x4C)
|
158
|
|
- self.write_data(0x18)
|
159
|
|
- self.write_data(0x0D)
|
160
|
|
- self.write_data(0x0B)
|
161
|
|
- self.write_data(0x1F)
|
162
|
|
- self.write_data(0x23)
|
163
|
|
- self.write_cmd(0xE1)
|
164
|
|
- self.write_data(0xD0)
|
165
|
|
- self.write_data(0x04)
|
166
|
|
- self.write_data(0x0C)
|
167
|
|
- self.write_data(0x11)
|
168
|
|
- self.write_data(0x13)
|
169
|
|
- self.write_data(0x2C)
|
170
|
|
- self.write_data(0x3F)
|
171
|
|
- self.write_data(0x44)
|
172
|
|
- self.write_data(0x51)
|
173
|
|
- self.write_data(0x2F)
|
174
|
|
- self.write_data(0x1F)
|
175
|
|
- self.write_data(0x1F)
|
176
|
|
- self.write_data(0x20)
|
177
|
|
- self.write_data(0x23)
|
178
|
|
- self.write_cmd(0x21)
|
179
|
|
- self.write_cmd(0x11)
|
180
|
|
- self.write_cmd(0x29)
|
181
|
|
-
|
182
|
|
- def show(self):
|
183
|
|
- self.write_cmd(0x2A)
|
184
|
|
- self.write_data(0x00)
|
185
|
|
- self.write_data(0x00)
|
186
|
|
- self.write_data(0x00)
|
187
|
|
- self.write_data(0xef)
|
188
|
|
- self.write_cmd(0x2B)
|
189
|
|
- self.write_data(0x00)
|
190
|
|
- self.write_data(0x00)
|
191
|
|
- self.write_data(0x00)
|
192
|
|
- self.write_data(0xEF)
|
193
|
|
- self.write_cmd(0x2C)
|
194
|
|
- self.cs(1)
|
195
|
|
- self.dc(1)
|
196
|
|
- self.cs(0)
|
197
|
|
- self.spi.write(self.buffer)
|
198
|
|
- self.cs(1)
|
199
|
|
-
|
200
|
|
- def circle(self, x, y, r, c):
|
201
|
|
- self.hline(x-r,y,r*2,c)
|
202
|
|
- for i in range(1,r):
|
203
|
|
- a = int(math.sqrt(r*r-i*i)) # Pythagoras!
|
204
|
|
- self.hline(x-a,y+i,a*2,c) # Lower half
|
205
|
|
- self.hline(x-a,y-i,a*2,c) # Upper half
|
206
|
|
-
|
207
|
|
- def ring(self, x, y, r, c):
|
208
|
|
- self.pixel(x-r,y,c)
|
209
|
|
- self.pixel(x+r,y,c)
|
210
|
|
- self.pixel(x,y-r,c)
|
211
|
|
- self.pixel(x,y+r,c)
|
212
|
|
- for i in range(1, r):
|
213
|
|
- a = int(math.sqrt(r*r-i*i))
|
214
|
|
- self.pixel(x-a,y-i,c)
|
215
|
|
- self.pixel(x+a,y-i,c)
|
216
|
|
- self.pixel(x-a,y+i,c)
|
217
|
|
- self.pixel(x+a,y+i,c)
|
218
|
|
- self.pixel(x-i,y-a,c)
|
219
|
|
- self.pixel(x+i,y-a,c)
|
220
|
|
- self.pixel(x-i,y+a,c)
|
221
|
|
- self.pixel(x+i,y+a,c)
|
222
|
|
-
|
223
|
|
- def arc(self, x_off, y_off, w, h, c,
|
224
|
|
- start_angle, end_angle,
|
225
|
|
- filled = True,
|
226
|
|
- num_segments = 128):
|
227
|
|
- start_segment = int(start_angle / 360 * num_segments)
|
228
|
|
- end_segment = int(end_angle / 360 * num_segments)
|
229
|
|
-
|
230
|
|
- gc.collect()
|
231
|
|
- point_list = array('h')
|
232
|
|
- point_list.append(0)
|
233
|
|
- point_list.append(0)
|
234
|
|
-
|
235
|
|
- for segment in range(start_segment, end_segment + 1):
|
236
|
|
- theta = 2.0 * 3.1415926 * segment / num_segments
|
237
|
|
- x = w * math.cos(theta) / 2
|
238
|
|
- y = h * math.sin(theta) / 2
|
239
|
|
-
|
240
|
|
- i = (segment - start_segment + 1) * 2
|
241
|
|
- point_list.append(int(x))
|
242
|
|
- point_list.append(int(y))
|
243
|
|
-
|
244
|
|
- self.poly(int(x_off), int(y_off), point_list, c, True)
|
245
|
|
-
|
246
|
|
- def pie(self, x0, y0, w, c_border, c_circle, v):
|
247
|
|
- if v > 0.0:
|
248
|
|
- self.arc(int(x0), int(y0), int(w), int(w), c_circle, -90, int(v * 360) - 90)
|
249
|
|
- self.ring(int(x0), int(y0), int(w / 2), c_border)
|
250
|
|
-
|
251
|
|
- def textC(self, s, x, y, c):
|
252
|
|
- self.text(s, x - int(len(s) * 8 / 2), y - 5, c)
|
253
|
|
-
|
254
|
|
- def textLine(self, s, c, off = 0):
|
255
|
|
- charsPerLine = int(self.width / 8)
|
256
|
|
- lines = list(s[0+i:charsPerLine+i] for i in range(0, len(s), charsPerLine))
|
257
|
|
- n = 0
|
258
|
|
- for i, l in enumerate(lines):
|
259
|
|
- self.text(l, 0, (i + off) * 10, c)
|
260
|
|
- n += 1
|
261
|
|
- if i >= (self.height / 10):
|
262
|
|
- break
|
263
|
|
- return n
|
264
|
|
-
|
265
|
|
- def textBlock(self, s, c):
|
266
|
|
- lines = s.split("\n")
|
267
|
|
- off = 0
|
268
|
|
- for l in lines:
|
269
|
|
- off += self.textLine(l, c, off)
|
|
1
|
+# https://www.waveshare.com/wiki/Pico-LCD-1.3
|
|
2
|
+# https://thepihut.com/blogs/raspberry-pi-tutorials/coding-graphics-with-micropython-on-raspberry-pi-pico-displays
|
|
3
|
+# https://api.arcade.academy/en/latest/_modules/arcade/draw_commands.html#draw_arc_filled
|
|
4
|
+
|
|
5
|
+from machine import Pin, SPI, PWM
|
|
6
|
+from array import array
|
|
7
|
+import framebuf
|
|
8
|
+import time
|
|
9
|
+import os
|
|
10
|
+import math
|
|
11
|
+import gc
|
|
12
|
+
|
|
13
|
+class KeyCheck:
|
|
14
|
+ def __init__(self, new, old):
|
|
15
|
+ self.new = new
|
|
16
|
+ self.old = old
|
|
17
|
+
|
|
18
|
+ def once(self, k):
|
|
19
|
+ return self.new[k] and not self.old[k]
|
|
20
|
+
|
|
21
|
+class LCD(framebuf.FrameBuffer):
|
|
22
|
+ def __init__(self):
|
|
23
|
+ self.pwm = PWM(Pin(13))
|
|
24
|
+ self.pwm.freq(1000)
|
|
25
|
+ self.brightness(0.0)
|
|
26
|
+
|
|
27
|
+ self.width = 240
|
|
28
|
+ self.height = 240
|
|
29
|
+
|
|
30
|
+ self.cs = Pin(9, Pin.OUT)
|
|
31
|
+ self.rst = Pin(12, Pin.OUT)
|
|
32
|
+
|
|
33
|
+ self.cs(1)
|
|
34
|
+
|
|
35
|
+ #self.spi = SPI(1)
|
|
36
|
+ #self.spi = SPI(1, 1_000_000)
|
|
37
|
+ self.spi = SPI(1, 100_000_000, polarity=0, phase=0, sck=Pin(10), mosi=Pin(11), miso=None)
|
|
38
|
+
|
|
39
|
+ self.dc = Pin(8, Pin.OUT)
|
|
40
|
+ self.dc(1)
|
|
41
|
+
|
|
42
|
+ gc.collect()
|
|
43
|
+ self.buffer = bytearray(self.height * self.width * 2)
|
|
44
|
+
|
|
45
|
+ super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
|
|
46
|
+ self.init_display()
|
|
47
|
+
|
|
48
|
+ self.red = self.color(0xFF, 0x00, 0x00)
|
|
49
|
+ self.green = self.color(0x00, 0xFF, 0x00)
|
|
50
|
+ self.blue = self.color(0x00, 0x00, 0xFF)
|
|
51
|
+ self.yellow = self.color(0xFF, 0xFF, 0x00)
|
|
52
|
+ self.white = self.color(0xFF, 0xFF, 0xFF)
|
|
53
|
+ self.black = self.color(0x00, 0x00, 0x00)
|
|
54
|
+
|
|
55
|
+ self.fill(self.black)
|
|
56
|
+ self.show()
|
|
57
|
+
|
|
58
|
+ self.keyA = Pin(15, Pin.IN, Pin.PULL_UP)
|
|
59
|
+ self.keyB = Pin(17, Pin.IN, Pin.PULL_UP)
|
|
60
|
+ self.keyX = Pin(19, Pin.IN, Pin.PULL_UP)
|
|
61
|
+ self.keyY = Pin(21, Pin.IN, Pin.PULL_UP)
|
|
62
|
+ self.up = Pin( 2, Pin.IN, Pin.PULL_UP)
|
|
63
|
+ self.down = Pin(18, Pin.IN, Pin.PULL_UP)
|
|
64
|
+ self.left = Pin(16, Pin.IN, Pin.PULL_UP)
|
|
65
|
+ self.right = Pin(20, Pin.IN, Pin.PULL_UP)
|
|
66
|
+ self.ctrl = Pin( 3, Pin.IN, Pin.PULL_UP)
|
|
67
|
+
|
|
68
|
+ self.keys_old = {
|
|
69
|
+ "a": False,
|
|
70
|
+ "b": False,
|
|
71
|
+ "x": False,
|
|
72
|
+ "y": False,
|
|
73
|
+ "up": False,
|
|
74
|
+ "down": False,
|
|
75
|
+ "left": False,
|
|
76
|
+ "right": False,
|
|
77
|
+ "enter": False,
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ def buttons(self):
|
|
81
|
+ keys = {
|
|
82
|
+ "a": self.keyA.value() == 0,
|
|
83
|
+ "b": self.keyB.value() == 0,
|
|
84
|
+ "x": self.keyX.value() == 0,
|
|
85
|
+ "y": self.keyY.value() == 0,
|
|
86
|
+ "up": self.up.value() == 0,
|
|
87
|
+ "down": self.down.value() == 0,
|
|
88
|
+ "left": self.left.value() == 0,
|
|
89
|
+ "right": self.right.value() == 0,
|
|
90
|
+ "enter": self.ctrl.value() == 0,
|
|
91
|
+ }
|
|
92
|
+ kc = KeyCheck(keys, self.keys_old)
|
|
93
|
+ self.keys_old = keys.copy()
|
|
94
|
+ return kc
|
|
95
|
+
|
|
96
|
+ # Convert RGB888 to RGB565
|
|
97
|
+ def color(self, R, G, B):
|
|
98
|
+ return (((G & 0b00011100) << 3) + ((B & 0b11111000) >> 3) << 8) + (R & 0b11111000) + ((G & 0b11100000) >> 5)
|
|
99
|
+
|
|
100
|
+ def brightness(self, v):
|
|
101
|
+ self.pwm.duty_u16(int(v * 65535))
|
|
102
|
+
|
|
103
|
+ def write_cmd(self, cmd):
|
|
104
|
+ self.cs(1)
|
|
105
|
+ self.dc(0)
|
|
106
|
+ self.cs(0)
|
|
107
|
+ self.spi.write(bytearray([cmd]))
|
|
108
|
+ self.cs(1)
|
|
109
|
+
|
|
110
|
+ def write_data(self, buf):
|
|
111
|
+ self.cs(1)
|
|
112
|
+ self.dc(1)
|
|
113
|
+ self.cs(0)
|
|
114
|
+ self.spi.write(bytearray([buf]))
|
|
115
|
+ self.cs(1)
|
|
116
|
+
|
|
117
|
+ def init_display(self):
|
|
118
|
+ self.rst(1)
|
|
119
|
+ self.rst(0)
|
|
120
|
+ self.rst(1)
|
|
121
|
+ self.write_cmd(0x36)
|
|
122
|
+ self.write_data(0x70)
|
|
123
|
+ self.write_cmd(0x3A)
|
|
124
|
+ self.write_data(0x05)
|
|
125
|
+ self.write_cmd(0xB2)
|
|
126
|
+ self.write_data(0x0C)
|
|
127
|
+ self.write_data(0x0C)
|
|
128
|
+ self.write_data(0x00)
|
|
129
|
+ self.write_data(0x33)
|
|
130
|
+ self.write_data(0x33)
|
|
131
|
+ self.write_cmd(0xB7)
|
|
132
|
+ self.write_data(0x35)
|
|
133
|
+ self.write_cmd(0xBB)
|
|
134
|
+ self.write_data(0x19)
|
|
135
|
+ self.write_cmd(0xC0)
|
|
136
|
+ self.write_data(0x2C)
|
|
137
|
+ self.write_cmd(0xC2)
|
|
138
|
+ self.write_data(0x01)
|
|
139
|
+ self.write_cmd(0xC3)
|
|
140
|
+ self.write_data(0x12)
|
|
141
|
+ self.write_cmd(0xC4)
|
|
142
|
+ self.write_data(0x20)
|
|
143
|
+ self.write_cmd(0xC6)
|
|
144
|
+ self.write_data(0x0F)
|
|
145
|
+ self.write_cmd(0xD0)
|
|
146
|
+ self.write_data(0xA4)
|
|
147
|
+ self.write_data(0xA1)
|
|
148
|
+ self.write_cmd(0xE0)
|
|
149
|
+ self.write_data(0xD0)
|
|
150
|
+ self.write_data(0x04)
|
|
151
|
+ self.write_data(0x0D)
|
|
152
|
+ self.write_data(0x11)
|
|
153
|
+ self.write_data(0x13)
|
|
154
|
+ self.write_data(0x2B)
|
|
155
|
+ self.write_data(0x3F)
|
|
156
|
+ self.write_data(0x54)
|
|
157
|
+ self.write_data(0x4C)
|
|
158
|
+ self.write_data(0x18)
|
|
159
|
+ self.write_data(0x0D)
|
|
160
|
+ self.write_data(0x0B)
|
|
161
|
+ self.write_data(0x1F)
|
|
162
|
+ self.write_data(0x23)
|
|
163
|
+ self.write_cmd(0xE1)
|
|
164
|
+ self.write_data(0xD0)
|
|
165
|
+ self.write_data(0x04)
|
|
166
|
+ self.write_data(0x0C)
|
|
167
|
+ self.write_data(0x11)
|
|
168
|
+ self.write_data(0x13)
|
|
169
|
+ self.write_data(0x2C)
|
|
170
|
+ self.write_data(0x3F)
|
|
171
|
+ self.write_data(0x44)
|
|
172
|
+ self.write_data(0x51)
|
|
173
|
+ self.write_data(0x2F)
|
|
174
|
+ self.write_data(0x1F)
|
|
175
|
+ self.write_data(0x1F)
|
|
176
|
+ self.write_data(0x20)
|
|
177
|
+ self.write_data(0x23)
|
|
178
|
+ self.write_cmd(0x21)
|
|
179
|
+ self.write_cmd(0x11)
|
|
180
|
+ self.write_cmd(0x29)
|
|
181
|
+
|
|
182
|
+ def show(self):
|
|
183
|
+ self.write_cmd(0x2A)
|
|
184
|
+ self.write_data(0x00)
|
|
185
|
+ self.write_data(0x00)
|
|
186
|
+ self.write_data(0x00)
|
|
187
|
+ self.write_data(0xef)
|
|
188
|
+ self.write_cmd(0x2B)
|
|
189
|
+ self.write_data(0x00)
|
|
190
|
+ self.write_data(0x00)
|
|
191
|
+ self.write_data(0x00)
|
|
192
|
+ self.write_data(0xEF)
|
|
193
|
+ self.write_cmd(0x2C)
|
|
194
|
+ self.cs(1)
|
|
195
|
+ self.dc(1)
|
|
196
|
+ self.cs(0)
|
|
197
|
+ self.spi.write(self.buffer)
|
|
198
|
+ self.cs(1)
|
|
199
|
+
|
|
200
|
+ def circle(self, x, y, r, c):
|
|
201
|
+ self.hline(x-r,y,r*2,c)
|
|
202
|
+ for i in range(1,r):
|
|
203
|
+ a = int(math.sqrt(r*r-i*i)) # Pythagoras!
|
|
204
|
+ self.hline(x-a,y+i,a*2,c) # Lower half
|
|
205
|
+ self.hline(x-a,y-i,a*2,c) # Upper half
|
|
206
|
+
|
|
207
|
+ def ring(self, x, y, r, c):
|
|
208
|
+ self.pixel(x-r,y,c)
|
|
209
|
+ self.pixel(x+r,y,c)
|
|
210
|
+ self.pixel(x,y-r,c)
|
|
211
|
+ self.pixel(x,y+r,c)
|
|
212
|
+ for i in range(1, r):
|
|
213
|
+ a = int(math.sqrt(r*r-i*i))
|
|
214
|
+ self.pixel(x-a,y-i,c)
|
|
215
|
+ self.pixel(x+a,y-i,c)
|
|
216
|
+ self.pixel(x-a,y+i,c)
|
|
217
|
+ self.pixel(x+a,y+i,c)
|
|
218
|
+ self.pixel(x-i,y-a,c)
|
|
219
|
+ self.pixel(x+i,y-a,c)
|
|
220
|
+ self.pixel(x-i,y+a,c)
|
|
221
|
+ self.pixel(x+i,y+a,c)
|
|
222
|
+
|
|
223
|
+ def arc(self, x_off, y_off, w, h, c,
|
|
224
|
+ start_angle, end_angle,
|
|
225
|
+ filled = True,
|
|
226
|
+ num_segments = 128):
|
|
227
|
+ start_segment = int(start_angle / 360 * num_segments)
|
|
228
|
+ end_segment = int(end_angle / 360 * num_segments)
|
|
229
|
+
|
|
230
|
+ gc.collect()
|
|
231
|
+ point_list = array('h')
|
|
232
|
+ point_list.append(0)
|
|
233
|
+ point_list.append(0)
|
|
234
|
+
|
|
235
|
+ for segment in range(start_segment, end_segment + 1):
|
|
236
|
+ theta = 2.0 * 3.1415926 * segment / num_segments
|
|
237
|
+ x = w * math.cos(theta) / 2
|
|
238
|
+ y = h * math.sin(theta) / 2
|
|
239
|
+
|
|
240
|
+ i = (segment - start_segment + 1) * 2
|
|
241
|
+ point_list.append(int(x))
|
|
242
|
+ point_list.append(int(y))
|
|
243
|
+
|
|
244
|
+ self.poly(int(x_off), int(y_off), point_list, c, True)
|
|
245
|
+
|
|
246
|
+ def pie(self, x0, y0, w, c_border, c_circle, v):
|
|
247
|
+ if v > 0.0:
|
|
248
|
+ self.arc(int(x0), int(y0), int(w), int(w), c_circle, -90, int(v * 360) - 90)
|
|
249
|
+ self.ring(int(x0), int(y0), int(w / 2), c_border)
|
|
250
|
+
|
|
251
|
+ def textC(self, s, x, y, c):
|
|
252
|
+ self.text(s, x - int(len(s) * 8 / 2), y - 5, c)
|
|
253
|
+
|
|
254
|
+ def textLine(self, s, c, off = 0):
|
|
255
|
+ charsPerLine = int(self.width / 8)
|
|
256
|
+ lines = list(s[0+i:charsPerLine+i] for i in range(0, len(s), charsPerLine))
|
|
257
|
+ n = 0
|
|
258
|
+ for i, l in enumerate(lines):
|
|
259
|
+ self.text(l, 0, (i + off) * 10, c)
|
|
260
|
+ n += 1
|
|
261
|
+ if i >= (self.height / 10):
|
|
262
|
+ break
|
|
263
|
+ return n
|
|
264
|
+
|
|
265
|
+ def textBlock(self, s, c):
|
|
266
|
+ lines = s.split("\n")
|
|
267
|
+ off = 0
|
|
268
|
+ for l in lines:
|
|
269
|
+ off += self.textLine(l, c, off)
|