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

Commit

Permalink
Added I2C support
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Dec 14, 2012
1 parent e3c2e7c commit cc06f13
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
127 changes: 127 additions & 0 deletions adafruit/Adafruit_I2C.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/usr/bin/python

import smbus

# ===========================================================================
# Adafruit_I2C Base Class
# ===========================================================================

class Adafruit_I2C:

def getPiRevision():
"Gets the version number of the Raspberry Pi board"
# Courtesy quick2wire-python-api
# https://github.com/quick2wire/quick2wire-python-api
try:
with open('/proc/cpuinfo','r') as f:
for line in f:
if line.startswith('Revision'):
return 1 if line.rstrip()[-1] in ['1','2'] else 2
except:
return 0

def __init__(self, address, bus=smbus.SMBus(1 if getPiRevision() > 1 else 0), debug=False):
self.address = address
# By default, the correct I2C bus is auto-detected using /proc/cpuinfo
self.bus = bus
# Alternatively, you can hard-code the bus version below:
# self.bus = smbus.SMBus(0); # Force I2C0 (early 256MB Pi's)
# self.bus = smbus.SMBus(1); # Force I2C1 (512MB Pi's)
self.debug = debug

def reverseByteOrder(self, data):
"Reverses the byte order of an int (16-bit) or long (32-bit) value"
# Courtesy Vishal Sapre
dstr = hex(data)[2:].replace('L','')
byteCount = len(dstr[::2])
val = 0
for i, n in enumerate(range(byteCount)):
d = data & 0xFF
val |= (d << (8 * (byteCount - i - 1)))
data >>= 8
return val

def write8(self, reg, value):
"Writes an 8-bit value to the specified register/address"
try:
self.bus.write_byte_data(self.address, reg, value)
if (self.debug):
print "I2C: Wrote 0x%02X to register 0x%02X" % (value, reg)
except IOError, err:
print "Error accessing 0x%02X: Check your I2C address" % self.address
return -1

def writeList(self, reg, list):
"Writes an array of bytes using I2C format"
try:
if (self.debug):
print "I2C: Writing list to register 0x%02X:" % reg
print list
self.bus.write_i2c_block_data(self.address, reg, list)
except IOError, err:
print "Error accessing 0x%02X: Check your I2C address" % self.address
return -1

def readList(self, reg, length):
"Read a list of bytes from the I2C device"
results = []
try:
results = self.bus.read_i2c_block_data(self.address, reg, length)
if (self.debug):
print "I2C: Device 0x%02X returned the following from reg 0x%02X" % (self.address, reg)
print results
return results
except IOError, err:
print "Error accessing 0x%02X: Check your I2C address" % self.address
return -1

def readU8(self, reg):
"Read an unsigned byte from the I2C device"
try:
result = self.bus.read_byte_data(self.address, reg)
if (self.debug):
print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg)
return result
except IOError, err:
print "Error accessing 0x%02X: Check your I2C address" % self.address
return -1

def readS8(self, reg):
"Reads a signed byte from the I2C device"
try:
result = self.bus.read_byte_data(self.address, reg)
if (self.debug):
print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg)
if (result > 127):
return result - 256
else:
return result
except IOError, err:
print "Error accessing 0x%02X: Check your I2C address" % self.address
return -1

def readU16(self, reg):
"Reads an unsigned 16-bit value from the I2C device"
try:
hibyte = self.bus.read_byte_data(self.address, reg)
result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1)
if (self.debug):
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
return result
except IOError, err:
print "Error accessing 0x%02X: Check your I2C address" % self.address
return -1

def readS16(self, reg):
"Reads a signed 16-bit value from the I2C device"
try:
hibyte = self.bus.read_byte_data(self.address, reg)
if (hibyte > 127):
hibyte -= 256
result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1)
if (self.debug):
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
return result
except IOError, err:
print "Error accessing 0x%02X: Check your I2C address" % self.address
return -1
26 changes: 26 additions & 0 deletions test/test_i2c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import gaugette.ssd1306 as ssd1306
import fonts.arial_regular_10

ssd1306 = ssd1306.SSD1306_I2C(buffer_rows=32, buffer_cols=128)

#ssd1306.draw_line(0,0,ssd1306.cols,ssd1306.rows,1)
#ssd1306.draw_circle(16,18,12,1)

ssd1306.draw_fast_vline(64, 0, 15)

# border-line cases
y=3
dh=0
for i in range(0,8):
ssd1306.draw_fast_vline(i, y+0, i+1+dh)
ssd1306.draw_fast_vline(8+i, y+i, 8-i+dh)
ssd1306.draw_fast_vline(16+i, i, 1)
ssd1306.draw_fast_vline(24+i, i, 2)
ssd1306.draw_fast_vline(32+i, i, 3)
ssd1306.draw_fast_vline(48+i, i, 15-2*i)
pass


#ssd1306.draw_fast_vline(1, 1, ssd1306.buffer_rows-2)
#ssd1306.draw_fast_vline(0, 0, ssd1306.buffer_rows)
#ssd1306.fill_rect(110, 8, 3, 3, 1)

0 comments on commit cc06f13

Please sign in to comment.