Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 130 additions & 3 deletions ev3dev/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import os
import mmap
import ctypes
import ev3dev.fonts as fonts
from PIL import Image, ImageDraw
from struct import pack
import fcntl

Expand Down Expand Up @@ -179,14 +181,18 @@ def _map_fb_memory(fbfid, fix_info):
)


class Screen(FbMem):
class Display(FbMem):
"""
A convenience wrapper for the FbMem class.
Provides drawing functions from the python imaging library (PIL).
"""

def __init__(self):
from PIL import Image, ImageDraw
GRID_COLUMNS = 22
GRID_COLUMN_PIXELS = 8
GRID_ROWS = 12
GRID_ROW_PIXELS = 10

def __init__(self, desc='Display'):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious... what is the description for?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is just so if you print the object as a string the __str__ has something to work with

FbMem.__init__(self)

self._img = Image.new(
Expand All @@ -195,6 +201,10 @@ def __init__(self):
"white")

self._draw = ImageDraw.Draw(self._img)
self.desc = desc

def __str__(self):
return self.desc

@property
def xres(self):
Expand Down Expand Up @@ -267,3 +277,120 @@ def update(self):
self.mmap[:] = self._img_to_rgb565_bytes()
else:
raise Exception("Not supported")

def image_filename(self, filename, clear_screen=True, x1=0, y1=0, x2=None, y2=None):

if clear_screen:
self.clear()

filename_im = Image.open(filename)

if x2 is not None and y2 is not None:
return self._img.paste(filename_im, (x1, y1, x2, y2))
else:
return self._img.paste(filename_im, (x1, y1))

def line(self, clear_screen=True, x1=10, y1=10, x2=50, y2=50, line_color='black', width=1):
"""
Draw a line from (x1, y1) to (x2, y2)
"""

if clear_screen:
self.clear()

return self.draw.line((x1, y1, x2, y2), fill=line_color, width=width)

def circle(self, clear_screen=True, x=50, y=50, radius=40, fill_color='black', outline_color='black'):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These defaults seem a bit random 😆

I swear, this is my last comment 😉 I need to be better about all-in-one reviews.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah those are just so the user sees a full circle on the screen if they are kicking the tires and call circle() without passing any args

"""
Draw a circle of 'radius' centered at (x, y)
"""

if clear_screen:
self.clear()

x1 = x - radius
y1 = y - radius
x2 = x + radius
y2 = y + radius

return self.draw.ellipse((x1, y1, x2, y2), fill=fill_color, outline=outline_color)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if you passed, say, red to this function in the EV3 (the EV3 has a monochrome display)? Does it convert the color brightness to a single greyscale channel, or do something else?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

red it converted to black on my EV3. I don't know if there is a cutoff where something close to white translates to white instead of black or not.


def rectangle(self, clear_screen=True, x=10, y=10, width=80, height=40, fill_color='black', outline_color='black'):
"""
Draw a rectangle 'width x height' where the top left corner is at (x, y)
"""

if clear_screen:
self.clear()

return self.draw.rectangle((x, y, width, height), fill=fill_color, outline=outline_color)

def point(self, clear_screen=True, x=10, y=10, point_color='black'):
"""
Draw a single pixel at (x, y)
"""

if clear_screen:
self.clear()

return self.draw.point((x, y), fill=point_color)

def text_pixels(self, text, clear_screen=True, x=0, y=0, text_color='black', font=None):
"""
Display `text` starting at pixel (x, y).
The EV3 display is 178x128 pixels
- (0, 0) would be the top left corner of the display
- (89, 64) would be right in the middle of the display
'text_color' : PIL says it supports "common HTML color names". There
are 140 HTML color names listed here that are supported by all modern
browsers. This is probably a good list to start with.
https://www.w3schools.com/colors/colors_names.asp
'font' : can be any font displayed here
http://ev3dev-lang.readthedocs.io/projects/python-ev3dev/en/stable/other.html#bitmap-fonts
"""

if clear_screen:
self.clear()

if font is not None:
assert font in fonts.available(), "%s is an invalid font" % font
return self.draw.text((x, y), text, fill=text_color, font=fonts.load(font))
else:
return self.draw.text((x, y), text, fill=text_color)

def text_grid(self, text, clear_screen=True, x=0, y=0, text_color='black', font=None):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document what these two text methods do... I'm a bit confused myself

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a page somewhere that shows what all of the different fonts look like on the EV3 screen? I'm looking but can't find it...was going to include that in the doc string

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http://python-ev3dev.readthedocs.io/en/latest/other.html#ev3dev.fonts.load (e6a1dde)

I couldn't find info on how this was generated... somewhere I think @ddemidov had a Jupyter notebook with some code for it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#225 links to the notebook with the code used to generate the image.

"""
Display 'text' starting at grid (x, y)
The EV3 display can be broken down in a grid that is 22 columns wide
and 12 rows tall. Each column is 8 pixels wide and each row is 10
pixels tall.
'text_color' : PIL says it supports "common HTML color names". There
are 140 HTML color names listed here that are supported by all modern
browsers. This is probably a good list to start with.
https://www.w3schools.com/colors/colors_names.asp
'font' : can be any font displayed here
http://ev3dev-lang.readthedocs.io/projects/python-ev3dev/en/stable/other.html#bitmap-fonts
"""

assert 0 <= x < Display.GRID_COLUMNS,\
"grid columns must be between 0 and %d, %d was requested" %\
((Display.GRID_COLUMNS - 1, x))

assert 0 <= y < Display.GRID_ROWS,\
"grid rows must be between 0 and %d, %d was requested" %\
((Display.GRID_ROWS - 1), y)

return self.text_pixels(text, clear_screen,
x * Display.GRID_COLUMN_PIXELS,
y * Display.GRID_ROW_PIXELS,
text_color, font)

def reset_screen(self):
self.clear()
self.update()