Skip to content

Instantly share code, notes, and snippets.

@boochow
Created November 11, 2018 04:41
Show Gist options
  • Save boochow/2488e73d1e1ffb2f9d6cd92052e40ed9 to your computer and use it in GitHub Desktop.
Save boochow/2488e73d1e1ffb2f9d6cd92052e40ed9 to your computer and use it in GitHub Desktop.
import framebuf
import uio
HEX="0123456789ABCDEF"
class DumpConsole(uio.IOBase):
def __init__(self, fb, bgcolor=0, fgcolor=-1, width=-1, height=-1, readobj=None):
self.readobj = readobj
self.fb = fb
if width > 0:
self.width=width
else:
try:
self.width=fb.width
except:
raise ValueError
if height > 0:
self.height=height
else:
try:
self.height=fb.height
except:
raise ValueError
self.bgcolor = bgcolor
self.fgcolor = fgcolor
self.line_height(8)
def cls(self):
self.x = 0
self.y = 0
self.fb.fill(self.bgcolor)
try:
self.fb.show()
except:
pass
def line_height(self, px):
self.lineheight = px
self.w = self.width // px
self.h = self.height // px
self.cls()
def _putc(self, c):
if c >= ' ':
self.fb.text(c, self.x * 8, self.y * self.lineheight, self.fgcolor)
self.x += 1
if self.x >= self.w:
self._newline()
def write(self, buf):
self._erase_current()
i = 0
while i < len(buf):
c = buf[i]
self._putc(HEX[c>>4])
self._putc(HEX[c&0xf])
self._putc(' ')
i += 1
self._newline()
try:
self.fb.show()
except:
pass
return len(buf)
def readinto(self, buf, nbytes=0):
if self.readobj != None:
return self.readobj.readinto(buf, nbytes)
else:
return None
def _newline(self):
self.x = 0
self.y += 1
if self.y >= self.h:
self.fb.scroll(0, -8)
self.fb.fill_rect(0, self.height - self.lineheight, self.width, self.lineheight, self.bgcolor)
self.y = self.h - 1
def _erase_current(self):
self.fb.fill_rect(self.x * 8, self.y * self.lineheight, 8, self.lineheight, self.bgcolor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment