Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Commit cd346a0

Browse files
Merge pull request #22 from marcoschwartz/master
Code to interface the VCNL4000 to the Raspberry Pi
2 parents 2c8254f + 52fe804 commit cd346a0

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

Adafruit_VCNL4000/Adafruit_I2C.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/python
2+
3+
import smbus
4+
5+
# ===========================================================================
6+
# Adafruit_I2C Base Class
7+
# ===========================================================================
8+
9+
class Adafruit_I2C :
10+
11+
def __init__(self, address, bus=smbus.SMBus(0), debug=False):
12+
self.address = address
13+
self.bus = bus
14+
self.debug = debug
15+
16+
def reverseByteOrder(self, data):
17+
"Reverses the byte order of an int (16-bit) or long (32-bit) value"
18+
# Courtesy Vishal Sapre
19+
dstr = hex(data)[2:].replace('L','')
20+
byteCount = len(dstr[::2])
21+
val = 0
22+
for i, n in enumerate(range(byteCount)):
23+
d = data & 0xFF
24+
val |= (d << (8 * (byteCount - i - 1)))
25+
data >>= 8
26+
return val
27+
28+
def write8(self, reg, value):
29+
"Writes an 8-bit value to the specified register/address"
30+
try:
31+
self.bus.write_byte_data(self.address, reg, value)
32+
if (self.debug):
33+
print("I2C: Wrote 0x%02X to register 0x%02X" % (value, reg))
34+
except IOError, err:
35+
print "Error accessing 0x%02X: Check your I2C address" % self.address
36+
return -1
37+
38+
def writeList(self, reg, list):
39+
"Writes an array of bytes using I2C format"
40+
try:
41+
self.bus.write_i2c_block_data(self.address, reg, list)
42+
except IOError, err:
43+
print "Error accessing 0x%02X: Check your I2C address" % self.address
44+
return -1
45+
46+
def readU8(self, reg):
47+
"Read an unsigned byte from the I2C device"
48+
try:
49+
result = self.bus.read_byte_data(self.address, reg)
50+
if (self.debug):
51+
print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg)
52+
return result
53+
except IOError, err:
54+
print "Error accessing 0x%02X: Check your I2C address" % self.address
55+
return -1
56+
57+
def readS8(self, reg):
58+
"Reads a signed byte from the I2C device"
59+
try:
60+
result = self.bus.read_byte_data(self.address, reg)
61+
if (self.debug):
62+
print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg)
63+
if (result > 127):
64+
return result - 256
65+
else:
66+
return result
67+
except IOError, err:
68+
print "Error accessing 0x%02X: Check your I2C address" % self.address
69+
return -1
70+
71+
def readU16(self, reg):
72+
"Reads an unsigned 16-bit value from the I2C device"
73+
try:
74+
hibyte = self.bus.read_byte_data(self.address, reg)
75+
result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1)
76+
if (self.debug):
77+
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
78+
return result
79+
except IOError, err:
80+
print "Error accessing 0x%02X: Check your I2C address" % self.address
81+
return -1
82+
83+
def readS16(self, reg):
84+
"Reads a signed 16-bit value from the I2C device"
85+
try:
86+
hibyte = self.bus.read_byte_data(self.address, reg)
87+
if (hibyte > 127):
88+
hibyte -= 256
89+
result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1)
90+
if (self.debug):
91+
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
92+
return result
93+
except IOError, err:
94+
print "Error accessing 0x%02X: Check your I2C address" % self.address
95+
return -1

Adafruit_VCNL4000/Adafruit_I2C.pyc

4.37 KB
Binary file not shown.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/python
2+
3+
import time
4+
from Adafruit_I2C import Adafruit_I2C
5+
6+
# ===========================================================================
7+
# VCNL4000 Class
8+
# ===========================================================================
9+
10+
# Address of the sensor
11+
VCNL4000_ADDRESS = 0x13
12+
13+
# Commands
14+
VCNL4000_COMMAND = 0x80
15+
VCNL4000_PRODUCTID = 0x81
16+
VCNL4000_IRLED = 0x83
17+
VCNL4000_AMBIENTPARAMETER = 0x84
18+
VCNL4000_AMBIENTDATA = 0x85
19+
VCNL4000_PROXIMITYDATA = 0x87
20+
VCNL4000_SIGNALFREQ = 0x89
21+
VCNL4000_PROXINITYADJUST = 0x8A
22+
23+
VCNL4000_3M125 = 0
24+
VCNL4000_1M5625 = 1
25+
VCNL4000_781K25 = 2
26+
VCNL4000_390K625 = 3
27+
28+
VCNL4000_MEASUREAMBIENT = 0x10
29+
VCNL4000_MEASUREPROXIMITY = 0x08
30+
VCNL4000_AMBIENTREADY = 0x40
31+
VCNL4000_PROXIMITYREADY = 0x20
32+
33+
class VCNL4000 :
34+
i2c = None
35+
36+
# Constructor
37+
def __init__(self, address=0x13):
38+
self.i2c = Adafruit_I2C(address)
39+
40+
self.address = address
41+
42+
# Write proximity adjustement register
43+
self.i2c.write8(VCNL4000_PROXINITYADJUST, 0x81);
44+
45+
# Read data from proximity sensor
46+
def read_proximity(self):
47+
self.i2c.write8(VCNL4000_COMMAND, VCNL4000_MEASUREPROXIMITY)
48+
while True:
49+
result = self.i2c.readU8(VCNL4000_COMMAND)
50+
if (result and VCNL4000_PROXIMITYREADY):
51+
return self.i2c.readU16(VCNL4000_PROXIMITYDATA)
52+
time.sleep(0.001)
53+
54+
1.79 KB
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/python
2+
3+
from Adafruit_VCNL4000 import VCNL4000
4+
import time
5+
6+
# ===========================================================================
7+
# Example Code
8+
# ===========================================================================
9+
10+
# Initialise the VNCL4000 sensor
11+
vcnl = VCNL4000(0x13)
12+
13+
# Print proximity sensor data every 100 ms
14+
while True:
15+
16+
print "Data from proximity sensor", vcnl.read_proximity()
17+
time.sleep(0.1)

0 commit comments

Comments
 (0)