Skip to content

Instantly share code, notes, and snippets.

@sandyjmacdonald
Created March 28, 2019 20:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandyjmacdonald/20c103b62f76d1a6806e965dd81d8963 to your computer and use it in GitHub Desktop.
Save sandyjmacdonald/20c103b62f76d1a6806e965dd81d8963 to your computer and use it in GitHub Desktop.
RGB compass for the Pimoroni Breakout Garden, LSM303D, and RGB matrix
#!/usr/bin/env python
import time
import math
import colorsys
from lsm303d import LSM303D
from rgbmatrix5x5 import RGBMatrix5x5
def raw_heading(minimums, maximums, zero=0):
"""Return a raw compass heading calculated from the magnetometer data."""
X = 0
Y = 2 # Change to 1 if you have the breakout flat
mag_range = 2
mag = list(lsm.magnetometer())
for i in range(len(mag)):
mag[i] = ((mag_range / (maximums[i] - minimums[i])) * mag[i]) - \
(mag_range / 2.0)
heading = math.atan2(mag[Y], mag[X])
if heading < 0:
heading += (2 * math.pi)
heading_degrees = (round(math.degrees(heading), 2) - zero) % 360
return heading_degrees
lsm = LSM303D(0x1d) # Change to 0x1e if you have soldered the address jumper
rgbmatrix5x5 = RGBMatrix5x5()
rgbmatrix5x5.set_clear_on_exit()
rgbmatrix5x5.set_brightness(0.8)
try:
input = raw_input
except NameError:
pass
input("Lay your LSM303D in Breakout Garden flat (LSM303D vertical), \n\
press a key to start, then rotate it 360 degrees, keeping it flat...\n")
t_start = time.time()
t_elapsed = 0
minimums = list(lsm.magnetometer())
maximums = list(lsm.magnetometer())
while t_elapsed < 30:
mag = lsm.magnetometer()
for i in range(len(mag)):
if mag[i] < minimums[i]:
minimums[i] = mag[i]
if mag[i] > maximums[i]:
maximums[i] = mag[i]
t_elapsed = time.time() - t_start
input("Calibration complete!\n\nIf you want to set a zero (North) point, \n\
then turn your breakout to that point and press a key...\n")
zero = raw_heading(minimums, maximums)
input("Press a key to begin readings!\n")
while True:
rh = raw_heading(minimums, maximums, zero=zero)
h = rh / 360
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(h, 1.0, 1.0)]
rgbmatrix5x5.set_all(r, g, b)
rgbmatrix5x5.show()
print("compass heading: {:0.0f} degrees".format(rh, h))
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment