|
| 1 | +# SPDX-FileCopyrightText: 2025 Ritesh |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import pathlib |
| 5 | +import sys |
| 6 | +import types |
| 7 | + |
| 8 | +# 1) Ensure project root (where adafruit_ili9341.py lives) is importable |
| 9 | +ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 10 | +if str(ROOT) not in sys.path: |
| 11 | + sys.path.insert(0, str(ROOT)) |
| 12 | + |
| 13 | +# 2) Stub 'busdisplay' so the driver can import BusDisplay without hardware deps |
| 14 | +busdisplay_stub = types.ModuleType("busdisplay") |
| 15 | + |
| 16 | + |
| 17 | +def _to_bytes(obj): |
| 18 | + if obj is None: |
| 19 | + return b"" |
| 20 | + if isinstance(obj, (bytes, bytearray, memoryview)): |
| 21 | + return bytes(obj) |
| 22 | + if isinstance(obj, (list, tuple)): |
| 23 | + try: |
| 24 | + return bytes(obj) |
| 25 | + except Exception: |
| 26 | + return b"" |
| 27 | + return b"" |
| 28 | + |
| 29 | + |
| 30 | +def _pick_bytes_from_args_kwargs(args, kwargs): |
| 31 | + # Prefer explicit kwarg value that looks bytes-like |
| 32 | + for v in kwargs.values(): |
| 33 | + b = _to_bytes(v) |
| 34 | + if b: |
| 35 | + return b |
| 36 | + # Common names: init_sequence, init, sequence |
| 37 | + for key in ("init_sequence", "init", "sequence"): |
| 38 | + if key in kwargs: |
| 39 | + b = _to_bytes(kwargs[key]) |
| 40 | + if b: |
| 41 | + return b |
| 42 | + # Otherwise, search positional args after the bus (args[0] usually 'bus') |
| 43 | + best = b"" |
| 44 | + for v in args[1:]: |
| 45 | + b = _to_bytes(v) |
| 46 | + if len(b) > len(best): |
| 47 | + best = b |
| 48 | + return best |
| 49 | + |
| 50 | + |
| 51 | +class BusDisplay: |
| 52 | + # Accept any signature; extract a bytes-like init sequence robustly |
| 53 | + def __init__(self, *args, **kwargs): |
| 54 | + init_seq = _pick_bytes_from_args_kwargs(args, kwargs) |
| 55 | + self.init_sequence = init_seq |
| 56 | + self._busdisplay_debug = { |
| 57 | + "arg_types": [type(a).__name__ for a in args], |
| 58 | + "kw_keys": list(kwargs.keys()), |
| 59 | + "init_seq_len": len(init_seq), |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | +busdisplay_stub.BusDisplay = BusDisplay |
| 64 | +sys.modules["busdisplay"] = busdisplay_stub |
| 65 | + |
| 66 | +import adafruit_ili9341 as ili |
| 67 | + |
| 68 | + |
| 69 | +def _last_madctl(seq: bytes) -> int: |
| 70 | + """ |
| 71 | + Return the last MADCTL data byte written in the init sequence. |
| 72 | +
|
| 73 | + Init sequence encoding per Adafruit style: |
| 74 | + [CMD][LEN|0x80 if delay][<LEN data bytes>][<1 delay byte if delay flag set>] |
| 75 | + """ |
| 76 | + cmd = ili._MADCTL |
| 77 | + i = 0 |
| 78 | + last = None |
| 79 | + L = len(seq) |
| 80 | + while i < L: |
| 81 | + if i >= L: |
| 82 | + break |
| 83 | + c = seq[i] |
| 84 | + i += 1 |
| 85 | + if i >= L: |
| 86 | + break |
| 87 | + length_byte = seq[i] |
| 88 | + i += 1 |
| 89 | + |
| 90 | + delay_flag = (length_byte & 0x80) != 0 |
| 91 | + n = length_byte & 0x7F # actual data length |
| 92 | + |
| 93 | + # If this is MADCTL, expect exactly 1 data byte |
| 94 | + if c == cmd: |
| 95 | + assert n == 1, f"Expected MADCTL length 1, got {n}" |
| 96 | + assert i + n <= L, "MADCTL payload truncated" |
| 97 | + last = seq[i] # the one data byte |
| 98 | + # advance over data |
| 99 | + i += n |
| 100 | + # consume delay byte if present |
| 101 | + if delay_flag: |
| 102 | + i += 1 |
| 103 | + assert last is not None, f"No MADCTL write found. seq={seq.hex(' ')}" |
| 104 | + return last |
| 105 | + |
| 106 | + |
| 107 | +def test_color_order_defaults_rgb(): |
| 108 | + d = ili.ILI9341(bus=object()) |
| 109 | + assert len(getattr(d, "init_sequence", b"")) > 0, ( |
| 110 | + f"Driver did not pass an init sequence to BusDisplay. " |
| 111 | + f"debug={getattr(d, '_busdisplay_debug', {})}" |
| 112 | + ) |
| 113 | + madctl = _last_madctl(d.init_sequence) |
| 114 | + # Default is RGB => BGR bit NOT set |
| 115 | + assert (madctl & ili._MADCTL_BGR) == 0 |
| 116 | + |
| 117 | + |
| 118 | +def test_color_order_bgr_sets_bit(): |
| 119 | + d = ili.ILI9341(bus=object(), color_order="BGR") |
| 120 | + assert len(getattr(d, "init_sequence", b"")) > 0, ( |
| 121 | + f"Driver did not pass an init sequence to BusDisplay. " |
| 122 | + f"debug={getattr(d, '_busdisplay_debug', {})}" |
| 123 | + ) |
| 124 | + madctl = _last_madctl(d.init_sequence) |
| 125 | + # BGR => BGR bit set |
| 126 | + assert (madctl & ili._MADCTL_BGR) == ili._MADCTL_BGR |
| 127 | + |
| 128 | + |
| 129 | +def test_legacy_bgr_true_sets_bit(): |
| 130 | + d = ili.ILI9341(bus=object(), bgr=True) |
| 131 | + assert len(getattr(d, "init_sequence", b"")) > 0, ( |
| 132 | + f"Driver did not pass an init sequence to BusDisplay. " |
| 133 | + f"debug={getattr(d, '_busdisplay_debug', {})}" |
| 134 | + ) |
| 135 | + madctl = _last_madctl(d.init_sequence) |
| 136 | + # legacy bgr=True still sets the bit |
| 137 | + assert (madctl & ili._MADCTL_BGR) == ili._MADCTL_BGR |
| 138 | + |
| 139 | + |
| 140 | +def _has_invon(seq: bytes) -> bool: |
| 141 | + # Scan TLV-style sequence; check command 0x21 is present |
| 142 | + i, L = 0, len(seq) |
| 143 | + while i + 2 <= L: |
| 144 | + cmd = seq[i] |
| 145 | + i += 1 |
| 146 | + lb = seq[i] |
| 147 | + i += 1 |
| 148 | + delay = (lb & 0x80) != 0 |
| 149 | + n = lb & 0x7F |
| 150 | + i += n |
| 151 | + if delay: |
| 152 | + i += 1 |
| 153 | + if cmd == 0x21: |
| 154 | + return True |
| 155 | + return False |
| 156 | + |
| 157 | + |
| 158 | +def test_invert_true_appends_invon(): |
| 159 | + d = ili.ILI9341(bus=object(), invert=True) |
| 160 | + assert _has_invon(d.init_sequence) |
0 commit comments