class Display: 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 writeChar(self, charNumber, value): global DIGIT_VALUES self.setBufferRow(charNumber, DIGIT_VALUES[value]) def setBufferRow(self, charNumber, value): self.disp.setBufferRow(charNumber, value)
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 clear(self): self.disp.clear() def setBrightness(self, bright): self.setBrightness(bright) 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)
class SevenSegment: disp = None invert = False # 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 ] # The same, but upside-down idigits = [ 0x3F, 0x30, 0x5B, 0x79, 0x74, 0x6D, 0x6F, 0x38, 0x7F, 0x7D, \ 0x7E, 0x67, 0x0F, 0x73, 0x4F, 0x4E ] # 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 # Decide which digit set to use: check self.invert d = self.idigits[value] if self.invert else self.digits[value] # If inverted, also reverse the character positions c = (4-charNumber) if self.invert else charNumber # Set the appropriate digit self.disp.setBufferRow(c, d | (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 # This sets all non-digit LEDs. Change (2,0xFFFF) to (2,2) to just # set colon on the 1.2" 7-Segment display. if (state): self.disp.setBufferRow(2, 0xFFFF) else: self.disp.setBufferRow(2, 0)
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 getBuffer(self): return self.disp.getBuffer() 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 # ATTN: This might be a bug? On the color matrix, this causes x=0 to draw on the last line instead of the first. 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()
class EightByEight: disp = None # Constructor def __init__(self, address=0x70, bus=Adafruit_I2C.getPiI2CBusNumber(), debug=False): if (debug): print "Initializing a new instance of LEDBackpack at 0x%02X" % address self.disp = LEDBackpack(address=address, bus=bus, 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()
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))
def __init__(self, address=0x70, bus=Adafruit_I2C.getPiI2CBusNumber(), debug=False): if (debug): print "Initializing a new instance of LEDBackpack at 0x%02X" % address self.disp = LEDBackpack(address=address, bus=bus, debug=debug)
class SevenSegment: disp = None # Some 7Segment-Display have several different colons, e.g. the # 1,2" display. To seperately control such different colons # use the following values where applicable: # # 0x00 - nothing # 0x02 - center colon # 0x04 - left colon - upper dot # 0x08 - left colon - lower dot # 0x10 - decimal point # 0xFFFF - everything (default) mask_colons = 0xFFFF # Hexadecimal character lookup table (row 1 = 0..9, row 2 = A..F) DIGITS_NORMAL = [ 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, \ 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 ] # inverted table from https://github.com/tomgidden/Adafruit-Raspberry-Pi-Python-Code/commit/87bed18ecc8e251f3c10433d8a31d363dbbd355c # thx to tomgidden@github for this DIGITS_INVERTED = [ 0x3F, 0x30, 0x5B, 0x79, 0x74, 0x6D, 0x6F, 0x38, 0x7F, 0x7D, \ 0x7E, 0x67, 0x0F, 0x73, 0x4F, 0x4E ] digits = DIGITS_NORMAL display_inverted = False # 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 invertDisplay(self): if (self.display_inverted): # we were inverted, so switch back to normal self.digits = self.DIGITS_NORMAL self.display_inverted = False else: # we were displaying normally, so switch to inverted self.digits = self.DIGITS_INVERTED self.display_inverted = True def writeDigitRaw(self, charNumber, value): "Sets a digit using the raw 16-bit value" if (charNumber > 7): return if ((self.display_inverted) & (charNumber < 5)): "if inverted we need to use reverse character positioning" charNumber = 4 - charNumber # 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 if ((self.display_inverted) & (charNumber < 5)): "if inverted we need to use reverse character positioning" charNumber = 4 - charNumber # 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, self.mask_colons) else: self.disp.setBufferRow(2, 0)
__author__ = 'Justin' # =========================================================================== # Main clock program # =========================================================================== import time import datetime from Adafruit_7Segment import SevenSegment from Adafruit_LEDBackpack import LEDBackpack from clock_API import ClockAPI print "Press CTRL+Z to exit" segment = SevenSegment(address=0x70) backpack = LEDBackpack() data = ClockAPI() backpack.setBrightness(0) API_data = data.getWeatherCondition('seattle', 'F') temp = API_data[2] print datetime.datetime.now() print API_data print temp # Continually update the time on a 4 char, 7-segment display # while True:
def __init__(self, address=0x70, debug=False): self.disp = LEDBackpack(address=address, debug=debug)
class EightByEight: disp = None rotation = 0 # 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 getRotation(self): return self.rotation def setRotation(self, rot): if rot >= 0 and rot <= 3: self.rotation = rot 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" # Check for invalid coordinates if (x < 0) or (x >= 8) or (y < 0) or (y >= 8): return # Check rotation, move pixel around if necessary if self.rotation == 1: x, y = y, x x = 8 - x - 1; elif self.rotation == 2: x = 8 - x - 1 y = 8 - y - 1 elif self.rotation == 3: x, y = y, x y = 8 - y - 1 # x += 7 # ATTN: This might be a bug? On the color matrix, this causes x=0 to draw on the last line instead of the first. x %= 8 # This modification fixes the pixel addressing error if (x == 0): x = 7 elif (x > 0): x = x - 1 # End of modification for the pixel addressing error # 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()
# function to return a datastream object. This either creates a new datastream, # or returns an existing one def get_datastream(feed): try: datastream = feed.datastreams.get("external_temp") return datastream except: datastream = feed.datastreams.create("external_temp", tags="temp_01") return datastream # =========================================================================== # Clock Example # =========================================================================== led=LEDBackpack(address=0x70) segment = SevenSegment(address=0x70) try: interval=int(sys.argv[1]) except: print 'Interval required' sys.exit(1) daybrightness=15 nightbrightness=5 minbrightness=1 maxbrightness=15
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)
class Alphanumeric(object): disp = None # Hexadecimal character lookup table lut = [ 0b0000000000000001, 0b0000000000000010, 0b0000000000000100, 0b0000000000001000, 0b0000000000010000, 0b0000000000100000, 0b0000000001000000, 0b0000000010000000, 0b0000000100000000, 0b0000001000000000, 0b0000010000000000, 0b0000100000000000, 0b0001000000000000, 0b0010000000000000, 0b0100000000000000, 0b1000000000000000, 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, 0b0001001011001001, 0b0001010111000000, 0b0001001011111001, 0b0000000011100011, 0b0000010100110000, 0b0001001011001000, 0b0011101000000000, 0b0001011100000000, 0b0000000000000000, # 0b0000000000000110, # ! 0b0000001000100000, # " 0b0001001011001110, # # 0b0001001011101101, # $ 0b0000110000100100, # % 0b0010001101011101, # & 0b0000010000000000, # ' 0b0010010000000000, # ( 0b0000100100000000, # ) 0b0011111111000000, # * 0b0001001011000000, # + 0b0000100000000000, # , 0b0000000011000000, # - 0b0000000000000000, # . 0b0000110000000000, # / 0b0000110000111111, # 0 0b0000000000000110, # 1 0b0000000011011011, # 2 0b0000000010001111, # 3 0b0000000011100110, # 4 0b0010000001101001, # 5 0b0000000011111101, # 6 0b0000000000000111, # 7 0b0000000011111111, # 8 0b0000000011101111, # 9 0b0001001000000000, # : 0b0000101000000000, # ; 0b0010010000000000, # < 0b0000000011001000, # = 0b0000100100000000, # > 0b0001000010000011, # ? 0b0000001010111011, # @ 0b0000000011110111, # A 0b0001001010001111, # B 0b0000000000111001, # C 0b0001001000001111, # D 0b0000000011111001, # E 0b0000000001110001, # F 0b0000000010111101, # G 0b0000000011110110, # H 0b0001001000000000, # I 0b0000000000011110, # J 0b0010010001110000, # K 0b0000000000111000, # L 0b0000010100110110, # M 0b0010000100110110, # N 0b0000000000111111, # O 0b0000000011110011, # P 0b0010000000111111, # Q 0b0010000011110011, # R 0b0000000011101101, # S 0b0001001000000001, # T 0b0000000000111110, # U 0b0000110000110000, # V 0b0010100000110110, # W 0b0010110100000000, # X 0b0001010100000000, # Y 0b0000110000001001, # Z 0b0000000000111001, # [ 0b0010000100000000, # 0b0000000000001111, # ] 0b0000110000000011, # ^ 0b0000000000001000, # _ 0b0000000100000000, # ` 0b0001000001011000, # a 0b0010000001111000, # b 0b0000000011011000, # c 0b0000100010001110, # d 0b0000100001011000, # e 0b0000000001110001, # f 0b0000010010001110, # g 0b0001000001110000, # h 0b0001000000000000, # i 0b0000000000001110, # j 0b0011011000000000, # k 0b0000000000110000, # l 0b0001000011010100, # m 0b0001000001010000, # n 0b0000000011011100, # o 0b0000000101110000, # p 0b0000010010000110, # q 0b0000000001010000, # r 0b0010000010001000, # s 0b0000000001111000, # t 0b0000000000011100, # u 0b0010000000000100, # v 0b0010100000010100, # w 0b0010100011000000, # x 0b0010000000001100, # y 0b0000100001001000, # z 0b0000100101001001, # { 0b0001001000000000, # | 0b0010010010001001, # } 0b0000010100100000, # ~ 0b0011111111111111 ] special = dict(degree=0b0000000011100011, cone=0b0010100000001000) def __init__(self, address=0x70, debug=False): self.disp = LEDBackpack(address=address, debug=debug) def writeCharRaw(self, charNumber, value): "Sets a digit using the raw 16-bit value" if (charNumber > 4): return # Set the appropriate digit self.disp.setBufferRow(charNumber, value) def writeChar(self, charNumber, value, dot=False): "Sets a single decimal or hexademical value (0..9 and A..F)" if (charNumber > 4): return if value in self.special: value = self.special[value] else: value = self.lut[ord(value)] # Set the appropriate digit self.disp.setBufferRow(charNumber, value | (dot << 14))
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=0x72, debug=False): if (debug): print "Initializing a new instance of LEDBackpack at 0x%02X" % address self.disp = LEDBackpack(address=address, debug=debug) # self.disp.setBrightness(1) 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) def setBrightness(self, brightness=15): # print "setBrightness called", brightness "Sets the display brightness" if (self.is_number(brightness) and brightness >= 0 and brightness <= 15): # print "Setting brightness to", brightness self.disp.setBrightness(brightness) def clear(self, update=True): self.disp.clear(update) def is_number(self, s): try: float(s) return True except ValueError: return False
#!/usr/bin/python import time import datetime from Adafruit_7Segment import SevenSegment from Adafruit_LEDBackpack import LEDBackpack backpack = LEDBackpack(address=0x74) backpack.setBrightness(15) # =========================================================================== # Clock Example # =========================================================================== segment = SevenSegment(address=0x74) # 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 colon segment.setColon(second % 2) # Toggle colon at 1Hz # Wait one second
#!/usr/bin/env python import time import datetime from Adafruit_7Segment import SevenSegment from Adafruit_LEDBackpack import LEDBackpack import signal import sys segment = SevenSegment(address=0x70) seven = LEDBackpack(address=0x70) # Kinda pack-rat-ish.. these are crumbs I'd like to implement seven.setBrightness(15) def exit_gracefully(signum, frame): # let's restore the original signal handlers signal.signal(signal.SIGTERM, original_sigterm) signal.signal(signal.SIGINT, original_sigint) signal.signal(signal.SIGHUP, original_sighup) # clean up gracefully here. bail when done. seven.clear() sys.exit(0) #just in case we do something during cleanup that means we *shouldn't" exit, we want our handler to stay intact. signal.signal(signal.SIGTERM, exit_gracefully) signal.signal(signal.SIGINT, exit_gracefully) signal.signal(signal.SIGHUP, exit_gracefully)
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) # Enabling/disabling small dots def writeDot(self, charNumber, dot): if charNumber > 4: return current = self.disp.getBufferRow(charNumber) #if dot: self.disp.setBufferRow(charNumber, current | (dot << 7))
def __init__(self, address = 0x70, debug=False): """ Constructor. """ if (debug): print "Initializing a new instance of LEDBackpack at 0x%02X" % address self.disp = LEDBackpack(address=address, debug=debug)
# Blink rate __HT16K33_BLINKRATE_OFF = 0x00 __HT16K33_BLINKRATE_2HZ = 0x01 __HT16K33_BLINKRATE_1HZ = 0x02 __HT16K33_BLINKRATE_HALFHZ = 0x03 #Colors __HT16K33_OFF = 0 __HT16K33_GREEN = 1 __HT16K33_RED = 2 __HT16K33_YELLOW = 3 # setup backpack grid = ColorEightByEight(address=0x72) backpack = LEDBackpack(address=0x72) # command from RasPiConnect Execution Code def completeCommand(): f = open("/home/pi/MouseAir/state/MouseCommand.txt", "w") f.write("DONE") f.close() def processCommand(): f = open("/home/pi/MouseAir/state/MouseCommand.txt", "r") command = f.read()
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) def writeInt(self, value): """ Write The 'Value' to the display """ if value > 9999: #Maxed OUT! value = 9999 else: value = int(value) self.writeDigit(0, (value / 1000) % 10) self.writeDigit(1, (value / 100) % 10) self.writeDigit(3, (value / 10) % 10) self.writeDigit(4, value % 10) def writeNum(self, value): if value > 999: self.writeInt(value) elif value > 99: self.writeDigit(0, int(value / 100)) self.writeDigit(1, (value / 10) % 10) self.writeDigit(3, value % 10, True) #Add Decimal self.writeDigit(4, (value * 10) % 10) elif value > 9: self.writeDigit(0, int(value / 10)) self.writeDigit(1, value % 10, True) self.writeDigit(3, (value * 10) % 10) self.writeDigit(4, (value * 100 % 10)) else: self.writeDigit(0, int(value * 10), True) self.writeDigit(1, (value * 100) % 10) self.writeDigit(3, (value * 1000) % 10) self.writeDigit(4, (value * 10000) % 10)
def __init__(self, address=0x70, debug=False): if (debug): print "Initializing a new instance of LEDBackpack at 0x%02X" % address LEDBackpack.__init__(self, 8, 8, address, debug) self.rotation = 0
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) def writeInt(self, value): """ Write The 'Value' to the display """ if value > 9999: #Maxed OUT! value = 9999 else: value = int(value) self.writeDigit(0, (value / 1000) % 10) self.writeDigit(1, (value / 100) % 10) self.writeDigit(3, (value / 10) % 10) self.writeDigit(4, value % 10) def writeNum(self, value): if value > 999: self.writeInt(value) elif value > 99: self.writeDigit(0, int(value / 100)) self.writeDigit(1, (value / 10) % 10) self.writeDigit(3, value % 10, True) #Add Decimal self.writeDigit(4, (value * 10) % 10) elif value > 9: self.writeDigit(0, int(value / 10)) self.writeDigit(1, value % 10, True) self.writeDigit(3, (value * 10) % 10) self.writeDigit(4, (value * 100 % 10)) else: self.writeDigit(0, int(value * 10), True) self.writeDigit(1, (value * 100) % 10) self.writeDigit(3, (value * 1000) % 10) self.writeDigit(4, (value * 10000) % 10)
#!/usr/bin/python # tsp_timer # # set timer display and handle button press import RPi.GPIO as GPIO import time import datetime from Adafruit_7Segment import SevenSegment from Adafruit_LEDBackpack import LEDBackpack segment = SevenSegment(address=0x73) backpack = LEDBackpack(address=0x73) backpack.setBrightness(15) BUTTON_GPIO = 17 # states constants STOP = 1 RUN = 2 HOLD = 3 timer_state = STOP last_timer_state = STOP hold_time = 0 timer_start = datetime.datetime.now() run_time = datetime.timedelta(0) GPIO.setmode(GPIO.BCM)
class Alphanumeric(object): disp = None # Hexadecimal character lookup table lut = [ 0b0000000000000001, 0b0000000000000010, 0b0000000000000100, 0b0000000000001000, 0b0000000000010000, 0b0000000000100000, 0b0000000001000000, 0b0000000010000000, 0b0000000100000000, 0b0000001000000000, 0b0000010000000000, 0b0000100000000000, 0b0001000000000000, 0b0010000000000000, 0b0100000000000000, 0b1000000000000000, 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, 0b0000000000000000, 0b0001001011001001, 0b0001010111000000, 0b0001001011111001, 0b0000000011100011, 0b0000010100110000, 0b0001001011001000, 0b0011101000000000, 0b0001011100000000, 0b0000000000000000, # 0b0000000000000110, # ! 0b0000001000100000, # " 0b0001001011001110, # # 0b0001001011101101, # $ 0b0000110000100100, # % 0b0010001101011101, # & 0b0000010000000000, # ' 0b0010010000000000, # ( 0b0000100100000000, # ) 0b0011111111000000, # * 0b0001001011000000, # + 0b0000100000000000, # , 0b0000000011000000, # - 0b0000000000000000, # . 0b0000110000000000, # / 0b0000110000111111, # 0 0b0000000000000110, # 1 0b0000000011011011, # 2 0b0000000010001111, # 3 0b0000000011100110, # 4 0b0010000001101001, # 5 0b0000000011111101, # 6 0b0000000000000111, # 7 0b0000000011111111, # 8 0b0000000011101111, # 9 0b0001001000000000, # : 0b0000101000000000, # ; 0b0010010000000000, # < 0b0000000011001000, # = 0b0000100100000000, # > 0b0001000010000011, # ? 0b0000001010111011, # @ 0b0000000011110111, # A 0b0001001010001111, # B 0b0000000000111001, # C 0b0001001000001111, # D 0b0000000011111001, # E 0b0000000001110001, # F 0b0000000010111101, # G 0b0000000011110110, # H 0b0001001000000000, # I 0b0000000000011110, # J 0b0010010001110000, # K 0b0000000000111000, # L 0b0000010100110110, # M 0b0010000100110110, # N 0b0000000000111111, # O 0b0000000011110011, # P 0b0010000000111111, # Q 0b0010000011110011, # R 0b0000000011101101, # S 0b0001001000000001, # T 0b0000000000111110, # U 0b0000110000110000, # V 0b0010100000110110, # W 0b0010110100000000, # X 0b0001010100000000, # Y 0b0000110000001001, # Z 0b0000000000111001, # [ 0b0010000100000000, # 0b0000000000001111, # ] 0b0000110000000011, # ^ 0b0000000000001000, # _ 0b0000000100000000, # ` 0b0001000001011000, # a 0b0010000001111000, # b 0b0000000011011000, # c 0b0000100010001110, # d 0b0000100001011000, # e 0b0000000001110001, # f 0b0000010010001110, # g 0b0001000001110000, # h 0b0001000000000000, # i 0b0000000000001110, # j 0b0011011000000000, # k 0b0000000000110000, # l 0b0001000011010100, # m 0b0001000001010000, # n 0b0000000011011100, # o 0b0000000101110000, # p 0b0000010010000110, # q 0b0000000001010000, # r 0b0010000010001000, # s 0b0000000001111000, # t 0b0000000000011100, # u 0b0010000000000100, # v 0b0010100000010100, # w 0b0010100011000000, # x 0b0010000000001100, # y 0b0000100001001000, # z 0b0000100101001001, # { 0b0001001000000000, # | 0b0010010010001001, # } 0b0000010100100000, # ~ 0b0011111111111111] special = dict(degree= 0b0000000011100011, cone=0b0010100000001000) def __init__(self, address=0x70, debug=False): self.disp = LEDBackpack(address=address, debug=debug) def writeCharRaw(self, charNumber, value): "Sets a digit using the raw 16-bit value" if (charNumber > 4): return # Set the appropriate digit self.disp.setBufferRow(charNumber, value) def writeChar(self, charNumber, value, dot=False): "Sets a single decimal or hexademical value (0..9 and A..F)" if (charNumber > 4): return if value in self.special: value = self.special[value] else: value = self.lut[ord(value)] # Set the appropriate digit self.disp.setBufferRow(charNumber, value | (dot << 14))
class FourteenSegment: disp = None # Hexadecimal character lookup table (row 1 = 0..9, row 2 = A..F) digits = [ 0x3F, 0x06, 0xDB, 0xCF, 0xE6, 0xED, 0xFD, 0x07, 0xFF, 0xEF, \ 0xF7, 0xFC, 0x39, 0xDE, 0x79, 0x71 ] chars = [ 0x1, # 0x2, # 0x4, # 0x8, # 0x10, # 0x20, # 0x40, # 0x80, # 0x100, # 0x200, # 0x400, # 0x800, # 0x1000, # 0x2000, # 0x4000, # 0x8000, # 0x0, # 0x0, # 0x0, # 0x0, # 0x0, # 0x0, # 0x0, # 0x0, # 0x12c9, # 0x15c0, # 0x12f9, # 0xe3, # 0x530, # 0x12c8, # 0x3a00, # 0x1700, # 0x0, # 0x6, # ! 0x220, # " 0x12ce, # # 0x12ed, # $ 0xc24, # % 0x235d, # & 0x400, # ' 0x2400, # ( 0x900, # ) 0x3fc0, # * 0x12c0, # + 0x800, # , 0xc0, # - 0x0, # . 0xc00, # / 0xc3f, # 0 0x6, # 1 0xdb, # 2 0x8f, # 3 0xe6, # 4 0x2069, # 5 0xfd, # 6 0x7, # 7 0xff, # 8 0xef, # 9 0x1200, # : 0xa00, # ; 0x2400, # < 0xc8, # = 0x900, # > 0x1083, # ? 0x2bb, # @ 0xf7, # A 0x128f, # B 0x39, # C 0x120f, # D 0xf9, # E 0x71, # F 0xbd, # G 0xf6, # H 0x1200, # I 0x1e, # J 0x2470, # K 0x38, # L 0x536, # M 0x2136, # N 0x3f, # O 0xf3, # P 0x203f, # Q 0x20f3, # R 0xed, # S 0x1201, # T 0x3e, # U 0xc30, # V 0x2836, # W 0x2d00, # X 0x1500, # Y 0xc09, # Z 0x39, # [ 0x2100, # 0xf, # ] 0xc03, # ^ 0x8, # _ 0x100, # ` 0x1058, # a 0x2078, # b 0xd8, # c 0x88e, # d 0x858, # e 0x71, # f 0x48e, # g 0x1070, # h 0x1000, # i 0xe, # j 0x3600, # k 0x30, # l 0x10d4, # m 0x1050, # n 0xdc, # o 0x170, # p 0x486, # q 0x50, # r 0x2088, # s 0x78, # t 0x1c, # u 0x2004, # v 0x2814, # w 0x28c0, # x 0x200c, # y 0x848, # z 0x949, # { 0x1200, # | 0x2489, # } 0x520, # ~ 0x3fff # ] # 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 << 14)) def writeChar(self, charNumber, value, dot=False): "Writes an ascii character on the screen" if (charNumber > 7): return # Set the appropriate character self.disp.setBufferRow(charNumber, self.chars[ord(value)] | (dot << 14)) 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, 0x4000) else: self.disp.setBufferRow(2, 0)
# function to return a datastream object. This either creates a new datastream, # or returns an existing one def get_datastream(feed): try: datastream = feed.datastreams.get("external_temp") return datastream except: datastream = feed.datastreams.create("external_temp", tags="temp_01") return datastream # =========================================================================== # Clock Example # =========================================================================== led = LEDBackpack(address=0x70) segment = SevenSegment(address=0x70) try: interval = int(sys.argv[1]) except: print 'Interval required' sys.exit(1) daybrightness = 15 nightbrightness = 5 minbrightness = 1 maxbrightness = 15
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) def clear(self , position = 99 , update=True): 'Clears display' if position == 99: self.disp.clear(update) else: self.disp.setBufferRow(position , 0) def getBuffer(self): 'Returns copy of display buffer' return self.disp.getBuffer() def setNumber(value): if value < 0: self.setColon(True) else: self.setColon(False) self.writeDigit(0 , (abs(value) / 1000)%10) self.writeDigit(1 , (abs(value) / 100)%10) self.writeDigit(3 , (abs(value) / 10)%10) self.writeDigit(4 , abs(value) % 10) def setLetter(value): if not 9 < value < 16: return self.clear() self.writeDigit(4 , value)
#!/usr/bin/env python import time, sys from Adafruit_LEDBackpack import LEDBackpack from LEDLetterValues import * from timeit import default_timer grids = [LEDBackpack(address=i) for i in range(0x70, 0x74)] wait_time = float( sys.argv[2] if len(sys.argv) > 2 else raw_input("Wait time: ")) text = sys.argv[1] if len( sys.argv) > 1 else raw_input("What should I scroll: ") printcon = textTo2D(text) print "Press CTRL+C to exit" def main(): scrolled = 0 while True: start = default_timer() for y, v in enumerate(printcon): buffers = [0x00, 0x00, 0x00, 0x00] for x, i in enumerate(v): if i: a = x - scrolled if a >= 0 and a < len(grids) * 16: buffers[a // 16] = buffers[a // 16] | 1 << (a % 16) for i, grid in enumerate(grids): grid.setBufferRow(y, buffers[i], update=False)
class ColorEightByEight(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 getBufferValue(self, i): buffer = self.disp.getBuffer() return buffer[i] def setPixel(self, x, y, color=1): "Sets a single pixel" if (x >= 8): return if (y >= 8): return x %= 8 # Set the appropriate pixel buffer = self.disp.getBuffer() # TODO : Named color constants? # ATNN : This code was mostly taken from the arduino code, but with the addition of clearing the other bit when setting red or green. # The arduino code does not do that, and might have the bug where if you draw red or green, then the other color, it actually draws yellow. # The bug doesn't show up in the examples because it's always clearing. if (color == 1): self.disp.setBufferRow(y, (buffer[y] | (1 << x)) & ~(1 << (x+8)) ) elif (color == 2): self.disp.setBufferRow(y, (buffer[y] | 1 << (x+8)) & ~(1 << x) ) elif (color == 3): self.disp.setBufferRow(y, buffer[y] | (1 << (x+8)) | (1 << x) ) else: self.disp.setBufferRow(y, buffer[y] & ~(1 << x) & ~(1 << (x+8)) ) def setBrightness(self, brightness): "Sets the brightness level from 0..15" self.disp.setBrightness(brightness) def setBlinkRate(self, blinkRate): self.disp.setBrightness(blinkRate) def clear(self): "Clears the entire display" self.disp.clear()
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) # =========================================================================== # A single row operations # =========================================================================== 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, self.wrapValue(value)) def writeMatrix(self, mx, color=1): "Sets a 8x8 matrix" row = 0 for value in mx: if row > 7: return self.disp.setBufferRow(row, self.wrapValue(value), False) row += 1 self.disp.writeDisplay() def wrapValue(self, value): "A function to mock 8x8 LED bug (offset and flip)" valueHI = value & 0xFF00 valueLO = value & 0x00FF valueLO = ((valueLO << 1) & 0xFF) | (valueLO >> 7) flip = 0x00 for i in range(8): flip = flip | ((2 ** (7 - i)) if (valueLO & 2 ** i) else 0) valueLO = flip & 0x00FF return valueHI | valueLO # =========================================================================== # A single pixel operations # =========================================================================== def setPixel(self, x, y): "Sets a single pixel" if (x > 7) | (y > 7): return # Set the appropriate pixel value = 2 ** x buffer = self.disp.getBuffer() self.disp.setBufferRow(y, buffer[y] | self.wrapValue(value)) def clearPixel(self, x, y): "Sets a single pixel" if (x > 7) | (y > 7): return # Set the appropriate pixel value = ~(2 ** x) & 0xFF buffer = self.disp.getBuffer() self.disp.setBufferRow(y, buffer[y] & self.wrapValue(value)) def switchPixel(self, x, y): "Sets a single pixel" if (x > 7) | (y > 7): return # Set the appropriate pixel value = 2 ** x buffer = self.disp.getBuffer() self.disp.setBufferRow(y, buffer[y] ^ self.wrapValue(value)) # =========================================================================== # # =========================================================================== def clear(self): "Clears the entire display" self.disp.clear()
#!/usr/bin/python import time import datetime import Adafruit_GPIO.SPI as SPI #import Adafruit_MAX31855.MAX31855 as MAX31855 from MAX31855 import MAX31855 from Adafruit_7Segment import SevenSegment from Adafruit_LEDBackpack import LEDBackpack segment = SevenSegment(address=0x72) backpack = LEDBackpack(address=0x72) backpack.setBrightness(1) # Define a function to convert celsius to fahrenheit. def c_to_f(c): return c * 9.0 / 5.0 + 32.0 # Raspberry Pi software SPI configuration. CLK = 24 CS = 23 DO = 18 #sensor = MAX31855.MAX31855(CLK, CS, DO) sensor = MAX31855(CLK, CS, DO) # =========================================================================== # Clock Example
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)
class SevenSegment: disp = None # Some 7Segment-Display have several different colons, e.g. the # 1,2" display. To seperately control such different colons # use the following values where applicable: # # 0x00 - nothing # 0x02 - center colon # 0x04 - left colon - upper dot # 0x08 - left colon - lower dot # 0x10 - decimal point # 0xFFFF - everything (default) mask_colons = 0xFFFF # Hexadecimal character lookup table (row 1 = 0..9, row 2 = A..F) DIGITS_NORMAL = [ 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, \ 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71 ] # inverted table from https://github.com/tomgidden/Adafruit-Raspberry-Pi-Python-Code/commit/87bed18ecc8e251f3c10433d8a31d363dbbd355c # thx to tomgidden@github for this DIGITS_INVERTED = [ 0x3F, 0x30, 0x5B, 0x79, 0x74, 0x6D, 0x6F, 0x38, 0x7F, 0x7D, \ 0x7E, 0x67, 0x0F, 0x73, 0x4F, 0x4E ] digits = DIGITS_NORMAL display_inverted = False # 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 invertDisplay(self): if (self.display_inverted ): # we were inverted, so switch back to normal self.digits = self.DIGITS_NORMAL self.display_inverted = False else: # we were displaying normally, so switch to inverted self.digits = self.DIGITS_INVERTED self.display_inverted = True def writeDigitRaw(self, charNumber, value): "Sets a digit using the raw 16-bit value" if (charNumber > 7): return if ((self.display_inverted) & (charNumber < 5)): "if inverted we need to use reverse character positioning" charNumber = 4 - charNumber # 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 if ((self.display_inverted) & (charNumber < 5)): "if inverted we need to use reverse character positioning" charNumber = 4 - charNumber # 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, self.mask_colons) else: self.disp.setBufferRow(2, 0)