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
28 changes: 26 additions & 2 deletions ev3dev2/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@
import os
import mmap
import ctypes
import ev3dev.fonts as fonts
from PIL import Image, ImageDraw
from . import fonts
from . import get_current_platform
from struct import pack
import fcntl

Expand Down Expand Up @@ -195,8 +196,19 @@ class Display(FbMem):
def __init__(self, desc='Display'):
FbMem.__init__(self)

self.platform = get_current_platform()

if self.var_info.bits_per_pixel == 1:
im_type = "1"
elif self.var_info.bits_per_pixel == 16:
im_type = "RGB"
elif self.platform == "ev3" and self.var_info.bits_per_pixel == 32:
im_type = "L"
else:
raise Exception("Not supported")

self._img = Image.new(
self.var_info.bits_per_pixel == 1 and "1" or "RGB",
im_type,
(self.fix_info.line_length * 8 // self.var_info.bits_per_pixel, self.yres),
"white")

Expand Down Expand Up @@ -266,6 +278,16 @@ def _img_to_rgb565_bytes(self):
pixels = [self._color565(r, g, b) for (r, g, b) in self._img.getdata()]
return pack('H' * len(pixels), *pixels)

def _color_xrgb(self, v):
"""Convert red, green, blue components to a 32-bit XRGB value. Components
should be values 0 to 255.
"""
return ((v << 16) | (v << 8) | v)

def _img_to_xrgb_bytes(self):
pixels = [self._color_xrgb(v) for v in self._img.getdata()]
return pack('I' * len(pixels), *pixels)

def update(self):
"""
Applies pending changes to the screen.
Expand All @@ -276,6 +298,8 @@ def update(self):
self.mmap[:len(b)] = b
elif self.var_info.bits_per_pixel == 16:
self.mmap[:] = self._img_to_rgb565_bytes()
elif self.platform == "ev3" and self.var_info.bits_per_pixel == 32:
self.mmap[:] = self._img_to_xrgb_bytes()
else:
raise Exception("Not supported")

Expand Down