@@ -257,6 +257,7 @@ def get_pixel(self, x, y):
257257 def get_line (self , x , y , buf ):
258258 l = len (buf ) // 2
259259 self ._fcmd2b ('<BBBBB' , 0x10 , l , x , y )
260+ l *= 2
260261 t = 1000
261262 while t :
262263 self .i2c .readfrom_into (self .i2c_addr , self .buf1 )
@@ -267,21 +268,28 @@ def get_line(self, x, y, buf):
267268 sleep_ms (1 )
268269 raise OSError (uerrno .ETIMEDOUT )
269270
270- def screen_dump (self , buf ):
271- line = bytearray (self .w + 1 )
272- h = len (buf ) // (2 * self .w )
273- if h > self .h :
274- h = self .h
275- for i in range (h ):
276- ix = i * self .w * 2
277- self .get_line (0 , i , line )
278- for j in range (1 , len (line )):
279- buf [ix ] = line [j ]
280- ix += 1
281- self .get_line (self .w // 2 , i , line )
282- for j in range (1 , len (line )):
283- buf [ix ] = line [j ]
284- ix += 1
271+ def screen_dump (self , buf , x = 0 , y = 0 , w = None , h = None ):
272+ if w is None :
273+ w = self .w - x
274+ if h is None :
275+ h = self .h - y
276+ if w <= 127 :
277+ line = bytearray (2 * w + 1 )
278+ line2 = None
279+ else :
280+ # split line if more than 254 bytes needed
281+ buflen = (w + 1 ) // 2
282+ line = bytearray (2 * buflen + 1 )
283+ line2 = memoryview (line )[:2 * (w - buflen ) + 1 ]
284+ for i in range (min (len (buf ) // (2 * w ), h )):
285+ ix = i * w * 2
286+ self .get_line (x , y + i , line )
287+ buf [ix :ix + len (line ) - 1 ] = memoryview (line )[1 :]
288+ ix += len (line ) - 1
289+ if line2 :
290+ self .get_line (x + buflen , y + i , line2 )
291+ buf [ix :ix + len (line2 ) - 1 ] = memoryview (line2 )[1 :]
292+ ix += len (line2 ) - 1
285293
286294 def screen_load (self , buf ):
287295 l = self .w * self .h * 2 + 2
0 commit comments