#!/usr/bin/python
import smbus
import time
 
# Define a class called Ranger

class Ranger():
	# Select the /dev/i2c-0 device
	b = smbus.SMBus(0)

	# Read the distance 
	def getValue(self):
		# Write in the command register (0x00) the command 
		# 0x51 (Real Ranging Mode - Result in centimeters)
		# using the default sfr02 I2C address 0x70
		self.b.write_byte_data(0x70,0x00,0x51)

		# Wait as explained on the datasheet
		time.sleep(0.066)
		
		# Read the hi value
		h = self.b.read_byte_data(0x70,0x02)
		
		# Read the low value
		l = self.b.read_byte_data(0x70,0x03)
		
		# Return the range in cm	
		return h*256+l 	

# Create a Ranger object called sfr02 
sfr02 = Ranger()

# Get the distance from it
print sfr02.getValue(), "cm"
