Permalink
Browse files

first commit

  • Loading branch information...
1 parent 6ce7ddc commit b1364eb2b1302e3d981d538a48caad53ea8606ac K. Townsend committed Aug 24, 2012
@@ -0,0 +1,52 @@
+#!/usr/bin/python
+
+import time
+import datetime
+from Adafruit_LEDBackpack import LEDBackpack
+
+# ===========================================================================
+# 7-Segment Display
+# ===========================================================================
+
+# This class is meant to be used with the four-character, seven segment
+# displays available from Adafruit
+
+class SevenSegment:
+ disp = None
+
+ # Hexadecimal character lookup table (row 1 = 0..9, row 2 = A..F)
+ digits = [ 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, \
+ 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 ]
+
+ # Constructor
+ def __init__(self, address=0x70, debug=False):
+ if (debug):
+ print "Initializing a new instance of LEDBackpack at 0x%02X" % address
+ self.disp = LEDBackpack(address=address, debug=debug)
+
+ def writeDigitRaw(self, charNumber, value):
+ "Sets a digit using the raw 16-bit value"
+ if (charNumber > 7):
+ return
+ # Set the appropriate digit
+ self.disp.setBufferRow(charNumber, value)
+
+ def writeDigit(self, charNumber, value, dot=False):
+ "Sets a single decimal or hexademical value (0..9 and A..F)"
+ if (charNumber > 7):
+ return
+ if (value > 0xF):
+ return
+ # Set the appropriate digit
+ self.disp.setBufferRow(charNumber, self.digits[value] | (dot << 7))
+
+ def setColon(self, state=True):
+ "Enables or disables the colon character"
+ # Warning: This function assumes that the colon is character '2',
+ # which is the case on 4 char displays, but may need to be modified
+ # if another display type is used
+ if (state):
+ self.disp.setBufferRow(2, 0xFFFF)
+ else:
+ self.disp.setBufferRow(2, 0)
+
@@ -0,0 +1,49 @@
+#!/usr/bin/python
+
+import time
+import datetime
+from Adafruit_LEDBackpack import LEDBackpack
+
+# ===========================================================================
+# 8x8 Pixel Display
+# ===========================================================================
+
+class EightByEight:
+ disp = None
+
+ # Constructor
+ def __init__(self, address=0x70, debug=False):
+ if (debug):
+ print "Initializing a new instance of LEDBackpack at 0x%02X" % address
+ self.disp = LEDBackpack(address=address, debug=debug)
+
+ def writeRowRaw(self, charNumber, value):
+ "Sets a row of pixels using a raw 16-bit value"
+ if (charNumber > 7):
+ return
+ # Set the appropriate row
+ self.disp.setBufferRow(charNumber, value)
+
+ def clearPixel(self, x, y):
+ "A wrapper function to clear pixels (purely cosmetic)"
+ self.setPixel(x, y, 0)
+
+ def setPixel(self, x, y, color=1):
+ "Sets a single pixel"
+ if (x >= 8):
+ return
+ if (y >= 8):
+ return
+ x += 7
+ x %= 8
+ # Set the appropriate pixel
+ buffer = self.disp.getBuffer()
+ if (color):
+ self.disp.setBufferRow(y, buffer[y] | 1 << x)
+ else:
+ self.disp.setBufferRow(y, buffer[y] & ~(1 << x))
+
+ def clear(self):
+ "Clears the entire display"
+ self.disp.clear()
+
@@ -0,0 +1,87 @@
+#!/usr/bin/python
+
+import time
+from copy import copy
+from Adafruit_I2C import Adafruit_I2C
+
+# ============================================================================
+# LEDBackpack Class
+# ============================================================================
+
+class LEDBackpack:
+ i2c = None
+
+ # Registers
+ __HT16K33_REGISTER_DISPLAY_SETUP = 0x80
+ __HT16K33_REGISTER_SYSTEM_SETUP = 0x20
+ __HT16K33_REGISTER_DIMMING = 0xE0
+
+ # Blink rate
+ __HT16K33_BLINKRATE_OFF = 0x00
+ __HT16K33_BLINKRATE_2HZ = 0x01
+ __HT16K33_BLINKRATE_1HZ = 0x02
+ __HT16K33_BLINKRATE_HALFHZ = 0x03
+
+ # Display buffer (8x16-bits)
+ __buffer = [0x0000, 0x0000, 0x0000, 0x0000, \
+ 0x0000, 0x0000, 0x0000, 0x0000 ]
+
+ # Constructor
+ def __init__(self, address=0x70, debug=False):
+ self.i2c = Adafruit_I2C(address)
+ self.address = address
+ self.debug = debug
+
+ # Turn the oscillator on
+ self.i2c.write8(self.__HT16K33_REGISTER_SYSTEM_SETUP | 0x01, 0x00)
+
+ # Turn blink off
+ self.setBlinkRate(self.__HT16K33_BLINKRATE_OFF)
+
+ # Set maximum brightness
+ self.setBrightness(15)
+
+ # Clear the screen
+ self.clear()
+
+ def setBrightness(self, brightness):
+ "Sets the brightness level from 0..15"
+ if (brightness > 15):
+ brightness = 15
+ self.i2c.write8(self.__HT16K33_REGISTER_DIMMING | brightness, 0x00)
+
+ def setBlinkRate(self, blinkRate):
+ "Sets the blink rate"
+ if (blinkRate > self.__HT16K33_BLINKRATE_HALFHZ):
+ blinkRate = self.__HT16K33_BLINKRATE_OFF
+ self.i2c.write8(self.__HT16K33_REGISTER_DISPLAY_SETUP | 0x01 | (blinkRate << 1), 0x00)
+
+ def setBufferRow(self, row, value, update=True):
+ "Updates a single 16-bit entry in the 8*16-bit buffer"
+ if (row > 7):
+ return # Prevent buffer overflow
+ self.__buffer[row] = value # value # & 0xFFFF
+ if (update):
+ self.writeDisplay() # Update the display
+
+ def getBuffer(self):
+ "Returns a copy of the raw buffer contents"
+ bufferCopy = copy(self.__buffer)
+ return bufferCopy
+
+ def writeDisplay(self):
+ "Updates the display memory"
+ bytes = []
+ for item in self.__buffer:
+ bytes.append(item & 0xFF)
+ bytes.append((item >> 8) & 0xFF)
+ self.i2c.writeList(0x00, bytes)
+
+ def clear(self, update=True):
+ "Clears the display memory"
+ self.__buffer = [ 0, 0, 0, 0, 0, 0, 0, 0 ]
+ if (update):
+ self.writeDisplay()
+
+led = LEDBackpack(0x70)
+
@@ -0,0 +1,29 @@
+#!/usr/bin/python
+
+import time
+import datetime
+from Adafruit_7Segment import SevenSegment
+
+# ===========================================================================
+# Clock Example
+# ===========================================================================
+segment = SevenSegment(address=0x70)
+
+print "Press CTRL+Z to exit"
+
+# Continually update the time on a 4 char, 7-segment display
+while(True):
+ now = datetime.datetime.now()
+ hour = now.hour
+ minute = now.minute
+ second = now.second
+ # Set hours
+ segment.writeDigit(0, int(hour / 10)) # Tens
+ segment.writeDigit(1, hour % 10) # Ones
+ # Set minutes
+ segment.writeDigit(3, int(minute / 10)) # Tens
+ segment.writeDigit(4, minute % 10) # Ones
+ # Toggle color
+ segment.setColon(second % 2) # Toggle colon at 1Hz
+ # Wait one second
+ time.sleep(1)
@@ -0,0 +1,22 @@
+#!/usr/bin/python
+
+import time
+import datetime
+from Adafruit_8x8 import EightByEight
+
+# ===========================================================================
+# 8x8 Pixel Example
+# ===========================================================================
+grid = EightByEight(address=0x70)
+
+print "Press CTRL+Z to exit"
+
+# Continually update the 8x8 display one pixel at a time
+while(True):
+ for x in range(0, 8):
+ for y in range(0, 8):
+ grid.setPixel(x, y)
+ time.sleep(0.05)
+ time.sleep(0.5)
+ grid.clear()
+ time.sleep(0.5)

0 comments on commit b1364eb

Please sign in to comment.