Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. _NOOP = const(0)
  2. _DIGIT0 = const(1)
  3. _DIGIT1 = const(2)
  4. _DIGIT2 = const(3)
  5. _DIGIT3 = const(4)
  6. _DIGIT4 = const(5)
  7. _DIGIT5 = const(6)
  8. _DIGIT6 = const(7)
  9. _DIGIT7 = const(8)
  10. _DECODEMODE = const(9)
  11. _INTENSITY = const(10)
  12. _SCANLIMIT = const(11)
  13. _SHUTDOWN = const(12)
  14. _DISPLAYTEST = const(15)
  15.  
  16.  
  17. class Matrix8x8:
  18. def __init__(self, spi, cs):
  19. self.spi = spi
  20. self.cs = cs
  21. self.cs.init(cs.OUT, True)
  22. self.buffer = bytearray(8)
  23. self.init()
  24.  
  25. def _register(self, command, data):
  26. self.cs.off()
  27. self.spi.write(bytearray([command, data]))
  28. self.cs.on()
  29.  
  30. def init(self):
  31. for command, data in (
  32. (_SHUTDOWN, 0),
  33. (_DISPLAYTEST, 0),
  34. (_SCANLIMIT, 7),
  35. (_DECODEMODE, 0),
  36. (_SHUTDOWN, 1),
  37. ):
  38. self._register(command, data)
  39.  
  40. def brightness(self, value):
  41. if not 0<= value <= 15:
  42. raise ValueError("Brightness out of range")
  43. self._register(_INTENSITY, value)
  44.  
  45. def fill(self, color):
  46. data = 0xff if color else 0x00
  47. for y in range(8):
  48. self.buffer[y] = data
  49.  
  50. def pixel(self, x, y, color=None):
  51. if color is None:
  52. return bool(self.buffer[y] & 1 << x)
  53. elif color:
  54. self.buffer[y] |= 1 << x
  55. else:
  56. self.buffer[y] &= ~(1 << x)
  57.  
  58. def show(self):
  59. for y in range(8):
  60. self._register(_DIGIT0 + y, self.buffer[y])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement