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

Commit 5563fad

Browse files
author
Daniel Havlik
committed
pep8
1 parent e26e23a commit 5563fad

File tree

1 file changed

+94
-85
lines changed
  • Adafruit_PWM_Servo_Driver/ada.pca9685/src/ada/pca9685

1 file changed

+94
-85
lines changed
Lines changed: 94 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,105 @@
1-
#!/usr/bin/python
1+
#coding:utf8
22

3-
import smbus
43

5-
# ===========================================================================
6-
# Adafruit_I2C Base Class
7-
# ===========================================================================
4+
import smbus
85

9-
class Adafruit_I2C :
106

11-
def __init__(self, address, bus_id=0, debug=False):
12-
bus = smbus.SMBus(bus_id)
13-
self.address = address
14-
self.bus = bus
15-
self.debug = debug
7+
class Adafruit_I2C(object):
8+
"""I2C Communication."""
9+
def __init__(self, address, bus=smbus.SMBus(0), debug=False):
10+
self.address = address
11+
self.bus = bus
12+
self.debug = debug
1613

17-
def reverseByteOrder(self, data):
18-
"Reverses the byte order of an int (16-bit) or long (32-bit) value"
19-
# Courtesy Vishal Sapre
20-
dstr = hex(data)[2:].replace('L','')
21-
byteCount = len(dstr[::2])
22-
val = 0
23-
for i, n in enumerate(range(byteCount)):
24-
d = data & 0xFF
25-
val |= (d << (8 * (byteCount - i - 1)))
26-
data >>= 8
27-
return val
14+
def reverseByteOrder(self, data):
15+
"Reverses the byte order of an int (16-bit) or long (32-bit) value"
16+
# Courtesy Vishal Sapre
17+
dstr = hex(data)[2:].replace('L', '')
18+
byteCount = len(dstr[::2])
19+
val = 0
20+
for i, n in enumerate(range(byteCount)):
21+
d = data & 0xFF
22+
val |= (d << (8 * (byteCount - i - 1)))
23+
data >>= 8
24+
return val
2825

29-
def write8(self, reg, value):
30-
"Writes an 8-bit value to the specified register/address"
31-
try:
32-
self.bus.write_byte_data(self.address, reg, value)
33-
if (self.debug):
34-
print("I2C: Wrote 0x%02X to register 0x%02X" % (value, reg))
35-
except IOError, err:
36-
print "Error accessing 0x%02X: Check your I2C address" % self.address
37-
return -1
26+
def write8(self, reg, value):
27+
"Writes an 8-bit value to the specified register/address"
28+
try:
29+
self.bus.write_byte_data(self.address, reg, value)
30+
if self.debug:
31+
print("I2C: Wrote 0x%02X to register 0x%02X" % (value, reg))
32+
except IOError, err:
33+
print "Error accessing 0x%02X: Check your I2C address" % (
34+
self.address)
35+
return -1
3836

39-
def writeList(self, reg, list):
40-
"Writes an array of bytes using I2C format"
41-
try:
42-
self.bus.write_i2c_block_data(self.address, reg, list)
43-
except IOError, err:
44-
print "Error accessing 0x%02X: Check your I2C address" % self.address
45-
return -1
37+
def writeList(self, reg, list):
38+
"Writes an array of bytes using I2C format"
39+
try:
40+
self.bus.write_i2c_block_data(self.address, reg, list)
41+
except IOError, err:
42+
print "Error accessing 0x%02X: Check your I2C address" % (
43+
self.address)
44+
return -1
4645

47-
def readU8(self, reg):
48-
"Read an unsigned byte from the I2C device"
49-
try:
50-
result = self.bus.read_byte_data(self.address, reg)
51-
if (self.debug):
52-
print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg)
53-
return result
54-
except IOError, err:
55-
print "Error accessing 0x%02X: Check your I2C address" % self.address
56-
return -1
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" % (
52+
self.address, result & 0xFF, reg)
53+
return result
54+
except IOError, err:
55+
print "Error accessing 0x%02X: Check your I2C address" % (
56+
self.address)
57+
return -1
5758

58-
def readS8(self, reg):
59-
"Reads a signed byte from the I2C device"
60-
try:
61-
result = self.bus.read_byte_data(self.address, reg)
62-
if (self.debug):
63-
print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg)
64-
if (result > 127):
65-
return result - 256
66-
else:
67-
return result
68-
except IOError, err:
69-
print "Error accessing 0x%02X: Check your I2C address" % self.address
70-
return -1
59+
def readS8(self, reg):
60+
"Reads a signed byte from the I2C device"
61+
try:
62+
result = self.bus.read_byte_data(self.address, reg)
63+
if self.debug:
64+
print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (
65+
self.address, result & 0xFF, reg)
66+
if (result > 127):
67+
return result - 256
68+
else:
69+
return result
70+
except IOError, err:
71+
print "Error accessing 0x%02X: Check your I2C address" % (
72+
self.address)
73+
return -1
7174

72-
def readU16(self, reg):
73-
"Reads an unsigned 16-bit value from the I2C device"
74-
try:
75-
hibyte = self.bus.read_byte_data(self.address, reg)
76-
result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1)
77-
if (self.debug):
78-
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
79-
return result
80-
except IOError, err:
81-
print "Error accessing 0x%02X: Check your I2C address" % self.address
82-
return -1
75+
def readU16(self, reg):
76+
"Reads an unsigned 16-bit value from the I2C device"
77+
try:
78+
hibyte = self.bus.read_byte_data(self.address, reg)
79+
result = (hibyte << 8) + \
80+
self.bus.read_byte_data(self.address, reg + 1)
81+
if self.debug:
82+
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (
83+
self.address, result & 0xFFFF, reg)
84+
return result
85+
except IOError, err:
86+
print "Error accessing 0x%02X: Check your I2C address" % (
87+
self.address)
88+
return -1
8389

84-
def readS16(self, reg):
85-
"Reads a signed 16-bit value from the I2C device"
86-
try:
87-
hibyte = self.bus.read_byte_data(self.address, reg)
88-
if (hibyte > 127):
89-
hibyte -= 256
90-
result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1)
91-
if (self.debug):
92-
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
93-
return result
94-
except IOError, err:
95-
print "Error accessing 0x%02X: Check your I2C address" % self.address
96-
return -1
90+
def readS16(self, reg):
91+
"Reads a signed 16-bit value from the I2C device"
92+
try:
93+
hibyte = self.bus.read_byte_data(self.address, reg)
94+
if (hibyte > 127):
95+
hibyte -= 256
96+
result = (hibyte << 8) + \
97+
self.bus.read_byte_data(self.address, reg + 1)
98+
if self.debug:
99+
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (
100+
self.address, result & 0xFFFF, reg)
101+
return result
102+
except IOError, err:
103+
print "Error accessing 0x%02X: Check your I2C address" % (
104+
self.address)
105+
return -1

0 commit comments

Comments
 (0)