Skip to content

Commit 7193260

Browse files
authored
Merge pull request #31 from jposada202020/progressbar_accelerometer
Progressbar accelerometer
2 parents c931094 + bd22963 commit 7193260

File tree

2 files changed

+236
-0
lines changed

2 files changed

+236
-0
lines changed

docs/examples.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,14 @@ Example showing both a Vertical and an Horizontal ProgressBar
6666
.. literalinclude:: ../examples/progressbar_combined.py
6767
:caption: examples/progressbar_combined.py
6868
:linenos:
69+
70+
71+
Accelerometer Example
72+
---------------------
73+
74+
With this example you would be able to use the progress bar to display accelerometer data
75+
in the X and Y directions
76+
77+
.. literalinclude:: ../examples/progressbar_accelerometer.py
78+
:caption: examples/progressbar_accelerometer.py
79+
:linenos:

examples/progressbar_accelerometer.py

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
# SPDX-FileCopyrightText: 2021 Jose David M.
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
With this example you would be able to use the progress bar to display accelerometer data
5+
in the X and Y directions
6+
"""
7+
import time
8+
import displayio
9+
import board
10+
from adafruit_progressbar.horizontalprogressbar import (
11+
HorizontalProgressBar,
12+
HorizontalFillDirection,
13+
)
14+
from adafruit_progressbar.verticalprogressbar import (
15+
VerticalProgressBar,
16+
VerticalFillDirection,
17+
)
18+
19+
# This data is used to show library capability. You could use an actual accelerometer
20+
# This data was extracted from an Adafruit MSA301 Triple Axis Accelerometer
21+
fake__accel_data = [
22+
(-0.071821, 1.450790, 9.533077),
23+
(-0.105338, 1.939175, 9.111725),
24+
(-0.306437, 2.906367, 9.178761),
25+
(-0.062245, 3.504878, 8.776562),
26+
(-0.143643, 4.792873, 8.091862),
27+
(-0.172371, 5.520662, 7.838097),
28+
(0.014364, 6.176630, 7.426321),
29+
(-0.205888, 7.526871, 6.200571),
30+
(-0.090974, 7.905128, 5.491934),
31+
(-0.445292, 8.216354, 5.118464),
32+
(-0.110126, 8.872322, 4.017202),
33+
(-0.344742, 9.542652, 1.498671),
34+
(-0.076609, 9.580959, -0.033517),
35+
(-0.158007, 9.518715, -1.273631),
36+
(0.282497, 9.446892, -2.877639),
37+
(-0.004788, 9.063847, -3.409117),
38+
(-0.153219, 8.599400, -4.630077),
39+
(-0.071821, 8.020042, -6.382517),
40+
(0.655968, 6.722471, -6.937935),
41+
(0.464444, 5.740913, -7.771063),
42+
(1.034226, 4.189575, -8.905838),
43+
(1.369392, 1.675830, -8.340843),
44+
(2.149850, -1.426849, -9.178761),
45+
(2.384466, -3.662886, -8.834019),
46+
(2.417983, -5.223801, -7.957798),
47+
(2.336586, -7.900341, -5.482357),
48+
(2.106757, -9.231426, -4.194363),
49+
(1.948751, -3.945382, -9.288883),
50+
(0.588934, 0.062245, -9.643204),
51+
(0.430928, 2.250400, -9.360706),
52+
(-0.402199, 7.249161, -6.176630),
53+
(-0.871432, 8.197201, -5.003550),
54+
(0.373471, 8.829227, -3.696402),
55+
(0.584146, 9.662357, -1.287995),
56+
(0.114914, 9.940063, 0.536266),
57+
(-0.201100, 9.207489, 2.848910),
58+
(-0.181947, 8.589825, 5.314775),
59+
(-0.517113, 5.573332, 7.598692),
60+
(-0.497961, 3.160136, 9.092575),
61+
(-0.114914, -1.541763, 9.940063),
62+
(-0.555418, -5.099310, 7.727970),
63+
(-0.387835, -7.091154, 7.095942),
64+
(0.162795, -8.652069, 4.768932),
65+
(0.531477, -8.934566, 0.751729),
66+
(0.775670, -9.542652, -4.141693),
67+
(1.809896, -7.177340, -7.282679),
68+
(0.957617, -2.868063, -9.308037),
69+
(1.450790, -0.866643, -9.571381),
70+
(1.039014, -0.660756, -9.758118),
71+
(0.914524, -4.907787, -8.379150),
72+
(1.302359, -8.120594, -5.870192),
73+
(1.043803, -9.916122, 2.365314),
74+
(1.086895, -8.412666, 5.223801),
75+
(2.034936, -5.942015, 7.488565),
76+
(2.010996, -2.767513, 9.140453),
77+
(0.397411, -2.322221, 9.130878),
78+
(-2.025360, -2.398830, 9.116512),
79+
(-2.824970, -2.264764, 8.896263),
80+
(-4.395462, -2.001419, 8.259445),
81+
(-5.640364, -1.220962, 7.569963),
82+
(-7.000181, -0.746941, 6.679379),
83+
(-8.077499, -0.004788, 5.338715),
84+
(-9.001598, 0.421352, 2.585566),
85+
(-9.408588, 1.106048, 1.053379),
86+
(-9.097363, 2.283916, -1.589644),
87+
(-8.522793, 2.714845, -3.021282),
88+
(-7.991314, 3.083527, -4.505589),
89+
(-6.416035, 3.720343, -6.732048),
90+
(-6.186207, 3.562336, -5.788795),
91+
(-3.289414, 3.428269, -8.254658),
92+
(-1.110836, 3.787375, -8.944141),
93+
(1.082107, 3.270263, -9.059055),
94+
(1.565704, 3.820892, -8.446182),
95+
(2.212095, 3.763435, -8.221142),
96+
(3.030858, 4.175211, -8.029617),
97+
(-2.365314, 2.633447, -8.221142),
98+
(-5.372232, 2.188155, -7.445473),
99+
(-8.465336, 2.116334, -4.577410),
100+
(-9.432529, 1.388545, 0.541054),
101+
(-6.957088, 1.623161, 6.085657),
102+
(-4.735416, 0.751729, 8.992023),
103+
(-1.800320, 2.063664, 9.762905),
104+
(-0.153219, -1.795532, 9.657566),
105+
(5.764854, -3.801740, 6.775141),
106+
(9.470833, -2.240824, 2.777089),
107+
(9.925701, -1.000710, -1.915234),
108+
(8.685585, 0.277709, -4.347582),
109+
(9.676720, -0.459656, -0.521901),
110+
(9.719814, -0.689484, 1.584856),
111+
(8.541943, -1.503459, 4.160847),
112+
(7.608267, -1.824261, 5.865404),
113+
(5.817524, -1.446002, 6.770353),
114+
(3.887925, -1.991844, 8.671223),
115+
(1.805108, -2.039724, 9.303249),
116+
(0.593723, -1.690194, 9.518715),
117+
(0.852279, -2.087605, 9.427738),
118+
(1.206597, -1.857777, 9.226639),
119+
(0.392623, -2.255188, 9.193123),
120+
(-2.475440, -2.154638, 9.173969),
121+
(-3.677250, -8.288174, 3.198441),
122+
(-0.981558, -2.944673, 8.977661),
123+
(1.517823, 3.409117, 8.977661),
124+
(2.796242, 5.989895, 5.807947),
125+
(4.151270, -2.552050, 9.418163),
126+
(4.242243, -9.844303, 1.694982),
127+
(5.946802, -3.543183, 6.890055),
128+
(7.028910, -4.462496, 4.199150),
129+
(-1.694982, -6.655439, 6.430399),
130+
(0.703849, -3.112255, 8.685585),
131+
(1.340664, 4.342793, 8.053558),
132+
(1.627949, 9.920914, -0.608087),
133+
(6.985817, 0.517113, 7.517294),
134+
(5.434477, -5.372232, 5.994682),
135+
(4.165634, -6.224510, 8.082287),
136+
(0.847491, -4.677959, 9.509136),
137+
(3.476150, -4.812025, 7.421532),
138+
]
139+
display = board.DISPLAY
140+
main_group = displayio.Group(max_size=10)
141+
display.show(main_group)
142+
143+
color_bitmap = displayio.Bitmap(display.width, display.height, 1)
144+
color_palette = displayio.Palette(1)
145+
color_palette[0] = 0x990099
146+
147+
background = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
148+
main_group.append(background)
149+
150+
# Accelerometer Properties. Normally accelerometer well calibrated
151+
# Will give a maximum output of 10 mts / s**2
152+
VALUES_X = (-10, 10)
153+
VALUES_Y = (-10, 10)
154+
155+
# Horizontal Bar Properties
156+
HORIZONTAL_BAR_X_ORIGIN = 10
157+
HORIZONTAL_BAR_Y_ORIGIN = 30
158+
HORIZONTAL_BAR_WIDTH = display.width // 4
159+
HORIZONTAL_BAR_HEIGHT = 30
160+
161+
# Vertical Bar Properties
162+
VERTICAL_BAR_HEIGHT = display.height // 4
163+
VERTICAL_BAR_WIDTH = 30
164+
165+
# We create our bar displays
166+
left_horizontal_bar = HorizontalProgressBar(
167+
(HORIZONTAL_BAR_X_ORIGIN, HORIZONTAL_BAR_Y_ORIGIN),
168+
(HORIZONTAL_BAR_WIDTH, HORIZONTAL_BAR_HEIGHT),
169+
min_value=0,
170+
max_value=-VALUES_X[0],
171+
direction=HorizontalFillDirection.RIGHT_TO_LEFT,
172+
)
173+
main_group.append(left_horizontal_bar)
174+
175+
right_horizontal_bar = HorizontalProgressBar(
176+
(HORIZONTAL_BAR_X_ORIGIN + HORIZONTAL_BAR_WIDTH, HORIZONTAL_BAR_Y_ORIGIN),
177+
(HORIZONTAL_BAR_WIDTH, HORIZONTAL_BAR_HEIGHT),
178+
min_value=0,
179+
max_value=VALUES_X[1],
180+
direction=HorizontalFillDirection.LEFT_TO_RIGHT,
181+
)
182+
main_group.append(right_horizontal_bar)
183+
184+
185+
top_vertical_bar = VerticalProgressBar(
186+
(HORIZONTAL_BAR_X_ORIGIN + 2 * HORIZONTAL_BAR_WIDTH + 20, HORIZONTAL_BAR_Y_ORIGIN),
187+
(VERTICAL_BAR_WIDTH, VERTICAL_BAR_HEIGHT),
188+
min_value=0,
189+
max_value=VALUES_Y[1],
190+
direction=VerticalFillDirection.BOTTOM_TO_TOP,
191+
)
192+
main_group.append(top_vertical_bar)
193+
194+
bottom_vertical_bar = VerticalProgressBar(
195+
(
196+
HORIZONTAL_BAR_X_ORIGIN + 2 * HORIZONTAL_BAR_WIDTH + 20,
197+
HORIZONTAL_BAR_Y_ORIGIN + VERTICAL_BAR_HEIGHT,
198+
),
199+
(VERTICAL_BAR_WIDTH, VERTICAL_BAR_HEIGHT),
200+
min_value=0,
201+
max_value=-VALUES_Y[0],
202+
direction=VerticalFillDirection.TOP_TO_BOTTOM,
203+
)
204+
main_group.append(bottom_vertical_bar)
205+
206+
delay = 0.5
207+
208+
while True:
209+
for val in fake__accel_data:
210+
if val[0] >= 0:
211+
left_horizontal_bar.value = 0
212+
right_horizontal_bar.value = val[0]
213+
if val[0] < 0:
214+
left_horizontal_bar.value = -val[0]
215+
right_horizontal_bar.value = 0
216+
217+
if val[1] >= 0:
218+
top_vertical_bar.value = val[1]
219+
bottom_vertical_bar.value = 0
220+
if val[1] < 0:
221+
bottom_vertical_bar.value = -val[1]
222+
top_vertical_bar.value = 0
223+
224+
display.refresh()
225+
time.sleep(delay)

0 commit comments

Comments
 (0)