def display_time(): global displaying_code, prev_time_of_day displaying_code = False daylight_saving_time = datetime.now() + timedelta( hours=1) # change hour to zero for winter time time_of_day = daylight_saving_time.strftime('%H:%M') if time_of_day == prev_time_of_day: return else: logger.info( "time changed: time_of_day: {} prev_time_of_day: {}".format( time_of_day, prev_time_of_day)) prev_time_of_day = time_of_day if display_mode == 'static': with canvas(device) as draw: text(draw, (1, 0), time_of_day, fill="white", font=proportional(CP437_FONT)) else: show_message(device, time_of_day, fill="white", font=proportional(CP437_FONT))
def set_matrix(char): with canvas(device) as draw: legacy.text(draw, (0,0), char, fill="white", font=proportional(CP437_FONT) ) return None
def randt_gamefunc(): global BEAM_PINS, target, p, targetlist, hitcount, timeup timeup = False gametime = 10 starttime = time.time() timer = threading.Thread(target=score_randt, args=(tm, starttime, gametime)) timer.start() while not timeup: target = random.choice(targetlist) p = Process(target=settarget, args=( target, device, chit, )) p.start() p.join() timer.join() with canvas(device) as draw: text(draw, (64, 0), "Game Over", fill="white", font=proportional(TINY_FONT)) time.sleep(5)
def selftestfunc(): global BEAM_PINS, target, p, targetlist, hitcount, timeup for pin in BEAM_PINS: GPIO.remove_event_detect(pin) for pin in BEAM_PINS: GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(pin, GPIO.BOTH, callback=test_callback) for test in range(5): target = test + 1 p = Process(target=settarget, args=( test + 1, device, )) p.start() p.join() with canvas(device) as draw: text(draw, (64, 0), "Test done", fill="white", font=proportional(TINY_FONT)) time.sleep(5)
def display(display_type, payload): if (display_type == 'input'): with canvas(device) as draw: text(draw, (0, 0), payload, fill='white', font=proportional(CP437_FONT)) elif (display_type == 'message'): if (payload != ''): show_message(device, payload, fill='white', font=proportional(CP437_FONT), y_offset=0, scroll_delay=0.03)
def main(): msg = "Neopixel WS2812 LED Matrix Demo" show_message(device, msg, y_offset=-1, fill="green", font=proportional(TINY_FONT)) time.sleep(1) with canvas(device) as draw: text(draw, (0, -1), txt="A", fill="red", font=TINY_FONT) text(draw, (4, -1), txt="T", fill="green", font=TINY_FONT) time.sleep(1) with canvas(device) as draw: draw.line((0, 0, 0, device.height), fill="red") draw.line((1, 0, 1, device.height), fill="orange") draw.line((2, 0, 2, device.height), fill="yellow") draw.line((3, 0, 3, device.height), fill="green") draw.line((4, 0, 4, device.height), fill="blue") draw.line((5, 0, 5, device.height), fill="indigo") draw.line((6, 0, 6, device.height), fill="violet") draw.line((7, 0, 7, device.height), fill="white") time.sleep(4) for _ in range(5): for intensity in range(16): device.contrast(intensity * 16) time.sleep(0.1) device.contrast(0x80) time.sleep(1) gfx(device)
def loop_time_and_temperature(temp, feels_like): rounded_temp = str(int(temp)) rounded_feels_like = str(int(feels_like)) feels_like_message = "Se simt ca" if language == "RO" else "Feels like" index = 0 while index < 20: index += 1 with canvas(virtual) as draw: current_time = datetime.now() offset = 4 if current_time.hour > 9: offset = 1 text(draw, (offset, 0), "{}:{}".format(str(current_time.hour), format_minute(current_time.minute)), fill="white", font=proportional(CP437_FONT)) sleep(5) with canvas(virtual) as draw: text(draw, (format_temperature_offset(temp), 0), "{}`C".format(rounded_temp), fill="white", font=proportional(CP437_FONT)) sleep(5) show_message(device, "{} {}`C".format(feels_like_message, rounded_feels_like), fill="white", font=proportional(LCD_FONT), scroll_delay=0.04)
def display_message(self, message, type, logo=None, delay=15): """Displays a message based on the MessageType Args: message (String): The message to display type (MessageType): MessageType to display """ if (os.environ.get('MODE', None) == 'CONSOLE'): print(f'display_message(message:{message}, type:{type}' + f', logo:{logo}, delay:{delay}') return if type == MessageType.STATIC: with canvas(self.device) as draw: text(draw, (0, 0), message, fill="white", font=proportional(CP437_FONT)) elif type == MessageType.SCROLLING: self.scroll_message(self.device, message, self.font) elif type == MessageType.BOUNCING: self.bounce_message(self.device, message, font=self.font, logo=logo, delay=delay) elif type == MessageType.FALLING: self.drop_message(self.device, message, font=self.font, logo=logo, delay=delay)
def displayLED(val): try: with canvas(virtual) as draw: if val == 0: text(draw, (0, 0), "O w O", fill="orange", font=proportional(CP437_FONT)) if val == 1: text(draw, (2, 0), "T n T", fill="orange", font=proportional(CP437_FONT)) if val == 2: text(draw, (0, 0), "^ u ^", fill="orange", font=proportional(CP437_FONT)) if val == 3: text(draw, (2, 0), "> w <", fill="orange", font=proportional(CP437_FONT)) if val == 4: text(draw, (1, 0), "- _ -", fill="orange", font=proportional(CP437_FONT)) except KeyboardInterrupt: GPIO.cleanup()
def main(): GPIO.setwarnings(True) GPIO.setmode(GPIO.BCM) device.height=8 device.width=64 max_depth = 64 device.contrast(50) update_time() update_weather() schedule.every(10).minutes.do(update_weather) while True: with canvas(device) as draw: update_time() update_dht11() draw_second_dot(draw) #text(draw,(0,0), weather_string + " " + time_string, fill="white", font=proportional(LCD_FONT)) text(draw, (0, 0), weather_string + " " + time_string + " " + dht11_string, fill="white", font=proportional(LCD_FONT)) if (weather_requested == 1): draw_loading_animation(draw) schedule.run_pending()
def updateLightBar(currentWeight, toppingWeight): bars = int(currentWeight / toppingWeight * 32) if bars < 32: with canvas(device) as draw: draw.rectangle((0, 0, bars, 8), outline="white", fill="white") time.sleep(.00001) else: if bars >= 36: with canvas(device) as draw: text(draw, (0, 0), "OVER", fill="white", font=proportional(LCD_FONT)) time.sleep(.00001) time.sleep(0.1) with canvas(device) as draw: draw.rectangle(device.bounding_box, outline="black", fill="black") time.sleep(.00001) else: with canvas(device) as draw: draw.rectangle(device.bounding_box, outline="white", fill="white") time.sleep(.00001) time.sleep(0.1) with canvas(device) as draw: draw.rectangle(device.bounding_box, outline="black", fill="black") time.sleep(.00001)
def rande_gamefunc(): print("rande started") global BEAM_PINS, target, p, targetlist, hitcount, timeup resetscore() timeup = False for pin in BEAM_PINS: GPIO.remove_event_detect(pin) for pin in BEAM_PINS: GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(pin, GPIO.BOTH, callback=rande_callback) starttime = time.time() timer = threading.Thread(target=score_rande, args=(tm, starttime)) timer.start() while not timeup: target = random.choice(targetlist) p = Process(target=settarget, args=( target, device, )) p.start() p.join() timer.join() with canvas(device) as draw: text(draw, (64, 0), "Game Over", fill="white", font=proportional(TINY_FONT)) time.sleep(5)
def last(draw, call): # Print last_call i = 16 j = 1 for c in call: if c is not '': w, h = draw.textsize(text=c, font=font) tab = (s.device.width - w) / 2 if j == 1: color = get_color('log', 'call_last') else: color = get_color('log', 'call') draw.text((tab, i), c, font=font, fill=color) legacy.text(draw, (16, i + 1), chr(s.letter[str(j)]), font=s.SMALL_BITMAP_FONT, fill=color) if s.transmit is False: k = 108 for l in s.call_time[j - 1]: legacy.text(draw, (k, i + 1), chr(s.letter[l]), fill=get_color('header', 'foreground'), font=s.SMALL_BITMAP_FONT) k += 4 i += h j += 1
def drawTemp(self, draw): s = u'{:+.1f} \xf8c'.format(self.weather.temp) w, _ = textsize(s, font=utils.proportional2(TINY_FONT)) text(draw, (36 - w, 2), s, fill="white", font=utils.proportional2(TINY_FONT))
def display_time_remaining(self, time_remaining): halfSecond = (time_remaining % 1) >= 0.5 logger.info("%d seconds left. halfSecond: %d", time_remaining, halfSecond) # Format the timer digits for display minutes = time.strftime("%M", time.gmtime(time_remaining)) seconds = time.strftime("%S", time.gmtime(time_remaining)) #hours = datetime.now().strftime('%H') #minutes = datetime.now().strftime('%M') if halfSecond or (minutes == "00" and seconds == "00"): sperator = ":" else: sperator = "~" if minutes == "00": minutes = " " if minutes.startswith('0'): minutes = minutes.replace('0', ' ', 1) # TODO: Update font to remove slash through zero seconds = seconds.replace("0", "O") minutes = minutes.replace("0", "O") outText = minutes + sperator + seconds with canvas(self.device) as draw: text(draw, (4, 1), outText, fill="white", font=alt_proportional(LCD_FONT))
def histogram(draw, legacy, position, height=15): qso_hour_max = max(s.qso_hour) i = 5 j = 100 for (t, q) in enumerate(s.qso_hour): if q != 0: h = l.interpolation(q, 0, qso_hour_max, 0, height) else: h = 0 draw.rectangle((0 + i, position, i + 2, (position - height)), fill=get_color('screen', 'background')) if t == s.hour: color = get_color('histogram', 'column_current') else: color = get_color('histogram', 'column') draw.rectangle((0 + i, position, i + 2, (position - h)), fill=color) j += 5 i += 5 for i, j, k in [(1, 0, 0), (33, 0, 6), (63, 1, 2), (93, 1, 8), (120, 2, 3)]: legacy.text(draw, (i, position + 2), chr(j) + chr(k), fill=get_color('histogram', 'legend'), font=s.SMALL_BITMAP_FONT)
def printMsg(self, txt, font=LCD_FONT, moving=False, pos=None): virtual = viewport(self.device, width=self.device.width, height=self.device.height*2) margins = [] if pos and len(txt) == 1: with canvas(virtual) as draw: text(draw, pos, txt, fill="white", font=proportional(font)) virtual.set_position((0, 0)) return if font == TINY_FONT: margins.append([0, 0]) margins.append([4, 0]) margins.append([0, 8]) margins.append([4, 8]) else: margins.append([0, 0]) margins.append([0, 8]) with canvas(virtual) as draw: for i, word in enumerate(txt): text(draw, (margins[i][0], margins[i][1]), word, fill="white", font=proportional(font)) if moving: for i in range(virtual.height - self.device.height): virtual.set_position((0, i)) else: virtual.set_position((0, 0))
def demo(): # create matrix device serial = spi(port=0, device=0, gpio=noop()) device = max7219(serial, cascaded=1, block_orientation=0, rotate=0) print("Created device") msg = "Hello Kareem!" show_message(device, msg, fill="white", font=proportional(LCD_FONT), scroll_delay=0.1) time.sleep(1) with canvas(device) as draw: text(draw, (0, 0), "A", fill="white") time.sleep(1) for _ in range(5): for intensity in range(16): device.contrast(intensity * 16) time.sleep(0.1) device.contrast(0x80) time.sleep(1)
def update(self, draw): self.color = "black" if self.backColor == "black" else "white" self.backColor = "white" if self.backColor == "black" else "black" draw.rectangle((0, 0, self.width, self.height), outline=self.backColor, fill=self.backColor) text(draw, (1, 0), self.text, fill=self.color, font=self.font)
def menufunc(): global BEAM_PINS, chit, targetlist gamevar = randt_gamefunc currentgame = "Rand E" menustate = "Init" menudict = { "Init": ["Games", "", currentgame, "Setup", "Start"], "Games": ["Rand T", "Rand E", "Seq", "", ""], "Setup": ["Sound", "Time", "", "", ""], "Start": ["", "", "", "", ""], "Rand T": randt_gamefunc, "Rand E": randt_gamefunc, "Seq": randt_gamefunc } chit = 0 for pin in BEAM_PINS: GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(pin, GPIO.FALLING, callback=menu_callback) # ~ with canvas(device) as draw: # ~ for mitem in range(5): # ~ text(draw, (32*mitem, 0), menudict[menustate][mitem], fill="white", font=proportional(TINY_FONT)) while True: time.sleep(0.1) if menustate == "Init": ##print("near start") ##print(menudict[menustate][chit-1]) print("In Menu") print(chit) if menudict[menustate][abs(chit - 1)] == "Start": print("start") gamevar() chit = -1 return print(menustate) if not callable(menudict[menustate] ) and not menustate == "Games" and chit in targetlist: menustate = menudict[menustate][chit - 1] chit = -1 elif menustate == "Games" and chit in targetlist: if menudict[menustate][chit - 1] in ["Rand T", "Rand E", "Seq"]: currentgame = menudict[menustate][chit - 1] menustate = "Init" menudict[menustate][2] = currentgame gamevar = menudict[currentgame] #print(gamevar) chit = -1 if not callable(menudict[menustate]): with canvas(device) as draw: for mitem in range(5): text(draw, (32 * mitem, 0), menudict[menustate][mitem], fill="white", font=proportional(TINY_FONT))
def extended_call(draw, limit=5): if s.device.height == 128: draw.rectangle((0, 1, s.device.height - 1, 13), fill=get_color('header', 'background')) legacy.text(draw, (0, 1), chr(0) + chr(1), fill=get_color('header', 'foreground'), font=s.SMALL_BITMAP_CLOCK) legacy.text(draw, (0, 9), chr(2) + chr(3), fill=get_color('header', 'foreground'), font=s.SMALL_BITMAP_CLOCK) title(draw, 'Derniers TX') if s.device.height == 128: i = 17 else: i = 16 for j in range(0, limit): label(draw, i, 42, get_color('label', 'background'), get_color('label', 'foreground'), s.call_time[j], s.call[j]) if s.device.height == 128: i += 11 else: i += 10
def menufunc(): global BEAM_PINS, menuc, targetlist, currentgame, gamevar, menudict print("New menu") menustate = "Init" gamevar = randt_gamefunc currentgame = "Rand T" menudict = { "Init": ["Games", "Test", currentgame, "Setup", "Start"], "Games": ["Rand T", "Rand E", "Seq", "", ""], "Setup": lambda: setupfunc(currentgame), "Start": gamevar, "Test": lambda: game_select(selftestfunc, "Test"), "Rand T": lambda: game_select(randt_gamefunc, "Rand T"), "Rand E": lambda: game_select(rande_gamefunc, "Rand E"), "Seq": lambda: game_select(rande_gamefunc, "Seq") } menuc = None for pin in BEAM_PINS: GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(pin, GPIO.FALLING, callback=menu_callback) # ~ with canvas(device) as draw: # ~ for mitem in range(5): # ~ text(draw, (32*mitem, 0), menudict[menustate][mitem], fill="white", font=proportional(TINY_FONT)) prevmenu = [] while True: time.sleep(0.1) if menuc is not None: print("menudict[menustate]" + str(menudict[menustate])) if callable(menudict[menustate]): print("before call") menudict[menustate]() menuc = None if not menudict[ menustate].__name__ == "<lambda>": # Start new menu after game is called for pin in BEAM_PINS: GPIO.remove_event_detect(pin) return else: print(gamevar) print(currentgame) menustate = "Init" else: menustate = menudict[menustate][menuc] if not callable(menudict[menustate]): menuc = None if not callable(menudict[menustate]): if not menudict[menustate] == prevmenu: with canvas(device) as draw: for mitem in range(5): text(draw, (32 * mitem, 0), menudict[menustate][mitem], fill="white", font=proportional(TINY_FONT)) prevmenu = menudict[menustate] time.sleep(0.1)
def extended_call(draw): draw.rectangle((0, 0, 127, 63), fill='black') legacy.text(draw, (0, -2), chr(0) + chr(1), fill='white', font=s.SMALL_BITMAP_CLOCK) legacy.text(draw, (0, 6), chr(2) + chr(3), fill='white', font=s.SMALL_BITMAP_CLOCK) w, h = draw.textsize(text=s.room + ' Last TX', font=font) tab = (s.device.width - w) / 2 draw.text((tab, 0), s.room + ' Last TX', font=font, fill='white') i = 16 for j in xrange(0, 5): draw.rectangle((0, i - 1, 42, i + 7), fill='white') draw.line((43, i, 43, i + 6), fill='white') draw.line((44, i + 2, 44, i + 4), fill='white') draw.point((45, i + 3), fill='white') draw.text((1, i), s.call_time[j], font=font, fill='black') draw.text((54, i), s.call[j], font=font, fill='white') i += 10
def play_omx(song=1): """ play_omx function is the basic function to load a new song and play it defulut song is ROTZ BEN SUSI """ player.load(songs[song]) if song in [7]: #lullabye in ragtime is a bit low volume player.set_volume(4) elif song in [11]: # i_love_chicklate is also low vol player.set_volume(2) else: player.set_volume(1) player.play() # song_time = player.duration() print("PLAYING " + songs[song].split('/')[-1]) led.blink(on_time=1.5, n=5) # printing to the matrix for _ in range(4): device.show() with canvas(device) as draw: text(draw, (0, 0), chr(3), fill='white') # this will print a heart text(draw, (16, 0), chr(2), fill='white') # filled smily time.sleep(0.75) device.hide() time.sleep(0.75)
def messagebar_static(ctx): if ctx.message is None: ctx.message = input("Your message: ") logger(ctx.message, "static") if system: with canvas(device) as draw: text(draw, (0, 0), ctx.message, fill='white', font=proportional(TINY_FONT)) refresh = 10 iterable = range(ctx.count * refresh) label = 'Displaying' fill_char = click.style('#', fg='green') empty_char = click.style('-', fg='white', dim=True) print('Message "' + ctx.message + '" sent successfully.') with click.progressbar(iterable=iterable, label=label, fill_char=fill_char, empty_char=empty_char) as bar: for tick in bar: time.sleep(1 / refresh) else: print('Message "' + ctx.message + '" has been logged successfully.')
def display(self, top_right, bottom_right, img): """ Display given strings and icon on the matrix screen on corresponding locations. If None is given, item is not displayed """ # if there is at least one item to display if top_right is not None or bottom_right is not None or img is not None: with canvas(self.device) as draw: if top_right is not None: text(draw, (17, -1), top_right, fill="white", font=TINY_FONT) if bottom_right is not None: # draw a small roof over inside temperature draw.line((20, 7, 17, 9), fill="white") draw.line((20, 7, 23, 9), fill="white") text(draw, (17, 10), bottom_right, fill="white", font=TINY_FONT) if img is not None: draw.bitmap((0, 0), img, fill="white") # if there are no items to display (all 3 are None) else: self.display_text("Error")
def render(draw, width, height): by_name = lambda device: device.name.lower() devices = sorted(get_devices(url), key=by_name) print(devices) y_offset = 0 for index, group in groupby(devices, key=by_name): group = sorted(group, key=lambda device: device.ipAddress) draw.text((1, y_offset), text=group[0].name, fill='white', font=default) y_offset += 11 for device in group: draw.text((10, y_offset), text=device.ipAddress, fill='white', font=default) legacy.text(draw, (width - 8, y_offset), '\1' if device.network == 'Ethernet' else '\0', fill='white', font=CUSTOM_BITMAP_FONT) y_offset += 11 draw.line((0, y_offset, width, y_offset), fill='grey') y_offset += 2
def show_symbol(self, name): """ Show a symbol on the display, provided by a specific name :param name: please see 'symbols' below for the current abilities """ # see https://en.wikipedia.org/wiki/Code_page_437 symbols = { 'smiley': 1, 'arrow_left': 27, 'arrow_right': 26, 'arrow_up': 24, 'arrow_down': 25, 'triangle_up': 30, 'triangle_down': 31, 'root': 251, 'sound': 14, 'space': 32, } if name not in symbols: logger.error("show symbol: " + name + " -> symbol unknown!!!") return if FakeIO: logger.info("show symbol: " + name + " -> " + str(symbols[name])) else: with canvas(self.device) as draw: text(draw, (0, 0), chr(symbols[name]), fill="white")
def startSwitch(self): print('start interrupt') global errorFlag, startFlag if (errorFlag == True and GPIO.input(start_job_pin)) == 1: errorFlag = False with canvas(virtual) as draw: sleep(1) text(draw, (1, 1), "Ready", fill="white", font=proportional(LCD_FONT)) sleep(0.5) elif (errorFlag == False and startFlag == False and GPIO.input(start_job_pin)) == 1: global job_start_time, flag, count flag = 1 startFlag = True blueStable() job_start_time = int(time.time()) print('Start time of job is: ') + str(job_start_time) with canvas(virtual) as draw: sleep(1) text(draw, (1, 1), "Busy", fill="white", font=proportional(LCD_FONT)) sleep(0.5) else: print('LOW')
def test_text_space(): """ Draw a space character. """ draw = Mock(unsafe=True) text(draw, (2, 2), " ", fill="white") draw.point.assert_not_called()
def demo(n, block_orientation, rotate): # create matrix device serial = spi(port=0, device=0, gpio=noop()) device = max7219(serial, cascaded=n or 1, block_orientation=block_orientation, rotate=rotate or 0) print("Created device") # start demo msg = "Hello AgilityContest" print(msg) show_message(device, msg, fill="white", font=proportional(CP437_FONT)) time.sleep(1) for x in xrange(5): msg = "Ring 1 Agility STD-G2 - Now running 28 " print(msg) show_message(device, msg, fill="white", font=proportional(LCD_FONT), scroll_delay=0.05) with canvas(device) as draw: text(draw, (0, 0), " 28 ", fill="white") time.sleep(5)
def demo(w, h, block_orientation, rotate): # create matrix device serial = spi(port=0, device=0, gpio=noop()) device = max7219(serial, width=w, height=h, rotate=rotate, block_orientation=block_orientation) print("Created device") with canvas(device) as draw: draw.rectangle(device.bounding_box, outline="white") text(draw, (2, 2), "Hello", fill="white", font=proportional(LCD_FONT)) text(draw, (2, 10), "World", fill="white", font=proportional(LCD_FONT)) time.sleep(300)
def action(self,data): action_type=data.type self.stop_disp(False) if (action_type==disp_action.MUSIC): with canvas(self.device) as draw: text(draw, (0, 0), chr(14), fill="white") if (action_type==disp_action.SPEAKING): self.display_anim=True while(self.display_anim): with canvas(self.device) as draw: val=self.sin_func(self.shift_counter,self.peak_counter) self.shift_counter+=1 if self.shift_counter%2 is 0: self.peak_counter=random.randint(1,3) for i in range(0,7): draw.point((i,int(val[i])), fill="white") rospy.sleep(0.1) self.disable_disp() if (action_type==disp_action.PROCESSING): self.display_anim=True while(self.display_anim): for image in [1,2,3,2]: with canvas(self.device) as draw: self.shift_counter+=1 for shift in range(1,image+1): for i in range(shift,8-shift): draw.point((i,shift-1), fill="white") draw.point((i,8-shift), fill="white") draw.point((shift-1,i), fill="white") draw.point((8-shift,i), fill="white") draw.point((shift,shift), fill="white") draw.point((7-shift,7-shift), fill="white") draw.point((shift,7-shift), fill="white") draw.point((7-shift,shift), fill="white") self.device.contrast(int(round(255*(math.sin(self.shift_counter%6/5.0*2*math.pi)+1))/2)) rospy.sleep(0.12*(math.sin(self.shift_counter%6/5.0*2*math.pi)+1)/2+0.10) self.disable_disp()
def main(): # Setup for Banggood version of 4 x 8x8 LED Matrix (https://bit.ly/2Gywazb) serial = spi(port=0, device=0, gpio=noop()) device = max7219(serial, cascaded=4, block_orientation=-90, blocks_arranged_in_reverse_order=True) device.contrast(16) # The time ascends from the abyss... animation(device, 8, 1) toggle = False # Toggle the second indicator every second while True: toggle = not toggle sec = datetime.now().second if sec == 59: # When we change minutes, animate the minute change minute_change(device) elif sec == 30: # Half-way through each minute, display the complete date/time, # animating the time display into and out of the abyss. full_msg = time.ctime() animation(device, 1, 8) show_message(device, full_msg, fill="white", font=proportional(CP437_FONT)) animation(device, 8, 1) else: # Do the following twice a second (so the seconds' indicator blips). # I'd optimize if I had to - but what's the point? # Even my Raspberry PI2 can do this at 4% of a single one of the 4 cores! hours = datetime.now().strftime('%H') minutes = datetime.now().strftime('%M') with canvas(device) as draw: text(draw, (0, 1), hours, fill="white", font=proportional(CP437_FONT)) text(draw, (15, 1), ":" if toggle else " ", fill="white", font=proportional(TINY_FONT)) text(draw, (17, 1), minutes, fill="white", font=proportional(CP437_FONT)) time.sleep(0.5)
def animation(device, from_y, to_y): '''Animate the whole thing, moving it into/out of the abyss.''' hourstime = datetime.now().strftime('%H') mintime = datetime.now().strftime('%M') current_y = from_y while current_y != to_y: with canvas(device) as draw: text(draw, (0, current_y), hourstime, fill="white", font=proportional(CP437_FONT)) text(draw, (15, current_y), ":", fill="white", font=proportional(TINY_FONT)) text(draw, (17, current_y), mintime, fill="white", font=proportional(CP437_FONT)) time.sleep(0.1) current_y += 1 if to_y > from_y else -1
def emotion(self,data): emo_type = data.type if (emo_type==disp_emotion.POSITIVE): with canvas(self.device) as draw: text(draw, (0, 0), chr(1), fill="white") draw.point((3,4), fill="black") draw.point((4,4), fill="black") if (emo_type==disp_emotion.NEGATIVE): with canvas(self.device) as draw: text(draw, (0, 0), chr(1), fill="white") draw.point((2,4), fill="black") draw.point((5,4), fill="black") draw.point((3,5), fill="black") draw.point((4,5), fill="black") draw.point((2,5), fill="white") draw.point((5,5), fill="white") if (emo_type==disp_emotion.HAPPY): with canvas(self.device) as draw: text(draw, (0, 0), chr(1), fill="white") rospy.sleep(data.duration) self.disable_disp()
serial = spi(port=0, device=0, gpio=noop()) device = max7219(serial, cascaded=5, block_orientation=0) currentLoop = 0 if __name__ == "__main__": parser = argparse.ArgumentParser(description='matrix_demo arguments', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--cascaded', '-n', type=int, default=5, help='Number of cascaded MAX7219 LED matrices') parser.add_argument('--block-orientation', type=int, default=0, choices=[0, 90, -90], help='Corrects block orientation when wired vertically') args = parser.parse_args() while True: currentLoop = currentLoop + 1 Tv = str(currentLoop) Tv = Tv.rjust(5, " ") with canvas(device) as draw: text(draw, (0, 0), Tv, fill="white") print(Tv) time.sleep(1) if currentLoop >= 99999: currentLoop = 0
def demo(n, block_orientation, rotate, inreverse): # create matrix device serial = spi(port=0, device=0, gpio=noop()) device = max7219(serial, cascaded=n or 1, block_orientation=block_orientation, rotate=rotate or 0, blocks_arranged_in_reverse_order=inreverse) print("Created device") # start demo msg = "MAX7219 LED Matrix Demo" print(msg) show_message(device, msg, fill="white", font=proportional(CP437_FONT)) time.sleep(1) msg = "Fast scrolling: Lorem ipsum dolor sit amet, consectetur adipiscing\ elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut\ enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut\ aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in\ voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint\ occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit\ anim id est laborum." msg = re.sub(" +", " ", msg) print(msg) show_message(device, msg, fill="white", font=proportional(LCD_FONT), scroll_delay=0) msg = "Slow scrolling: The quick brown fox jumps over the lazy dog" print(msg) show_message(device, msg, fill="white", font=proportional(LCD_FONT), scroll_delay=0.1) print("Vertical scrolling") words = [ "Victor", "Echo", "Romeo", "Tango", "India", "Charlie", "Alpha", "Lima", " ", "Sierra", "Charlie", "Romeo", "Oscar", "Lima", "Lima", "India", "November", "Golf", " " ] virtual = viewport(device, width=device.width, height=len(words) * 8) with canvas(virtual) as draw: for i, word in enumerate(words): text(draw, (0, i * 8), word, fill="white", font=proportional(CP437_FONT)) for i in range(virtual.height - device.height): virtual.set_position((0, i)) time.sleep(0.05) msg = "Brightness" print(msg) show_message(device, msg, fill="white") time.sleep(1) with canvas(device) as draw: text(draw, (0, 0), "A", fill="white") time.sleep(1) for _ in range(5): for intensity in range(16): device.contrast(intensity * 16) time.sleep(0.1) device.contrast(0x80) time.sleep(1) msg = "Alternative font!" print(msg) show_message(device, msg, fill="white", font=SINCLAIR_FONT) time.sleep(1) msg = "Proportional font - characters are squeezed together!" print(msg) show_message(device, msg, fill="white", font=proportional(SINCLAIR_FONT)) # http://www.squaregear.net/fonts/tiny.shtml time.sleep(1) msg = "Tiny is, I believe, the smallest possible font \ (in pixel size). It stands at a lofty four pixels \ tall (five if you count descenders), yet it still \ contains all the printable ASCII characters." msg = re.sub(" +", " ", msg) print(msg) show_message(device, msg, fill="white", font=proportional(TINY_FONT)) time.sleep(1) msg = "CP437 Characters" print(msg) show_message(device, msg) time.sleep(1) for x in range(256): with canvas(device) as draw: text(draw, (0, 0), chr(x), fill="white") time.sleep(0.1)
def helper(current_y): with canvas(device) as draw: text(draw, (0, 1), hours, fill="white", font=proportional(CP437_FONT)) text(draw, (15, 1), ":", fill="white", font=proportional(TINY_FONT)) text(draw, (17, current_y), minutes, fill="white", font=proportional(CP437_FONT)) time.sleep(0.1)
def displayStatic(msg): # print(msg) with canvas(device) as draw: text(draw, (1, 0), msg, fill="white", font=proportional(LCD_FONT))