Luma.Emulator

https://github.com/rm-hull/luma.emulator/workflows/luma.emulator/badge.svg?branch=master https://coveralls.io/repos/github/rm-hull/luma.emulator/badge.svg?branch=master https://img.shields.io/maintenance/yes/2020.svg?maxAge=2592000 https://img.shields.io/pypi/pyversions/luma.emulator.svg https://img.shields.io/pypi/v/luma.emulator.svg https://img.shields.io/pypi/dm/luma.emulator

Introduction

There are various display emulators available for running code against, for debugging and screen capture functionality:

  • The luma.emulator.device.capture device will persist a numbered PNG file to disk every time its display method is called.
  • The luma.emulator.device.gifanim device will record every image when its display method is called, and on program exit (or Ctrl-C), will assemble the images into an animated GIF.
  • The luma.emulator.device.pygame device uses the pygame library to render the displayed image to a pygame display surface.

Check out the examples on how to use the luma.emulator devices.

Installation

Note

The library has been tested against Python 3.6 and newer.

System packages

Install dependencies for pygame first:

$ sudo apt install python3-dev python3-pip build-essential
$ sudo apt install libsdl-dev libportmidi-dev libsdl-ttf2.0-dev libsdl-mixer1.2-dev libsdl-image1.2-dev

And upgrade pip and setuptools:

$ sudo -H pip install --upgrade --ignore-installed pip setuptools

Installing from PyPi

Install the latest version of the luma.emulator library directly from PyPI:

$ sudo -H pip install --upgrade luma.emulator

API Documentation

Inheritance diagram of luma.core.device, luma.emulator, luma.core.mixin, luma.core.virtual, luma.emulator.device

luma.emulator.device

class luma.emulator.device.capture(width=128, height=64, rotate=0, mode='RGB', transform='scale2x', scale=2, file_template='luma_{0:06}.png', **kwargs)[source]

Bases: luma.emulator.device.emulator

Pseudo-device that acts like a physical display, except that it writes the image to a numbered PNG file when the display() method is called. Supports 24-bit color depth.

capabilities(width, height, rotate, mode='1')

Assigns attributes such as width, height, size and bounding_box correctly oriented from the supplied parameters.

Parameters:
  • width (int) – The device width.
  • height (int) – The device height.
  • rotate (int) – An integer value of 0 (default), 1, 2 or 3 only, where 0 is no rotation, 1 is rotate 90° clockwise, 2 is 180° rotation and 3 represents 270° rotation.
  • mode (str) – The supported color model, one of "1", "RGB" or "RGBA" only.
cleanup()

Attempt to switch the device off or put into low power mode (this helps prolong the life of the device), clear the screen and close resources associated with the underlying serial interface.

If persist is True, the device will not be switched off.

This is a managed function, which is called when the python processs is being shutdown, so shouldn’t usually need be called directly in application code.

clear()

Initializes the device memory with an empty (blank) image.

command(*cmd)

Sends a command or sequence of commands through to the delegated serial interface.

contrast(value)

Switches the display contrast to the desired level, in the range 0-255. Note that setting the level to a low (or zero) value will not necessarily dim the display to nearly off. In other words, this method is NOT suitable for fade-in/out animation.

Parameters:level (int) – Desired contrast level in the range of 0-255.
data(data)

Sends a data byte or sequence of data bytes through to the delegated serial interface.

display(image)[source]

Takes a PIL.Image and dumps it to a numbered PNG file.

hide()

Switches the display mode OFF, putting the device in low-power sleep mode.

preprocess(image)

Provides a preprocessing facility (which may be overridden) whereby the supplied image is rotated according to the device’s rotate capability. If this method is overridden, it is important to call the super method.

Parameters:image (PIL.Image.Image) – An image to pre-process.
Returns:A new processed image.
Return type:PIL.Image.Image
show()

Sets the display mode ON, waking the device out of a prior low-power sleep mode.

to_surface(image, alpha=1.0)

Converts a PIL.Image into a pygame.Surface, transforming it according to the transform and scale constructor arguments.

class luma.emulator.device.gifanim(width=128, height=64, rotate=0, mode='RGB', transform='scale2x', scale=2, filename='luma_anim.gif', duration=0.01, loop=0, max_frames=None, **kwargs)[source]

Bases: luma.emulator.device.emulator

Pseudo-device that acts like a physical display, except that it collects the images when the display() method is called, and on exit, assembles them into an animated GIF image. Supports 24-bit color depth, albeit with an indexed color palette.

capabilities(width, height, rotate, mode='1')

Assigns attributes such as width, height, size and bounding_box correctly oriented from the supplied parameters.

Parameters:
  • width (int) – The device width.
  • height (int) – The device height.
  • rotate (int) – An integer value of 0 (default), 1, 2 or 3 only, where 0 is no rotation, 1 is rotate 90° clockwise, 2 is 180° rotation and 3 represents 270° rotation.
  • mode (str) – The supported color model, one of "1", "RGB" or "RGBA" only.
cleanup()

Attempt to switch the device off or put into low power mode (this helps prolong the life of the device), clear the screen and close resources associated with the underlying serial interface.

If persist is True, the device will not be switched off.

This is a managed function, which is called when the python processs is being shutdown, so shouldn’t usually need be called directly in application code.

clear()

Initializes the device memory with an empty (blank) image.

command(*cmd)

Sends a command or sequence of commands through to the delegated serial interface.

contrast(value)

Switches the display contrast to the desired level, in the range 0-255. Note that setting the level to a low (or zero) value will not necessarily dim the display to nearly off. In other words, this method is NOT suitable for fade-in/out animation.

Parameters:level (int) – Desired contrast level in the range of 0-255.
data(data)

Sends a data byte or sequence of data bytes through to the delegated serial interface.

display(image)[source]

Takes an image, scales it according to the nominated transform, and stores it for later building into an animated GIF.

hide()

Switches the display mode OFF, putting the device in low-power sleep mode.

preprocess(image)

Provides a preprocessing facility (which may be overridden) whereby the supplied image is rotated according to the device’s rotate capability. If this method is overridden, it is important to call the super method.

Parameters:image (PIL.Image.Image) – An image to pre-process.
Returns:A new processed image.
Return type:PIL.Image.Image
show()

Sets the display mode ON, waking the device out of a prior low-power sleep mode.

to_surface(image, alpha=1.0)

Converts a PIL.Image into a pygame.Surface, transforming it according to the transform and scale constructor arguments.

write_animation()[source]
class luma.emulator.device.pygame(width=128, height=64, rotate=0, mode='RGB', transform='scale2x', scale=2, frame_rate=60, **kwargs)[source]

Bases: luma.emulator.device.emulator

Pseudo-device that acts like a physical display, except that it renders to a displayed window. The frame rate is limited to 60FPS (much faster than a Raspberry Pi can achieve, but this can be overridden as necessary). Supports 24-bit color depth.

pygame is used to render the emulated display window, and it’s event loop is checked to see if the ESC key was pressed or the window was dismissed: if so sys.exit() is called.

capabilities(width, height, rotate, mode='1')

Assigns attributes such as width, height, size and bounding_box correctly oriented from the supplied parameters.

Parameters:
  • width (int) – The device width.
  • height (int) – The device height.
  • rotate (int) – An integer value of 0 (default), 1, 2 or 3 only, where 0 is no rotation, 1 is rotate 90° clockwise, 2 is 180° rotation and 3 represents 270° rotation.
  • mode (str) – The supported color model, one of "1", "RGB" or "RGBA" only.
cleanup()

Attempt to switch the device off or put into low power mode (this helps prolong the life of the device), clear the screen and close resources associated with the underlying serial interface.

If persist is True, the device will not be switched off.

This is a managed function, which is called when the python processs is being shutdown, so shouldn’t usually need be called directly in application code.

clear()

Initializes the device memory with an empty (blank) image.

command(*cmd)

Sends a command or sequence of commands through to the delegated serial interface.

contrast(value)

Switches the display contrast to the desired level, in the range 0-255. Note that setting the level to a low (or zero) value will not necessarily dim the display to nearly off. In other words, this method is NOT suitable for fade-in/out animation.

Parameters:level (int) – Desired contrast level in the range of 0-255.
data(data)

Sends a data byte or sequence of data bytes through to the delegated serial interface.

display(image)[source]

Takes a PIL.Image and renders it to a pygame display surface.

hide()

Switches the display mode OFF, putting the device in low-power sleep mode.

preprocess(image)

Provides a preprocessing facility (which may be overridden) whereby the supplied image is rotated according to the device’s rotate capability. If this method is overridden, it is important to call the super method.

Parameters:image (PIL.Image.Image) – An image to pre-process.
Returns:A new processed image.
Return type:PIL.Image.Image
show()

Sets the display mode ON, waking the device out of a prior low-power sleep mode.

to_surface(image, alpha=1.0)

Converts a PIL.Image into a pygame.Surface, transforming it according to the transform and scale constructor arguments.

class luma.emulator.device.asciiart(width=128, height=64, rotate=0, mode='RGB', transform='scale2x', scale=2, **kwargs)[source]

Bases: luma.emulator.device.emulator

Pseudo-device that acts like a physical display, except that it converts the image to display into an ASCII-art representation and downscales colors to match the xterm-256 color scheme. Supports 24-bit color depth.

This device takes hold of the terminal window (using curses), and any output for sysout and syserr is captured and stored, and is replayed when the cleanup method is called.

Loosely based on https://github.com/ajalt/pyasciigen/blob/master/asciigen.py

New in version 0.2.0.

capabilities(width, height, rotate, mode='1')

Assigns attributes such as width, height, size and bounding_box correctly oriented from the supplied parameters.

Parameters:
  • width (int) – The device width.
  • height (int) – The device height.
  • rotate (int) – An integer value of 0 (default), 1, 2 or 3 only, where 0 is no rotation, 1 is rotate 90° clockwise, 2 is 180° rotation and 3 represents 270° rotation.
  • mode (str) – The supported color model, one of "1", "RGB" or "RGBA" only.
cleanup()[source]

Attempt to switch the device off or put into low power mode (this helps prolong the life of the device), clear the screen and close resources associated with the underlying serial interface.

If persist is True, the device will not be switched off.

This is a managed function, which is called when the python processs is being shutdown, so shouldn’t usually need be called directly in application code.

clear()

Initializes the device memory with an empty (blank) image.

command(*cmd)

Sends a command or sequence of commands through to the delegated serial interface.

contrast(value)

Switches the display contrast to the desired level, in the range 0-255. Note that setting the level to a low (or zero) value will not necessarily dim the display to nearly off. In other words, this method is NOT suitable for fade-in/out animation.

Parameters:level (int) – Desired contrast level in the range of 0-255.
data(data)

Sends a data byte or sequence of data bytes through to the delegated serial interface.

display(image)[source]

Takes a PIL.Image and renders it to the current terminal as ASCII-art.

hide()

Switches the display mode OFF, putting the device in low-power sleep mode.

preprocess(image)

Provides a preprocessing facility (which may be overridden) whereby the supplied image is rotated according to the device’s rotate capability. If this method is overridden, it is important to call the super method.

Parameters:image (PIL.Image.Image) – An image to pre-process.
Returns:A new processed image.
Return type:PIL.Image.Image
show()

Sets the display mode ON, waking the device out of a prior low-power sleep mode.

to_surface(image, alpha=1.0)

Converts a PIL.Image into a pygame.Surface, transforming it according to the transform and scale constructor arguments.

class luma.emulator.device.asciiblock(width=128, height=64, rotate=0, mode='RGB', transform='scale2x', scale=2, **kwargs)[source]

Bases: luma.emulator.device.emulator

Pseudo-device that acts like a physical display, except that it converts the image pixels to display into colored ASCII half-blocks (ASCII code 220, ‘▄’), where the upper part background is used for one pixel, and the lower part foreground is used for the pixel on the next row. As most terminal display characters are in ratio 2:1, the half-block appears square.

Inspired by Command Line Curiosities - Making the Terminal Sing by Hamza Haiken

New in version 1.1.0.

capabilities(width, height, rotate, mode='1')

Assigns attributes such as width, height, size and bounding_box correctly oriented from the supplied parameters.

Parameters:
  • width (int) – The device width.
  • height (int) – The device height.
  • rotate (int) – An integer value of 0 (default), 1, 2 or 3 only, where 0 is no rotation, 1 is rotate 90° clockwise, 2 is 180° rotation and 3 represents 270° rotation.
  • mode (str) – The supported color model, one of "1", "RGB" or "RGBA" only.
cleanup()[source]

Attempt to switch the device off or put into low power mode (this helps prolong the life of the device), clear the screen and close resources associated with the underlying serial interface.

If persist is True, the device will not be switched off.

This is a managed function, which is called when the python processs is being shutdown, so shouldn’t usually need be called directly in application code.

clear()

Initializes the device memory with an empty (blank) image.

command(*cmd)

Sends a command or sequence of commands through to the delegated serial interface.

contrast(value)

Switches the display contrast to the desired level, in the range 0-255. Note that setting the level to a low (or zero) value will not necessarily dim the display to nearly off. In other words, this method is NOT suitable for fade-in/out animation.

Parameters:level (int) – Desired contrast level in the range of 0-255.
data(data)

Sends a data byte or sequence of data bytes through to the delegated serial interface.

display(image)[source]

Takes a PIL.Image and renders it to the current terminal as ASCII-blocks.

hide()

Switches the display mode OFF, putting the device in low-power sleep mode.

preprocess(image)

Provides a preprocessing facility (which may be overridden) whereby the supplied image is rotated according to the device’s rotate capability. If this method is overridden, it is important to call the super method.

Parameters:image (PIL.Image.Image) – An image to pre-process.
Returns:A new processed image.
Return type:PIL.Image.Image
show()

Sets the display mode ON, waking the device out of a prior low-power sleep mode.

to_surface(image, alpha=1.0)

Converts a PIL.Image into a pygame.Surface, transforming it according to the transform and scale constructor arguments.

luma.emulator.render

class luma.emulator.render.transformer(pygame, width, height, scale)[source]

Bases: object

Helper class used to dispatch transformation operations.

identity(surface)[source]

Fast scale operation that does not sample the results

led_matrix(surface)[source]

Transforms the input surface into an LED matrix (1 pixel = 1 LED)

none(surface)[source]

No-op transform - used when scale = 1

scale2x(surface)[source]

Scales using the AdvanceMAME Scale2X algorithm which does a ‘jaggie-less’ scale of bitmap graphics.

seven_segment(surface)[source]
smoothscale(surface)[source]

Smooth scaling using MMX or SSE extensions if available

Contributing

Pull requests (code changes / documentation / typos / feature requests / setup) are gladly accepted. If you are intending to introduce some large-scale changes, please get in touch first to make sure we’re on the same page: try to include a docstring for any new method or class, and keep method bodies small, readable and PEP8-compliant. Add tests and strive to keep the code coverage levels high.

GitHub

The source code is available to clone at: https://github.com/rm-hull/luma.emulator

Contributors

  • Thijs Triemstra (@thijstriemstra)
  • Christoph Handel (@fragfutter)
  • Boeeerb (@Boeeerb)
  • xes (@xes)
  • Roger Dahl (@rogerdahl)
  • Václav Šmilauer (@eudoxos)
  • Claus Bjerre (@bjerrep)

ChangeLog

Version Description Date
1.4.0
  • Remove Python 3.5 support. Only Python 3.6 or newer is supported.
2020/12/29
1.3.0
  • Remove Python 2.7 support. Only Python 3.5 or newer is supported.
2020/07/04
1.2.0
  • Rework namespace handling for luma sub-projects
2019/06/16
1.1.0
  • Added ASCII-block emulator
2018/02/03
1.0.2
  • Changed version number to inside luma/emulator/__init__.py
2017/11/23
1.0.1
  • Workaround pygame bug & use io module
2017/11/22
1.0.0
  • Stable version
  • Minor documentation and setup fixes
2017/11/03
0.2.5
  • Correct noop import
2017/09/06
0.2.4
  • Move show/hide/contrast methods into emulator
  • Add segment mapper
  • Refactor tests
2017/04/30
0.2.3
  • Added ASCII-art emulator
2017/04/15
0.1.3
  • Add contrast & show/hide functionality to pygame emulator
2017/03/02
0.1.2
  • Restrict exported Python symbols from luma.emulator.device
2017/03/02
0.1.1
  • Split out emulator functionality from rm-hull/luma.core
2017/02/17

The MIT License (MIT)

Copyright (c) 2017-2020 Richard Hull and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.