Simple test

Ensure your device works with this simple test.

examples/progressbar_simpletest.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import displayio
from adafruit_progressbar.horizontalprogressbar import (
    HorizontalProgressBar,
    HorizontalFillDirection,
)

# Make the display context
splash = displayio.Group(max_size=10)
board.DISPLAY.show(splash)

# set progress bar width and height relative to board's display
width = board.DISPLAY.width - 40
height = 30

x = board.DISPLAY.width // 2 - width // 2
y = board.DISPLAY.height // 3

# Create a new progress_bar object at (x, y)
progress_bar = HorizontalProgressBar(
    (x, y), (width, height), direction=HorizontalFillDirection.LEFT_TO_RIGHT
)

# Append progress_bar to the splash group
splash.append(progress_bar)

current_value = progress_bar.minimum
while True:
    # range end is exclusive so we need to use 1 bigger than max number that we want
    for current_value in range(progress_bar.minimum, progress_bar.maximum + 1, 1):
        print("Progress: {}%".format(current_value))
        progress_bar.value = current_value
        time.sleep(0.01)
    time.sleep(0.3)
    progress_bar.value = progress_bar.minimum
    time.sleep(0.3)

Color Scale

Example showing how to change the progressbar color while updating the values

examples/progressbar_displayio_blinka_color_scale.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3

# SPDX-FileCopyrightText: 2021 Hugo Dahl for Adafruit Industries
# SPDX-FileCopyrightText: 2021 Jose David M.
# SPDX-License-Identifier: MIT

# Before you can run this example, you will need to install the
# required libraries identifies in the `requirements.txt` file.
# You can do so automatically by using the "pip" utility.

"""
Shows the ability of the progress bar to change color on the fly.
This example is and adaptation from the progressbar_displayio_blinka test
"""

import time
import adafruit_fancyled.adafruit_fancyled as fancy
import displayio
from blinka_displayio_pygamedisplay import PyGameDisplay
from adafruit_progressbar.horizontalprogressbar import (
    HorizontalProgressBar,
    HorizontalFillDirection,
)

display = PyGameDisplay(width=320, height=240, auto_refresh=False)
splash = displayio.Group(max_size=10)
display.show(splash)

# Setting up the grayscale values, You could use a different scale, and add more entries
# to have detailed transitions
# see learning guide regarding the FancyLed library
# https://learn.adafruit.com/fancyled-library-for-circuitpython/palettes
grad = [
    (0.0, 0x000000),
    (0.20, 0x333333),
    (0.40, 0x666666),
    (0.60, 0x999999),
    (0.80, 0xCCCCCC),
    (1.0, 0xEEEEEE),
]

# Creating the grayscale Palette using the FancyLed Library
palette = fancy.expand_gradient(grad, 50)

colors = list()

# We create an equal space palette. This is done for convenience and clarity as we use
# a value from 0 to 100 in our ProgressBar
for i in range(99):
    color = fancy.palette_lookup(palette, i / 100)
    colors.append(color.pack())

# Background creation
color_bitmap = displayio.Bitmap(display.width, display.height, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0x2266AA  # Teal-ish-kinda

bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)


horizontal_bar = HorizontalProgressBar(
    (10, 80),
    (180, 40),
    fill_color=0x990099,
    outline_color=0x0000FF,
    bar_color=0x00FF00,
    direction=HorizontalFillDirection.LEFT_TO_RIGHT,
)
splash.append(horizontal_bar)

# List of step values for the progress bar
test_value_range_1 = [i for i in range(99)]

# Must check display.running in the main loop!
while display.running:
    print("\nDemonstration of values between 0 and 100 - Horizontal")
    for val in test_value_range_1:
        horizontal_bar.value = val
        horizontal_bar.bar_color = colors[val]
        display.refresh()
        time.sleep(0.1)

Vertical Simple test

Simple test to show a vertical Progress bar

examples/progressbar_vertical_simpletest.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import displayio
from adafruit_progressbar.verticalprogressbar import (
    VerticalProgressBar,
    VerticalFillDirection,
)

# Make the display context
splash = displayio.Group(max_size=10)
board.DISPLAY.show(splash)

# set progress bar width and height relative to board's display
width = 10
height = board.DISPLAY.height - 40

x = width * 2
y = 10

# Create a new VerticalProgressBar object at (x, y)
vertical_progress_bar = VerticalProgressBar(
    (x, y), (width, height), direction=VerticalFillDirection.TOP_TO_BOTTOM
)

# Append progress_bar to the splash group
splash.append(vertical_progress_bar)

x = x * 2
# Create a second VerticalProgressBar object at (x+20, y)
vertical_progress_bar2 = VerticalProgressBar(
    (x, y), (width, height), direction=VerticalFillDirection.BOTTOM_TO_TOP
)

# Append progress_bar to the splash group
splash.append(vertical_progress_bar2)


current_progress = 0.0
while True:
    # range end is exclusive so we need to use 1 bigger than max number that we want
    for current_progress in range(0, 101, 1):
        print("Progress: {}%".format(current_progress))
        vertical_progress_bar.value = current_progress
        vertical_progress_bar2.value = current_progress
        time.sleep(0.05)
    time.sleep(0.3)
    vertical_progress_bar.value = vertical_progress_bar.minimum
    vertical_progress_bar2.value = vertical_progress_bar.minimum
    time.sleep(0.3)

MatrixPortal Example

Progressbar using the MatrixPortal

examples/examples/progressbar_matrixportal.py
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# Source: https://github.com/ajs256/matrixportal-weather-display

# ############## IMPORTS ###############

# HARDWARE
import time
import board

# DISPLAY
import displayio  # Main display library
import framebufferio  # For showing things on the display
import rgbmatrix  # For talking to matrices specifically

# CONTROLS

import digitalio

from adafruit_progressbar.horizontalprogressbar import (
    HorizontalProgressBar,
    HorizontalFillDirection,
)
from adafruit_progressbar.verticalprogressbar import (
    VerticalProgressBar,
    VerticalFillDirection,
)

# ############## DISPLAY SETUP ###############

# If there was a display before (protomatter, LCD, or E-paper), release it so
# we can create ours
displayio.release_displays()

print("Setting up RGB matrix")

# This next call creates the RGB Matrix object itself. It has the given width
# and height.
#
# These lines are for the Matrix Portal. If you're using a different board,
# check the guide to find the pins and wiring diagrams for your board.
# If you have a matrix with a different width or height, change that too.
matrix = rgbmatrix.RGBMatrix(
    width=64,
    height=32,
    bit_depth=3,
    rgb_pins=[
        board.MTX_R1,
        board.MTX_G1,
        board.MTX_B1,
        board.MTX_R2,
        board.MTX_G2,
        board.MTX_B2,
    ],
    addr_pins=[board.MTX_ADDRA, board.MTX_ADDRB, board.MTX_ADDRC, board.MTX_ADDRD],
    clock_pin=board.MTX_CLK,
    latch_pin=board.MTX_LAT,
    output_enable_pin=board.MTX_OE,
)

# Associate the RGB matrix with a Display so that we can use displayio features
display = framebufferio.FramebufferDisplay(matrix)

print("Adding display group")
group = displayio.Group(max_size=5)  # Create a group to hold all our labels
display.show(group)

print("Creating progress bars and adding to group")

# A horizontal percentage progress bar, valued at 60%
progress_bar = HorizontalProgressBar((2, 8), (40, 8), value=60)
group.insert(0, progress_bar)

# Another progress bar, with explicit range and fill from the right
ranged_bar = HorizontalProgressBar(
    (2, 20),
    (40, 8),
    value=40,
    min_value=0,
    max_value=100,
    direction=HorizontalFillDirection.RIGHT_TO_LEFT,
)
group.insert(1, ranged_bar)

# Sample thermometer from -40C to 50C, with a value of +15C
vertical_bar = VerticalProgressBar(
    (44, 4),
    (8, 24),
    min_value=-40,
    max_value=50,
    value=15,
    bar_color=0x1111FF,
    fill_color=None,
    margin_size=0,
    outline_color=0x2222AA,
)
group.insert(2, vertical_bar)

vertical_bar2 = VerticalProgressBar(
    (54, 4),
    (8, 24),
    min_value=-40,
    max_value=50,
    value=15,
    bar_color=0x1111FF,
    fill_color=None,
    margin_size=0,
    outline_color=0x2222AA,
    direction=VerticalFillDirection.TOP_TO_BOTTOM,
)
group.insert(3, vertical_bar2)

# Countdown to the start of the bars demo
countdown_bar = HorizontalProgressBar(
    (2, 2),
    (20, 5),
    0,
    5,
    value=5,
    bar_color=0x11FF11,
    fill_color=0x333333,
    border_thickness=0,
    margin_size=0,
)

countdown_end_color = 0xFF1111

group.insert(4, countdown_bar)
# group.insert(0, countdown_bar)

print("Progress bars added. Starting demo...")

print("Using countdown bar")

for timer in range(countdown_bar.maximum, countdown_bar.minimum, -1):
    bar_color_to_set = (0x20 * (6 - timer) + 20, (0x20 * (timer - 1)) + 20, 0x10)
    countdown_bar.bar_color = bar_color_to_set
    countdown_bar.value = timer
    time.sleep(1)

print("Removing countdown bar")

countdown_bar.value = 0
group.remove(countdown_bar)

progress_bar_value = 0.0
progress_bar_incr = 3.0

button1 = digitalio.DigitalInOut(board.BUTTON_UP)
button1.switch_to_input(digitalio.Pull.UP)
button2 = digitalio.DigitalInOut(board.BUTTON_DOWN)
button2.switch_to_input(digitalio.Pull.UP)


print("Start forever loop")
while True:

    print("Setting progress bar value to", progress_bar_value)

    progress_bar.value = progress_bar_value
    ranged_bar.value = progress_bar_value
    progress_bar_value += progress_bar_incr

    if not (button1.value and button2.value):

        if not button1.value:  # "UP" button pushed
            print("UP button pressed. Increasing vertical bars by 3")
            vertical_bar.value = min(vertical_bar.maximum, vertical_bar.value + 3)
            vertical_bar2.value = min(vertical_bar2.maximum, vertical_bar2.value + 3)

        if not button2.value:  # "DOWN" button pushed
            print("DOWN button pressed. Decreasing vertical bars by 3")
            vertical_bar.value = max(vertical_bar.minimum, vertical_bar.value - 3)
            vertical_bar2.value = max(vertical_bar2.minimum, vertical_bar2.value - 3)

    if progress_bar_value > progress_bar.maximum:
        progress_bar_value = progress_bar.maximum
        progress_bar_incr *= -1

    if progress_bar_value < progress_bar.minimum:
        progress_bar_value = progress_bar.minimum
        progress_bar_incr *= -1

    time.sleep(0.5)

MagTag Example

Progressbar using the MagTag

examples/progressbar_magtag_simpletest.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
Basic progressbar example script
adapted for use on MagTag.
"""
import time
import board
import displayio
import digitalio
from adafruit_progressbar.progressbar import HorizontalProgressBar

# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.)
# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
display = board.DISPLAY
time.sleep(display.time_to_refresh)

# B/up button will be used to increase the progress
up_btn = digitalio.DigitalInOut(board.BUTTON_B)
up_btn.direction = digitalio.Direction.INPUT
up_btn.pull = digitalio.Pull.UP

# C/down button will be used to increase the progress
down_btn = digitalio.DigitalInOut(board.BUTTON_C)
down_btn.direction = digitalio.Direction.INPUT
down_btn.pull = digitalio.Pull.UP

# Make the display context
splash = displayio.Group(max_size=10)
display.show(splash)

# set progress bar width and height relative to board's display
BAR_WIDTH = display.width - 40
BAR_HEIGHT = 30

x = display.width // 2 - BAR_WIDTH // 2
y = display.height // 3

# Create a new progress_bar object at (x, y)
progress_bar = HorizontalProgressBar(
    (x, y),
    (BAR_WIDTH, BAR_HEIGHT),
    bar_color=0xFFFFFF,
    outline_color=0xAAAAAA,
    fill_color=0x777777,
)

# Append progress_bar to the splash group
splash.append(progress_bar)

# Get a random starting value within our min/max range
current_progress = time.monotonic() % 101
print(current_progress)
progress_bar.value = current_progress

# refresh the display
display.refresh()

value_incrementor = 3

prev_up = up_btn.value
prev_down = down_btn.value
while True:
    cur_up = up_btn.value
    cur_down = down_btn.value
    do_refresh = False
    # if up_btn was just pressed down
    if not cur_up and prev_up:
        current_progress += value_incrementor
        # Wrap if we get over the maximum value
        if current_progress > progress_bar.maximum:
            current_progress = progress_bar.minimum

        do_refresh = True

    if not cur_down and prev_down:
        current_progress -= value_incrementor
        # Wrap if we get below the minimum value
        if current_progress < progress_bar.minimum:
            current_progress = progress_bar.maximum

        do_refresh = True

    if do_refresh:
        print(current_progress)
        progress_bar.value = current_progress

        time.sleep(display.time_to_refresh)
        display.refresh()
        time.sleep(display.time_to_refresh)

    prev_up = cur_up
    prev_down = cur_down

DisplayIO Blinka

Progressbar using DisplayIO in Blinka

examples/progressbar_displayio_blinka.py
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3

# SPDX-FileCopyrightText: 2021 Hugo Dahl for Adafruit Industries
# SPDX-License-Identifier: MIT

# Before you can run this example, you will need to install the
# required libraries identifies in the `requirements.txt` file.
# You can do so automatically by using the "pip" utility.

import time
import sys
import displayio
from blinka_displayio_pygamedisplay import PyGameDisplay
from adafruit_progressbar.progressbar import ProgressBar
from adafruit_progressbar.horizontalprogressbar import (
    HorizontalProgressBar,
    HorizontalFillDirection,
)
from adafruit_progressbar.verticalprogressbar import (
    VerticalProgressBar,
    VerticalFillDirection,
)

display = PyGameDisplay(width=320, height=240, auto_refresh=False)
splash = displayio.Group(max_size=10)
display.show(splash)

color_bitmap = displayio.Bitmap(display.width, display.height, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0x2266AA  # Teal-ish-kinda

bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)

progress_bar = ProgressBar(
    width=180,
    height=40,
    x=10,
    y=20,
    progress=0.0,
    bar_color=0x1100FF,
    outline_color=0x777777,
)
splash.append(progress_bar)

horizontal_bar = HorizontalProgressBar(
    (10, 80),
    (180, 40),
    fill_color=0x778822,
    outline_color=0x0000FF,
    bar_color=0x00FF00,
    direction=HorizontalFillDirection.LEFT_TO_RIGHT,
)
splash.append(horizontal_bar)

horizontal_thermometer = HorizontalProgressBar(
    (10, 140),
    (180, 40),
    value=-10,
    min_value=(-40),
    max_value=130,
    fill_color=0x00FF00,
    outline_color=0x0000FF,
    bar_color=0x22BB88,
    direction=HorizontalFillDirection.RIGHT_TO_LEFT,
)
splash.append(horizontal_thermometer)

vertical_bar = VerticalProgressBar(
    (200, 30),
    (32, 180),
    direction=VerticalFillDirection.TOP_TO_BOTTOM,
)
splash.append(vertical_bar)

vertical_thermometer = VerticalProgressBar(
    (260, 30),
    (32, 180),
    min_value=-40,
    max_value=130,
    direction=VerticalFillDirection.BOTTOM_TO_TOP,
)
splash.append(vertical_thermometer)

test_value_range_1 = [99, 100, 99, 1, 0, 1]
test_value_range_2 = [120, 130, 129, -20, -39, -40, -28]
delay = 2
_incr = 1

# Must check display.running in the main loop!
while display.running:

    print("\nDemonstration of legacy functionality and syntax, increment by 0.01")
    for val in range(0, 101):
        if not display.running:
            sys.exit(0)
        _use_value = round((val * 0.01), 2)
        if (val % 10) == 0:
            print(f"Value has reached {_use_value:2}")
        progress_bar.progress = round(_use_value, 2)
        display.refresh()
        time.sleep(0.05)

    print("\nDemonstration of values between 0 and 100 - Horizontal and vertical")
    for val in test_value_range_1:
        if not display.running:
            sys.exit(0)
        print(f"Setting value to {val}")
        vertical_bar.value = val
        horizontal_bar.value = val
        display.refresh()
        time.sleep(delay)

    print("\nDemonstration of Fahrenheit range -40 and 130 - Horizontal and vertical")
    for val in test_value_range_2:
        if not display.running:
            sys.exit(0)
        print(f"Setting value to {val}")
        vertical_thermometer.value = val
        horizontal_thermometer.value = val
        display.refresh()
        time.sleep(delay)

Combined ProgressBar

Example showing both a Vertical and an Horizontal ProgressBar

examples/progressbar_combined.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3

# SPDX-FileCopyrightText: 2021 Hugo Dahl for Adafruit Industries
# SPDX-License-Identifier: MIT

# Before you can run this example, you will need to install the
# required libraries identifies in the `requirements.txt` file.
# You can do so automatically by using the "pip" utility.

import time
import board
import displayio
from adafruit_progressbar.horizontalprogressbar import HorizontalProgressBar
from adafruit_progressbar.verticalprogressbar import VerticalProgressBar

# Make the display context
splash = displayio.Group(max_size=10)
board.DISPLAY.show(splash)

# set horizontal progress bar width and height relative to board's display
h_width = board.DISPLAY.width - 40
h_height = 30

v_width = 30
v_height = 140

h_x = 20
h_y = 20

v_x = 60
v_y = 70

# Create a new progress_bar objects at their x, y locations
progress_bar = HorizontalProgressBar((h_x, h_y), (h_width, h_height), 0, 100)
vert_progress_bar = VerticalProgressBar((v_x, v_y), (v_width, v_height), 0, 200)

# Append progress_bars to the splash group
splash.append(progress_bar)
splash.append(vert_progress_bar)

current_progress = 0.0
while True:
    # range end is exclusive so we need to use 1 bigger than max number that we want
    for current_progress in range(0, 101, 1):
        print("Progress: {}%".format(current_progress))
        progress_bar.value = current_progress
        vert_progress_bar.value = current_progress * 2
        time.sleep(0.01)
    time.sleep(0.3)
    # reset to empty
    progress_bar.value = 0
    vert_progress_bar.value = 0
    time.sleep(0.3)

Accelerometer Example

With this example you would be able to use the progress bar to display accelerometer data in the X and Y directions

examples/progressbar_accelerometer.py
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# SPDX-FileCopyrightText: 2021 Jose David M.
# SPDX-License-Identifier: MIT
"""
With this example you would be able to use the progress bar to display accelerometer data
in the X and Y directions
"""
import time
import displayio
import board
from adafruit_progressbar.horizontalprogressbar import (
    HorizontalProgressBar,
    HorizontalFillDirection,
)
from adafruit_progressbar.verticalprogressbar import (
    VerticalProgressBar,
    VerticalFillDirection,
)

# This data is used to show library capability. You could use an actual accelerometer
# This data was extracted from an Adafruit MSA301 Triple Axis Accelerometer
fake__accel_data = [
    (-0.071821, 1.450790, 9.533077),
    (-0.105338, 1.939175, 9.111725),
    (-0.306437, 2.906367, 9.178761),
    (-0.062245, 3.504878, 8.776562),
    (-0.143643, 4.792873, 8.091862),
    (-0.172371, 5.520662, 7.838097),
    (0.014364, 6.176630, 7.426321),
    (-0.205888, 7.526871, 6.200571),
    (-0.090974, 7.905128, 5.491934),
    (-0.445292, 8.216354, 5.118464),
    (-0.110126, 8.872322, 4.017202),
    (-0.344742, 9.542652, 1.498671),
    (-0.076609, 9.580959, -0.033517),
    (-0.158007, 9.518715, -1.273631),
    (0.282497, 9.446892, -2.877639),
    (-0.004788, 9.063847, -3.409117),
    (-0.153219, 8.599400, -4.630077),
    (-0.071821, 8.020042, -6.382517),
    (0.655968, 6.722471, -6.937935),
    (0.464444, 5.740913, -7.771063),
    (1.034226, 4.189575, -8.905838),
    (1.369392, 1.675830, -8.340843),
    (2.149850, -1.426849, -9.178761),
    (2.384466, -3.662886, -8.834019),
    (2.417983, -5.223801, -7.957798),
    (2.336586, -7.900341, -5.482357),
    (2.106757, -9.231426, -4.194363),
    (1.948751, -3.945382, -9.288883),
    (0.588934, 0.062245, -9.643204),
    (0.430928, 2.250400, -9.360706),
    (-0.402199, 7.249161, -6.176630),
    (-0.871432, 8.197201, -5.003550),
    (0.373471, 8.829227, -3.696402),
    (0.584146, 9.662357, -1.287995),
    (0.114914, 9.940063, 0.536266),
    (-0.201100, 9.207489, 2.848910),
    (-0.181947, 8.589825, 5.314775),
    (-0.517113, 5.573332, 7.598692),
    (-0.497961, 3.160136, 9.092575),
    (-0.114914, -1.541763, 9.940063),
    (-0.555418, -5.099310, 7.727970),
    (-0.387835, -7.091154, 7.095942),
    (0.162795, -8.652069, 4.768932),
    (0.531477, -8.934566, 0.751729),
    (0.775670, -9.542652, -4.141693),
    (1.809896, -7.177340, -7.282679),
    (0.957617, -2.868063, -9.308037),
    (1.450790, -0.866643, -9.571381),
    (1.039014, -0.660756, -9.758118),
    (0.914524, -4.907787, -8.379150),
    (1.302359, -8.120594, -5.870192),
    (1.043803, -9.916122, 2.365314),
    (1.086895, -8.412666, 5.223801),
    (2.034936, -5.942015, 7.488565),
    (2.010996, -2.767513, 9.140453),
    (0.397411, -2.322221, 9.130878),
    (-2.025360, -2.398830, 9.116512),
    (-2.824970, -2.264764, 8.896263),
    (-4.395462, -2.001419, 8.259445),
    (-5.640364, -1.220962, 7.569963),
    (-7.000181, -0.746941, 6.679379),
    (-8.077499, -0.004788, 5.338715),
    (-9.001598, 0.421352, 2.585566),
    (-9.408588, 1.106048, 1.053379),
    (-9.097363, 2.283916, -1.589644),
    (-8.522793, 2.714845, -3.021282),
    (-7.991314, 3.083527, -4.505589),
    (-6.416035, 3.720343, -6.732048),
    (-6.186207, 3.562336, -5.788795),
    (-3.289414, 3.428269, -8.254658),
    (-1.110836, 3.787375, -8.944141),
    (1.082107, 3.270263, -9.059055),
    (1.565704, 3.820892, -8.446182),
    (2.212095, 3.763435, -8.221142),
    (3.030858, 4.175211, -8.029617),
    (-2.365314, 2.633447, -8.221142),
    (-5.372232, 2.188155, -7.445473),
    (-8.465336, 2.116334, -4.577410),
    (-9.432529, 1.388545, 0.541054),
    (-6.957088, 1.623161, 6.085657),
    (-4.735416, 0.751729, 8.992023),
    (-1.800320, 2.063664, 9.762905),
    (-0.153219, -1.795532, 9.657566),
    (5.764854, -3.801740, 6.775141),
    (9.470833, -2.240824, 2.777089),
    (9.925701, -1.000710, -1.915234),
    (8.685585, 0.277709, -4.347582),
    (9.676720, -0.459656, -0.521901),
    (9.719814, -0.689484, 1.584856),
    (8.541943, -1.503459, 4.160847),
    (7.608267, -1.824261, 5.865404),
    (5.817524, -1.446002, 6.770353),
    (3.887925, -1.991844, 8.671223),
    (1.805108, -2.039724, 9.303249),
    (0.593723, -1.690194, 9.518715),
    (0.852279, -2.087605, 9.427738),
    (1.206597, -1.857777, 9.226639),
    (0.392623, -2.255188, 9.193123),
    (-2.475440, -2.154638, 9.173969),
    (-3.677250, -8.288174, 3.198441),
    (-0.981558, -2.944673, 8.977661),
    (1.517823, 3.409117, 8.977661),
    (2.796242, 5.989895, 5.807947),
    (4.151270, -2.552050, 9.418163),
    (4.242243, -9.844303, 1.694982),
    (5.946802, -3.543183, 6.890055),
    (7.028910, -4.462496, 4.199150),
    (-1.694982, -6.655439, 6.430399),
    (0.703849, -3.112255, 8.685585),
    (1.340664, 4.342793, 8.053558),
    (1.627949, 9.920914, -0.608087),
    (6.985817, 0.517113, 7.517294),
    (5.434477, -5.372232, 5.994682),
    (4.165634, -6.224510, 8.082287),
    (0.847491, -4.677959, 9.509136),
    (3.476150, -4.812025, 7.421532),
]
display = board.DISPLAY
main_group = displayio.Group(max_size=10)
display.show(main_group)

color_bitmap = displayio.Bitmap(display.width, display.height, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0x990099

background = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
main_group.append(background)

# Accelerometer Properties. Normally accelerometer well calibrated
# Will give a maximum output of 10 mts / s**2
VALUES_X = (-10, 10)
VALUES_Y = (-10, 10)

# Horizontal Bar Properties
HORIZONTAL_BAR_X_ORIGIN = 10
HORIZONTAL_BAR_Y_ORIGIN = 30
HORIZONTAL_BAR_WIDTH = display.width // 4
HORIZONTAL_BAR_HEIGHT = 30

# Vertical Bar Properties
VERTICAL_BAR_HEIGHT = display.height // 4
VERTICAL_BAR_WIDTH = 30

# We create our bar displays
left_horizontal_bar = HorizontalProgressBar(
    (HORIZONTAL_BAR_X_ORIGIN, HORIZONTAL_BAR_Y_ORIGIN),
    (HORIZONTAL_BAR_WIDTH, HORIZONTAL_BAR_HEIGHT),
    min_value=0,
    max_value=-VALUES_X[0],
    direction=HorizontalFillDirection.RIGHT_TO_LEFT,
)
main_group.append(left_horizontal_bar)

right_horizontal_bar = HorizontalProgressBar(
    (HORIZONTAL_BAR_X_ORIGIN + HORIZONTAL_BAR_WIDTH, HORIZONTAL_BAR_Y_ORIGIN),
    (HORIZONTAL_BAR_WIDTH, HORIZONTAL_BAR_HEIGHT),
    min_value=0,
    max_value=VALUES_X[1],
    direction=HorizontalFillDirection.LEFT_TO_RIGHT,
)
main_group.append(right_horizontal_bar)


top_vertical_bar = VerticalProgressBar(
    (HORIZONTAL_BAR_X_ORIGIN + 2 * HORIZONTAL_BAR_WIDTH + 20, HORIZONTAL_BAR_Y_ORIGIN),
    (VERTICAL_BAR_WIDTH, VERTICAL_BAR_HEIGHT),
    min_value=0,
    max_value=VALUES_Y[1],
    direction=VerticalFillDirection.BOTTOM_TO_TOP,
)
main_group.append(top_vertical_bar)

bottom_vertical_bar = VerticalProgressBar(
    (
        HORIZONTAL_BAR_X_ORIGIN + 2 * HORIZONTAL_BAR_WIDTH + 20,
        HORIZONTAL_BAR_Y_ORIGIN + VERTICAL_BAR_HEIGHT,
    ),
    (VERTICAL_BAR_WIDTH, VERTICAL_BAR_HEIGHT),
    min_value=0,
    max_value=-VALUES_Y[0],
    direction=VerticalFillDirection.TOP_TO_BOTTOM,
)
main_group.append(bottom_vertical_bar)

delay = 0.5

while True:
    for val in fake__accel_data:
        if val[0] >= 0:
            left_horizontal_bar.value = 0
            right_horizontal_bar.value = val[0]
        if val[0] < 0:
            left_horizontal_bar.value = -val[0]
            right_horizontal_bar.value = 0

        if val[1] >= 0:
            top_vertical_bar.value = val[1]
            bottom_vertical_bar.value = 0
        if val[1] < 0:
            bottom_vertical_bar.value = -val[1]
            top_vertical_bar.value = 0

        display.refresh()
        time.sleep(delay)