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 # 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): self.disp.clear() def clearDigit(self,charNumber): self.disp.setBufferRow(charNumber, 0x00) def setBrightness(self,value): self.disp.setBrightness(value) def writeMinus(self, charNumber): self.disp.setBufferRow(charNumber, 0x40)
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)
#!/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)
time.sleep(1) try: last = get_latest_file() temp = int(round(last)) tempC = int(round((last - 32) / 1.8)) except: pass write_temp(temp) time.sleep(interval / 2) write_temp(tempC, F=False) time.sleep(interval / 2) try: alt, az = sun_altitude.sun_altitude() except: pass if True: lux = LightSensor.calculateLux() # 90 lux is dim daylight # 3000 lux is flashlight shining on sensor brightness = int(lux / 10) brightness = min(brightness, maxbrightness) brightness = max(brightness, minbrightness) led.setBrightness(brightness) #print lux,brightness else: if alt > 0: led.setBrightness(daybrightness) else: led.setBrightness(nightbrightness)
try: last=get_latest_file() temp=int(round(last)) tempC=int(round((last-32)/1.8)) except: pass write_temp(temp) time.sleep(interval/2) write_temp(tempC, F=False) time.sleep(interval/2) try: alt,az=sun_altitude.sun_altitude() except: pass if True: lux=LightSensor.calculateLux() # 90 lux is dim daylight # 3000 lux is flashlight shining on sensor brightness=int(lux/10) brightness=min(brightness, maxbrightness) brightness=max(brightness, minbrightness) led.setBrightness(brightness) #print lux,brightness else: if alt > 0: led.setBrightness(daybrightness) else: led.setBrightness(nightbrightness)
# 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: # now = datetime.datetime.now() # hour = now.hour # minute = now.minute
#!/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)