Skip to content

Commit fcf6357

Browse files
authored
Merge pull request #63 from jithinbp/development
- Added Support for SX1276 LoRa module for long range wireless communication - Code edits and bug fixes for various sensor libraries - Added a transaction function (sciencelab.SPI.xfer) for bulk data exchange via the SPI bus
2 parents fcf6a0d + 2e6c264 commit fcf6357

File tree

9 files changed

+390
-68
lines changed

9 files changed

+390
-68
lines changed

PSL/Peripherals.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ def __captureStart__(self,address,location,sample_length,total_samples,tg):
427427
def __retrievebuffer__(self):
428428
'''
429429
Fetch data acquired by the I2C scope. refer to :func:`__captureStart__`
430-
431430
'''
432431
total_int_samples = self.total_bytes/2
433432
DATA_SPLITTING = 500
@@ -440,7 +439,7 @@ def __retrievebuffer__(self):
440439
self.H.__sendInt__(DATA_SPLITTING)
441440
self.H.__sendInt__(i*DATA_SPLITTING)
442441
rem = DATA_SPLITTING*2+1
443-
for a in range(200):
442+
for _ in range(200):
444443
partial = self.H.fd.read(rem) #reading int by int sometimes causes a communication error. this works better.
445444
rem -=len(partial)
446445
data+=partial
@@ -457,7 +456,7 @@ def __retrievebuffer__(self):
457456
self.H.__sendInt__(total_int_samples%DATA_SPLITTING)
458457
self.H.__sendInt__(total_int_samples-total_int_samples%DATA_SPLITTING)
459458
rem = 2*(total_int_samples%DATA_SPLITTING)+1
460-
for a in range(20):
459+
for _ in range(20):
461460
partial = self.H.fd.read(rem) #reading int by int sometimes causes a communication error. this works better.
462461
rem -=len(partial)
463462
data+=partial
@@ -825,6 +824,13 @@ def send16_burst(self, value):
825824
except Exception as ex:
826825
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)
827826

827+
def xfer(self,chan,data):
828+
self.start(chan)
829+
reply=[]
830+
for a in data:
831+
reply.append(self.send8(a))
832+
self.stop(chan)
833+
return reply
828834

829835
class DACCHAN:
830836
def __init__(self, name, span, channum, **kwargs):

PSL/SENSORS/BH1750.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from __future__ import print_function
2-
from numpy import int16
32

43

54
def connect(route, **args):

PSL/SENSORS/HMC5883L.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ def setGain(self, gain):
7777
self.gainValue = self.gain_choices.index(gain)
7878
self.__writeCONFB__()
7979

80-
def getVals(self, addr, bytes):
81-
vals = self.I2C.readBulk(self.ADDRESS, addr, bytes)
80+
def getVals(self, addr, numbytes):
81+
vals = self.I2C.readBulk(self.ADDRESS, addr, numbytes)
8282
return vals
8383

8484
def getRaw(self):

PSL/SENSORS/MLX90614.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from __future__ import print_function
2-
from numpy import int16
32

43

54
def connect(route, **args):
@@ -27,8 +26,8 @@ def __init__(self, I2C, **args):
2726
try:
2827
print('switching baud to 100k')
2928
self.I2C.configI2C(100e3)
30-
except:
31-
print('FAILED TO CHANGE BAUD RATE')
29+
except Exception as e:
30+
print('FAILED TO CHANGE BAUD RATE',e.message)
3231

3332
def select_source(self, source):
3433
if source == 'object temperature':
@@ -40,8 +39,8 @@ def readReg(self, addr):
4039
x = self.getVals(addr, 2)
4140
print(hex(addr), hex(x[0] | (x[1] << 8)))
4241

43-
def getVals(self, addr, bytes):
44-
vals = self.I2C.readBulk(self.ADDRESS, addr, bytes)
42+
def getVals(self, addr, numbytes):
43+
vals = self.I2C.readBulk(self.ADDRESS, addr, numbytes)
4544
return vals
4645

4746
def getRaw(self):

PSL/SENSORS/MPU6050.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ def __init__(self, I2C, **args):
3737
'KalmanFilter': {'dataType':'double','min':0,'max':1000,'prefix':'value: '} }
3838
self.setGyroRange(2000)
3939
self.setAccelRange(16)
40-
'''
41-
try:
42-
self.I2C.configI2C(400e3)
43-
except:
44-
pass
45-
'''
4640
self.powerUp()
4741
self.K = None
4842

@@ -60,8 +54,8 @@ def KalmanFilter(self, opt):
6054
sd = std(noise[a])
6155
self.K[a] = KalmanFilter(1. / opt, sd ** 2)
6256

63-
def getVals(self, addr, bytes):
64-
vals = self.I2C.readBulk(self.ADDRESS, addr, bytes)
57+
def getVals(self, addr, numbytes):
58+
vals = self.I2C.readBulk(self.ADDRESS, addr, numbytes)
6559
return vals
6660

6761
def powerUp(self):

PSL/SENSORS/MPU925x.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,9 @@ def __init__(self,I2C,**args):
3737
self.params={'powerUp':None,'setGyroRange':[250,500,1000,2000],'setAccelRange':[2,4,8,16],'KalmanFilter':[.01,.1,1,10,100,1000,10000,'OFF']}
3838
self.setGyroRange(2000)
3939
self.setAccelRange(16)
40-
'''
41-
try:
42-
self.I2C.configI2C(400e3)
43-
except:
44-
pass
45-
'''
4640
self.powerUp()
4741
self.K=None
4842

49-
50-
5143
def KalmanFilter(self,opt):
5244
if opt=='OFF':
5345
self.K=None
@@ -62,9 +54,8 @@ def KalmanFilter(self,opt):
6254
sd = std(noise[a])
6355
self.K[a] = KalmanFilter(1./opt, sd**2)
6456

65-
def getVals(self,addr,bytes):
66-
vals = self.I2C.readBulk(self.ADDRESS,addr,bytes)
67-
return vals
57+
def getVals(self,addr,numbytes):
58+
return self.I2C.readBulk(self.ADDRESS,addr,numbytes)
6859

6960
def powerUp(self):
7061
self.I2C.writeBulk(self.ADDRESS,[0x6B,0])
@@ -79,8 +70,7 @@ def setAccelRange(self,rs):
7970

8071
def getRaw(self):
8172
'''
82-
This method must be defined if you want GUIs to use this class to generate
83-
plots on the fly.
73+
This method must be defined if you want GUIs to use this class to generate plots on the fly.
8474
It must return a set of different values read from the sensor. such as X,Y,Z acceleration.
8575
The length of this list must not change, and must be defined in the variable NUMPLOTS.
8676
@@ -151,8 +141,8 @@ def getMag(self):
151141

152142
def WhoAmI(self):
153143
'''
154-
Returns the ID .
155-
It is 71 for MPU9250 .
144+
Returns the ID.
145+
It is 71 for MPU9250.
156146
'''
157147
v = self.I2C.readBulk(self.ADDRESS,0x75,1)[0]
158148
if v not in [0x71,0x73]:return 'Error %s'%hex(v)
@@ -164,8 +154,8 @@ def WhoAmI(self):
164154

165155
def WhoAmI_AK8963(self):
166156
'''
167-
Returns the ID fo magnetometer AK8963 if found.
168-
It should be 0x48.
157+
Returns the ID fo magnetometer AK8963 if found.
158+
It should be 0x48.
169159
'''
170160
self.initMagnetometer()
171161
v= self.I2C.readBulk(self.AK8963_ADDRESS,0,1) [0]
@@ -175,10 +165,9 @@ def WhoAmI_AK8963(self):
175165
def initMagnetometer(self):
176166
'''
177167
For MPU925x with integrated magnetometer.
178-
It's called a 10 DoF sensor, but technically speaking ,
179-
the 3-axis Accel , 3-Axis Gyro, temperature sensor are integrated in one IC, and the 3-axis magnetometer is implemented in a
168+
It's called a 10 DoF sensor, but technically speaking ,
169+
the 3-axis Accel , 3-Axis Gyro, temperature sensor are integrated in one IC, and the 3-axis magnetometer is implemented in a
180170
separate IC which can be accessed via an I2C passthrough.
181-
182171
Therefore , in order to detect the magnetometer via an I2C scan, the passthrough must first be enabled on IC#1 (Accel,gyro,temp)
183172
'''
184173
self.I2C.writeBulk(self.ADDRESS,[self.INT_PIN_CFG,0x22]) #I2C passthrough
@@ -190,8 +179,8 @@ def initMagnetometer(self):
190179
if __name__ == "__main__":
191180
from PSL import sciencelab
192181
I = sciencelab.connect()
193-
A = connect(I.I2C)
194-
t,x,y,z = I.I2C.capture(A.ADDRESS,0x43,6,5000,1000,'int')
182+
A = connect(I.I2C)
183+
t,x,y,z = I.I2C.capture(A.ADDRESS,0x43,6,5000,1000,'int')
195184
#print (t,x,y,z)
196185
from pylab import *
197186
plot(t,x)

PSL/SENSORS/SHT21.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from __future__ import print_function
2-
from numpy import int16
32
import time
43

54

@@ -9,6 +8,27 @@ def connect(route, **args):
98
'''
109
return SHT21(route, **args)
1110

11+
def rawToTemp( vals):
12+
if vals:
13+
if len(vals):
14+
v = (vals[0] << 8) | (vals[1] & 0xFC) # make integer & remove status bits
15+
v *= 175.72
16+
v /= (1 << 16)
17+
v -= 46.85
18+
return [v]
19+
return False
20+
21+
def rawToRH( vals):
22+
if vals:
23+
if len(vals):
24+
v = (vals[0] << 8) | (vals[1] & 0xFC) # make integer & remove status bits
25+
v *= 125.
26+
v /= (1 << 16)
27+
v -= 6
28+
return [v]
29+
return False
30+
31+
1232

1333
class SHT21():
1434
RESET = 0xFE
@@ -38,25 +58,6 @@ def init(self):
3858
self.I2C.writeBulk(self.ADDRESS, [self.RESET]) # soft reset
3959
time.sleep(0.1)
4060

41-
def rawToTemp(self, vals):
42-
if vals:
43-
if len(vals):
44-
v = (vals[0] << 8) | (vals[1] & 0xFC) # make integer & remove status bits
45-
v *= 175.72
46-
v /= (1 << 16)
47-
v -= 46.85
48-
return [v]
49-
return False
50-
51-
def rawToRH(self, vals):
52-
if vals:
53-
if len(vals):
54-
v = (vals[0] << 8) | (vals[1] & 0xFC) # make integer & remove status bits
55-
v *= 125.
56-
v /= (1 << 16)
57-
v -= 6
58-
return [v]
59-
return False
6061

6162
@staticmethod
6263
def _calculate_checksum(data, number_of_bytes):
@@ -69,7 +70,7 @@ def _calculate_checksum(data, number_of_bytes):
6970
# calculates 8-Bit checksum with given polynomial
7071
for byteCtr in range(number_of_bytes):
7172
crc ^= (data[byteCtr])
72-
for bit in range(8, 0, -1):
73+
for _ in range(8, 0, -1):
7374
if crc & 0x80:
7475
crc = (crc << 1) ^ POLYNOMIAL
7576
else:
@@ -95,6 +96,6 @@ def getRaw(self):
9596
print(vals)
9697
return False
9798
if self.selected == self.TEMP_ADDRESS:
98-
return self.rawToTemp(vals)
99+
return rawToTemp(vals)
99100
elif self.selected == self.HUMIDITY_ADDRESS:
100-
return self.rawToRH(vals)
101+
return rawToRH(vals)

0 commit comments

Comments
 (0)