1212All text above must be included in any redistribution. 
1313""" 
1414
15- #pylint:disable=invalid-name, no-self-use 
16- 
1715import  gc 
1816import  time 
1917import  board 
2018import  displayio 
2119import  adafruit_logging  as  logging 
20+ 
2221try :
2322    import  adafruit_touchscreen 
2423except  ImportError :
2928except  ImportError :
3029    pass 
3130
31+ 
3232class  Color (object ):
3333    """Standard colors""" 
34+ 
3435    WHITE  =  0xFFFFFF 
3536    BLACK  =  0x000000 
3637    RED  =  0xFF0000 
@@ -46,32 +47,35 @@ class Color(object):
4647    def  __init__ (self ):
4748        pass 
4849
50+ 
4951################################################################################ 
5052
53+ 
5154class  TouchscreenPoller (object ):
5255    """Get 'pressed' and location updates from a touch screen device.""" 
5356
5457    def  __init__ (self , splash , cursor_bmp ):
55-         logging .getLogger (' Paint' debug (' Creating a TouchscreenPoller' 
58+         logging .getLogger (" Paint" debug (" Creating a TouchscreenPoller" 
5659        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 ))
62-         self ._cursor_grp  =  displayio .Group (max_size = 1 )
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+         )
66+         self ._cursor_grp  =  displayio .Group ()
6367        self ._cur_palette  =  displayio .Palette (3 )
6468        self ._cur_palette .make_transparent (0 )
6569        self ._cur_palette [1 ] =  0xFFFFFF 
6670        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+         )
6974        self ._cursor_grp .append (self ._cur_sprite )
7075        self ._display_grp .append (self ._cursor_grp )
7176        self ._x_offset  =  cursor_bmp .width  //  2 
7277        self ._y_offset  =  cursor_bmp .height  //  2 
7378
74- 
7579    def  poll (self ):
7680        """Check for input. Returns contact (a bool), False (no button B), 
7781        and it's location ((x,y) or None)""" 
@@ -98,26 +102,26 @@ def set_cursor_bitmap(self, bmp):
98102        :param bmp: the new cursor bitmap 
99103        """ 
100104        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 )
103106        self ._cursor_grp .append (self ._cur_sprite )
104107        self .poke ()
105108
109+ 
106110################################################################################ 
107111
112+ 
108113class  CursorPoller (object ):
109114    """Get 'pressed' and location updates from a D-Pad/joystick device.""" 
110115
111116    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+         )
117121        self ._x_offset  =  cursor_bmp .width  //  2 
118122        self ._y_offset  =  cursor_bmp .height  //  2 
119123        self ._cursor  =  DebouncedCursorManager (self ._mouse_cursor )
120-         self ._logger  =  logging .getLogger (' Paint' 
124+         self ._logger  =  logging .getLogger (" Paint" 
121125
122126    def  poll (self ):
123127        """Check for input. Returns press of A (a bool), B, 
@@ -126,16 +130,16 @@ def poll(self):
126130        self ._cursor .update ()
127131        a_button  =  self ._cursor .held 
128132        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+             )
131137        return  a_button , location 
132138
133-     #pylint:disable=unused-argument 
134139    def  poke (self , x = None , y = None ):
135140        """Force a bitmap refresh.""" 
136141        self ._mouse_cursor .hide ()
137142        self ._mouse_cursor .show ()
138-     #pylint:enable=unused-argument 
139143
140144    def  set_cursor_bitmap (self , bmp ):
141145        """Update the cursor bitmap. 
@@ -145,10 +149,11 @@ def set_cursor_bitmap(self, bmp):
145149        self ._mouse_cursor .cursor_bitmap  =  bmp 
146150        self .poke ()
147151
152+ 
148153################################################################################ 
149- class  Paint (object ):
150154
151-     #pylint:disable=too-many-statements 
155+ 
156+ class  Paint (object ):
152157    def  __init__ (self , display = board .DISPLAY ):
153158        self ._logger  =  logging .getLogger ("Paint" )
154159        self ._logger .setLevel (logging .DEBUG )
@@ -158,39 +163,39 @@ def __init__(self, display=board.DISPLAY):
158163        self ._x  =  self ._w  //  2 
159164        self ._y  =  self ._h  //  2 
160165
161-         self ._splash  =  displayio .Group (max_size = 5 )
166+         self ._splash  =  displayio .Group ()
162167
163168        self ._bg_bitmap  =  displayio .Bitmap (self ._w , self ._h , 1 )
164169        self ._bg_palette  =  displayio .Palette (1 )
165170        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+         )
169174        self ._splash .append (self ._bg_sprite )
170175
171176        self ._palette_bitmap  =  displayio .Bitmap (self ._w , self ._h , 5 )
172177        self ._palette_palette  =  displayio .Palette (len (Color .colors ))
173178        for  i , c  in  enumerate (Color .colors ):
174179            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+         )
178183        self ._splash .append (self ._palette_sprite )
179184
180185        self ._fg_bitmap  =  displayio .Bitmap (self ._w , self ._h , 5 )
181186        self ._fg_palette  =  displayio .Palette (len (Color .colors ))
182187        for  i , c  in  enumerate (Color .colors ):
183188            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+         )
187192        self ._splash .append (self ._fg_sprite )
188193
189194        self ._number_of_palette_options  =  len (Color .colors ) +  2 
190195        self ._swatch_height  =  self ._h  //  self ._number_of_palette_options 
191196        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 )
194199
195200        self ._palette  =  self ._make_palette ()
196201        self ._splash .append (self ._palette )
@@ -206,20 +211,19 @@ def __init__(self, display=board.DISPLAY):
206211
207212        self ._brush  =  0 
208213        self ._cursor_bitmaps  =  [self ._cursor_bitmap_1 (), self ._cursor_bitmap_3 ()]
209-         if  hasattr (board , ' TOUCH_XL' 
214+         if  hasattr (board , " TOUCH_XL" 
210215            self ._poller  =  TouchscreenPoller (self ._splash , self ._cursor_bitmaps [0 ])
211-         elif  hasattr (board , ' BUTTON_CLOCK' 
216+         elif  hasattr (board , " BUTTON_CLOCK" 
212217            self ._poller  =  CursorPoller (self ._splash , self ._cursor_bitmaps [0 ])
213218        else :
214-             raise  AttributeError (' PyPaint requires a touchscreen or cursor.' 
219+             raise  AttributeError (" PyPaint requires a touchscreen or cursor." 
215220
216221        self ._a_pressed  =  False 
217222        self ._last_a_pressed  =  False 
218223        self ._location  =  None 
219224        self ._last_location  =  None 
220225
221226        self ._pencolor  =  7 
222-     #pylint:enable=too-many-statements 
223227
224228    def  _make_palette (self ):
225229        self ._palette_bitmap  =  displayio .Bitmap (self ._w  //  10 , self ._h , 5 )
@@ -253,9 +257,9 @@ def _make_palette(self):
253257        for  i  in  range (self ._h ):
254258            self ._palette_bitmap [self ._swatch_width  -  1 , i ] =  7 
255259
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+         )
259263
260264    def  _cursor_bitmap_1 (self ):
261265        bmp  =  displayio .Bitmap (9 , 9 , 3 )
@@ -291,8 +295,6 @@ def _plot(self, x, y, c):
291295                except  IndexError :
292296                    pass 
293297
294-     #pylint:disable=too-many-branches,too-many-statements 
295- 
296298    def  _draw_line (self , start , end ):
297299        """Draw a line from the previous position to the current one. 
298300
@@ -351,35 +353,33 @@ def _draw_line(self, start, end):
351353            else :
352354                x0  +=  1 
353355
354-     #pylint:enable=too-many-branches,too-many-statements 
355- 
356356    def  _handle_palette_selection (self , location ):
357357        selected  =  location [1 ] //  self ._swatch_height 
358358        if  selected  >=  self ._number_of_palette_options :
359359            return 
360-         self ._logger .debug (' Palette selection: %d' selected )
360+         self ._logger .debug (" Palette selection: %d" selected )
361361        if  selected  <  len (Color .colors ):
362362            self ._pencolor  =  selected 
363363        else :
364364            self ._brush  =  selected  -  len (Color .colors )
365365            self ._poller .set_cursor_bitmap (self ._cursor_bitmaps [self ._brush ])
366366
367367    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+         )
369371        self ._draw_line (start , end )
370372
371373    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 
374376            self ._handle_palette_selection (location )
375377        else :
376378            self ._plot (location [0 ], location [1 ], self ._pencolor )
377379            self ._poller .poke ()
378380
379-     #pylint:disable=unused-argument 
380381    def  _handle_a_release (self , location ):
381-         self ._logger .debug ('A Released!' )
382-     #pylint:enable=unused-argument 
382+         self ._logger .debug ("A Released!" )
383383
384384    @property  
385385    def  _was_a_just_pressed (self ):
@@ -402,7 +402,6 @@ def _update(self):
402402        self ._last_a_pressed , self ._last_location  =  self ._a_pressed , self ._location 
403403        self ._a_pressed , self ._location  =  self ._poller .poll ()
404404
405- 
406405    def  run (self ):
407406        """Run the painting program.""" 
408407        while  True :
@@ -415,5 +414,6 @@ def run(self):
415414                self ._handle_motion (self ._last_location , self ._location )
416415            time .sleep (0.1 )
417416
417+ 
418418painter  =  Paint ()
419419painter .run ()
0 commit comments