Adafruit_7Segment.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. import time
  10. import datetime
  11. from Adafruit_LEDBackpack import LEDBackpack
  12. # ===========================================================================
  13. # 7-Segment Display
  14. # ===========================================================================
  15. # This class is meant to be used with the four-character, seven segment
  16. # displays available from Adafruit
  17. class SevenSegment:
  18. disp = None
  19. # Hexadecimal character lookup table (row 1 = 0..9, row 2 = A..F)
  20. digits = [0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F,
  21. 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71]
  22. # Constructor
  23. def __init__(self, address=0x70, debug=False):
  24. if (debug):
  25. print "Initializing a new instance of LEDBackpack at \
  26. 0x%02X" % address
  27. self.disp = LEDBackpack(address=address, debug=debug)
  28. def writeDigitRaw(self, charNumber, value):
  29. "Sets a digit using the raw 16-bit value"
  30. if (charNumber > 7):
  31. return
  32. # Set the appropriate digit
  33. self.disp.setBufferRow(charNumber, value)
  34. def writeDigit(self, charNumber, value, dot=False):
  35. "Sets a single decimal or hexademical value (0..9 and A..F)"
  36. if (charNumber > 7):
  37. return
  38. if (value > 0xF):
  39. return
  40. # Set the appropriate digit
  41. self.disp.setBufferRow(charNumber, self.digits[value] | (dot << 7))
  42. def setColon(self, state=True):
  43. "Enables or disables the colon character"
  44. # Warning: This function assumes that the colon is character '2',
  45. # which is the case on 4 char displays, but may need to be modified
  46. # if another display type is used
  47. if (state):
  48. self.disp.setBufferRow(2, 0xFFFF)
  49. else:
  50. self.disp.setBufferRow(2, 0)
  51. def setBrightness(self, brightness):
  52. self.disp.setBrightness(brightness)