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 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 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) 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()
if timer_state == RUN: run_time = now - timer_start else: # Toggle colon segment.setColon(now.second % 2) # Toggle colon at 1Hz minute = run_time.seconds / 60 min_1 = int(minute / 10) min_2 = minute % 10 second = run_time.seconds % 60 sec_1 = int(second / 10) sec_2 = second % 10 backpack.clear() # Set minutes if min_1 > 0: segment.writeDigit(0, min_1) # Tens if min_1 + min_2 > 0: segment.writeDigit(1, min_2) # Ones # Set minutes if min_1 + min_2 + sec_1 > 0: segment.writeDigit(3, sec_1) # Tens segment.writeDigit(4, sec_2) # Ones if(GPIO.input(BUTTON_GPIO) == 1): hold_time = hold_time + 1 else: hold_time = 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) # =========================================================================== # 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 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()