How to do it...

Create a Python 3-friendly version of the XLoBorg library (XLoBorg3.py) using wiringpi2, as follows:

#!/usr/bin/env python3 #XLoBorg3.py import wiringpi2 import struct import time def readBlockData(bus,device,register,words): magData=[] for i in range(words): magData.append(bus.readReg16(device,register+i)) return magData class compass: def __init__(self): addr = 0x0E #compass self.i2c = wiringpi2.I2C() self.devMAG=self.i2c.setup(addr) self.initCompass() def initCompass(self): # Acquisition mode register = 0x11 # CTRL_REG2 data = (1 << 7) # Reset before each acquisition data |= (1 << 5) # Raw mode, do not apply user offsets data |= (0 << 5) # Disable reset cycle self.i2c.writeReg8(self.devMAG,register,data) # System operation ...

Get Raspberry Pi 3 Cookbook for Python Programmers - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.