Skip to content

Commit 762cda9

Browse files
committed
Merge pull request #26 from dlech/master
Get working on 16bpp RGB565 framebuffer device.
2 parents 5c5fc3e + 7fc5fda commit 762cda9

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

ev3dev.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import mmap
3838
import ctypes
3939
from PIL import Image, ImageDraw
40-
from struct import unpack
40+
from struct import pack, unpack
4141

4242
#------------------------------------------------------------------------------
4343
# Guess platform we are running on
@@ -2220,7 +2220,7 @@ def __init__(self):
22202220
FbMem.__init__(self)
22212221

22222222
self._img = Image.new(
2223-
"%s" % self.var_info.bits_per_pixel,
2223+
self.var_info.bits_per_pixel == 1 and "1" or "RGB",
22242224
(self.fix_info.line_length * 8 / self.var_info.bits_per_pixel, self.yres),
22252225
"white")
22262226

@@ -2262,11 +2262,24 @@ def clear(self):
22622262
"""
22632263
self._draw.rectangle(((0,0), self.shape), fill="white")
22642264

2265+
def _color565(self, r, g, b):
2266+
"""Convert red, green, blue components to a 16-bit 565 RGB value. Components
2267+
should be values 0 to 255.
2268+
"""
2269+
return (((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3))
2270+
2271+
def _img_to_rgb565_bytes(self):
2272+
pixels = [self._color565(r,g,b) for (r,g,b) in self._img.getdata()]
2273+
return pack('H' * len(pixels), *pixels)
2274+
22652275
def update(self):
22662276
"""
22672277
Applies pending changes to the screen.
22682278
Nothing will be drawn on the screen until this function is called.
22692279
"""
2270-
self.mmap[:] = self._img.tobytes("raw", "%s;I%s" % (self.var_info.bits_per_pixel,
2271-
self.fix_info.visual in [FbMem.FB_VISUAL_MONO01, FbMem.FB_VISUAL_MONO10] and "R" or ""))
2272-
2280+
if self.var_info.bits_per_pixel == 1:
2281+
self.mmap[:] = self._img.tobytes("raw", "1;IR")
2282+
elif self.var_info.bits_per_pixel == 16:
2283+
self.mmap[:] = self._img_to_rgb565_bytes()
2284+
else:
2285+
raise Exception("Not supported")

0 commit comments

Comments
 (0)