def test_multiline_text_with_empty_line_inbetween(tmpdir): out = tmpdir.join('img').ensure() disp = Display(16, 24, str(out)) with disp as d: d.font_size(1) d.puts(1, 0, 'Ag\n\nabc') assert img_eq( ''' ..OO............ .O..O........... .O..O..OOO...... .OOOO.O..O...... .O..O..OO....... .O..O.O......... .......OOO...... ................ ................ ................ ................ ................ ................ ................ ................ ................ ......O......... ......O......... ..OOO.OOO...OO.. .O..O.O..O.O.... .O.OO.O..O.O.... ..O.O.OOO...OO.. ................ ................ ''', disp, out)
def test_display_performance(tmpdir): out = tmpdir.join('output').ensure() d1 = Display(132, 32, str(out)) time_text(d1, 50000) time_text(d1, 50000, anchor='right') time_line(d1, 10000)
def test_largest_W(tmpdir): out = tmpdir.join('img').ensure() disp = Display(16, 24, str(out)) with disp as d: d.font_size(8) d.puts(0, 0, 'W') assert img_eq( ''' ................ ................ ................ .OO....OO....... .OO....OO....... .OO....OO....... .OO....OO....... .OO....OO....... .OO.OO.OO....... .OO.OO.OO....... .OO.OO.OO....... .OO.OO.OO....... .OOO..OOO....... .OOO..OOO....... .OO....OO....... .OO....OO....... ................ ................ ................ ................ ................ ................ ................ ................ ''', disp, out)
def test_diagonal_lines(tmpdir): out = tmpdir.join('img').ensure() disp = Display(8, 8, str(out)) with disp as d: d.line(0, 0, 6, 6) assert img_eq( ''' O....... .O...... ..O..... ...O.... ....O... .....O.. ......O. ........ ''', disp, out) with disp as d: d.line(0, 0, 7, 3) assert img_eq( ''' OO...... ..OO.... ....OO.. ......OO ........ ........ ........ ........ ''', disp, out)
def test_multiline_text_center_aligned_anchored_center(tmpdir): out = tmpdir.join('img').ensure() disp = Display(24, 16, str(out)) with disp as d: d.font_size(1) d.puts(10, 0, 'Ag\nabw', align='center', anchor='center') assert img_eq( ''' ......OO................ .....O..O............... .....O..O..OOO.......... .....OOOO.O..O.......... .....O..O..OO........... .....O..O.O............. ...........OOO.......... ........................ ........O............... ........O............... ....OOO.OOO..O..O....... ...O..O.O..O.O..O....... ...O.OO.O..O.OOOO....... ....O.O.OOO..OOOO....... ........................ ........................ ''', disp, out)
def test_blit(tmpdir): out = tmpdir.join('img').ensure() disp = Display(8, 8, str(out)) pattern1 = [ 1, -1, 0, 0, 1, 0, 0, -1, 1, ] pattern2 = [ -1, -1, 1, -1, -1, -1, 1, -1, -1, ] with disp as d: d.blit(1, 2, pattern1, 3) d.blit(1, 2, pattern2, 3) assert img_eq( ''' ........ ........ .O.O.... ..O..... .O.O.... ........ ........ ........ ''', disp, out)
def test_combined_output(tmpdir): out = tmpdir.join('img').ensure() disp = Display(16, 16, str(out)) with disp as d: d.font_size(8) d.point(5, 2) d.line(3, 1, 6, 6) d.puts(0, 0, "U") d.font_size(3) d.puts(3, 6, "i") assert img_eq( ''' ................ ...O............ ....OO.......... .OO.O..OO....... .OO..O.OO....... .OO..O.OO....... .OO...OOO....... .OO..O.OO....... .OO....OO....... .OO.OO.OO....... .OO..O.OO....... .OO..O.OO....... .OO..O.OO....... .OO.OOOOO....... ..OO..OO........ ...OOOO......... ''', disp, out)
def test_blit_string(tmpdir): out = tmpdir.join('img').ensure() disp = Display(8, 8, str(out)) pattern1 = ( 'O .' '.O.' '. O' ) pattern2 = ( ' O' ' ' 'O ' ) with disp as d: d.blit_string(1, 2, pattern1, 3) d.blit_string(1, 2, pattern2, 3) assert img_eq( ''' ........ ........ .O.O.... ..O..... .O.O.... ........ ........ ........ ''', disp, out)
def test_multiline_text_right_aligned(tmpdir): out = tmpdir.join('img').ensure() disp = Display(16, 16, str(out)) with disp as d: d.font_size(1) d.puts(1, 0, 'Ag\nabw', align='right') assert img_eq( ''' .......OO....... ......O..O...... ......O..O..OOO. ......OOOO.O..O. ......O..O..OO.. ......O..O.O.... ............OOO. ................ ......O......... ......O......... ..OOO.OOO..O..O. .O..O.O..O.O..O. .O.OO.O..O.OOOO. ..O.O.OOO..OOOO. ................ ................ ''', disp, out)
def menu(tmpdir): output = tmpdir.join('output').ensure() display = Display(128, 32, str(output)) menu = Menu(None, State(settings), display) class Home(Page): title = 'MockHome' menu.register_page('home', Home) yield menu menu.cleanup()
def test_straight_lines(tmpdir): out = tmpdir.join('img').ensure() disp = Display(8, 8, str(out)) with disp as d: d.line(0, 0, 4, 0) assert img_eq( ''' OOOOO... ........ ........ ........ ........ ........ ........ ........ ''', disp, out) disp.line(2, 1, 2, 4) disp.update() assert img_eq( ''' OOOOO... ..O..... ..O..... ..O..... ..O..... ........ ........ ........ ''', disp, out)
def test_display_without_context_manager(tmpdir): out = tmpdir.join('img').ensure() disp = Display(8, 8, str(out)) disp.clear() disp.point(0, 0) disp.update() assert img_eq( ''' O....... ........ ........ ........ ........ ........ ........ ........ ''', disp, out)
def test_filled_rect(tmpdir): out = tmpdir.join('img').ensure() disp = Display(8, 8, str(out)) with disp as d: d.rect(1, 1, 4, 4, fill=1) assert img_eq( ''' ........ .OOOO... .OOOO... .OOOO... .OOOO... ........ ........ ........ ''', disp, out)
def test_single_pixel(tmpdir): out = tmpdir.join('img').ensure() disp = Display(8, 8, str(out)) with disp as d: d.point(0, 0) assert img_eq( ''' O....... ........ ........ ........ ........ ........ ........ ........ ''', disp, out)
def start_ui(state, settings, menu_debug): from queue import Queue from mg.input.manager import InputManager from mg.input.mdev import MdevInput from mg.ui.display import Display from mg.handler import EventHandler from mg.ui.menu import Menu from mg.ui.pages.main import Home, VolumeDeck, ChienThresholdPage, MultiChienThresholdPage from mg.ui.pages.config import PresetConfigDeck from mg.ui.pages.strings import MelodyDeck, DroneDeck, TrompetteDeck if settings.display_device == 'disabled': # use dummy display from mg.ui.display.base import BaseDisplay display = BaseDisplay(128, 32) else: display = Display(128, 32, settings.display_device, mmap=settings.display_mmap) event_queue = Queue() menu = Menu(event_queue, state, display) menu.debug = menu_debug menu.register_page('home', Home) menu.register_page('melody', MelodyDeck) menu.register_page('drone', DroneDeck) menu.register_page('trompette', TrompetteDeck) menu.register_page('config', PresetConfigDeck) menu.register_page('volume', VolumeDeck) menu.register_page('chien_threshold', ChienThresholdPage) menu.register_page('multi_chien_threshold', MultiChienThresholdPage) menu.goto('home') input_manager = InputManager(event_queue) config_filename = find_config_file(settings.input_config) input_manager.load_config(config_filename) input_manager.register(MdevInput('/tmp/mgurdy', 'mdev Input Handler')) # noqa event_handler = EventHandler(event_queue, state, menu) return menu, input_manager, event_handler
def test_filled_rect_with_color_black(tmpdir): out = tmpdir.join('img').ensure() disp = Display(8, 8, str(out)) with disp as d: d.point(2, 2, 1) d.rect(1, 1, 4, 4, fill=0) assert img_eq( ''' ........ .OOOO... .O..O... .O..O... .OOOO... ........ ........ ........ ''', disp, out)
def test_scrolling_text_without_shift_timeout(tmpdir): out = tmpdir.join('img').ensure() disp = Display(24, 8, str(out)) with disp as d: d.font_size(1) d.scrolltext(1, 1, 18, '12345678901234567890') assert img_eq( ''' ........................ ...O...OO..OOOO...O..... ..OO..O..O....O..OO..... ...O.....O..OO..O.O..... ...O....O.....O.OOO..... ...O...O...O..O...O..... ..OOO.OOOO..OO....O..... ........................ ''', disp, out)
def test_center_anchored(tmpdir): out = tmpdir.join('img').ensure() disp = Display(24, 8, str(out)) with disp as d: d.font_size(1) d.puts(10, 0, 'Abw', anchor='center') assert img_eq( ''' ....OO..O............... ...O..O.O............... ...O..O.OOO..O..O....... ...OOOO.O..O.O..O....... ...O..O.O..O.OOOO....... ...O..O.OOO..OOOO....... ........................ ........................ ''', disp, out)
def test_smallest_A(tmpdir): out = tmpdir.join('img').ensure() disp = Display(8, 8, str(out)) with disp as d: d.font_size(1) d.puts(1, 0, 'A') assert img_eq( ''' ..OO.... .O..O... .O..O... .OOOO... .O..O... .O..O... ........ ........ ''', disp, out)
def test_scrolltext_without_need_for_scrolling(tmpdir): out = tmpdir.join('img').ensure() disp = Display(24, 8, str(out)) with disp as d: d.font_size(1) d.scrolltext(1, 1, 18, '123', shift_delay=1) time.sleep(0.5) assert img_eq( ''' ........................ ...O...OO..OOOO......... ..OO..O..O....O......... ...O.....O..OO.......... ...O....O.....O......... ...O...O...O..O......... ..OOO.OOOO..OO.......... ........................ ''', disp, out)
import time from mg.ui.display import Display display = Display(128, 32, '/tmp/mgimage', mmap=False) with display as d: d.font_size(3) d.puts(0, 0, "test") d.scrolltext(10, 10, 50, "0123456789abdcefghij", shift_delay=20) time.sleep(3) with display as d: d.font_size(3) d.puts(0, 0, "test") d.scrolltext(10, 20, 50, "0123456789abdcefghij", shift_delay=20) time.sleep(3) with display as d: d.scrolltext(5, 5, 30, "abcdefghijklmnop", 1, initial_delay=1000, shift_delay=10, end_delay=500) time.sleep(10)
#!/usr/bin/python """ Displays centered text in the OLED display. Use $'line1\nline2' for multi-line text in bash. """ import argparse from mg.ui.display import Display display = Display(128, 32, '/dev/fb0', mmap=False) def main(): parser = argparse.ArgumentParser() parser.add_argument('msg') parser.add_argument('--font-size', type=int, default=3) parser.add_argument('--fill', action="store_true", default=False) args = parser.parse_args() # turn \n to real line breaks args.msg = args.msg.replace(r'\n', '\n') if args.fill: font = display.get_font(args.font_size) line_len = display.width // font.char_width msg = args.msg.replace('\n', ' ') lines = [msg[i:i + line_len] for i in range(0, len(msg), line_len)] with display as d: d.font_size(args.font_size) d.puts(0, 0, '\n'.join(lines)) else: