Repository navigation
EV3-G API Display #410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EV3-G API Display #410
Changes from all commits
e290720
dcb4a29
4073bcc
674e49b
944db4c
efff17b
fb36a19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
||
|
|
@@ -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'): | ||
| FbMem.__init__(self) | ||
|
|
||
| self._img = Image.new( | ||
|
|
@@ -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): | ||
|
|
@@ -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'): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| """ | ||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would happen if you passed, say,
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| 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): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. k
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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