|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2021 Randall Bohn |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +"Test displayio_guage.py -- In certain conditions the fill color was leaking out." |
| 5 | +import math |
| 6 | +import random |
| 7 | +import time |
| 8 | +from adafruit_funhouse import FunHouse |
| 9 | +from displayio_gauge import Gauge |
| 10 | + |
| 11 | +funhouse = FunHouse() |
| 12 | +display = funhouse.display |
| 13 | +top_group = funhouse.splash |
| 14 | +display.show(top_group) |
| 15 | + |
| 16 | +color_pass = 0x009900 |
| 17 | +color_fail = 0x990000 |
| 18 | + |
| 19 | +def make_gauge(radius, thickness): |
| 20 | + return Gauge(x=display.width//2,y=display.height//2, |
| 21 | + radius=radius, thickness=thickness, level=10, |
| 22 | + outline_color=0xEEEEEE, |
| 23 | + foreground_color=0xFFCC00, |
| 24 | + background_color=0x3333FF) |
| 25 | + |
| 26 | +# pylint: disable=W0212 |
| 27 | +# test access to gauge._bitmap |
| 28 | +def check(gauge): |
| 29 | + bitmap = gauge._bitmap |
| 30 | + top_left = (1,1) |
| 31 | + top_right = (bitmap.width-1,1) |
| 32 | + bottom_center = (bitmap.width//2, bitmap.height-1) |
| 33 | + if bitmap[top_left[0],top_left[1]] != 0: |
| 34 | + raise ValueError("leak detected top_left") |
| 35 | + if bitmap[top_right[0], top_right[1]] != 0: |
| 36 | + raise ValueError("leak detected top_right") |
| 37 | + if bitmap[bottom_center[0], bottom_center[1]] != 0: |
| 38 | + raise ValueError("leak detected bottom_center") |
| 39 | + |
| 40 | + |
| 41 | +def find_leaks(gauge): |
| 42 | + for x in range(360*3.0): |
| 43 | + theta = math.radians(x) |
| 44 | + level = int(abs(math.sin(theta) * 100)) |
| 45 | + gauge.level = level |
| 46 | + check(gauge) |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +for iteration in range(5): |
| 51 | + my_radius = random.randrange(10,90) |
| 52 | + my_thickness = int(min( |
| 53 | + max(my_radius*0.2, my_radius*random.random()),my_radius*0.85)) |
| 54 | + print(f"iteration={iteration} radius={my_radius} thickness={my_thickness}") |
| 55 | + my_gauge = make_gauge(my_radius, my_thickness) |
| 56 | + top_group[0] = my_gauge |
| 57 | + try: |
| 58 | + find_leaks(my_gauge) |
| 59 | + my_gauge.background_color=color_pass |
| 60 | + time.sleep(3) |
| 61 | + except ValueError: |
| 62 | + my_gauge.background_color=color_fail |
| 63 | + break |
| 64 | + |
| 65 | +if my_gauge.background_color == color_pass: |
| 66 | + print("PASS!") |
| 67 | +else: |
| 68 | + print(f"FAIL: Leak detected in iteration {iteration} level={my_gauge.level}.") |
| 69 | + |
| 70 | +while True: |
| 71 | + time.sleep(1) |
0 commit comments