2929 460800 : 8 ,
3030}
3131
32+
3233class LCD160CR :
3334 def __init__ (self , connect = None , * , pwr = None , i2c = None , spi = None , i2c_addr = 98 ):
34- if connect in ('X' , 'Y' , 'XY' , 'YX' ):
35+ if connect in ("X" , "Y" , "XY" , "YX" ):
3536 i = connect [- 1 ]
3637 j = connect [0 ]
37- y = j + '4'
38- elif connect == 'C' :
38+ y = j + "4"
39+ elif connect == "C" :
3940 i = 2
4041 j = 2
41- y = 'A7'
42+ y = "A7"
4243 else :
4344 if pwr is None or i2c is None or spi is None :
4445 raise ValueError ('must specify valid "connect" or all of "pwr", "i2c" and "spi"' )
@@ -74,8 +75,8 @@ def __init__(self, connect=None, *, pwr=None, i2c=None, spi=None, i2c_addr=98):
7475
7576 # set default orientation and window
7677 self .set_orient (PORTRAIT )
77- self ._fcmd2b (' <BBBBBB' , 0x76 , 0 , 0 , self .w , self .h ) # viewport 'v'
78- self ._fcmd2b (' <BBBBBB' , 0x79 , 0 , 0 , self .w , self .h ) # window 'y'
78+ self ._fcmd2b (" <BBBBBB" , 0x76 , 0 , 0 , self .w , self .h ) # viewport 'v'
79+ self ._fcmd2b (" <BBBBBB" , 0x79 , 0 , 0 , self .w , self .h ) # window 'y'
7980
8081 def _send (self , cmd ):
8182 i = self .i2c .writeto (self .i2c_addr , cmd )
@@ -135,7 +136,7 @@ def iflush(self):
135136
136137 @staticmethod
137138 def rgb (r , g , b ):
138- return ((b & 0xf8 ) << 8 ) | ((g & 0xfc ) << 3 ) | (r >> 3 )
139+ return ((b & 0xF8 ) << 8 ) | ((g & 0xFC ) << 3 ) | (r >> 3 )
139140
140141 @staticmethod
141142 def clip_line (c , w , h ):
@@ -207,43 +208,43 @@ def set_power(self, on):
207208 sleep_ms (15 )
208209
209210 def set_orient (self , orient ):
210- self ._fcmd2 (' <BBB' , 0x14 , (orient & 3 ) + 4 )
211+ self ._fcmd2 (" <BBB" , 0x14 , (orient & 3 ) + 4 )
211212 # update width and height variables
212213 self .iflush ()
213- self ._send (b' \x02 g0' )
214+ self ._send (b" \x02 g0" )
214215 self ._waitfor (4 , self .buf [5 ])
215216 self .w = self .buf [5 ][1 ]
216217 self .h = self .buf [5 ][2 ]
217218
218219 def set_brightness (self , value ):
219- self ._fcmd2 (' <BBB' , 0x16 , value )
220+ self ._fcmd2 (" <BBB" , 0x16 , value )
220221
221222 def set_i2c_addr (self , addr ):
222223 # 0x0e set i2c addr
223224 if addr & 3 :
224- raise ValueError (' must specify mod 4 aligned address' )
225- self ._fcmd2 (' <BBW' , 0x0e , 0x433249 | (addr << 24 ))
225+ raise ValueError (" must specify mod 4 aligned address" )
226+ self ._fcmd2 (" <BBW" , 0x0E , 0x433249 | (addr << 24 ))
226227
227228 def set_uart_baudrate (self , baudrate ):
228229 try :
229230 baudrate = _uart_baud_table [baudrate ]
230231 except KeyError :
231- raise ValueError (' invalid baudrate' )
232- self ._fcmd2 (' <BBB' , 0x18 , baudrate )
232+ raise ValueError (" invalid baudrate" )
233+ self ._fcmd2 (" <BBB" , 0x18 , baudrate )
233234
234235 def set_startup_deco (self , value ):
235- self ._fcmd2 (' <BBB' , 0x19 , value )
236+ self ._fcmd2 (" <BBB" , 0x19 , value )
236237
237238 def save_to_flash (self ):
238- self ._send (b' \x02 fn' )
239+ self ._send (b" \x02 fn" )
239240
240241 #### PIXEL ACCESS ####
241242
242243 def set_pixel (self , x , y , c ):
243- self ._fcmd2b (' <BBBBH' , 0x41 , x , y , c )
244+ self ._fcmd2b (" <BBBBH" , 0x41 , x , y , c )
244245
245246 def get_pixel (self , x , y ):
246- self ._fcmd2 (' <BBBB' , 0x61 , x , y )
247+ self ._fcmd2 (" <BBBB" , 0x61 , x , y )
247248 t = 1000
248249 while t :
249250 self .i2c .readfrom_into (self .i2c_addr , self .buf1 )
@@ -256,7 +257,7 @@ def get_pixel(self, x, y):
256257
257258 def get_line (self , x , y , buf ):
258259 l = len (buf ) // 2
259- self ._fcmd2b (' <BBBBB' , 0x10 , l , x , y )
260+ self ._fcmd2b (" <BBBBB" , 0x10 , l , x , y )
260261 l *= 2
261262 t = 1000
262263 while t :
@@ -280,42 +281,47 @@ def screen_dump(self, buf, x=0, y=0, w=None, h=None):
280281 # split line if more than 254 bytes needed
281282 buflen = (w + 1 ) // 2
282283 line = bytearray (2 * buflen + 1 )
283- line2 = memoryview (line )[:2 * (w - buflen ) + 1 ]
284+ line2 = memoryview (line )[: 2 * (w - buflen ) + 1 ]
284285 for i in range (min (len (buf ) // (2 * w ), h )):
285286 ix = i * w * 2
286287 self .get_line (x , y + i , line )
287- buf [ix : ix + len (line ) - 1 ] = memoryview (line )[1 :]
288+ buf [ix : ix + len (line ) - 1 ] = memoryview (line )[1 :]
288289 ix += len (line ) - 1
289290 if line2 :
290291 self .get_line (x + buflen , y + i , line2 )
291- buf [ix : ix + len (line2 ) - 1 ] = memoryview (line2 )[1 :]
292+ buf [ix : ix + len (line2 ) - 1 ] = memoryview (line2 )[1 :]
292293 ix += len (line2 ) - 1
293294
294295 def screen_load (self , buf ):
295- l = self .w * self .h * 2 + 2
296- self ._fcmd2b (' <BBHBBB' , 0x70 , l , 16 , self .w , self .h )
296+ l = self .w * self .h * 2 + 2
297+ self ._fcmd2b (" <BBHBBB" , 0x70 , l , 16 , self .w , self .h )
297298 n = 0
298299 ar = memoryview (buf )
299300 while n < len (buf ):
300301 if len (buf ) - n >= 0x200 :
301- self ._send (ar [n : n + 0x200 ])
302+ self ._send (ar [n : n + 0x200 ])
302303 n += 0x200
303304 else :
304305 self ._send (ar [n :])
305306 while n < self .w * self .h * 2 :
306- self ._send (b' \x00 ' )
307+ self ._send (b" \x00 " )
307308 n += 1
308309
309310 #### TEXT COMMANDS ####
310311
311312 def set_pos (self , x , y ):
312- self ._fcmd2 (' <BBBB' , 0x58 , x , y )
313+ self ._fcmd2 (" <BBBB" , 0x58 , x , y )
313314
314315 def set_text_color (self , fg , bg ):
315- self ._fcmd2 (' <BBHH' , 0x63 , fg , bg )
316+ self ._fcmd2 (" <BBHH" , 0x63 , fg , bg )
316317
317318 def set_font (self , font , scale = 0 , bold = 0 , trans = 0 , scroll = 0 ):
318- self ._fcmd2 ('<BBBB' , 0x46 , (scroll << 7 ) | (trans << 6 ) | ((font & 3 ) << 4 ) | (bold & 0xf ), scale & 0xff )
319+ self ._fcmd2 (
320+ "<BBBB" ,
321+ 0x46 ,
322+ (scroll << 7 ) | (trans << 6 ) | ((font & 3 ) << 4 ) | (bold & 0xF ),
323+ scale & 0xFF ,
324+ )
319325
320326 def write (self , s ):
321327 # TODO: eventually check for room in LCD input queue
@@ -324,14 +330,14 @@ def write(self, s):
324330 #### PRIMITIVE DRAWING COMMANDS ####
325331
326332 def set_pen (self , line , fill ):
327- self ._fcmd2 (' <BBHH' , 0x50 , line , fill )
333+ self ._fcmd2 (" <BBHH" , 0x50 , line , fill )
328334
329335 def erase (self ):
330- self ._send (b' \x02 \x45 ' )
336+ self ._send (b" \x02 \x45 " )
331337
332338 def dot (self , x , y ):
333339 if 0 <= x < self .w and 0 <= y < self .h :
334- self ._fcmd2 (' <BBBB' , 0x4b , x , y )
340+ self ._fcmd2 (" <BBBB" , 0x4B , x , y )
335341
336342 def rect (self , x , y , w , h , cmd = 0x72 ):
337343 if x + w <= 0 or y + h <= 0 or x >= self .w or y >= self .h :
@@ -348,19 +354,19 @@ def rect(self, x, y, w, h, cmd=0x72):
348354 y = 0
349355 if cmd == 0x51 or cmd == 0x72 :
350356 # draw interior
351- self ._fcmd2b (' <BBBBBB' , 0x51 , x , y , min (w , 255 ), min (h , 255 ))
357+ self ._fcmd2b (" <BBBBBB" , 0x51 , x , y , min (w , 255 ), min (h , 255 ))
352358 if cmd == 0x57 or cmd == 0x72 :
353359 # draw outline
354360 if left :
355- self ._fcmd2b (' <BBBBBB' , 0x57 , x , y , 1 , min (h , 255 ))
361+ self ._fcmd2b (" <BBBBBB" , 0x57 , x , y , 1 , min (h , 255 ))
356362 if top :
357- self ._fcmd2b (' <BBBBBB' , 0x57 , x , y , min (w , 255 ), 1 )
363+ self ._fcmd2b (" <BBBBBB" , 0x57 , x , y , min (w , 255 ), 1 )
358364 if x + w < self .w :
359- self ._fcmd2b (' <BBBBBB' , 0x57 , x + w , y , 1 , min (h , 255 ))
365+ self ._fcmd2b (" <BBBBBB" , 0x57 , x + w , y , 1 , min (h , 255 ))
360366 if y + h < self .h :
361- self ._fcmd2b (' <BBBBBB' , 0x57 , x , y + h , min (w , 255 ), 1 )
367+ self ._fcmd2b (" <BBBBBB" , 0x57 , x , y + h , min (w , 255 ), 1 )
362368 else :
363- self ._fcmd2b (' <BBBBBB' , cmd , x , y , min (w , 255 ), min (h , 255 ))
369+ self ._fcmd2b (" <BBBBBB" , cmd , x , y , min (w , 255 ), min (h , 255 ))
364370
365371 def rect_outline (self , x , y , w , h ):
366372 self .rect (x , y , w , h , 0x57 )
@@ -375,60 +381,62 @@ def line(self, x1, y1, x2, y2):
375381 ar4 [2 ] = x2
376382 ar4 [3 ] = y2
377383 if self .clip_line (ar4 , self .w , self .h ):
378- self ._fcmd2b (' <BBBBBB' , 0x4c , ar4 [0 ], ar4 [1 ], ar4 [2 ], ar4 [3 ])
384+ self ._fcmd2b (" <BBBBBB" , 0x4C , ar4 [0 ], ar4 [1 ], ar4 [2 ], ar4 [3 ])
379385
380386 def dot_no_clip (self , x , y ):
381- self ._fcmd2 (' <BBBB' , 0x4b , x , y )
387+ self ._fcmd2 (" <BBBB" , 0x4B , x , y )
382388
383389 def rect_no_clip (self , x , y , w , h ):
384- self ._fcmd2b (' <BBBBBB' , 0x72 , x , y , w , h )
390+ self ._fcmd2b (" <BBBBBB" , 0x72 , x , y , w , h )
385391
386392 def rect_outline_no_clip (self , x , y , w , h ):
387- self ._fcmd2b (' <BBBBBB' , 0x57 , x , y , w , h )
393+ self ._fcmd2b (" <BBBBBB" , 0x57 , x , y , w , h )
388394
389395 def rect_interior_no_clip (self , x , y , w , h ):
390- self ._fcmd2b (' <BBBBBB' , 0x51 , x , y , w , h )
396+ self ._fcmd2b (" <BBBBBB" , 0x51 , x , y , w , h )
391397
392398 def line_no_clip (self , x1 , y1 , x2 , y2 ):
393- self ._fcmd2b (' <BBBBBB' , 0x4c , x1 , y1 , x2 , y2 )
399+ self ._fcmd2b (" <BBBBBB" , 0x4C , x1 , y1 , x2 , y2 )
394400
395401 def poly_dot (self , data ):
396402 if len (data ) & 1 :
397- raise ValueError (' must specify even number of bytes' )
398- self ._fcmd2 (' <BBB' , 0x71 , len (data ) // 2 )
403+ raise ValueError (" must specify even number of bytes" )
404+ self ._fcmd2 (" <BBB" , 0x71 , len (data ) // 2 )
399405 self ._send (data )
400406
401407 def poly_line (self , data ):
402408 if len (data ) & 1 :
403- raise ValueError (' must specify even number of bytes' )
404- self ._fcmd2 (' <BBB' , 0x78 , len (data ) // 2 )
409+ raise ValueError (" must specify even number of bytes" )
410+ self ._fcmd2 (" <BBB" , 0x78 , len (data ) // 2 )
405411 self ._send (data )
406412
407413 #### TOUCH COMMANDS ####
408414
409415 def touch_config (self , calib = False , save = False , irq = None ):
410- self ._fcmd2 (' <BBBB' , 0x7a , (irq is not None ) << 2 | save << 1 | calib , bool (irq ) << 7 )
416+ self ._fcmd2 (" <BBBB" , 0x7A , (irq is not None ) << 2 | save << 1 | calib , bool (irq ) << 7 )
411417
412418 def is_touched (self ):
413- self ._send (b' \x02 T' )
419+ self ._send (b" \x02 T" )
414420 b = self .buf [4 ]
415421 self ._waitfor (3 , b )
416422 return b [1 ] >> 7 != 0
417423
418424 def get_touch (self ):
419- self ._send (b' \x02 T' ) # implicit LCD output flush
425+ self ._send (b" \x02 T" ) # implicit LCD output flush
420426 b = self .buf [4 ]
421427 self ._waitfor (3 , b )
422428 return b [1 ] >> 7 , b [2 ], b [3 ]
423429
424430 #### ADVANCED COMMANDS ####
425431
426432 def set_spi_win (self , x , y , w , h ):
427- pack_into ('<BBBHHHHHHHH' , self .buf19 , 0 , 2 , 0x55 , 10 , x , y , x + w - 1 , y + h - 1 , 0 , 0 , 0 , 0xffff )
433+ pack_into (
434+ "<BBBHHHHHHHH" , self .buf19 , 0 , 2 , 0x55 , 10 , x , y , x + w - 1 , y + h - 1 , 0 , 0 , 0 , 0xFFFF
435+ )
428436 self ._send (self .buf19 )
429437
430438 def fast_spi (self , flush = True ):
431- self ._send (b' \x02 \x12 ' )
439+ self ._send (b" \x02 \x12 " )
432440 if flush :
433441 self .oflush ()
434442 return self .spi
@@ -437,27 +445,27 @@ def show_framebuf(self, buf):
437445 self .fast_spi ().write (buf )
438446
439447 def set_scroll (self , on ):
440- self ._fcmd2 (' <BBB' , 0x15 , on )
448+ self ._fcmd2 (" <BBB" , 0x15 , on )
441449
442- def set_scroll_win (self , win , x = - 1 , y = 0 , w = 0 , h = 0 , vec = 0 , pat = 0 , fill = 0x07e0 , color = 0 ):
443- pack_into (' <BBBHHHHHHHH' , self .buf19 , 0 , 2 , 0x55 , win , x , y , w , h , vec , pat , fill , color )
450+ def set_scroll_win (self , win , x = - 1 , y = 0 , w = 0 , h = 0 , vec = 0 , pat = 0 , fill = 0x07E0 , color = 0 ):
451+ pack_into (" <BBBHHHHHHHH" , self .buf19 , 0 , 2 , 0x55 , win , x , y , w , h , vec , pat , fill , color )
444452 self ._send (self .buf19 )
445453
446454 def set_scroll_win_param (self , win , param , value ):
447- self ._fcmd2b (' <BBBBH' , 0x75 , win , param , value )
455+ self ._fcmd2b (" <BBBBH" , 0x75 , win , param , value )
448456
449457 def set_scroll_buf (self , s ):
450458 l = len (s )
451459 if l > 32 :
452- raise ValueError (' length must be 32 or less' )
453- self ._fcmd2 (' <BBB' , 0x11 , l )
460+ raise ValueError (" length must be 32 or less" )
461+ self ._fcmd2 (" <BBB" , 0x11 , l )
454462 self ._send (s )
455463
456464 def jpeg_start (self , l ):
457- if l > 0xffff :
458- raise ValueError (' length must be 65535 or less' )
465+ if l > 0xFFFF :
466+ raise ValueError (" length must be 65535 or less" )
459467 self .oflush ()
460- self ._fcmd2 (' <BBH' , 0x6a , l )
468+ self ._fcmd2 (" <BBH" , 0x6A , l )
461469
462470 def jpeg_data (self , buf ):
463471 self ._send (buf )
@@ -467,8 +475,8 @@ def jpeg(self, buf):
467475 self .jpeg_data (buf )
468476
469477 def feed_wdt (self ):
470- self ._send (b' \x02 \x17 ' )
478+ self ._send (b" \x02 \x17 " )
471479
472480 def reset (self ):
473- self ._send (b' \x02 Y\xef \xbe \xad \xde ' )
481+ self ._send (b" \x02 Y\xef \xbe \xad \xde " )
474482 sleep_ms (15 )
0 commit comments