|
| 1 | +# SPDX-FileCopyrightText: 2021 Stefan Krüger |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +############################# |
| 5 | +""" |
| 6 | +This is a basic demonstration of a Cartesian widget for line-ploting |
| 7 | +""" |
| 8 | + |
| 9 | +import time |
| 10 | +import board |
| 11 | +import displayio |
| 12 | +import random |
| 13 | +from displayio_cartesian import Cartesian |
| 14 | + |
| 15 | +# create the display on the PyPortal or Clue or PyBadge(for example) |
| 16 | +display = board.DISPLAY |
| 17 | +# otherwise change this to setup the display |
| 18 | +# for display chip driver and pinout you have (e.g. ILI9341) |
| 19 | + |
| 20 | +# Generate data - here we'll make a normal distribution |
| 21 | +X_LOWER_BOUND = 0 |
| 22 | +X_UPPER_BOUND = 10 |
| 23 | +raw_data = [] |
| 24 | +data = [] |
| 25 | + |
| 26 | +for _ in range(100): |
| 27 | + data_point = int(10 * random.random(X_LOWER_BOUND, X_UPPER_BOUND + 1)) |
| 28 | + raw_data.append(data_point) |
| 29 | + |
| 30 | +y_upper_bound = 0 |
| 31 | +for value in range(len(X_UPPER_BOUND + 1)): |
| 32 | + value_count = raw_data.count(value) |
| 33 | + data.append(value_count) |
| 34 | + y_upper_bound = max(y_upper_bound, value_count) |
| 35 | + |
| 36 | +# pybadge display: 160x128 |
| 37 | +# Create a Cartesian widget |
| 38 | +# https://circuitpython.readthedocs.io/projects/displayio-layout/en/latest/api.html#module-adafruit_displayio_layout.widgets.cartesian |
| 39 | +my_plane = Cartesian( |
| 40 | + x=15, # x position for the plane |
| 41 | + y=2, # y plane position |
| 42 | + width=140, # display width |
| 43 | + height=105, # display height |
| 44 | + xrange=(X_LOWER_BOUND, X_UPPER_BOUND), # x range |
| 45 | + yrange=(0, y_upper_bound), # y range |
| 46 | + fill_area=True |
| 47 | +) |
| 48 | + |
| 49 | +my_group = displayio.Group() |
| 50 | +my_group.append(my_plane) |
| 51 | +display.show(my_group) # add high level Group to the display |
| 52 | + |
| 53 | +print("examples/displayio_layout_cartesian_fillarea.py") |
| 54 | + |
| 55 | +for x, y in data: |
| 56 | + my_plane.add_plot_line(x, y) |
| 57 | + time.sleep(0.5) |
| 58 | + |
| 59 | +while True: |
| 60 | + pass |
0 commit comments