Adafruit_7Segment.py 1.7 KB

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