sevensegment.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*- coding: utf-8-*-
  2. #
  3. # This file is part of stdd, the simple time display daemon,
  4. # written by Helmut Pozimski <helmut@pozimski.eu>,
  5. # licensed under the 3-clause BSD license
  6. # This file was originally written by Written by Limor Fried, Kevin Townsend
  7. # and Mikey Sklar for Adafruit Industries. BSD license,
  8. # all text above must be included in any redistribution
  9. """
  10. ===========================================================================
  11. 7-Segment Display
  12. ===========================================================================
  13. This class is meant to be used with the four-character, seven segment
  14. displays available from Adafruit.
  15. """
  16. from __future__ import print_function
  17. from adafruit_7segment.ledbackpack import LEDBackpack
  18. class SevenSegment(object):
  19. """This class represents the seven segment display and allows to access and
  20. write data to it.
  21. """
  22. disp = None
  23. # Hexadecimal character lookup table (row 1 = 0..9, row 2 = A..F)
  24. digits = [0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,
  25. 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71]
  26. # Constructor
  27. def __init__(self, address=0x70, debug=False):
  28. if debug:
  29. print("Initializing a new instance of LEDBackpack at \
  30. 0x%02X" % address)
  31. self.disp = LEDBackpack(address=address, debug=debug)
  32. def write_digit_raw(self, char_number, value):
  33. """Sets a digit using the raw 16-bit value"""
  34. if char_number > 7:
  35. return
  36. # Set the appropriate digit
  37. self.disp.set_buffer_row(char_number, value)
  38. def write_digit(self, char_number, value, dot=False):
  39. """Sets a single decimal or hexademical value (0..9 and A..F)"""
  40. if char_number > 7:
  41. return
  42. if value > 0xF:
  43. return
  44. # Set the appropriate digit
  45. self.disp.set_buffer_row(char_number, self.digits[value] | (dot << 7))
  46. def set_colon(self, state=True):
  47. """Enables or disables the colon character"""
  48. # Warning: This function assumes that the colon is character '2',
  49. # which is the case on 4 char displays, but may need to be modified
  50. # if another display type is used
  51. if state:
  52. self.disp.set_buffer_row(2, 0xFFFF)
  53. else:
  54. self.disp.set_buffer_row(2, 0)
  55. def set_brightness(self, brightness):
  56. """ Sets the brightness. """
  57. self.disp.set_brightness(brightness)