Guest User

Untitled

a guest
Jun 24th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. class FBConsole:
  2. def __init__(self, framebuf, bgcolor=0, fgcolor=-1, width=-1, height=-1):
  3. self.fb = framebuf
  4. if width > 0:
  5. self.width=width
  6. else:
  7. try:
  8. self.width=framebuf.width
  9. except:
  10. raise ValueError
  11. if height > 0:
  12. self.height=height
  13. else:
  14. try:
  15. self.height=framebuf.height
  16. except:
  17. raise ValueError
  18. self.bgcolor = bgcolor
  19. self.fgcolor = fgcolor
  20. self.line_height(8)
  21.  
  22. def cls(self):
  23. self.x = 0
  24. self.y = 0
  25. self.fb.fill(self.bgcolor)
  26. try:
  27. self.fb.show()
  28. except:
  29. pass
  30.  
  31. def line_height(self, px):
  32. self.lineheight = px
  33. self.w = self.width // px
  34. self.h = self.height // px
  35. self.cls()
  36.  
  37. def _putc(self, c):
  38. c = chr(c)
  39. if c == '\n':
  40. self.x = 0
  41. self._newline()
  42. elif c == '\x08':
  43. self._backspace()
  44. elif c >= ' ':
  45. self.fb.text(c, self.x * 8, self.y * 8, self.fgcolor)
  46. self.x += 1
  47. if self.x >= self.w:
  48. self._newline()
  49. self.x = 0
  50.  
  51. def write(self, buf):
  52. i = 0
  53. while i < len(buf):
  54. c = buf[i]
  55. if c == 0x1b: # skip escape sequence
  56. i += 1
  57. while chr(buf[i]) in '[;0123456789':
  58. i += 1
  59. else:
  60. self._putc(c)
  61. i += 1
  62. try:
  63. self.fb.show()
  64. except:
  65. pass
  66.  
  67. def readinto(self, buf, nbytes=0):
  68. return None
  69.  
  70. def _newline(self):
  71. self.y += 1
  72. if self.y >= self.h:
  73. self.fb.scroll(0, -8)
  74. self.fb.fill_rect(0, self.height - 8, self.width, 8, 0)
  75. self.y = self.h - 1
  76.  
  77. def _erase_current(self):
  78. self.fb.fill_rect(self.x * 8, self.y * 8, 8, 8, 0)
  79.  
  80. def _backspace(self):
  81. if self.x == 0:
  82. if self.y > 0:
  83. self.y -= 1
  84. self.x = self.w - 1
  85. else:
  86. self.x -= 1
  87. self.fb.fill_rect(self.x * 8, self.y * 8, 8, 8, 0)
Add Comment
Please, Sign In to add comment