-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
python: update spin the wheel experiment
- Loading branch information
Showing
1 changed file
with
102 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,123 @@ | ||
# Imports go at the top | ||
from microbit import * | ||
|
||
DEGREES_N: int = 0 | ||
DEGREES_NE: int = 45 | ||
DEGREES_E: int = 90 | ||
DEGREES_SE: int = 135 | ||
DEGREES_S: int = 180 | ||
DEGREES_SW: int = 225 | ||
DEGREES_W: int = 270 | ||
DEGREES_NW: int = 315 | ||
DEGREES_N_ALT: int = 360 | ||
|
||
heading: int = 0 | ||
class Wheel: | ||
"""Manages the animation of a spinning compass/wheel.""" | ||
|
||
print("INFO: Use buttons to turn the arrow") | ||
DEGREES_N: int = 0 | ||
DEGREES_NE: int = 45 | ||
DEGREES_E: int = 90 | ||
DEGREES_SE: int = 135 | ||
DEGREES_S: int = 180 | ||
DEGREES_SW: int = 225 | ||
DEGREES_W: int = 270 | ||
DEGREES_NW: int = 315 | ||
DEGREES_N_ALT: int = 360 | ||
|
||
def __init__(self) -> None: | ||
self.heading: int = 0 | ||
|
||
def turn_clockwise(heading: int) -> int: | ||
print("INFO: turning clockwise") | ||
display.show(Image.ARROW_N) | ||
|
||
if heading >= DEGREES_N_ALT: | ||
heading = 0 | ||
else: | ||
heading += 5 | ||
def turn_clockwise(self) -> None: | ||
"""Turns the wheel clockwise.""" | ||
|
||
return heading | ||
print("INFO: turning clockwise") | ||
|
||
if self.heading >= self.DEGREES_N_ALT: | ||
self.heading = 0 | ||
else: | ||
self.heading += 5 | ||
|
||
def turn_counterclockwise(heading: int) -> int: | ||
print("INFO: turning counterclockwise") | ||
def turn_counterclockwise(self) -> None: | ||
"""turns the wheel counterclockwise.""" | ||
|
||
if heading <= DEGREES_N: | ||
heading = 359 | ||
else: | ||
heading -= 5 | ||
print("INFO: turning counterclockwise") | ||
|
||
return heading | ||
if self.heading <= self.DEGREES_N: | ||
self.heading = 355 | ||
else: | ||
self.heading -= 5 | ||
|
||
def step(self) -> None: | ||
"""Runs the interactive animation.""" | ||
|
||
# Code in a 'while True:' loop repeats forever | ||
while True: | ||
print("INFO: heading: " + str(heading) + "°") | ||
print("INFO: Use buttons to turn the arrow") | ||
|
||
if button_a.is_pressed(): | ||
heading = turn_counterclockwise(heading) | ||
print("INFO: self.heading: " + str(self.heading) + "°") | ||
|
||
if button_b.is_pressed(): | ||
heading = turn_clockwise(heading) | ||
a_pressed: bool = False | ||
b_pressed: bool = False | ||
|
||
if (heading >= DEGREES_N and heading <= (DEGREES_N + 22)) or ( | ||
heading < DEGREES_N_ALT and heading >= (DEGREES_N_ALT - 22) | ||
): | ||
print("INFO: NORTH is between 337°-360°, 0°-22°") | ||
display.show(Image.ARROW_N) | ||
while not a_pressed and not b_pressed: | ||
a_pressed = button_a.was_pressed() | ||
b_pressed = button_b.was_pressed() | ||
sleep(500) | ||
|
||
if a_pressed: | ||
self.turn_counterclockwise() | ||
|
||
if b_pressed: | ||
self.turn_clockwise() | ||
|
||
if (self.heading >= self.DEGREES_N and self.heading <= (self.DEGREES_N + 22)) or ( | ||
self.heading < self.DEGREES_N_ALT and self.heading >= (self.DEGREES_N_ALT - 22) | ||
): | ||
print("INFO: NORTH is between 337°-360°, 0°-22°") | ||
display.show(Image.ARROW_N) | ||
|
||
elif (self.heading <= self.DEGREES_NE and self.heading >= (self.DEGREES_NE - 23)) or ( | ||
self.heading >= self.DEGREES_NE and self.heading <= (self.DEGREES_NE + 23) | ||
): | ||
print("INFO: NORTH EAST is between 23°-45°, 45°-67°") | ||
display.show(Image.ARROW_NE) | ||
|
||
elif (self.heading <= self.DEGREES_E and self.heading >= (self.DEGREES_E - 22)) or ( | ||
self.heading >= self.DEGREES_E and self.heading <= (self.DEGREES_E + 22) | ||
): | ||
print("INFO: EAST is between 23°-45°, 45°-67°") | ||
display.show(Image.ARROW_E) | ||
|
||
elif (self.heading <= self.DEGREES_SE and self.heading >= (self.DEGREES_SE - 23)) or ( | ||
self.heading >= self.DEGREES_SE and self.heading <= (self.DEGREES_SE + 23) | ||
): | ||
print("INFO: SOUTH EAST is between 23°-45°, 45°-67°") | ||
display.show(Image.ARROW_SE) | ||
|
||
elif (heading <= DEGREES_NE and heading >= (DEGREES_NE - 23)) or ( | ||
heading >= DEGREES_NE and heading <= (DEGREES_NE + 23) | ||
): | ||
print("INFO: NORTH EAST is between 23°-45°, 45°-67°") | ||
display.show(Image.ARROW_NE) | ||
|
||
elif (heading <= DEGREES_E and heading >= (DEGREES_E - 22)) or ( | ||
heading >= DEGREES_E and heading <= (DEGREES_E + 22) | ||
): | ||
print("INFO: EAST is between 23°-45°, 45°-67°") | ||
display.show(Image.ARROW_E) | ||
|
||
elif (heading <= DEGREES_SE and heading >= (DEGREES_SE - 23)) or ( | ||
heading >= DEGREES_SE and heading <= (DEGREES_SE + 23) | ||
): | ||
print("INFO: SOUTH EAST is between 23°-45°, 45°-67°") | ||
display.show(Image.ARROW_SE) | ||
|
||
elif (heading <= DEGREES_S and heading >= (DEGREES_S - 22)) or ( | ||
heading >= DEGREES_S and heading <= (DEGREES_S + 22) | ||
): | ||
print("INFO: SOUTH is between 203°-180°, 180°-158°") | ||
display.show(Image.ARROW_S) | ||
|
||
elif (heading >= DEGREES_SW and heading <= (DEGREES_SW + 23)) or ( | ||
heading <= DEGREES_SW and heading >= (DEGREES_SW - 23) | ||
): | ||
print("INFO: SOUTH WEST is between 292°-270°, 270°-248°") | ||
display.show(Image.ARROW_SW) | ||
|
||
elif (heading >= DEGREES_W and heading <= (DEGREES_W + 22)) or ( | ||
heading <= DEGREES_W and heading >= (DEGREES_W - 22) | ||
): | ||
print("INFO: WEST is between 292°-270°, 270°-248°") | ||
display.show(Image.ARROW_W) | ||
|
||
elif (heading >= DEGREES_NW and heading <= (DEGREES_NW + 23)) or ( | ||
heading <= DEGREES_NW and heading >= (DEGREES_NW - 23) | ||
): | ||
print("INFO: NORTH WEST is between 292°-270°, 270°-248°") | ||
display.show(Image.ARROW_NW) | ||
elif (self.heading <= self.DEGREES_S and self.heading >= (self.DEGREES_S - 22)) or ( | ||
self.heading >= self.DEGREES_S and self.heading <= (self.DEGREES_S + 22) | ||
): | ||
print("INFO: SOUTH is between 203°-180°, 180°-158°") | ||
display.show(Image.ARROW_S) | ||
|
||
elif (self.heading >= self.DEGREES_SW and self.heading <= (self.DEGREES_SW + 23)) or ( | ||
self.heading <= self.DEGREES_SW and self.heading >= (self.DEGREES_SW - 23) | ||
): | ||
print("INFO: SOUTH WEST is between 292°-270°, 270°-248°") | ||
display.show(Image.ARROW_SW) | ||
|
||
elif (self.heading >= self.DEGREES_W and self.heading <= (self.DEGREES_W + 22)) or ( | ||
self.heading <= self.DEGREES_W and self.heading >= (self.DEGREES_W - 22) | ||
): | ||
print("INFO: WEST is between 292°-270°, 270°-248°") | ||
display.show(Image.ARROW_W) | ||
|
||
elif (self.heading >= self.DEGREES_NW and self.heading <= (self.DEGREES_NW + 23)) or ( | ||
self.heading <= self.DEGREES_NW and self.heading >= (self.DEGREES_NW - 23) | ||
): | ||
print("INFO: NORTH WEST is between 292°-270°, 270°-248°") | ||
display.show(Image.ARROW_NW) | ||
|
||
|
||
print() | ||
print("Interactive Compass/Wheel Animation") | ||
print() | ||
|
||
app: Wheel = Wheel() | ||
|
||
# Code in a 'while True:' loop repeats forever | ||
while True: | ||
app.step() | ||
|
||
sleep(1_000) |