matrix_demo.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (c) 2017-18 Richard Hull and contributors
  4. # See LICENSE.rst for details.
  5. import re
  6. import time
  7. import argparse
  8. from luma.led_matrix.device import max7219
  9. from luma.core.interface.serial import spi, noop
  10. from luma.core.render import canvas
  11. from luma.core.virtual import viewport
  12. from luma.core.legacy import text, show_message
  13. from luma.core.legacy.font import proportional, CP437_FONT, TINY_FONT, SINCLAIR_FONT, LCD_FONT
  14. def demo(n, block_orientation, rotate, inreverse):
  15. # create matrix device
  16. serial = spi(port=0, device=0, gpio=noop())
  17. device = max7219(serial, cascaded=n or 1, block_orientation=block_orientation,
  18. rotate=rotate or 0, blocks_arranged_in_reverse_order=inreverse)
  19. print("Created device")
  20. # start demo
  21. msg = "MAX7219 LED Matrix Demo"
  22. print(msg)
  23. show_message(device, msg, fill="white", font=proportional(CP437_FONT))
  24. time.sleep(1)
  25. msg = "Fast scrolling: Lorem ipsum dolor sit amet, consectetur adipiscing\
  26. elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut\
  27. enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut\
  28. aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in\
  29. voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint\
  30. occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit\
  31. anim id est laborum."
  32. msg = re.sub(" +", " ", msg)
  33. print(msg)
  34. show_message(device, msg, fill="white", font=proportional(LCD_FONT), scroll_delay=0)
  35. msg = "Slow scrolling: The quick brown fox jumps over the lazy dog"
  36. print(msg)
  37. show_message(device, msg, fill="white", font=proportional(LCD_FONT), scroll_delay=0.1)
  38. print("Vertical scrolling")
  39. words = [
  40. "Victor", "Echo", "Romeo", "Tango", "India", "Charlie", "Alpha",
  41. "Lima", " ", "Sierra", "Charlie", "Romeo", "Oscar", "Lima", "Lima",
  42. "India", "November", "Golf", " "
  43. ]
  44. virtual = viewport(device, width=device.width, height=len(words) * 8)
  45. with canvas(virtual) as draw:
  46. for i, word in enumerate(words):
  47. text(draw, (0, i * 8), word, fill="white", font=proportional(CP437_FONT))
  48. for i in range(virtual.height - device.height):
  49. virtual.set_position((0, i))
  50. time.sleep(0.05)
  51. msg = "Brightness"
  52. print(msg)
  53. show_message(device, msg, fill="white")
  54. time.sleep(1)
  55. with canvas(device) as draw:
  56. text(draw, (0, 0), "A", fill="white")
  57. time.sleep(1)
  58. for _ in range(5):
  59. for intensity in range(16):
  60. device.contrast(intensity * 16)
  61. time.sleep(0.1)
  62. device.contrast(0x80)
  63. time.sleep(1)
  64. msg = "Alternative font!"
  65. print(msg)
  66. show_message(device, msg, fill="white", font=SINCLAIR_FONT)
  67. time.sleep(1)
  68. msg = "Proportional font - characters are squeezed together!"
  69. print(msg)
  70. show_message(device, msg, fill="white", font=proportional(SINCLAIR_FONT))
  71. # http://www.squaregear.net/fonts/tiny.shtml
  72. time.sleep(1)
  73. msg = "Tiny is, I believe, the smallest possible font \
  74. (in pixel size). It stands at a lofty four pixels \
  75. tall (five if you count descenders), yet it still \
  76. contains all the printable ASCII characters."
  77. msg = re.sub(" +", " ", msg)
  78. print(msg)
  79. show_message(device, msg, fill="white", font=proportional(TINY_FONT))
  80. time.sleep(1)
  81. msg = "CP437 Characters"
  82. print(msg)
  83. show_message(device, msg)
  84. time.sleep(1)
  85. for x in range(256):
  86. with canvas(device) as draw:
  87. text(draw, (0, 0), chr(x), fill="white")
  88. time.sleep(0.1)
  89. if __name__ == "__main__":
  90. parser = argparse.ArgumentParser(description='matrix_demo arguments',
  91. formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  92. parser.add_argument('--cascaded', '-n', type=int, default=1, help='Number of cascaded MAX7219 LED matrices')
  93. parser.add_argument('--block-orientation', type=int, default=0, choices=[0, 90, -90], help='Corrects block orientation when wired vertically')
  94. parser.add_argument('--rotate', type=int, default=0, choices=[0, 1, 2, 3], help='Rotate display 0=0°, 1=90°, 2=180°, 3=270°')
  95. parser.add_argument('--reverse-order', type=bool, default=False, help='Set to true if blocks are in reverse order')
  96. args = parser.parse_args()
  97. try:
  98. demo(args.cascaded, args.block_orientation, args.rotate, args.reverse_order)
  99. except KeyboardInterrupt:
  100. pass