29
29
460800 : 8 ,
30
30
}
31
31
32
+
32
33
class LCD160CR :
33
34
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" ):
35
36
i = connect [- 1 ]
36
37
j = connect [0 ]
37
- y = j + '4'
38
- elif connect == 'C' :
38
+ y = j + "4"
39
+ elif connect == "C" :
39
40
i = 2
40
41
j = 2
41
- y = 'A7'
42
+ y = "A7"
42
43
else :
43
44
if pwr is None or i2c is None or spi is None :
44
45
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):
74
75
75
76
# set default orientation and window
76
77
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'
79
80
80
81
def _send (self , cmd ):
81
82
i = self .i2c .writeto (self .i2c_addr , cmd )
@@ -135,7 +136,7 @@ def iflush(self):
135
136
136
137
@staticmethod
137
138
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 )
139
140
140
141
@staticmethod
141
142
def clip_line (c , w , h ):
@@ -207,43 +208,43 @@ def set_power(self, on):
207
208
sleep_ms (15 )
208
209
209
210
def set_orient (self , orient ):
210
- self ._fcmd2 (' <BBB' , 0x14 , (orient & 3 ) + 4 )
211
+ self ._fcmd2 (" <BBB" , 0x14 , (orient & 3 ) + 4 )
211
212
# update width and height variables
212
213
self .iflush ()
213
- self ._send (b' \x02 g0' )
214
+ self ._send (b" \x02 g0" )
214
215
self ._waitfor (4 , self .buf [5 ])
215
216
self .w = self .buf [5 ][1 ]
216
217
self .h = self .buf [5 ][2 ]
217
218
218
219
def set_brightness (self , value ):
219
- self ._fcmd2 (' <BBB' , 0x16 , value )
220
+ self ._fcmd2 (" <BBB" , 0x16 , value )
220
221
221
222
def set_i2c_addr (self , addr ):
222
223
# 0x0e set i2c addr
223
224
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 ))
226
227
227
228
def set_uart_baudrate (self , baudrate ):
228
229
try :
229
230
baudrate = _uart_baud_table [baudrate ]
230
231
except KeyError :
231
- raise ValueError (' invalid baudrate' )
232
- self ._fcmd2 (' <BBB' , 0x18 , baudrate )
232
+ raise ValueError (" invalid baudrate" )
233
+ self ._fcmd2 (" <BBB" , 0x18 , baudrate )
233
234
234
235
def set_startup_deco (self , value ):
235
- self ._fcmd2 (' <BBB' , 0x19 , value )
236
+ self ._fcmd2 (" <BBB" , 0x19 , value )
236
237
237
238
def save_to_flash (self ):
238
- self ._send (b' \x02 fn' )
239
+ self ._send (b" \x02 fn" )
239
240
240
241
#### PIXEL ACCESS ####
241
242
242
243
def set_pixel (self , x , y , c ):
243
- self ._fcmd2b (' <BBBBH' , 0x41 , x , y , c )
244
+ self ._fcmd2b (" <BBBBH" , 0x41 , x , y , c )
244
245
245
246
def get_pixel (self , x , y ):
246
- self ._fcmd2 (' <BBBB' , 0x61 , x , y )
247
+ self ._fcmd2 (" <BBBB" , 0x61 , x , y )
247
248
t = 1000
248
249
while t :
249
250
self .i2c .readfrom_into (self .i2c_addr , self .buf1 )
@@ -256,7 +257,7 @@ def get_pixel(self, x, y):
256
257
257
258
def get_line (self , x , y , buf ):
258
259
l = len (buf ) // 2
259
- self ._fcmd2b (' <BBBBB' , 0x10 , l , x , y )
260
+ self ._fcmd2b (" <BBBBB" , 0x10 , l , x , y )
260
261
l *= 2
261
262
t = 1000
262
263
while t :
@@ -280,42 +281,47 @@ def screen_dump(self, buf, x=0, y=0, w=None, h=None):
280
281
# split line if more than 254 bytes needed
281
282
buflen = (w + 1 ) // 2
282
283
line = bytearray (2 * buflen + 1 )
283
- line2 = memoryview (line )[:2 * (w - buflen ) + 1 ]
284
+ line2 = memoryview (line )[: 2 * (w - buflen ) + 1 ]
284
285
for i in range (min (len (buf ) // (2 * w ), h )):
285
286
ix = i * w * 2
286
287
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 :]
288
289
ix += len (line ) - 1
289
290
if line2 :
290
291
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 :]
292
293
ix += len (line2 ) - 1
293
294
294
295
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 )
297
298
n = 0
298
299
ar = memoryview (buf )
299
300
while n < len (buf ):
300
301
if len (buf ) - n >= 0x200 :
301
- self ._send (ar [n : n + 0x200 ])
302
+ self ._send (ar [n : n + 0x200 ])
302
303
n += 0x200
303
304
else :
304
305
self ._send (ar [n :])
305
306
while n < self .w * self .h * 2 :
306
- self ._send (b' \x00 ' )
307
+ self ._send (b" \x00 " )
307
308
n += 1
308
309
309
310
#### TEXT COMMANDS ####
310
311
311
312
def set_pos (self , x , y ):
312
- self ._fcmd2 (' <BBBB' , 0x58 , x , y )
313
+ self ._fcmd2 (" <BBBB" , 0x58 , x , y )
313
314
314
315
def set_text_color (self , fg , bg ):
315
- self ._fcmd2 (' <BBHH' , 0x63 , fg , bg )
316
+ self ._fcmd2 (" <BBHH" , 0x63 , fg , bg )
316
317
317
318
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
+ )
319
325
320
326
def write (self , s ):
321
327
# TODO: eventually check for room in LCD input queue
@@ -324,14 +330,14 @@ def write(self, s):
324
330
#### PRIMITIVE DRAWING COMMANDS ####
325
331
326
332
def set_pen (self , line , fill ):
327
- self ._fcmd2 (' <BBHH' , 0x50 , line , fill )
333
+ self ._fcmd2 (" <BBHH" , 0x50 , line , fill )
328
334
329
335
def erase (self ):
330
- self ._send (b' \x02 \x45 ' )
336
+ self ._send (b" \x02 \x45 " )
331
337
332
338
def dot (self , x , y ):
333
339
if 0 <= x < self .w and 0 <= y < self .h :
334
- self ._fcmd2 (' <BBBB' , 0x4b , x , y )
340
+ self ._fcmd2 (" <BBBB" , 0x4B , x , y )
335
341
336
342
def rect (self , x , y , w , h , cmd = 0x72 ):
337
343
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):
348
354
y = 0
349
355
if cmd == 0x51 or cmd == 0x72 :
350
356
# 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 ))
352
358
if cmd == 0x57 or cmd == 0x72 :
353
359
# draw outline
354
360
if left :
355
- self ._fcmd2b (' <BBBBBB' , 0x57 , x , y , 1 , min (h , 255 ))
361
+ self ._fcmd2b (" <BBBBBB" , 0x57 , x , y , 1 , min (h , 255 ))
356
362
if top :
357
- self ._fcmd2b (' <BBBBBB' , 0x57 , x , y , min (w , 255 ), 1 )
363
+ self ._fcmd2b (" <BBBBBB" , 0x57 , x , y , min (w , 255 ), 1 )
358
364
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 ))
360
366
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 )
362
368
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 ))
364
370
365
371
def rect_outline (self , x , y , w , h ):
366
372
self .rect (x , y , w , h , 0x57 )
@@ -375,60 +381,62 @@ def line(self, x1, y1, x2, y2):
375
381
ar4 [2 ] = x2
376
382
ar4 [3 ] = y2
377
383
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 ])
379
385
380
386
def dot_no_clip (self , x , y ):
381
- self ._fcmd2 (' <BBBB' , 0x4b , x , y )
387
+ self ._fcmd2 (" <BBBB" , 0x4B , x , y )
382
388
383
389
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 )
385
391
386
392
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 )
388
394
389
395
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 )
391
397
392
398
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 )
394
400
395
401
def poly_dot (self , data ):
396
402
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 )
399
405
self ._send (data )
400
406
401
407
def poly_line (self , data ):
402
408
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 )
405
411
self ._send (data )
406
412
407
413
#### TOUCH COMMANDS ####
408
414
409
415
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 )
411
417
412
418
def is_touched (self ):
413
- self ._send (b' \x02 T' )
419
+ self ._send (b" \x02 T" )
414
420
b = self .buf [4 ]
415
421
self ._waitfor (3 , b )
416
422
return b [1 ] >> 7 != 0
417
423
418
424
def get_touch (self ):
419
- self ._send (b' \x02 T' ) # implicit LCD output flush
425
+ self ._send (b" \x02 T" ) # implicit LCD output flush
420
426
b = self .buf [4 ]
421
427
self ._waitfor (3 , b )
422
428
return b [1 ] >> 7 , b [2 ], b [3 ]
423
429
424
430
#### ADVANCED COMMANDS ####
425
431
426
432
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
+ )
428
436
self ._send (self .buf19 )
429
437
430
438
def fast_spi (self , flush = True ):
431
- self ._send (b' \x02 \x12 ' )
439
+ self ._send (b" \x02 \x12 " )
432
440
if flush :
433
441
self .oflush ()
434
442
return self .spi
@@ -437,27 +445,27 @@ def show_framebuf(self, buf):
437
445
self .fast_spi ().write (buf )
438
446
439
447
def set_scroll (self , on ):
440
- self ._fcmd2 (' <BBB' , 0x15 , on )
448
+ self ._fcmd2 (" <BBB" , 0x15 , on )
441
449
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 )
444
452
self ._send (self .buf19 )
445
453
446
454
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 )
448
456
449
457
def set_scroll_buf (self , s ):
450
458
l = len (s )
451
459
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 )
454
462
self ._send (s )
455
463
456
464
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" )
459
467
self .oflush ()
460
- self ._fcmd2 (' <BBH' , 0x6a , l )
468
+ self ._fcmd2 (" <BBH" , 0x6A , l )
461
469
462
470
def jpeg_data (self , buf ):
463
471
self ._send (buf )
@@ -467,8 +475,8 @@ def jpeg(self, buf):
467
475
self .jpeg_data (buf )
468
476
469
477
def feed_wdt (self ):
470
- self ._send (b' \x02 \x17 ' )
478
+ self ._send (b" \x02 \x17 " )
471
479
472
480
def reset (self ):
473
- self ._send (b' \x02 Y\xef \xbe \xad \xde ' )
481
+ self ._send (b" \x02 Y\xef \xbe \xad \xde " )
474
482
sleep_ms (15 )
0 commit comments