Adafruit_LEDBackpack.py 2.9 KB

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