Skip to content

Commit f485adb

Browse files
author
K. Townsend
committed
Added write16 and reverseByteOrder
1 parent 0c82bd7 commit f485adb

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Adafruit_I2C/Adafruit_I2C.py

+20
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ def __init__(self, address, bus=smbus.SMBus(0), debug=False):
1313
self.bus = bus
1414
self.debug = debug
1515

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+
1628
def write8(self, reg, value):
1729
"Writes an 8-bit value to the specified register/address"
1830
try:
@@ -22,6 +34,14 @@ def write8(self, reg, value):
2234
except IOError, err:
2335
print "Error accessing 0x%02X: Check your I2C address" % self.address
2436
return -1
37+
38+
def write16(self, reg, value):
39+
"Writes 16 bits of data to the specified register/address"
40+
try:
41+
self.bus.write_word_data(self.address, reg, value)
42+
except IOError, err:
43+
print "Error accessing 0x%02X: Check your I2C address" % self.address
44+
return -1
2545

2646
def readU8(self, reg):
2747
"Read an unsigned byte from the I2C device"

0 commit comments

Comments
 (0)