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