sevensegment_demo.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. """
  6. Example for seven segment displays.
  7. """
  8. import time
  9. from datetime import datetime
  10. from luma.led_matrix.device import max7219
  11. from luma.core.interface.serial import spi, noop
  12. from luma.core.virtual import viewport, sevensegment
  13. def date(seg):
  14. """
  15. Display current date on device.
  16. """
  17. now = datetime.now()
  18. seg.text = now.strftime("%y-%m-%d")
  19. def clock(seg, seconds):
  20. """
  21. Display current time on device.
  22. """
  23. interval = 0.5
  24. for i in range(int(seconds / interval)):
  25. now = datetime.now()
  26. seg.text = now.strftime("%H-%M-%S")
  27. # calculate blinking dot
  28. if i % 2 == 0:
  29. seg.text = now.strftime("%H-%M-%S")
  30. else:
  31. seg.text = now.strftime("%H %M %S")
  32. time.sleep(interval)
  33. def show_message_vp(device, msg, delay=0.1):
  34. # Implemented with virtual viewport
  35. width = device.width
  36. padding = " " * width
  37. msg = padding + msg + padding
  38. n = len(msg)
  39. virtual = viewport(device, width=n, height=8)
  40. sevensegment(virtual).text = msg
  41. for i in reversed(list(range(n - width))):
  42. virtual.set_position((i, 0))
  43. time.sleep(delay)
  44. def show_message_alt(seg, msg, delay=0.1):
  45. # Does same as above but does string slicing itself
  46. width = seg.device.width
  47. padding = " " * width
  48. msg = padding + msg + padding
  49. for i in range(len(msg)):
  50. seg.text = msg[i:i + width]
  51. time.sleep(delay)
  52. def main():
  53. # create seven segment device
  54. serial = spi(port=0, device=0, gpio=noop())
  55. device = max7219(serial, cascaded=1)
  56. seg = sevensegment(device)
  57. print('Simple text...')
  58. for _ in range(8):
  59. seg.text = "HELLO"
  60. time.sleep(0.6)
  61. seg.text = " GOODBYE"
  62. time.sleep(0.6)
  63. # Digit slicing
  64. print("Digit slicing")
  65. seg.text = "_" * seg.device.width
  66. time.sleep(1.0)
  67. for i, ch in enumerate([9, 8, 7, 6, 5, 4, 3, 2]):
  68. seg.text[i] = str(ch)
  69. time.sleep(0.6)
  70. for i in range(len(seg.text)):
  71. del seg.text[0]
  72. time.sleep(0.6)
  73. # Scrolling Alphabet Text
  74. print('Scrolling alphabet text...')
  75. show_message_vp(device, "HELLO EVERYONE!")
  76. show_message_vp(device, "PI is 3.14159 ... ")
  77. show_message_vp(device, "IP is 127.0.0.1 ... ")
  78. show_message_alt(seg, "0123456789 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  79. # Digit futzing
  80. date(seg)
  81. time.sleep(5)
  82. clock(seg, seconds=10)
  83. # Brightness
  84. print('Brightness...')
  85. for x in range(5):
  86. for intensity in range(16):
  87. seg.device.contrast(intensity * 16)
  88. time.sleep(0.1)
  89. device.contrast(0x7F)
  90. if __name__ == '__main__':
  91. main()