Skip to content

Commit

Permalink
python: add real compass experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed May 11, 2024
1 parent 3efe9ae commit 962d811
Show file tree
Hide file tree
Showing 3 changed files with 322 additions and 1 deletion.
6 changes: 5 additions & 1 deletion python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Add a graph of the temperature?

- `temperature()`: returns the integer temperature in Celsius

### Compass
### Fake Compass

Uses an arrow to show which direction it's pointing in.

Expand All @@ -82,6 +82,10 @@ Need a new version that ses animation to show where North, East, West and South

- `heading()`: has the annoying habit of forcing recalibrations, returns a number from 0 to 355 representing the degrees from North starting at 0°.

### Real Compass

This version behaves like a real compass.

### Spin the Wheel

Uses buttons instead of the compass to spin the arrow.
Expand Down
File renamed without changes.
317 changes: 317 additions & 0 deletions python/src/compass-real.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,317 @@
# 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

print("If you don't see any output, please calibrate the compass.")


def draw_arrows(bearing: str) -> None:
display.clear()

north_arrow(bearing)
south_arrow(bearing)
west_arrow(bearing)
east_arrow(bearing)


def north_arrow(bearing: str) -> None:
# 0,0 -> top-left corner
column: int = 0
row: int = 0
brightness: int = 9

if bearing == "north":
column = 2
row = 0
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)
display.set_pixel(column, row + 1, brightness - 3)
display.set_pixel(column, row + 2, brightness - 6)

elif bearing == "north-east":
column = 0
row = 0
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)
display.set_pixel(column + 1, row + 1, brightness - 3)
display.set_pixel(column + 2, row + 2, brightness - 6)

elif bearing == "east":
column = 0
row = 2
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)
display.set_pixel(column + 1, row, brightness - 3)
display.set_pixel(column + 2, row, brightness - 6)

elif bearing == "south-east":
column = 0
row = 4
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)
display.set_pixel(column + 1, row - 1, brightness - 3)
display.set_pixel(column + 2, row - 2, brightness - 6)

elif bearing == "south":
column = 2
row = 4
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)
display.set_pixel(column, row - 1, brightness - 3)
display.set_pixel(column, row - 2, brightness - 6)

elif bearing == "south-west":
column = 4
row = 4
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)
display.set_pixel(column - 1, row - 1, brightness - 3)
display.set_pixel(column - 2, row - 2, brightness - 6)

elif bearing == "west":
column = 4
row = 2
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)
display.set_pixel(column - 1, row, brightness - 3)
display.set_pixel(column - 2, row, brightness - 6)

elif bearing == "north-west":
column = 4
row = 0
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)
display.set_pixel(column - 1, row + 1, brightness - 3)
display.set_pixel(column - 2, row + 2, brightness - 6)


def south_arrow(bearing: str) -> None:
# 0,0 -> top-left corner
column: int = 0
row: int = 0
brightness: int = 9 - 3

if bearing == "north":
column = 2
row = 4
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "north-east":
column = 4
row = 4
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "east":
column = 4
row = 2
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "south-east":
column = 4
row = 0
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "south":
column = 2
row = 0
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "south-west":
column = 0
row = 0
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "west":
column = 0
row = 2
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "north-west":
column = 0
row = 4
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)


def west_arrow(bearing: str) -> None:
# 0,0 -> top-left corner
column: int = 0
row: int = 0
brightness: int = 9 - 6

if bearing == "north":
column = 0
row = 2
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "north-east":
column = 0
row = 4
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "east":
column = 2
row = 4
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "south-east":
column = 4
row = 4
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "south":
column = 4
row = 2
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "south-west":
column = 4
row = 0
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "west":
column = 2
row = 0
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "north-west":
column = 0
row = 0
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)


def east_arrow(bearing: str) -> None:
# 0,0 -> top-left corner
column: int = 0
row: int = 0
brightness: int = 9 - 6

if bearing == "north":
column = 4
row = 2
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "north-east":
column = 4
row = 0
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "east":
column = 2
row = 0
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "south-east":
column = 0
row = 0
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "south":
column = 0
row = 2
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "south-west":
column = 0
row = 4
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "west":
column = 2
row = 4
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)

elif bearing == "north-west":
column = 4
row = 4
# column (0..4), row (0..4), brightness (0..9)
display.set_pixel(column, row, brightness)


# Code in a 'while True:' loop repeats forever
while True:
# this call will make you play fill the screen/dots to calibrate
# the compas - when it's ready it will display a smily face
heading: int = compass.heading()
print("INFO: heading: " + str(heading) + "°")

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°")
draw_arrows("north")

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°")
draw_arrows("north-east")

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°")
draw_arrows("east")

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°")
draw_arrows("south-east")

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°")
draw_arrows("south")

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°")
draw_arrows("south-west")

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°")
draw_arrows("west")

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°")
draw_arrows("north-west")

sleep(1000)

0 comments on commit 962d811

Please sign in to comment.