def lookForButtons(buttonNum): global pressed offscreen_canvas = matrix.CreateFrameCanvas() input_state = GPIO.input(buttonNum) if not input_state: clearlights() # print('press '+str(buttonNum)) if buttonNum != pressed: # new button was pressed GPIO.output(leds.get(buttonNum, ''), True) buttontext = { 18: 'button 1', 8: 'button 2', 24: 'button 3', 7: 'button 4', 9: 'button 5' } graphics.DrawText(offscreen_canvas, font, 0, 20, red, buttontext.get(buttonNum,'')) offscreen_canvas = matrix.SwapOnVSync(offscreen_canvas) time.sleep(0.2) pressed = buttonNum else: # active button was re-pressed, turn it off and clear screen pressed = 0 offscreen_canvas = matrix.SwapOnVSync(offscreen_canvas) offscreen_canvas.Clear() time.sleep(0.6)
def Run(self): #Change to take request as a arguement. Request contains text,colour,font. offscreenCanvas = self.matrix.CreateFrameCanvas() font = graphics.Font() font.LoadFont("matrix/fonts/7x13.bdf") red = request.json['colour']['red'] green = request.json['colour']['green'] blue = request.json['colour']['blue'] textColor = graphics.Color(red, green, blue) pos = offscreenCanvas.width myText = request.json['text'] while True: offscreenCanvas.Clear() len = graphics.DrawText(offscreenCanvas, font, pos, 10, textColor, myText) pos -= 1 if (pos + len < 0): pos = offscreenCanvas.width time.sleep(0.05) offscreenCanvas = self.matrix.SwapOnVSync(offscreenCanvas)
def __render_scroll_text(self): scroll_text = "W: %s %s-%s L: %s %s-%s" % ( self.game.winning_pitcher, self.game.winning_pitcher_wins, self.game.winning_pitcher_losses, self.game.losing_pitcher, self.game.losing_pitcher_wins, self.game.losing_pitcher_losses) return graphics.DrawText(self.canvas, self.font, self.scroll_pos, 31, self.text_color, scroll_text)
def __render_game_status(self): color = graphics.Color(*ledcolors.scoreboard.text) text = self.scoreboard.game_status if self.canvas.width == 32 and text == POSTPONED: text = POSTPONED_SHORTHAND text_x = center_text_position(text, self.canvas.width) graphics.DrawText(self.canvas, self.font, text_x, 20, color, text)
def Run(self): offscreenCanvas = self.matrix.CreateFrameCanvas() font = graphics.Font() font.LoadFont("/home/pi/rgb-led-array-stuff/fonts/7x13.bdf") randomRed = random.randint(0,255) randomBlue = random.randint(0,255) randomGreen = random.randint(0,255) textColor = graphics.Color(randomRed, randomBlue, randomGreen) pos = offscreenCanvas.width #myText = time.strftime ('%l:%M%p')#self.args["text"] height = random.randint(10,32) while True: offscreenCanvas.Clear() len = graphics.DrawText(offscreenCanvas, font, pos, height, textColor, time.strftime ('%l:%M %p %b %d')) pos -= 1 if (pos + len < 0): pos = offscreenCanvas.width height = random.randint(10,32) randomRed = random.randint(0,255) randomBlue = random.randint(0,255) randomGreen = random.randint(0,255) textColor = graphics.Color(randomRed, randomBlue, randomGreen) time.sleep(0.10) offscreenCanvas = self.matrix.SwapOnVSync(offscreenCanvas)
def displayTemp(self, centerX, centerY, currTemp, minTemp, maxTemp, dispColour): dispColour =self.getColourTemp(float(currTemp)) #Draw border squareSize = 6 self.drawDataBoarder(centerX, centerY, squareSize, dispColour) #Draw text centerX -= 3 centerY += 3 dispString = "%2.0f" % (float(currTemp)) dispString = dispString.strip() if len(dispString) == 1: charWidth = -2 elif len(dispString) == 2: charWidth = 0 else: if int(dispString) == 11: charWidth = 0 else: charWidth = 1 graphics.DrawText(self.offscreen_canvas, self.__font, centerX - charWidth, centerY, dispColour, dispString)
def run(self): offscreen_canvas = self.matrix.CreateFrameCanvas() font = graphics.Font() font.LoadFont("../../../fonts/7x13.bdf") textColor = graphics.Color(255, 255, 255) #white pos = offscreen_canvas.width my_text = self.args.text while True: offscreen_canvas.Clear() len = graphics.DrawText(offscreen_canvas, font, pos, 10, textColor, my_text) pos -= 1 if (pos + len < 0): pos = offscreen_canvas.width time.sleep(0.05) current_time = datetime.datetime.now() str_time = strftime(current_time.hour) + ":" + strftime( current_time.minute) # Reset the text to the current time my_text = str_time # Set the offscreen info to on screen offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
def drawWinScreen(self, winningPlayer): self.matrix.Clear() # display the winning player or tie if there is one (winningPlayer = 2) font = graphics.Font() font.LoadFont("./fonts/6x12.bdf") if winningPlayer == 2: graphics.DrawText(self.matrix, font, 5, 25, graphics.Color(200, 200, 200), "It's a tie!") else: graphics.DrawText(self.matrix, font, 5, 15, graphics.Color(200, 200, 200), "Congrats") graphics.DrawText(self.matrix, font, 5, 30, graphics.Color(200, 200, 200), "player {},".format(winningPlayer + 1)) graphics.DrawText(self.matrix, font, 5, 45, graphics.Color(200, 200, 200), "nice job!")
def render_nohit_text(canvas, layout, colors): font = layout.font("nohitter") coords = layout.coords("nohitter") text_color = colors.graphics_color("nohit_text") text = _get_nohitter_text(layout) graphics.DrawText(canvas, font["font"], coords["x"], coords["y"], text_color, text)
def display(self): offscreen_canvas = self.runner.matrix.CreateFrameCanvas() font = graphics.Font() font.LoadFont(self.get_font_path(self.message)) textColor = self.get_text_color(self.message) pos = offscreen_canvas.width #height = math.ceil(offscreen_canvas.width/2.0) +math.ceil(font.height/2.0)-1 height = math.ceil(offscreen_canvas.width / 2.0) + math.ceil( (font.height - 2) / 2.0) scrolling = True while scrolling: offscreen_canvas.Clear() len = graphics.DrawText(offscreen_canvas, font, pos, height, textColor, '{0}'.format(self.message['body'])) pos -= 1 if (pos + len < 0): pos = offscreen_canvas.width scrolling = False time.sleep(0.05) offscreen_canvas = self.runner.matrix.SwapOnVSync(offscreen_canvas) offscreen_canvas.Clear() offscreen_canvas = self.runner.matrix.SwapOnVSync(offscreen_canvas)
def draw_error(self, name: str): canvas = self.canvas graphics.DrawCircle(self.canvas, 12, 16, 10, self.circle_error_color) x_pos = 8 y_pos = 20 for i in range(0, 9): canvas.SetPixel(x_pos + i, y_pos - i, 255, 255, 255) x_pos = 8 y_pos = 12 for i in range(0, 9): canvas.SetPixel(x_pos +i, y_pos + i, 255, 255, 255) graphics.DrawText(self.canvas, self.font_error, 26, 14, self.font_error_color, 'FAILED ON') graphics.DrawText(self.canvas, self.font_info, 26, 24, self.font_info_color, name)
def show_spotify(self): # Refresh token payload = {'grant_type':'refresh_token', 'refresh_token':config.spotify_refresh_token} headers={"Authorization": config.spotify_client_auth} r = requests.post("https://accounts.spotify.com/api/token", headers=headers, data=payload) # Get current song token = r.json()['access_token'] headers={"Authorization": "Bearer {}".format(token)} url = "https://api.spotify.com/v1/me/player" response = requests.get(url, headers=headers) if response.text: json = response.json() current_song = str("{} by {}".format( json['item']['name'].encode('utf8'), json['item']['artists'][0]['name'].encode('utf8') ), errors='ignore') start = time.time() pos = self.canvas.width if self.spotify_song != current_song: self.spotify_song = current_song while time.time() < start + 30: self.canvas.Clear() len = graphics.DrawText(self.canvas, self.font, pos, 10, self.green, current_song) pos -= 1 if (pos + len < 0): pos = self.canvas.width time.sleep(0.05) self.canvas = self.matrix.SwapOnVSync(self.canvas)
def run(self): offscreen_canvas = self.matrix.CreateFrameCanvas() font = graphics.Font() font.LoadFont(f"../../fonts/{self.get_font_size(self.args.size)}.bdf") textColor = self.get_color(self.args.color) pos = offscreen_canvas.width my_text = self.args.text vert_offset = self.get_vertical_offset(self.args.size) sleeptime = self.get_sleep(self.args.speed) print(f"will only sleep for {sleeptime}") while True: offscreen_canvas.Clear() len = graphics.DrawText(offscreen_canvas, font, pos, vert_offset, textColor, my_text) pos -= 1 if pos + len < 0: pos = offscreen_canvas.width time.sleep(sleeptime) offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
def run(self): offscreen_canvas = self.matrix.CreateFrameCanvas() small_font = graphics.Font() small_font.LoadFont("../../../fonts/nanum9.bdf") large_font = graphics.Font() large_font.LoadFont("../../../fonts/nanum18.bdf") textColor = graphics.Color(255, 0, 0) pos = offscreen_canvas.width my_text = self.args.text wait_txt = ['탐스에오신걸환영합니다.'.encode('utf-8')] wait_txt.append('안전운전하세요.'.encode('utf-8')) wait_txt.append('TOMS'.encode('utf-8')) a = 0 i = 0 while True: offscreen_canvas.Clear() len = graphics.DrawText(offscreen_canvas, large_font, pos, 25, textColor, my_text) pos = pos - 10 if (pos + len < 0): a = a + 1 if a % 2 == 0: i = i + 1 pos = offscreen_canvas.width time.sleep(0.1) offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas) if a == 2: break
def __render_warmup(self): warmup_text = self.game.status coords = self.layout.coords("pregame.warmup_text") font = self.layout.font("pregame.warmup_text") color = self.colors.graphics_color("pregame.warmup_text") warmup_x = center_text_position(warmup_text, coords["x"], font["size"]["width"]) graphics.DrawText(self.canvas, font["font"], warmup_x, coords["y"], color, warmup_text)
def __render_clock(self): time_text = time.strftime("%-I:%M%p") coords = self.layout.coords("offday.time") font = self.layout.font("offday.time") color = self.colors.graphics_color("offday.time") text_x = center_text_position(time_text, coords["x"], font["size"]["width"]) graphics.DrawText(self.canvas, font["font"], text_x, coords["y"], color, time_text)
def process(self, matrix): #try: now = datetime.datetime.now() onOff = (time.mktime(now.timetuple()) % 2 == 0) if onOff: current_time = now.strftime("%H:%M") else: current_time = now.strftime("%H %M") current_date = now.strftime("%a%d") Utils.draw_blank_image(matrix, 40, 0, 24, 15) graphics.DrawText(matrix, self.font, 40, 7, self.color, current_time) graphics.DrawText(matrix, self.font, 40, 14, self.color, current_date)
def render_everything(can): can.Clear() len = graphics.DrawText(can, font, can.width - 12, 11, white_color, str(timer_number)) render_pick_ups(can) graphics.DrawCircle(can, player_x, player_y, 0, blue_color) can = matrix.SwapOnVSync(can)
def draw_text(self, x: int, y: int, text: str, color: int): color = graphics.Color(*hex2rgb(color)) font = graphics.Font() path = os.path.join(os.path.dirname(__file__), "..", "fonts", "7x13.bdf") font.LoadFont(path) graphics.DrawText(self.canvas, font, x, y, color, text)
def render_message(canvas, message, color): global lock_timer lock_timer = True canvas.Clear() len = graphics.DrawText(canvas, font, 2, 11, color, message) canvas = matrix.SwapOnVSync(canvas) time.sleep(5)
def initialize(self, width, height, doubleBuffer): self.font = graphics.Font() self.fontTime = graphics.Font() self.fontTime.LoadFont("/home/pi/rpi-rgb-led-matrix/fonts/7x14.bdf") self.font.LoadFont("/home/pi/rpi-rgb-led-matrix/fonts/10x20.bdf") self.now = datetime.now() currentTime = self.now.strftime("%-I:%M") currentMonth = self.now.strftime("%b %d") offset = 20 if self.now.hour % 12 == 0 or self.now.hour % 12 > 9: offset = 13 doubleBuffer.Clear() length = graphics.DrawText(doubleBuffer, self.fontTime, offset, 12, graphics.Color(225, 255, 0), currentTime) length = graphics.DrawText(doubleBuffer, self.font, 2, 28, graphics.Color(0, 255, 0), currentMonth)
def runTime(): global matrix offscreen_canvas = matrix.CreateFrameCanvas() # Figure out the font we are using. json_font = FontList['default'].get('font') json_size = FontList['default'].get('size') font = graphics.Font() font.LoadFont("/usr/share/fonts/TTF/" + json_font) #font.LoadFont("/root/rpi-rgb-led-matrix/fonts/5x8.bdf") textColor = graphics.Color(255, 255, 0) pos = offscreen_canvas.width my_text = 'foo' previousTime = '' print("/usr/share/fonts/TTF/" + json_font) while True: offscreen_canvas.Clear() len = graphics.DrawText(offscreen_canvas, font, pos, 10, textColor, my_text) pos -= 1 if (pos + len < 0): pos = offscreen_canvas.width print pos time.sleep(0.05) offscreen_canvas = matrix.SwapOnVSync(offscreen_canvas)
def led_scroll_text(): matrix = RGBMatrix(options=options) offscreen_canvas = matrix.CreateFrameCanvas() cwd = os.path.dirname(__file__) font_path = os.path.join(cwd, font_filename) font = graphics.Font() font.LoadFont(font_path) textColor = graphics.Color(*text_color) pos = offscreen_canvas.width scroll_text = grab_scores() count = 0 while True: offscreen_canvas.Clear() len = graphics.DrawText(offscreen_canvas, font, pos, 13, textColor, scroll_text) pos -= 1 if pos + len < 0: pos = offscreen_canvas.width count += 1 if count >= 1: count = 0 print("Refreshing scores...") scroll_text = grab_scores() time.sleep(ticker_speed) offscreen_canvas = matrix.SwapOnVSync(offscreen_canvas)
async def update_data(self): width = configuration.matrix_options.cols * configuration.matrix_options.chain_length canvas = matrix.matrix.CreateFrameCanvas() self.text_length = graphics.DrawText(canvas, self.font, width, 12, self.color, self.text) del canvas self.total_steps = width + self.text_length
def render(self): font = self.layout.font("nohitter") coords = self.layout.coords("nohitter") text_color = self.colors.graphics_color("nohit_text") text = self.nohitter_text() graphics.DrawText(self.canvas, font["font"], coords["x"], coords["y"], text_color, text)
def __display_todays_high(self): font = graphics.Font() font.LoadFont("/home/pi/projects/weather-python/rpi-rgb-led-matrix/fonts/5x7.bdf") temp_max_int = round(self.todays_high) text = '\u2191 ' + str(temp_max_int) + '\u00b0F' graphics.DrawText(self.matrix, font, 2, 62, self.__get_color_gradient(temp_max_int), text)
def __render_game_status(canvas, layout, colors, scoreboard, short_text): color = colors.graphics_color("status.text") text = __get_text_for_status(scoreboard, short_text) coords = layout.coords("status.text") font = layout.font("status.text") text_x = center_text_position(text, coords["x"], font["size"]["width"]) graphics.DrawText(canvas, font["font"], text_x, coords["y"], color, text)
def __render_start_time(self): time_text = self.game.start_time coords = self.layout.coords("pregame.start_time") font = self.layout.font("pregame.start_time") color = self.colors.graphics_color("pregame.start_time") time_x = center_text_position(time_text, coords["x"], font["size"]["width"]) graphics.DrawText(self.canvas, font["font"], time_x, coords["y"], color, time_text)
def __render_final_inning(self): color = graphics.Color(*ledcolors.scoreboard.text) text = "FINAL" if self.scoreboard.inning.number > NORMAL_GAME_LENGTH: text += " " + str(self.scoreboard.inning.number) text_x = center_text_position(text, self.canvas.width) graphics.DrawText(self.canvas, self.font, text_x, 20, color, text)
def render_standings(matrix, canvas, division): font = graphics.Font() font.LoadFont('Assets/tom-thumb.bdf') text_color = graphics.Color(*ledcolors.standings.text) canvas.Fill(*ledcolors.standings.fill) stat = 'w' starttime = time.time() while True: offset = 6 for team in division.teams: abbrev = '{:3s}'.format(team.team_abbrev) text = '%s %s' % (abbrev, getattr(team, stat)) graphics.DrawText(canvas, font, 1, offset, text_color, text) for x in range(0, canvas.width): canvas.SetPixel(x, offset, *ledcolors.standings.divider) for y in range(0, canvas.height): canvas.SetPixel(14, y, *ledcolors.standings.divider) offset += 6 matrix.SwapOnVSync(canvas) time.sleep(5.0 - ((time.time() - starttime) % 5.0)) canvas.Fill(*ledcolors.standings.fill) stat = 'w' if stat == 'l' else 'l'