def hmc5883_init(index): fd = -1 try: fd = I2C.open_channel(index) except Exception, e: print e return None
def hmc5883_read(fd): try: I2C.write_byte(fd, RADDR, 6) # read command x1 = I2C.read_bytefrom(fd, 0x03, 6) x2 = I2C.read_bytefrom(fd, 0x04, 6) x = x1 << 8 | x2 # Combine MSB and LSB of X Data output register z1 = I2C.read_bytefrom(fd, 0x05, 6) z2 = I2C.read_bytefrom(fd, 0x06, 6) z = z1 << 8 | z2 # Combine MSB and LSB of Z Data output register y1 = I2C.read_bytefrom(fd, 0x07, 6) y2 = I2C.read_bytefrom(fd, 0x08, 6) except IOError, e: print e return None
def hmc5883_deinit(fd): I2C.close_channel(fd)
angle = math.atan2(y, x) * (180 / 3.14159265) + 180 if angle >= 0 and angle <= 360: return int(angle) else: return None def hmc5883_init(index): fd = -1 try: fd = I2C.open_channel(index) except Exception, e: print e return None try: I2C.set_slave(fd, HMC5883_ADDRESS) I2C.write_byte(fd, WADDR, 6) I2C.write_byteto(fd, 0x00, 0x70, 6) I2C.write_byte(fd, WADDR, 6) I2C.write_byteto(fd, 0x02, 0x00, 6) I2C.write_byte(fd, RADDR, 6) except Exception, e: print e time.sleep(0.1) return fd def hmc5883_deinit(fd): I2C.close_channel(fd)
angle = math.atan2(y, x) * (180 / 3.14159265) + 180 if angle >= 0 and angle <= 360: return int(angle) else: return None def hmc5883_init(index): fd = -1 try: fd = I2C.open_channel(index) except Exception, e: print e return None try: I2C.set_slave(fd, HMC5883_ADDRESS) I2C.write_byte(fd, WADDR, 6) I2C.write_byteto(fd, 0x00, 0x70, 6) I2C.write_byte(fd, WADDR, 6) I2C.write_byteto(fd, 0x02, 0x00, 6) I2C.write_byte(fd, RADDR, 6) except Exception, e: print e time.sleep(0.1) return fd def hmc5883_deinit(fd): I2C.close_channel(fd) def main(): fd = hmc5883_init(0)