From 35c96f144675f5807163e8de86a2a7040a83662c Mon Sep 17 00:00:00 2001 From: James Bowman Date: Mon, 4 Mar 2019 11:28:47 -0800 Subject: [PATCH] Sparkfun MAG3110 3D Magnetometer --- python/samples/mag3110.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 python/samples/mag3110.py diff --git a/python/samples/mag3110.py b/python/samples/mag3110.py new file mode 100644 index 0000000..9c2f7c4 --- /dev/null +++ b/python/samples/mag3110.py @@ -0,0 +1,35 @@ +""" +Example Xtrinsic MAG3110 Three-Axis Digital Magnetometer +Breakout available from Sparkfun. +""" + +import sys +import struct +import time + +from i2cdriver import I2CDriver, EDS + +class MAG3110: + def __init__(self, i2, a = 0x0e): + self.i2 = i2 + self.a = a + self.i2.regwr(self.a, 0x10, 0b00000001) # CTRL_REG1. ACTIVE mode, 80 Hz conversions + + def rd(self): + """ Read the measurement STATUS_REG and OUT_X,Y,Z """ + return self.i2.regrd(self.a, 0x00, ">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) + +if __name__ == '__main__': + i2 = I2CDriver(sys.argv[1]) + i2.scan() + + d = MAG3110(i2) + while 1: + print(d.measurement())