adafruit / Adafruit_CircuitPython_CharLCD

Library code for character LCD interfacing
MIT License
69 stars 50 forks source link

Added typing and made an edit to fix a typing issue (non-breaking) #73

Closed BiffoBear closed 1 year ago

BiffoBear commented 1 year ago

Added type hints to all functions. Edited character_lcd.color setter to resolve a typing conflict with a float used when an int was expected in the line pin.duty_cycle = int(_map(color[number], 0, 100, 65535, 0)). Converted r, g and b from float to int.

            r = int((color >> 16) / 2.55)
            g = int(((color >> 8) & 0xFF) / 2.55)
            b = int((color & 0xFF) / 2.55)
            color = [r, g, b]  # This is List[float], should be List[int]
        self._color = color
        for number, pin in enumerate(self.rgb_led):
            if hasattr(pin, "duty_cycle"):
                # Assume a pwmio.PWMOut or compatible interface and set duty cycle:
                pin.duty_cycle = int(_map(color[number], 0, 100, 65535, 0))

This matches the intent of the doc-string:

    def color(self) -> List[int, int, int]:
        """
        The color of the display. Provide a list of three integers ranging 0 - 100, ``[R, G, B]``.
        ``0`` is no color, or "off". ``100`` is maximum color. For example, the brightest red would
        be ``[100, 0, 0]``, and a half-bright purple would be, ``[50, 0, 50]``.

        If PWM is unavailable, ``0`` is off, and non-zero is on. For example, ``[1, 0, 0]`` would
        be red.

        The following example turns the LCD red and displays, "Hello, world!".

        The property returns a list, but can be set as an int in the format ``0xRRGGBB``,
        as output by `rainbowio.colorwheel` for example.
        """
        ...

BTW, character_lcd._set_bit() is not called anywhere and can be deleted.