diff --git a/Adafruit_LEDBackpack/Adafruit_Bargraph.py b/Adafruit_LEDBackpack/Adafruit_Bargraph.py new file mode 100644 index 00000000..804991fa --- /dev/null +++ b/Adafruit_LEDBackpack/Adafruit_Bargraph.py @@ -0,0 +1,55 @@ +#!/usr/bin/python + +import time +import datetime +from Adafruit_LEDBackpack import LEDBackpack + +# =========================================================================== +# Bargraph Display +# =========================================================================== + +# This class is meant to be used with the 24-LED bicolor bargraph +# displays available from Adafruit + +class Bargraph: + disp = None + + LED_OFF = 0 + LED_RED = 1 + LED_GREEN = 2 + LED_YELLOW = 3 + + # Constructor + def __init__(self, address=0x70, debug=False): + self.debug = debug + + if self.debug: + print "Initializing a new instance of LEDBackpack at 0x%02X" % address + self.disp = LEDBackpack(address=address, debug=debug) + + def setLed(self, bar, color): + if bar > 24: + return + if color > 3: + return + + if bar < 12: + c = bar / 4 + else: + c = (bar - 12) / 4 + + a = bar % 4; + if bar >= 12: + a += 4; + + if self.debug: + print "Ano = %d Cath %d" % (a, c) + + bufRow = self.disp.getBufferRow(c) & ~((1 << a) | (1 << (a+8))) # turn off the LED + + if color == self.LED_RED: + self.disp.setBufferRow(c, bufRow | (1 << a)) + elif color == self.LED_YELLOW: + self.disp.setBufferRow(c, bufRow | (1 << a) | (1 << (a+8))) + elif color == self.LED_GREEN: + self.disp.setBufferRow(c, bufRow | 1 << (a+8)) diff --git a/Adafruit_LEDBackpack/Adafruit_LEDBackpack.py b/Adafruit_LEDBackpack/Adafruit_LEDBackpack.py index a0c58295..b2a20065 100644 --- a/Adafruit_LEDBackpack/Adafruit_LEDBackpack.py +++ b/Adafruit_LEDBackpack/Adafruit_LEDBackpack.py @@ -64,6 +64,12 @@ def setBufferRow(self, row, value, update=True): if (update): self.writeDisplay() # Update the display + def getBufferRow(self, row): + "Returns a single 16-bit entry in the 8*16-bit buffer" + if (row > 7): + return + return self.__buffer[row] + def getBuffer(self): "Returns a copy of the raw buffer contents" bufferCopy = copy(self.__buffer) diff --git a/Adafruit_LEDBackpack/ex_bargraph.py b/Adafruit_LEDBackpack/ex_bargraph.py new file mode 100644 index 00000000..1fa0a31e --- /dev/null +++ b/Adafruit_LEDBackpack/ex_bargraph.py @@ -0,0 +1,19 @@ +#!/usr/bin/python + +import time +import datetime +from Adafruit_Bargraph import Bargraph + +# =========================================================================== +# Scroll through colors example +# =========================================================================== +bargraph = Bargraph(address=0x70) + +print "Press CTRL+C to exit" + +while(True): + for color in range(1, 4): + for i in range(24): + print i + bargraph.setLed(i, color) + time.sleep(0.05)