framebuf2.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. try:
  2. import framebuf
  3. except ImportError:
  4. print("SKIP")
  5. raise SystemExit
  6. def printbuf():
  7. print("--8<--")
  8. for y in range(h):
  9. for x in range(w):
  10. print('%u' % ((buf[(x + y * w) // 4] >> ((x & 3) << 1)) & 3), end='')
  11. print()
  12. print("-->8--")
  13. w = 8
  14. h = 5
  15. buf = bytearray(w * h // 4)
  16. fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.GS2_HMSB)
  17. # fill
  18. fbuf.fill(3)
  19. printbuf()
  20. fbuf.fill(0)
  21. printbuf()
  22. # put pixel
  23. fbuf.pixel(0, 0, 1)
  24. fbuf.pixel(3, 0, 2)
  25. fbuf.pixel(0, 4, 3)
  26. fbuf.pixel(3, 4, 2)
  27. printbuf()
  28. # get pixel
  29. print(fbuf.pixel(0, 4), fbuf.pixel(1, 1))
  30. # scroll
  31. fbuf.fill(0)
  32. fbuf.pixel(2, 2, 3)
  33. printbuf()
  34. fbuf.scroll(0, 1)
  35. printbuf()
  36. fbuf.scroll(1, 0)
  37. printbuf()
  38. fbuf.scroll(-1, -2)
  39. printbuf()
  40. w2 = 2
  41. h2 = 3
  42. buf2 = bytearray(w2 * h2 // 4)
  43. fbuf2 = framebuf.FrameBuffer(buf2, w2, h2, framebuf.GS2_HMSB)
  44. # blit
  45. fbuf2.fill(0)
  46. fbuf2.pixel(0, 0, 1)
  47. fbuf2.pixel(0, 2, 2)
  48. fbuf2.pixel(1, 0, 1)
  49. fbuf2.pixel(1, 2, 2)
  50. fbuf.fill(3)
  51. fbuf.blit(fbuf2, 3, 3, 0)
  52. fbuf.blit(fbuf2, -1, -1, 0)
  53. fbuf.blit(fbuf2, 16, 16, 0)
  54. printbuf()