|
37 | 37 | import mmap |
38 | 38 | import ctypes |
39 | 39 | from PIL import Image, ImageDraw |
40 | | -from struct import unpack |
| 40 | +from struct import pack, unpack |
41 | 41 |
|
42 | 42 | #------------------------------------------------------------------------------ |
43 | 43 | # Guess platform we are running on |
@@ -2220,7 +2220,7 @@ def __init__(self): |
2220 | 2220 | FbMem.__init__(self) |
2221 | 2221 |
|
2222 | 2222 | self._img = Image.new( |
2223 | | - "%s" % self.var_info.bits_per_pixel, |
| 2223 | + self.var_info.bits_per_pixel == 1 and "1" or "RGB", |
2224 | 2224 | (self.fix_info.line_length * 8 / self.var_info.bits_per_pixel, self.yres), |
2225 | 2225 | "white") |
2226 | 2226 |
|
@@ -2262,11 +2262,24 @@ def clear(self): |
2262 | 2262 | """ |
2263 | 2263 | self._draw.rectangle(((0,0), self.shape), fill="white") |
2264 | 2264 |
|
| 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 | + |
2265 | 2275 | def update(self): |
2266 | 2276 | """ |
2267 | 2277 | Applies pending changes to the screen. |
2268 | 2278 | Nothing will be drawn on the screen until this function is called. |
2269 | 2279 | """ |
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