12
12
All text above must be included in any redistribution.
13
13
"""
14
14
15
- #pylint:disable=invalid-name, no-self-use
16
-
17
15
import gc
18
16
import time
19
17
import board
20
18
import displayio
21
19
import adafruit_logging as logging
20
+
22
21
try :
23
22
import adafruit_touchscreen
24
23
except ImportError :
29
28
except ImportError :
30
29
pass
31
30
31
+
32
32
class Color (object ):
33
33
"""Standard colors"""
34
+
34
35
WHITE = 0xFFFFFF
35
36
BLACK = 0x000000
36
37
RED = 0xFF0000
@@ -46,32 +47,35 @@ class Color(object):
46
47
def __init__ (self ):
47
48
pass
48
49
50
+
49
51
################################################################################
50
52
53
+
51
54
class TouchscreenPoller (object ):
52
55
"""Get 'pressed' and location updates from a touch screen device."""
53
56
54
57
def __init__ (self , splash , cursor_bmp ):
55
- logging .getLogger (' Paint' ).debug (' Creating a TouchscreenPoller' )
58
+ logging .getLogger (" Paint" ).debug (" Creating a TouchscreenPoller" )
56
59
self ._display_grp = splash
57
- self ._touchscreen = adafruit_touchscreen .Touchscreen (board .TOUCH_XL , board .TOUCH_XR ,
58
- board .TOUCH_YD , board .TOUCH_YU ,
59
- calibration = ((9000 , 59000 ),
60
- (8000 , 57000 )),
61
- size = (320 , 240 ))
60
+ self ._touchscreen = adafruit_touchscreen .Touchscreen (
61
+ board .TOUCH_XL , board .TOUCH_XR ,
62
+ board .TOUCH_YD , board .TOUCH_YU ,
63
+ calibration = ((9000 , 59000 ), (8000 , 57000 )),
64
+ size = (320 , 240 ),
65
+ )
62
66
self ._cursor_grp = displayio .Group ()
63
67
self ._cur_palette = displayio .Palette (3 )
64
68
self ._cur_palette .make_transparent (0 )
65
69
self ._cur_palette [1 ] = 0xFFFFFF
66
70
self ._cur_palette [2 ] = 0x0000
67
- self ._cur_sprite = displayio .TileGrid (cursor_bmp ,
68
- pixel_shader = self ._cur_palette )
71
+ self ._cur_sprite = displayio .TileGrid (
72
+ cursor_bmp , pixel_shader = self ._cur_palette
73
+ )
69
74
self ._cursor_grp .append (self ._cur_sprite )
70
75
self ._display_grp .append (self ._cursor_grp )
71
76
self ._x_offset = cursor_bmp .width // 2
72
77
self ._y_offset = cursor_bmp .height // 2
73
78
74
-
75
79
def poll (self ):
76
80
"""Check for input. Returns contact (a bool), False (no button B),
77
81
and it's location ((x,y) or None)"""
@@ -98,26 +102,26 @@ def set_cursor_bitmap(self, bmp):
98
102
:param bmp: the new cursor bitmap
99
103
"""
100
104
self ._cursor_grp .remove (self ._cur_sprite )
101
- self ._cur_sprite = displayio .TileGrid (bmp ,
102
- pixel_shader = self ._cur_palette )
105
+ self ._cur_sprite = displayio .TileGrid (bmp , pixel_shader = self ._cur_palette )
103
106
self ._cursor_grp .append (self ._cur_sprite )
104
107
self .poke ()
105
108
109
+
106
110
################################################################################
107
111
112
+
108
113
class CursorPoller (object ):
109
114
"""Get 'pressed' and location updates from a D-Pad/joystick device."""
110
115
111
116
def __init__ (self , splash , cursor_bmp ):
112
- logging .getLogger ('Paint' ).debug ('Creating a CursorPoller' )
113
- self ._mouse_cursor = Cursor (board .DISPLAY ,
114
- display_group = splash ,
115
- bmp = cursor_bmp ,
116
- cursor_speed = 2 )
117
+ logging .getLogger ("Paint" ).debug ("Creating a CursorPoller" )
118
+ self ._mouse_cursor = Cursor (
119
+ board .DISPLAY , display_group = splash , bmp = cursor_bmp , cursor_speed = 2
120
+ )
117
121
self ._x_offset = cursor_bmp .width // 2
118
122
self ._y_offset = cursor_bmp .height // 2
119
123
self ._cursor = DebouncedCursorManager (self ._mouse_cursor )
120
- self ._logger = logging .getLogger (' Paint' )
124
+ self ._logger = logging .getLogger (" Paint" )
121
125
122
126
def poll (self ):
123
127
"""Check for input. Returns press of A (a bool), B,
@@ -126,16 +130,16 @@ def poll(self):
126
130
self ._cursor .update ()
127
131
a_button = self ._cursor .held
128
132
if a_button :
129
- location = (self ._mouse_cursor .x + self ._x_offset ,
130
- self ._mouse_cursor .y + self ._y_offset )
133
+ location = (
134
+ self ._mouse_cursor .x + self ._x_offset ,
135
+ self ._mouse_cursor .y + self ._y_offset ,
136
+ )
131
137
return a_button , location
132
138
133
- #pylint:disable=unused-argument
134
139
def poke (self , x = None , y = None ):
135
140
"""Force a bitmap refresh."""
136
141
self ._mouse_cursor .hide ()
137
142
self ._mouse_cursor .show ()
138
- #pylint:enable=unused-argument
139
143
140
144
def set_cursor_bitmap (self , bmp ):
141
145
"""Update the cursor bitmap.
@@ -145,10 +149,11 @@ def set_cursor_bitmap(self, bmp):
145
149
self ._mouse_cursor .cursor_bitmap = bmp
146
150
self .poke ()
147
151
152
+
148
153
################################################################################
149
- class Paint (object ):
150
154
151
- #pylint:disable=too-many-statements
155
+
156
+ class Paint (object ):
152
157
def __init__ (self , display = board .DISPLAY ):
153
158
self ._logger = logging .getLogger ("Paint" )
154
159
self ._logger .setLevel (logging .DEBUG )
@@ -163,34 +168,34 @@ def __init__(self, display=board.DISPLAY):
163
168
self ._bg_bitmap = displayio .Bitmap (self ._w , self ._h , 1 )
164
169
self ._bg_palette = displayio .Palette (1 )
165
170
self ._bg_palette [0 ] = Color .BLACK
166
- self ._bg_sprite = displayio .TileGrid (self . _bg_bitmap ,
167
- pixel_shader = self ._bg_palette ,
168
- x = 0 , y = 0 )
171
+ self ._bg_sprite = displayio .TileGrid (
172
+ self . _bg_bitmap , pixel_shader = self ._bg_palette , x = 0 , y = 0
173
+ )
169
174
self ._splash .append (self ._bg_sprite )
170
175
171
176
self ._palette_bitmap = displayio .Bitmap (self ._w , self ._h , 5 )
172
177
self ._palette_palette = displayio .Palette (len (Color .colors ))
173
178
for i , c in enumerate (Color .colors ):
174
179
self ._palette_palette [i ] = c
175
- self ._palette_sprite = displayio .TileGrid (self . _palette_bitmap ,
176
- pixel_shader = self ._palette_palette ,
177
- x = 0 , y = 0 )
180
+ self ._palette_sprite = displayio .TileGrid (
181
+ self . _palette_bitmap , pixel_shader = self ._palette_palette , x = 0 , y = 0
182
+ )
178
183
self ._splash .append (self ._palette_sprite )
179
184
180
185
self ._fg_bitmap = displayio .Bitmap (self ._w , self ._h , 5 )
181
186
self ._fg_palette = displayio .Palette (len (Color .colors ))
182
187
for i , c in enumerate (Color .colors ):
183
188
self ._fg_palette [i ] = c
184
- self ._fg_sprite = displayio .TileGrid (self . _fg_bitmap ,
185
- pixel_shader = self ._fg_palette ,
186
- x = 0 , y = 0 )
189
+ self ._fg_sprite = displayio .TileGrid (
190
+ self . _fg_bitmap , pixel_shader = self ._fg_palette , x = 0 , y = 0
191
+ )
187
192
self ._splash .append (self ._fg_sprite )
188
193
189
194
self ._number_of_palette_options = len (Color .colors ) + 2
190
195
self ._swatch_height = self ._h // self ._number_of_palette_options
191
196
self ._swatch_width = self ._w // 10
192
- self ._logger .debug (' Height: %d' , self ._h )
193
- self ._logger .debug (' Swatch height: %d' , self ._swatch_height )
197
+ self ._logger .debug (" Height: %d" , self ._h )
198
+ self ._logger .debug (" Swatch height: %d" , self ._swatch_height )
194
199
195
200
self ._palette = self ._make_palette ()
196
201
self ._splash .append (self ._palette )
@@ -206,20 +211,19 @@ def __init__(self, display=board.DISPLAY):
206
211
207
212
self ._brush = 0
208
213
self ._cursor_bitmaps = [self ._cursor_bitmap_1 (), self ._cursor_bitmap_3 ()]
209
- if hasattr (board , ' TOUCH_XL' ):
214
+ if hasattr (board , " TOUCH_XL" ):
210
215
self ._poller = TouchscreenPoller (self ._splash , self ._cursor_bitmaps [0 ])
211
- elif hasattr (board , ' BUTTON_CLOCK' ):
216
+ elif hasattr (board , " BUTTON_CLOCK" ):
212
217
self ._poller = CursorPoller (self ._splash , self ._cursor_bitmaps [0 ])
213
218
else :
214
- raise AttributeError (' PyPaint requires a touchscreen or cursor.' )
219
+ raise AttributeError (" PyPaint requires a touchscreen or cursor." )
215
220
216
221
self ._a_pressed = False
217
222
self ._last_a_pressed = False
218
223
self ._location = None
219
224
self ._last_location = None
220
225
221
226
self ._pencolor = 7
222
- #pylint:enable=too-many-statements
223
227
224
228
def _make_palette (self ):
225
229
self ._palette_bitmap = displayio .Bitmap (self ._w // 10 , self ._h , 5 )
@@ -253,9 +257,9 @@ def _make_palette(self):
253
257
for i in range (self ._h ):
254
258
self ._palette_bitmap [self ._swatch_width - 1 , i ] = 7
255
259
256
- return displayio .TileGrid (self . _palette_bitmap ,
257
- pixel_shader = self ._palette_palette ,
258
- x = 0 , y = 0 )
260
+ return displayio .TileGrid (
261
+ self . _palette_bitmap , pixel_shader = self ._palette_palette , x = 0 , y = 0
262
+ )
259
263
260
264
def _cursor_bitmap_1 (self ):
261
265
bmp = displayio .Bitmap (9 , 9 , 3 )
@@ -291,8 +295,6 @@ def _plot(self, x, y, c):
291
295
except IndexError :
292
296
pass
293
297
294
- #pylint:disable=too-many-branches,too-many-statements
295
-
296
298
def _draw_line (self , start , end ):
297
299
"""Draw a line from the previous position to the current one.
298
300
@@ -351,35 +353,33 @@ def _draw_line(self, start, end):
351
353
else :
352
354
x0 += 1
353
355
354
- #pylint:enable=too-many-branches,too-many-statements
355
-
356
356
def _handle_palette_selection (self , location ):
357
357
selected = location [1 ] // self ._swatch_height
358
358
if selected >= self ._number_of_palette_options :
359
359
return
360
- self ._logger .debug (' Palette selection: %d' , selected )
360
+ self ._logger .debug (" Palette selection: %d" , selected )
361
361
if selected < len (Color .colors ):
362
362
self ._pencolor = selected
363
363
else :
364
364
self ._brush = selected - len (Color .colors )
365
365
self ._poller .set_cursor_bitmap (self ._cursor_bitmaps [self ._brush ])
366
366
367
367
def _handle_motion (self , start , end ):
368
- self ._logger .debug ('Moved: (%d, %d) -> (%d, %d)' , start [0 ], start [1 ], end [0 ], end [1 ])
368
+ self ._logger .debug (
369
+ "Moved: (%d, %d) -> (%d, %d)" , start [0 ], start [1 ], end [0 ], end [1 ]
370
+ )
369
371
self ._draw_line (start , end )
370
372
371
373
def _handle_a_press (self , location ):
372
- self ._logger .debug (' A Pressed!' )
373
- if location [0 ] < self ._w // 10 : # in color picker
374
+ self ._logger .debug (" A Pressed!" )
375
+ if location [0 ] < self ._w // 10 : # in color picker
374
376
self ._handle_palette_selection (location )
375
377
else :
376
378
self ._plot (location [0 ], location [1 ], self ._pencolor )
377
379
self ._poller .poke ()
378
380
379
- #pylint:disable=unused-argument
380
381
def _handle_a_release (self , location ):
381
- self ._logger .debug ('A Released!' )
382
- #pylint:enable=unused-argument
382
+ self ._logger .debug ("A Released!" )
383
383
384
384
@property
385
385
def _was_a_just_pressed (self ):
@@ -402,7 +402,6 @@ def _update(self):
402
402
self ._last_a_pressed , self ._last_location = self ._a_pressed , self ._location
403
403
self ._a_pressed , self ._location = self ._poller .poll ()
404
404
405
-
406
405
def run (self ):
407
406
"""Run the painting program."""
408
407
while True :
@@ -415,5 +414,6 @@ def run(self):
415
414
self ._handle_motion (self ._last_location , self ._location )
416
415
time .sleep (0.1 )
417
416
417
+
418
418
painter = Paint ()
419
419
painter .run ()
0 commit comments