Add COMPASS code and sample, color compass app
This commit is contained in:
parent
55f99f655e
commit
cfe4eed9ad
|
@ -220,3 +220,21 @@ class Clock:
|
|||
self.i2.start(self.a, 1)
|
||||
print(list(self.i2.read(16)))
|
||||
self.i2.stop()
|
||||
|
||||
class Magnet:
|
||||
""" MAGNET is an ST LIS3MDL 3-axis magnetometer """
|
||||
def __init__(self, i2, a = 0x1c):
|
||||
self.i2 = i2
|
||||
self.a = a
|
||||
self.i2.regwr(self.a, 0x22, 0) # CTRL_REG3 operating mode 0: continuous conversion
|
||||
|
||||
def rd(self):
|
||||
""" Read the measurement STATUS_REG and OUT_X,Y,Z """
|
||||
return self.i2.regrd(self.a, 0x27, "<B3h")
|
||||
|
||||
def measurement(self):
|
||||
""" Wait for a new field reading, return the (x,y,z) """
|
||||
while True:
|
||||
(status, x, y, z) = self.rd()
|
||||
if status & 8:
|
||||
return (x, y, z)
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
import sys
|
||||
import struct
|
||||
import time
|
||||
|
||||
from i2cdriver import I2CDriver, EDS
|
||||
|
||||
if __name__ == '__main__':
|
||||
i2 = I2CDriver(sys.argv[1])
|
||||
|
||||
d = EDS.Magnet(i2)
|
||||
while 1:
|
||||
print(d.measurement())
|
|
@ -6,7 +6,6 @@ from i2cdriver import I2CDriver, EDS
|
|||
|
||||
if __name__ == '__main__':
|
||||
i2 = I2CDriver(sys.argv[1])
|
||||
i2.scan()
|
||||
|
||||
d = EDS.Pot(i2)
|
||||
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
"""
|
||||
Demo of a simple combination of parts from Electric Dollar Store:
|
||||
|
||||
* MAGNET - 3-axis magnetometer
|
||||
* LED - RGB LED
|
||||
|
||||
This demo takes the compass direction, and uses it to set the LED's
|
||||
color. So as you move the module around, the color changes according to
|
||||
its direction. There is a direction that is pure red, another that is
|
||||
pure green, etc.
|
||||
|
||||
https://electricdollarstore.com
|
||||
|
||||
"""
|
||||
import sys
|
||||
import struct
|
||||
import time
|
||||
|
||||
from i2cdriver import I2CDriver, EDS
|
||||
|
||||
if __name__ == '__main__':
|
||||
i2 = I2CDriver(sys.argv[1])
|
||||
|
||||
magnet = EDS.Magnet(i2)
|
||||
led = EDS.LED(i2)
|
||||
led.rgb(0, 0, 0)
|
||||
while 1:
|
||||
(x, y, z) = magnet.measurement()
|
||||
r = max(0, min(255, (x + 4000) // 32))
|
||||
g = max(0, min(255, (y + 4000) // 32))
|
||||
b = max(0, min(255, (z + 4000) // 32))
|
||||
led.rgb(r, g, b)
|
Loading…
Reference in New Issue