class test_class:#(SampleBase): def __init__(self, *args, **kwargs): self.o = RGBMatrixOptions() self.o.rows = 16 self.o.cols = 32 self.o.chain_length = 3 self.o.parallel = 3 self.o.multiplexing = 8 self.o.row_address_type = 0 self.o.pwm_lsb_nanoseconds = 90 self.o.brightness = 1 self.matrix = RGBMatrix(options=self.o) # super(test_class, self).__init__(*args, **kwargs) def run(self): print "Running Test!" canvas = self.matrix.CreateFrameCanvas() for y in range(self.matrix.height): for x in range(self.matrix.width): canvas.SetPixel(x,y,255,255,255) print "(%i, %i)" % (x, y) canvas = self.matrix.SwapOnVSync(canvas) def b_test(self): canvas = self.matrix.CreateFrameCanvas() canvas.Fill(255,255,255) while True: self.matrix.brightness = self.matrix.brightness + 1 canvas.Fill(255,255,255) print str(self.matrix.brightness) + "%" canvas = self.matrix.SwapOnVSync(canvas) sleep(1)
class Sign(object): #Handles LED Maxtix Display taking in 2 buses and displaying them on the screen def __init__(self): self.options = RGBMatrixOptions() self.options.rows = 16 self.options.chain_length = 2 self.options.parallel = 1 self.options.hardware_mapping = 'adafruit-hat' # If you have an Adafruit HAT: 'adafruit-hat' self.matrix = RGBMatrix(options=self.options) self.outlineColor = graphics.Color(128, 128, 128) self.textColor = graphics.Color(0, 128, 255) self.font = graphics.Font() self.font.LoadFont(FONT) def makeBus(self, topBus, botBus): #set up canvas blank canvas self.canvas = self.matrix.CreateFrameCanvas() #Draw boarders around each line of text graphics.DrawLine(self.canvas, 0, 7, 63, 7, self.outlineColor) graphics.DrawLine(self.canvas, 0, 8, 63, 8, self.outlineColor) graphics.DrawLine(self.canvas, 0, 0, 63, 0, self.outlineColor) graphics.DrawLine(self.canvas, 0, 15, 63, 15, self.outlineColor) graphics.DrawLine(self.canvas, 0, 0, 0, 15, self.outlineColor) graphics.DrawLine(self.canvas, 63, 0, 63, 15, self.outlineColor) #draw Top Bus if topBus: print 'making bus' if len(topBus[u'des']) > 8: topDest = topBus[u'des'][:7] else: topDest = topBus[u'des'] busString = '{} {}: {}'.format(topBus[u'rt'], topDest, topBus[u'prdctdn']) graphics.DrawText(self.canvas, self.font, 2, 7, self.textColor, busString) #Draw Bottom Bus if botBus: busString = '{} {}: {}'.format(botBus[u'rt'], botBus[u'des'], botBus[u'prdctdn']) graphics.DrawText(self.canvas, self.font, 2, 15, self.textColor, busString) def display(self): self.canvas = self.matrix.SwapOnVSync(self.canvas) def blankDisplay(self): self.canvas = self.matrix.CreateFrameCanvas() self.display()
def initialize(): global options, matrix, canvas, store global default_font, light_font options = RGBMatrixOptions() options.rows = led_rows options.cols = led_cols options.chain_length = led_chain options.parallel = led_parallel options.brightness = led_brightness options.hardware_mapping = led_hardware_mapping options.disable_hardware_pulsing = True options.pwm_lsb_nanoseconds = 130 # if you're not using raspberry pi 3 comment line below options.gpio_slowdown = 2 matrix = RGBMatrix(options=options) canvas = matrix.CreateFrameCanvas() default_font = graphics.Font() default_font.LoadFont(default_font_path) light_font = graphics.Font() light_font.LoadFont(light_font_path) store = redis.StrictRedis(host='localhost', port=6379, db=0)
def run(options): """run the countdown clock""" matrix = RGBMatrix(options=options) offscreen_canvas = matrix.CreateFrameCanvas() font = graphics.Font() font.LoadFont("rpi-rgb-led-matrix/fonts/9x18B.bdf") time_color = graphics.Color(255, 217, 25) co2_color = graphics.Color(153, 0, 230) while True: offscreen_canvas.Clear() # calculate remaining seconds and co2 now = datetime.now() seconds_since_start = (now - START_DATE).total_seconds() remaining_co2 = START_BUDGET - seconds_since_start * TONS_PER_SEC remaining_seconds = remaining_co2 / TONS_PER_SEC # format text remaining_time_text = seconds_to_timestring(remaining_seconds) remaining_co2_text = "{:,} TONS".format(int(remaining_co2)) # draw to screen graphics.DrawText(offscreen_canvas, font, 1, 14, time_color, remaining_time_text) graphics.DrawText(offscreen_canvas, font, 1, 28, co2_color, remaining_co2_text) offscreen_canvas = matrix.SwapOnVSync(offscreen_canvas) time.sleep(PAUSE)
class guerillaDisplay(object): def __init__(self): self.options = RGBMatrixOptions() self.options.rows = 16 self.options.pwm_bits = 6 self.options.pwm_lsb_nanoseconds = 300 self.options.chain_length = 1 self.options.parallel = 1 self.options.hardware_mapping = 'regular' # If you have an Adafruit HAT: 'adafruit-hat' self.font = graphics.Font() self.font.LoadFont("rpi-rgb-led-matrix/fonts/5x8.bdf") self.textColor = graphics.Color(255, 0, 0) def initiate(self): self.matrix = RGBMatrix(options=self.options) self.offscreen_canvas = self.matrix.CreateFrameCanvas() def set(self, m): self.msg = m def cellOn(self, x, y, r, g, b): self.r = int(r) self.g = int(g) self.b = int(b) self.matrix.SetPixel(x, y, self.r, self.g, self.b) def cellOff(self, x, y): self.matrix.SetPixel(x, y, 0, 0, 0) def clear(self): self.matrix.Clear()
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)
class RGBMatrixDisplay(): def __init__(self, config): from rgbmatrix import RGBMatrix, RGBMatrixOptions options = RGBMatrixOptions() options.rows = config.getint('rows') options.cols = config.getint('cols') options.hardware_mapping = config['led_hardware_mapping'] options.chain_length = config.getint('led_chain') options.parallel = config.getint('led_parallel') options.pwm_bits = config.getint('led_pwm_bits') options.brightness = config.getint('led_brightness') options.pwm_lsb_nanoseconds = config.getint('led_pwm_lsb_nanoseconds') options.inverse_colors = config.getboolean('led_inverse') options.led_rgb_sequence = config['led_rgb_sequence'] options.pixel_mapper_config = config['led_pixel_mapper'] options.row_address_type = config.getint('led_row_addr_type') options.multiplexing = config.getint('led_multiplexing') options.scan_mode = config.getint('led_scan_mode') options.gpio_slowdown = config.getint('led_slowdown_gpio') options.disable_hardware_pulsing = config.getboolean( 'led_no_hardware_pulse') options.show_refresh_rate = config.getboolean('led_show_refresh') options.pwm_dither_bits = config.getint('led_pwm_dither_bits') #options.panel_type = config['led_panel_type'] self.matrix = RGBMatrix(options=options) self.offscreen_canvas = self.matrix.CreateFrameCanvas() def display(self, img): self.offscreen_canvas.SetImage(img) self.offscreen_canvas = self.matrix.SwapOnVSync(self.offscreen_canvas)
class MatrixBase: def __init__(self, rows, cols, refresh_rate=20): self.rows = rows self.cols = cols self.refresh_rate = refresh_rate options = RGBMatrixOptions() options.rows = rows options.cols = cols options.hardware_mapping = 'adafruit-hat' self.options = options self.matrix = RGBMatrix(options=options) self.font = create_font() self.widths = widths( self.font, '$!@#%^*()[]0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-=+_. ' ) self.frame = self.matrix.CreateFrameCanvas() def flush(self): self.frame = self.matrix.SwapOnVSync(self.frame) def clear(self): self.frame.Clear() def stop(self): self.matrix.Clear()
def main(): # init the RGB matrix as 32 Rows, 2 panels (represents 32 x 64 panel), 1 chain MyMatrix = RGBMatrix(32, 2, 1) # Bits used for PWM. Something between 1..11. Default: 11 MyMatrix.pwmBits = 11 # Sets brightness level. Default: 100. Range: 1..100" MyMatrix.brightness = 25 # Create the buffer canvas MyOffsetCanvas = MyMatrix.CreateFrameCanvas() # clear down the matrix MyOffsetCanvas.Clear() # loops of random colour pixels for z in range(0, 256): for x in range(0, 64): for y in range(0, 32): R = numpy.random.randint(0, 255) G = numpy.random.randint(0, 255) B = numpy.random.randint(0, 255) MyOffsetCanvas.SetPixel(x, y, R, G, B) #endfor #endfor MyOffsetCanvas = MyMatrix.SwapOnVSync(MyOffsetCanvas) #endfor return
class Cocktapus(object): def __init__(self): self.options = RGBMatrixOptions() self.options.hardware_mapping = 'adafruit-hat' self.options.rows = 32 self.options.parallel = 1 self.options.chain_length = 4 self.options.pwm_bits = 11 self.options.brightness = 100 self.options.pwm_lsb_nanoseconds = 130 self.matrix = RGBMatrix(options=self.options) self.canvas = self.matrix.CreateFrameCanvas() self.default_color = graphics.Color(255, 0, 0) #self.emulator = Matrix() #self.emulator.test() def test(self): for row in xrange(len(self.emulator.pixels[0])): for column in xrange(len(self.emulator.pixels[0][0])): if self.emulator.pixels[0][column][row]: self.matrix.set_pixel(column, row, self.default_color) self.matrix.SwapOnVSync(self.canvas) while True: pass
def matrix_options(): ''' Params: options - Display Board connection options matrix - Sets the options Returns: matrix, offscreen_canvas ''' options = RGBMatrixOptions() options.rows = 16 options.cols = 32 options.gpio_slowdown = 4 options.brightness = 100 options.chain_length = 1 options.parallel = 1 options.row_address_type = 0 options.pwm_bits = 11 options.pwm_lsb_nanoseconds = 130 options.led_rgb_sequence = 'RGB' options.pixel_mapper_config = '' #options.panel_type = '' # -> supported: str('FM6126A') options.show_refresh_rate = 0 #options.hardware_mapping = 'regular' matrix = RGBMatrix(options = options) offscreen_canvas = matrix.CreateFrameCanvas() return (matrix, offscreen_canvas)
def run(options): matrix = RGBMatrix(options=options) canvas = matrix.CreateFrameCanvas() font_file = ['5x8.bdf', '6x13B.bdf', '9x18B.bdf'][options.chain_length - 1] font = graphics.Font() font.LoadFont(relpath(font_file)) t_color = hex2color(TIME_COLOR) ta_color = hex2color(TIME_ALT_COLOR) b_color = hex2color(BUDGET_COLOR) ba_color = hex2color(BUDGET_ALT_COLOR) while not time.sleep(.1): time_remaining, emissions_budget = time_and_budget() years, r = divmod(time_remaining, SECONDS_PER_YEAR) days, r = divmod(r, 24 * 3600) hours, r = divmod(r, 3600) minutes, seconds = divmod(r, 60) canvas.Clear() t_strings = [ f"{years:1.0f}", f"{'YR' if years == 1 else 'YRS'}", f"{days:3.0f}", f"{'DAY' if days == 1 else 'DAYS'}", f"{hours:02.0f}:{minutes:02.0f}:{seconds:02.0f}", ] b_strings = [ f"{int(emissions_budget):,}"[:12], f"{int(emissions_budget):,}"[12:], f"TONS", ] # Place text fragments according to number of screens t_colors = [t_color, ta_color, t_color, ta_color, t_color] b_colors = [b_color, b_color, ba_color] if options.chain_length == 3: t_pos = (2, 14), (16, 14), (47, 14), (78, 14), (118, 14) b_pos = (2, 28), (111, 28), (145, 28) elif options.chain_length == 2: t_pos = (2, 14), (10, 14), (31, 14), (51, 14), (78, 14) b_pos = (2, 28), (74, 28), (96, 28) elif options.chain_length == 1: t_pos = (3, 7), (9, 7), (26, 7), (42, 7), (3, 15), (42, 15) t_strings.append(f".{int(seconds%1*1000):03d}") t_colors.append(t_color) b_pos = (3, 23), (3, 31), (22, 31) b_strings[2] = "TONS CO₂" for pos, strings, colors in ((t_pos, t_strings, t_colors), (b_pos, b_strings, b_colors)): for (x, y), s, c in zip(pos, strings, colors): graphics.DrawText(canvas, font, x, y, c, s) canvas = matrix.SwapOnVSync(canvas)
class Matrix(object): def __init__(self, rows=16, chain_length=1, font_width='5', font_height='8'): self.rgbmatrix = RGBMatrix(rows, chain_length) self.font_width = int(font_width) self.font_height = int(font_height) self.font = graphics.Font() self.font.LoadFont("fonts/{0}x{1}.bdf".format(font_width, font_height)) self.canvas = self.rgbmatrix.CreateFrameCanvas() def clear(self): utils.debug("Matrix cleared") self.canvas.Clear() def render_arrival(self, arrival): utils.debug(arrival) eta = arrival.eta() if eta < timedelta(minutes=1): minutes = 1 eta_text = "<1 min!" else: minutes = old_div(eta.seconds, 60) eta_text = "{0} min".format(minutes) self.render_line(1, arrival.route, 0xff00ff) self.render_line(2, eta_text, self.urgency_color(minutes)) self.canvas = self.rgbmatrix.SwapOnVSync(self.canvas) def render_no_arrivals(self): self.render_line(1, "No :(", 0xff00ff) self.render_line(2, "Buses", 0xff00ff) self.canvas = self.rgbmatrix.SwapOnVSync(self.canvas) def render_line(self, line_num, text, rgb): y_offset = (line_num - 0) * self.font_height - 1 r = (rgb >> 16) & 0xff g = (rgb >> 8) & 0xff b = (rgb >> 0) & 0xff text_color = graphics.Color(r, g, b) graphics.DrawText(self.canvas, self.font, 1, y_offset, text_color, text) def urgency_color(self, minutes): global URGENCY_COLORS if minutes >= URGENCY_COLORS['green']: utils.debug("{0} minutes -- green".format(minutes)) return 0x00ff00 elif minutes >= URGENCY_COLORS['yellow'] and minutes < URGENCY_COLORS[ 'green']: utils.debug("{0} minutes -- yellow".format(minutes)) return 0xffff00 else: utils.debug("{0} minutes -- red".format(minutes)) return 0xff0000
def led_display(): matrix = RGBMatrix(options=options) offscreen_canvas = matrix.CreateFrameCanvas() pos = offscreen_canvas.width font_path = "fonts/" count = 0 # Kolonial delivery = delivery_status() clock_color = graphics.Color(255, 165, 20) clock_font = graphics.Font() clock_font.LoadFont(os.path.join(font_path, "6x9.bdf")) k_header = "Kolonial.no Delivery Tracker" k_header_color = graphics.Color(255, 165, 20) k_header_font = graphics.Font() k_header_font.LoadFont(os.path.join(font_path, "6x9.bdf")) d_title = delivery.get("title") d_title_color = graphics.Color(0, 255, 0) d_title_font = graphics.Font() d_title_font.LoadFont(os.path.join(font_path, "clR6x12.bdf")) d_status = delivery.get("status") d_status_color = graphics.Color(238, 238, 228) d_status_font = graphics.Font() d_status_font.LoadFont(os.path.join(font_path, "6x12.bdf")) while True: clock = str(datetime.now().strftime("%H:%M:%S")) count += 1 if count == refresh_rate: count = 0 print(f"{datetime.now()} - Refreshing data...") delivery = delivery_status() d_title = delivery.get("title") d_status = delivery.get("status") offscreen_canvas.Clear() graphics.DrawText(offscreen_canvas, k_header_font, 0, 6, k_header_color, k_header) graphics.DrawText(offscreen_canvas, clock_font, 207, 6, clock_color, clock) graphics.DrawText( offscreen_canvas, d_title_font, 12, 19, d_title_color, d_title) len = graphics.DrawText(offscreen_canvas, d_status_font, pos, 31, d_status_color, d_status) pos -= 1 if pos + len < 0: pos = offscreen_canvas.width time.sleep(text_speed) offscreen_canvas = matrix.SwapOnVSync(offscreen_canvas)
class Asteroids(): def __init__(self, gamepad): self.gamepad = gamepad self.player = SpaceShip() self.num_rocks = 10 self.rocks = [] for i in range(self.num_rocks): self.rocks.append(Rock()) options = RGBMatrixOptions() options.hardware_mapping = "regular" options.rows = 64 options.cols = 64 options.chain_length = 1 options.parallel = 1 options.row_address_type = 0 options.multiplexing = 0 options.pwm_bits = 11 options.brightness = 100 options.pwm_lsb_nanoseconds = 130 #options.led_rgb_sequence = "RGB" options.pixel_mapper_config = "" self.matrix = RGBMatrix(options = options) def moveObjects(self): self.player.move() for i in range(len(self.rocks)): self.rocks[i].move() def drawFrame(self): canvas = self.matrix.CreateFrameCanvas() self.player.draw(canvas) for i in range(len(self.rocks)): self.rocks[i].draw(canvas) offset_canvas = self.matrix.SwapOnVSync(canvas) def run(self): while True: if (self.gamepad.buttons[gamepad.leftCode]): self.player.orientation -= 0.125 elif (self.gamepad.buttons[gamepad.rightCode]): self.player.orientation += 0.125 elif (self.gamepad.buttons[gamepad.upCode]): self.player.xvel += 0.0125 * math.cos(self.player.orientation) self.player.yvel += 0.0125 * math.sin(self.player.orientation) for i in range(len(self.rocks)): if self.player.collision(self.rocks[i]): self.player = SpaceShip() break self.moveObjects() self.drawFrame()
class Hardware(object): def __init__(self): self.options = RGBMatrixOptions() self.options.hardware_mapping = 'adafruit-hat' self.options.rows = 32 self.options.parallel = 1 self.options.chain_length = 4 self.options.pwm_bits = 11 self.options.brightness = 100 self.options.pwm_lsb_nanoseconds = 130 self.matrix = RGBMatrix(options=self.options) self.canvas = self.matrix.CreateFrameCanvas() self.default_color = graphics.Color(255, 0, 0)
class RunText(): def __init__(self, *args, **kwargs): options = RGBMatrixOptions() options.hardware_mapping = "adafruit-hat" options.rows = 32 options.cols = 64 self.matrix = RGBMatrix(options=options) try: print("Press CTRL-C to stop sample") self.run() except KeyboardInterrupt: print("Exiting\n") sys.exit(0) return True def run(self): offscreen_canvas = self.matrix.CreateFrameCanvas() font = graphics.Font() font.LoadFont("fonts/10x20.bdf") textColorR = graphics.Color(255, 0, 0) textColorG = graphics.Color(0, 255, 0) textColorB = graphics.Color(0, 0, 255) pos = offscreen_canvas.width my_text = "Hallo Tante Dorli! :-)" counter = 0 while True: offscreen_canvas.Clear() if counter % 60 < 20: textColor = textColorR elif counter % 60 < 40: textColor = textColorG elif counter % 60 < 60: textColor = textColorB counter += 1 len = graphics.DrawText(offscreen_canvas, font, pos, 20, textColor, my_text) pos -= 1 if (pos + len < 0): pos = offscreen_canvas.width time.sleep(0.03) offscreen_canvas = self.matrix.SwapOnVSync(offscreen_canvas)
class ScoreboardDisplay: def __init__(self): options = RGBMatrixOptions() # TODO MOVE TO CONFIG FILE options.hardware_mapping = "adafruit-hat" options.disable_hardware_pulsing = True options.rows = 16 options.cols = 32 options.chain_length = 4 options.pixel_mapper_config = "U-mapper" options.multiplexing = 8 # CORRECT value for 1/4 SCAN PANELS self.matrix = RGBMatrix(options=options) self.offscreen_canvas = self.matrix.CreateFrameCanvas()
class Renderer: def __init__(self): self.useEmulator = os.getenv("EMULATOR") is not None if not self.useEmulator: os.environ["SDL_VIDEODRIVER"] = "dummy" os.environ['SDL_AUDIODRIVER'] = 'dsp' pygame.init() if self.useEmulator: self.size = (32, 32) self.screen = pygame.display.set_mode((512, 512)) self.Content = pygame.Surface(self.size) else: self.size = (32, 32) self.Content = pygame.display.set_mode(self.size) from rgbmatrix import RGBMatrix self.matrix = RGBMatrix(options=self.__generateMatrixOptions()) self.canvas = self.matrix.CreateFrameCanvas() def __generateMatrixOptions(self): from rgbmatrix import RGBMatrixOptions options = RGBMatrixOptions() options.rows = 32 options.chain_length = 1 options.parallel = 1 options.hardware_mapping = 'regular' options.pwm_lsb_nanoseconds = 160 options.brightness = 80 options.drop_privileges = False return options def run(self): if self.useEmulator: pygame.transform.scale(self.Content, (512, 512), self.screen) pygame.display.flip() else: pygame.display.flip() for x in range(32): for y in range(32): color = self.Content.get_at((x, y)) self.canvas.SetPixel(x, y, color.r, color.g, color.b) self.matrix.SwapOnVSync(self.canvas) def close(self): pygame.display.quit() pygame.quit()
class LedDisplay(Display): defaultFont = LedFont("fonts/tom-thumb.bdf") defaultColor = LedColor.Green def __init__(self, options, font=defaultFont): self._logger = LogManager.get_logger(__name__) self._logger.debug("Initializing display with '{0}'".format(options)) matrix_options = RGBMatrixOptions() matrix_options.hardware_mapping = options.led_gpio_mapping matrix_options.rows = options.led_rows matrix_options.cols = options.led_cols matrix_options.chain_length = options.led_chain matrix_options.parallel = options.led_parallel matrix_options.row_address_type = options.led_row_addr_type matrix_options.multiplexing = options.led_multiplexing matrix_options.pwm_bits = options.led_pwm_bits matrix_options.brightness = options.led_brightness matrix_options.pwm_lsb_nanoseconds = options.led_pwm_lsb_nanoseconds matrix_options.led_rgb_sequence = options.led_rgb_sequence matrix_options.show_refresh_rate = options.led_show_refresh matrix_options.gpio_slowdown = options.led_slowdown_gpio matrix_options.disable_hardware_pulsing = options.led_no_hardware_pulse self._matrix = RGBMatrix(options = matrix_options) self._offscreen_canvas = self._matrix.CreateFrameCanvas() self._font = font def write(self, message, color=defaultColor): self._offscreen_canvas.Clear() x = 0 y = self._font.baseline for c in message: # support for '\n' if c == "\n": x = 0 y += (self._font.baseline + 1) # add an extra space so that the next line doesn't hug the baseline continue x += self._font.DrawGlyph(self._offscreen_canvas, x, y, graphics.Color(color.R, color.G, color.B), ord(c)) self._offscreen_canvas = self._matrix.SwapOnVSync(self._offscreen_canvas) def clear(self): self._offscreen_canvas.Clear() self._offscreen_canvas = self._matrix.SwapOnVSync(self._offscreen_canvas)
class Led_Setup(object): #インスタンス生成時に実行 def __init__(self,chain=4,bright=50): #デフォルト設定(引数なしの場合) #Options self.options = RGBMatrixOptions() self.options.rows = 32 self.options.chain_length = chain self.options.parallel = 1 self.options.hardware_mapping = 'adafruit-hat-pwm' self.options.brightness = bright self.options.show_refresh_rate = 0 self.matrix = RGBMatrix(options=self.options) self.canvas = self.matrix.CreateFrameCanvas() #Load Font self.gothic = graphics.Font() self.gothic.LoadFont("Resources/Gothic-16.bdf") #明朝は現時点で未使用なので、コメントアウト """ self.mincho = graphics.Font() self.mincho.LoadFont("Resources/Mincho-16.bdf") """ #Load images self.icon_twitter = Image.open("Resources/icon_twitter.ppm").convert('RGB') self.icon_twitter_width,self.icon_twitter_height = self.icon_twitter.size self.atos = Image.open("Resources/atos.png").convert('RGB') self.atos_en = Image.open("Resources/atos-en.png").convert('RGB') #LED長さ self._width = self.canvas.width self._height = self.canvas.height #Colors self.orange = graphics.Color(255, 110, 0) self.blue = graphics.Color(0, 220, 255) self.white = graphics.Color(255, 255, 255) self.red = graphics.Color(255, 0, 0) self.green = graphics.Color(0,255,0) self.peach = graphics.Color(255, 0, 255) #ループ制御用変数 self.stopper = False #マイクロ秒換算関数 def usleep(self, value): time.sleep(value / 1000000.0)
class Metroclock(object): #インスタンス生成時に実行 def __init__(self, chain=4, bright=50): #デフォルト設定(引数なしの場合) #Options self.options = RGBMatrixOptions() self.options.rows = 32 self.options.chain_length = chain self.options.parallel = 1 self.options.hardware_mapping = 'adafruit-hat-pwm' self.options.brightness = bright self.options.show_refresh_rate = 0 self.matrix = RGBMatrix(options=self.options) self.canvas = self.matrix.CreateFrameCanvas() # 時計用フォント読み込み self.clcfont = graphics.Font() self.clcfont.LoadFont("Resources/Metroclock.bdf") #LED長さ self._width = self.canvas.width self._height = self.canvas.height #Colors self.orange = graphics.Color(255, 110, 0) self.blue = graphics.Color(0, 220, 255) self.white = graphics.Color(255, 255, 255) self.red = graphics.Color(255, 0, 0) self.green = graphics.Color(0, 255, 0) #ループ制御用変数 self.stopper = False def run(self): while True: self.canvas.Clear() hour = dt.now().strftime("%H") minute = dt.now().strftime("%M") sec = dt.now().strftime("%S") if int(sec) % 2 == 0: text = hour + ':' + minute else: text = hour + ' ' + minute graphics.DrawText(self.canvas, self.clcfont, 0, 16, self.white, text) self.canvas = self.matrix.SwapOnVSync(self.canvas) time.sleep(0.1)
class RGBBase: def __init__(self): # RGB Matrix Configuration self.__options__ = RGBMatrixOptions() self.__options__.rows = 16 self.__options__.cols = 32 self.__options__.chain_length = 3 self.__options__.parallel = 3 self.__options__.multiplexing = 3 self.__options__.row_address_type = 2 self.__options__.brightness = 1 # Create the matrix stuff self.__matrix__ = RGBMatrix(options=self.__options__) self.__offscreen_canvas__ = self.__matrix__.CreateFrameCanvas() self.__offscreen_canvas__.Clear() # Create arrays to hold the child views self.__children__ = [] def redraw(self): self.__offscreen_canvas__.Clear() for child in self.__children__: child.render(self.__matrix__, self.__offscreen_canvas__) self.__offscreen_canvas__ = self.__matrix__.SwapOnVSync( self.__offscreen_canvas__) for child in self.__children__: if type(child) != RGBLabel: child.render(self.__matrix__, self.__offscreen_canvas__) elif child.__textStyle__ == TextStyle.IMAGE: child.render(self.__matrix__, self.__offscreen_canvas__) def addView(self, view): self.__children__.append(view) self.redraw() def removeAllViews(self): self.__offscreen_canvas__.Clear() self.__children__ = [] self.redraw() print('Removed All View') def setBrightness(self, dataStr): self.__matrix__.brightness = int(dataStr) self.redraw()
def run(self): print('[Display] Display thread started.') # Configure matrix options options = RGBMatrixOptions() options.hardware_mapping = constants.matrix_hardware_mapping options.rows = constants.matrix_rows options.cols = constants.matrix_cols options.brightness = constants.matrix_brightness options.pwm_bits = constants.matrix_pwm_bits options.pwm_lsb_nanoseconds = constants.matrix_pwm_lsb_nanoseconds # Init matrix and canvas matrix = RGBMatrix(options=options) canvas = matrix.CreateFrameCanvas() # Configure font and colors font = graphics.Font() font.LoadFont('font.bdf') primaryColor = graphics.Color(255, 217, 0) secondaryColor = graphics.Color(255, 255, 255) # Init loop update_time = 0 count = 0 # Loop until the stop flag is raised while not self.stop_flag.is_set(): # Animate counting if self.count != count: update_time = time.time() delta = self.count - count count = count + \ int(math.ceil(abs(delta) * constants.count_speed)) * \ (delta < 0 and -1 or 1) # Select color cooldown = update_time > time.time() - constants.highlight_cooldown color = secondaryColor if cooldown else primaryColor # Draw digits to canvas canvas.Clear() graphics.DrawText(canvas, font, 0, 25, color, str(count).zfill(4)) canvas = matrix.SwapOnVSync(canvas) # Wait for next cycle time.sleep(0.1)
class PixelWall: def __init__(self, width, height): self.width = width self.height = height self.matrix = RGBMatrix(32, 6, 1) self.matrix.pwmBits = 11 self.matrix.brightness = 50 self.matrix.luminanceCorrect = True self.bufferCanvas = self.matrix.CreateFrameCanvas() def setPixel(self, x, y, r, g, b): x = self.width - x - 1 if y > 31: x = 191 - x y = (y % 32) else: y = 31 - y self.bufferCanvas.SetPixel(x, y, r, g, b) def render(self): self.bufferCanvas = self.matrix.SwapOnVSync(self.bufferCanvas)
class Display: def __init__(self): self.matrix = RGBMatrix(16) self.matrix.brightness = 30 self.canvas = self.matrix.CreateFrameCanvas() self.width = 32 self.height = 16 def render(self, frame): min_height = min(self.height, frame.height) min_width = min(self.width, frame.width) for row in range(min_height): for col in range(min_width): pixel = frame.get_pixel(col, row) r, g, b = pixel.red, pixel.green, pixel.blue self.canvas.SetPixel(col, row, r, g, b) self.canvas = self.matrix.SwapOnVSync(self.canvas) def clean(self): self.canvas.Fill(0, 0, 0) self.canvas = self.matrix.SwapOnVSync(self.canvas)
class test_class: def __init__(self, *args, **kwargs): o = RGBMatrixOptions() o.rows = 16 o.cols = 32 o.chain_length = 3 o.parallel = 3 o.multiplexing = 8 o.row_address_type = 0 o.pwm_lsb_nanoseconds = 100 o.brightness = 25 self.matrix = RGBMatrix(options=o) def run(self): print "Running Test!" center_x = self.matrix.width // 2 center_y = self.matrix.height // 2 canvas = self.matrix.CreateFrameCanvas() canvas.Fill(0, 0, 255) radius_max = canvas.width / 2.0 angle_step = 1 / 360.0 a = 0 r = 0 while r < center_y: dot_x = cos(a * 2 * pi) * r dot_y = sin(a * 2 * pi) * r canvas.SetPixel(center_x + dot_x, center_y + dot_y, 255, 0, 0) canvas = self.matrix.SwapOnVSync(canvas) print "x: %i; y: %i" % (center_x + dot_x, center_y + dot_y) # sleep(0.1) a += angle_step r += angle_step
class DriverRgbMatrix(DriverBase): def __init__(self, clear_screen=True): options = RGBMatrixOptions() options.rows = Config.get_or_throw('leds.display_height') options.cols = Config.get_or_throw('leds.display_width') options.chain_length = 1 options.parallel = 1 options.hardware_mapping = 'adafruit-hat' options.drop_privileges = False self.__matrix = RGBMatrix(options=options) self.__pixels = self.__matrix.CreateFrameCanvas() if clear_screen: self.clear_screen() def display_frame(self, frame): img = Image.fromarray(frame, mode='RGB') self.__pixels.SetImage(img) self.__pixels = self.__matrix.SwapOnVSync(self.__pixels) def clear_screen(self): self.__pixels.Clear() self.__matrix.Clear() # For some drivers, only one instance of the driver can exist at a time because all of them # would send competing signals to the LEDs. The screensaver, video playback, etc processes # that the Queue launches might have their own instance of the driver as well as the # Queue process itself, which could cause problems. # # Thus, for some drivers (like the RGB Matrix driver), when the Queue needs to perform # operations like clearing the screen, it creates a short lived instance to avoid the # problems with multiple long lived driver instances. This approach does not work for other # drivers (like the APA102 driver). # # See: https://github.com/hzeller/rpi-rgb-led-matrix/issues/640 # https://github.com/dasl-/pifi/pull/32 # https://github.com/dasl-/pifi/issues/33 def can_multiple_driver_instances_coexist(self): return False
class Screen: def __init__(self, cols: int, rows: int, **kwargs): options = RGBMatrixOptions() options.cols = cols options.rows = rows for k, v in kwargs.items(): setattr(options, k, v) self.matrix = RGBMatrix(options=options) self.canvas = self.matrix.CreateFrameCanvas() def clear(self): self.canvas.Clear() def fill(self, color: int): self.canvas.Fill(*hex2rgb(color)) def set_pixel(self, x: int, y: int, color: int): self.canvas.SetPixel(x, y, *hex2rgb(color)) def draw_circle(self, x: int, y: int, radius: int, color: int): color = graphics.Color(*hex2rgb(color)) graphics.DrawCircle(self.canvas, x, y, radius, color) def draw_line(self, x1: int, y1: int, x2: int, y2: int, color: int): color = graphics.Color(*hex2rgb(color)) graphics.DrawLine(self.canvas, x1, y1, x2, y2, color) 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 update(self): self.canvas = self.matrix.SwapOnVSync(self.canvas)
class contentdisplayer(): width = 32 cols = 64 thread = None def __init__(self): # Configuration for the matrix options = RGBMatrixOptions() options.cols = self.cols options.rows = self.width options.chain_length = 1 options.parallel = 1 options.gpio_slowdown = 2 options.hardware_mapping = 'adafruit-hat' options.pwm_lsb_nanoseconds = 250 self.matrix = RGBMatrix(options = options) self.offscreen_canvas = self.matrix.CreateFrameCanvas() print("init done") def display(self, imagepath, text, textcolor, bgcolor, scroll, blink): #print("text: " + text) if (self.thread != None): self.thread.setStopFlag(True) self.thread.join() self.thread = None self.thread = DisplayerThread(self.offscreen_canvas, self.matrix, imagepath, text, textcolor, bgcolor, scroll, blink) self.thread.daemon = True self.thread.start() def stop(self): #print("stop called") if (self.thread != None): self.thread.setStopFlag(True) self.thread.join()