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)
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 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))
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
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 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 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))
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 # 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() def setBrightness(self, brightness): "Sets the brightness level from 0..15" self.disp.setBrightness(brightness)
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 # 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, 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()
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)
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)
sensor = MAX31855(CLK, CS, DO) # =========================================================================== # Clock Example # =========================================================================== # Loop printing measurements every second. while True: temp = sensor.readTempC() internal = sensor.readInternalC() #print 'Thermocouple: {0:0.3F}*C'.format(temp, c_to_f(temp)) #print 'Internal: {0:0.3F}*C'.format(internal, c_to_f(internal)) if (temp >= 100): segment.writeDigit(0, int(temp / 100)) else: backpack.setBufferRow(0, 0) segment.writeDigit(1, int(temp / 10)) segment.writeDigit(3, int(temp) % 10) segment.writeDigit(4, 0xC) # set deg #backpack.setBufferRow(5,1) segment.writeDigit(2, 0xf) time.sleep(1.0)
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)
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 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()
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)
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 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()
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)