-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenElectrons_i2c.py
145 lines (123 loc) · 4.54 KB
/
OpenElectrons_i2c.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
#
# Copyright (c) 2014 OpenElectrons.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# History:
# Date Author Comments
# 01/30/14 Deepak Initial authoring.
#
#
## @package OpenElectrons_i2c
# This is the i2c module for OpenElectrons i2c devices.
import smbus
import ctypes
## OpenElectrons_i2c: this class provides i2c functions
# for read and write operations.
class OpenElectrons_i2c(object):
@staticmethod
def pi_rev():
try:
with open('/proc/cpuinfo','r') as cpuinfo:
for line in cpuinfo:
if line.startswith('Hardware'):
#print " rstrip output " +str(line.rstrip()[-4:])
cpu = 10 if line.rstrip()[-4:] in ['2709'] else 0
if line.startswith('Revision'):
# case '3' is for some rare pi board - Deepak
#print " rstrip output " +str(line.rstrip()[-1])
rev = 1 if line.rstrip()[-1] in ['1','2','3'] else 2
return cpu+rev
except:
return 0
@staticmethod
def which_bus():
return 1 if OpenElectrons_i2c.pi_rev() > 1 else 0
## Initialize the class with the i2c address of your device
# @param i2c_address address of your device
def __init__(self, i2c_address):
self.address = i2c_address
b = OpenElectrons_i2c.which_bus()
self.bus = smbus.SMBus(b)
## Write a byte to your i2c device at a given location
# @param self The object pointer.
# @param reg the register to write value at.
# @param value value to write.
def writeByte(self, reg, value):
self.bus.write_byte_data(self.address, reg, value)
def readByte(self, reg):
result = self.bus.read_byte_data(self.address, reg)
return (result)
# for read_i2c_block_data and write_i2c_block_data to work correctly,
# ensure that i2c speed is set correctly on your pi:
# ensure following file with contents as follows:
# /etc/modprobe.d/i2c.conf
# options i2c_bcm2708 baudrate=50000
# (without the first # and space on line above)
#
def readArray(self, reg, length):
results = self.bus.read_i2c_block_data(self.address, reg, length)
return results
def writeArray(self, reg, arr):
self.bus.write_i2c_block_data(self.address, reg, arr)
def writeArray_byte_at_a_time(self, reg, arr):
x=0
for y in arr:
self.writeByte(reg+x, y)
x+=1
return
def readString(self, reg, length):
ss = ''
for x in range(0, length):
ss = ''.join([ss, chr(self.readByte(reg+x))])
return ss
def readArray_byte_at_a_time(self, reg, length):
ss = []
for x in range(0, length):
w=self.readByte(reg+x)
ss.append(w)
return ss
def readInteger(self, reg):
b0 = self.readByte(reg)
b1 = self.readByte(reg+1)
r = b0 + (b1<<8)
return r
def readIntegerSigned(self, reg):
a = self.readInteger(reg)
signed_a = ctypes.c_int(a).value
return signed_a
def readLong(self, reg):
b0 = self.readByte(reg)
b1 = self.readByte(reg+1)
b2 = self.readByte(reg+2)
b3 = self.readByte(reg+3)
r = b0 + (b1<<8) + (b2<<16) + (b3<<24)
return r
def readLongSigned(self, reg):
a = self.readLong(reg)
signed_a = ctypes.c_long(a).value
return signed_a
## Read the firmware version of the i2c device
def GetFirmwareVersion(self):
ver = self.readString(0x00, 8)
return ver
## Read the vendor name of the i2c device
def GetVendorName(self):
vendor = self.readString(0x08, 8)
return vendor
## Read the i2c device id
def GetDeviceId(self):
device = self.readString(0x10, 8)
return device