ledbackpack.py 3.1 KB

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