Adafruit_LEDBackpack.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/python
  2. import time
  3. from copy import copy
  4. from Adafruit_I2C import Adafruit_I2C
  5. # ============================================================================
  6. # LEDBackpack Class
  7. # ============================================================================
  8. class LEDBackpack:
  9. i2c = None
  10. # Registers
  11. __HT16K33_REGISTER_DISPLAY_SETUP = 0x80
  12. __HT16K33_REGISTER_SYSTEM_SETUP = 0x20
  13. __HT16K33_REGISTER_DIMMING = 0xE0
  14. # Blink rate
  15. __HT16K33_BLINKRATE_OFF = 0x00
  16. __HT16K33_BLINKRATE_2HZ = 0x01
  17. __HT16K33_BLINKRATE_1HZ = 0x02
  18. __HT16K33_BLINKRATE_HALFHZ = 0x03
  19. # Display buffer (8x16-bits)
  20. __buffer = [0x0000, 0x0000, 0x0000, 0x0000, \
  21. 0x0000, 0x0000, 0x0000, 0x0000 ]
  22. # Constructor
  23. def __init__(self, address=0x70, debug=False):
  24. self.i2c = Adafruit_I2C(address)
  25. self.address = address
  26. self.debug = debug
  27. # Turn the oscillator on
  28. self.i2c.write8(self.__HT16K33_REGISTER_SYSTEM_SETUP | 0x01, 0x00)
  29. # Turn blink off
  30. self.setBlinkRate(self.__HT16K33_BLINKRATE_OFF)
  31. # Set maximum brightness
  32. self.setBrightness(15)
  33. # Clear the screen
  34. self.clear()
  35. def setBrightness(self, brightness):
  36. "Sets the brightness level from 0..15"
  37. if (brightness > 15):
  38. brightness = 15
  39. self.i2c.write8(self.__HT16K33_REGISTER_DIMMING | brightness, 0x00)
  40. def setBlinkRate(self, blinkRate):
  41. "Sets the blink rate"
  42. if (blinkRate > self.__HT16K33_BLINKRATE_HALFHZ):
  43. blinkRate = self.__HT16K33_BLINKRATE_OFF
  44. self.i2c.write8(self.__HT16K33_REGISTER_DISPLAY_SETUP | 0x01 | (blinkRate << 1), 0x00)
  45. def setBufferRow(self, row, value, update=True):
  46. "Updates a single 16-bit entry in the 8*16-bit buffer"
  47. if (row > 7):
  48. return # Prevent buffer overflow
  49. self.__buffer[row] = value # value # & 0xFFFF
  50. if (update):
  51. self.writeDisplay() # Update the display
  52. def getBuffer(self):
  53. "Returns a copy of the raw buffer contents"
  54. bufferCopy = copy(self.__buffer)
  55. return bufferCopy
  56. def writeDisplay(self):
  57. "Updates the display memory"
  58. bytes = []
  59. for item in self.__buffer:
  60. bytes.append(item & 0xFF)
  61. bytes.append((item >> 8) & 0xFF)
  62. self.i2c.writeList(0x00, bytes)
  63. def clear(self, update=True):
  64. "Clears the display memory"
  65. self.__buffer = [ 0, 0, 0, 0, 0, 0, 0, 0 ]
  66. if (update):
  67. self.writeDisplay()
  68. led = LEDBackpack(0x70)