]> Repositories - hackapet/Adafruit_Blinka_Displayio.git/blobdiff - displayio/display.py
Merge pull request #31 from makermelissa/pwm
[hackapet/Adafruit_Blinka_Displayio.git] / displayio / display.py
index 4325671fa209479ce15e6a5d089abbe2786ae22c..1ea5d5555d5911a4d0b127af498e3d129ede13ff 100644 (file)
@@ -38,6 +38,7 @@ displayio for Blinka
 import time
 import struct
 import threading
 import time
 import struct
 import threading
+import digitalio
 from PIL import Image
 import numpy
 from recordclass import recordclass
 from PIL import Image
 import numpy
 from recordclass import recordclass
@@ -48,6 +49,9 @@ __repo__ = "https://github.com/adafruit/Adafruit_Blinka_displayio.git"
 Rectangle = recordclass("Rectangle", "x1 y1 x2 y2")
 displays = []
 
 Rectangle = recordclass("Rectangle", "x1 y1 x2 y2")
 displays = []
 
+BACKLIGHT_IN_OUT = 1
+BACKLIGHT_PWM = 2
+
 # pylint: disable=unnecessary-pass, unused-argument
 
 # pylint: disable=too-many-instance-attributes
 # pylint: disable=unnecessary-pass, unused-argument
 
 # pylint: disable=too-many-instance-attributes
@@ -137,7 +141,7 @@ class Display:
         self._rowstart = rowstart
         self._rotation = rotation
         self._auto_brightness = auto_brightness
         self._rowstart = rowstart
         self._rotation = rotation
         self._auto_brightness = auto_brightness
-        self._brightness = brightness
+        self._brightness = 1.0
         self._auto_refresh = auto_refresh
         self._initialize(init_sequence)
         self._buffer = Image.new("RGB", (width, height))
         self._auto_refresh = auto_refresh
         self._initialize(init_sequence)
         self._buffer = Image.new("RGB", (width, height))
@@ -149,6 +153,23 @@ class Display:
         if self._auto_refresh:
             self.auto_refresh = True
 
         if self._auto_refresh:
             self.auto_refresh = True
 
+        self._backlight_type = None
+        if backlight_pin is not None:
+            try:
+                from pulseio import PWMOut  # pylint: disable=import-outside-toplevel
+
+                # 100Hz looks decent and doesn't keep the CPU too busy
+                self._backlight = PWMOut(backlight_pin, frequency=100, duty_cycle=0)
+                self._backlight_type = BACKLIGHT_PWM
+            except ImportError:
+                # PWMOut not implemented on this platform
+                pass
+            if self._backlight_type is None:
+                self._backlight_type = BACKLIGHT_IN_OUT
+                self._backlight = digitalio.DigitalInOut(backlight_pin)
+                self._backlight.switch_to_output()
+            self.brightness = brightness
+
     # pylint: enable=too-many-locals
 
     def _initialize(self, init_sequence):
     # pylint: enable=too-many-locals
 
     def _initialize(self, init_sequence):
@@ -158,6 +179,7 @@ class Display:
             data_size = init_sequence[i + 1]
             delay = (data_size & 0x80) > 0
             data_size &= ~0x80
             data_size = init_sequence[i + 1]
             delay = (data_size & 0x80) > 0
             data_size &= ~0x80
+
             self._write(command, init_sequence[i + 2 : i + 2 + data_size])
             delay_time_ms = 10
             if delay:
             self._write(command, init_sequence[i + 2 : i + 2 + data_size])
             delay_time_ms = 10
             if delay:
@@ -169,14 +191,18 @@ class Display:
             i += 2 + data_size
 
     def _write(self, command, data):
             i += 2 + data_size
 
     def _write(self, command, data):
-        if self._single_byte_bounds:
-            self._bus.send(True, bytes([command]) + data, toggle_every_byte=True)
+        self._bus.begin_transaction()
+        if self._data_as_commands:
+            if command is not None:
+                self._bus.send(True, bytes([command]), toggle_every_byte=True)
+            self._bus.send(command is not None, data)
         else:
             self._bus.send(True, bytes([command]), toggle_every_byte=True)
             self._bus.send(False, data)
         else:
             self._bus.send(True, bytes([command]), toggle_every_byte=True)
             self._bus.send(False, data)
+        self._bus.end_transaction()
 
     def _release(self):
 
     def _release(self):
-        self._bus.release()
+        self._bus._release()  # pylint: disable=protected-access
         self._bus = None
 
     def show(self, group):
         self._bus = None
 
     def show(self, group):
@@ -197,6 +223,8 @@ class Display:
         When auto refresh is on, updates the display immediately. (The display will also
         update without calls to this.)
         """
         When auto refresh is on, updates the display immediately. (The display will also
         update without calls to this.)
         """
+        self._subrectangles = []
+
         # Go through groups and and add each to buffer
         if self._current_group is not None:
             buffer = Image.new("RGBA", (self._width, self._height))
         # Go through groups and and add each to buffer
         if self._current_group is not None:
             buffer = Image.new("RGBA", (self._width, self._height))
@@ -204,9 +232,10 @@ class Display:
             self._current_group._fill_area(buffer)  # pylint: disable=protected-access
             # save image to buffer (or probably refresh buffer so we can compare)
             self._buffer.paste(buffer)
             self._current_group._fill_area(buffer)  # pylint: disable=protected-access
             # save image to buffer (or probably refresh buffer so we can compare)
             self._buffer.paste(buffer)
-        time.sleep(1)
-        # Eventually calculate dirty rectangles here
-        self._subrectangles.append(Rectangle(0, 0, self._width, self._height))
+
+        if self._current_group is not None:
+            # Eventually calculate dirty rectangles here
+            self._subrectangles.append(Rectangle(0, 0, self._width, self._height))
 
         for area in self._subrectangles:
             self._refresh_display_area(area)
 
         for area in self._subrectangles:
             self._refresh_display_area(area)
@@ -217,7 +246,14 @@ class Display:
 
     def _refresh_display_area(self, rectangle):
         """Loop through dirty rectangles and redraw that area."""
 
     def _refresh_display_area(self, rectangle):
         """Loop through dirty rectangles and redraw that area."""
-        data = numpy.array(self._buffer.crop(rectangle).convert("RGB")).astype("uint16")
+
+        img = self._buffer.convert("RGB").crop(rectangle)
+        img = img.rotate(self._rotation, expand=True)
+
+        display_rectangle = self._apply_rotation(rectangle)
+        img = img.crop(self._clip(display_rectangle))
+
+        data = numpy.array(img).astype("uint16")
         color = (
             ((data[:, :, 0] & 0xF8) << 8)
             | ((data[:, :, 1] & 0xFC) << 3)
         color = (
             ((data[:, :, 0] & 0xF8) << 8)
             | ((data[:, :, 1] & 0xFC) << 3)
@@ -231,16 +267,64 @@ class Display:
         self._write(
             self._set_column_command,
             self._encode_pos(
         self._write(
             self._set_column_command,
             self._encode_pos(
-                rectangle.x1 + self._colstart, rectangle.x2 + self._colstart
+                display_rectangle.x1 + self._colstart,
+                display_rectangle.x2 + self._colstart - 1,
             ),
         )
         self._write(
             self._set_row_command,
             self._encode_pos(
             ),
         )
         self._write(
             self._set_row_command,
             self._encode_pos(
-                rectangle.y1 + self._rowstart, rectangle.y2 + self._rowstart
+                display_rectangle.y1 + self._rowstart,
+                display_rectangle.y2 + self._rowstart - 1,
             ),
         )
             ),
         )
-        self._write(self._write_ram_command, pixels)
+
+        if self._data_as_commands:
+            self._write(None, pixels)
+        else:
+            self._write(self._write_ram_command, pixels)
+
+    def _clip(self, rectangle):
+        if self._rotation in (90, 270):
+            width, height = self._height, self._width
+        else:
+            width, height = self._width, self._height
+
+        if rectangle.x1 < 0:
+            rectangle.x1 = 0
+        if rectangle.y1 < 0:
+            rectangle.y1 = 0
+        if rectangle.x2 > width:
+            rectangle.x2 = width
+        if rectangle.y2 > height:
+            rectangle.y2 = height
+
+        return rectangle
+
+    def _apply_rotation(self, rectangle):
+        """Adjust the rectangle coordinates based on rotation"""
+        if self._rotation == 90:
+            return Rectangle(
+                self._height - rectangle.y2,
+                rectangle.x1,
+                self._height - rectangle.y1,
+                rectangle.x2,
+            )
+        if self._rotation == 180:
+            return Rectangle(
+                self._width - rectangle.x2,
+                self._height - rectangle.y2,
+                self._width - rectangle.x1,
+                self._height - rectangle.y1,
+            )
+        if self._rotation == 270:
+            return Rectangle(
+                rectangle.y1,
+                self._width - rectangle.x2,
+                rectangle.y2,
+                self._width - rectangle.x1,
+            )
+        return rectangle
 
     def _encode_pos(self, x, y):
         """Encode a postion into bytes."""
 
     def _encode_pos(self, x, y):
         """Encode a postion into bytes."""
@@ -279,7 +363,16 @@ class Display:
 
     @brightness.setter
     def brightness(self, value):
 
     @brightness.setter
     def brightness(self, value):
-        self._brightness = value
+        if 0 <= float(value) <= 1.0:
+            self._brightness = value
+            if self._backlight_type == BACKLIGHT_IN_OUT:
+                self._backlight.value = round(self._brightness)
+            elif self._backlight_type == BACKLIGHT_PWM:
+                self._backlight.duty_cycle = self._brightness * 65535
+            elif self._brightness_command is not None:
+                self._write(self._brightness_command, round(value * 255))
+        else:
+            raise ValueError("Brightness must be between 0.0 and 1.0")
 
     @property
     def auto_brightness(self):
 
     @property
     def auto_brightness(self):