Skip to content

Commit fff2fa2

Browse files
committed
Added a transaction function to sciencelab.SPI . made code edits based on Codacy recommendations
sciencelab.SPI.xfer created. fixes fossasia#64 . Minor changes to Sensor libraries
1 parent ad2f4eb commit fff2fa2

File tree

9 files changed

+340
-350
lines changed

9 files changed

+340
-350
lines changed

PSL/Peripherals.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def __captureStart__(self,address,location,sample_length,total_samples,tg):
384384
Blocking call that starts fetching data from I2C sensors like an oscilloscope fetches voltage readings
385385
You will then have to call `__retrievebuffer__` to fetch this data, and `__dataProcessor` to process and return separate channels
386386
refer to `capture` if you want a one-stop solution.
387-
387+
388388
.. tabularcolumns:: |p{3cm}|p{11cm}|
389389
================== ============================================================================================
390390
**Arguments**
@@ -440,7 +440,7 @@ def __retrievebuffer__(self):
440440
self.H.__sendInt__(DATA_SPLITTING)
441441
self.H.__sendInt__(i*DATA_SPLITTING)
442442
rem = DATA_SPLITTING*2+1
443-
for a in range(200):
443+
for _ in range(200):
444444
partial = self.H.fd.read(rem) #reading int by int sometimes causes a communication error. this works better.
445445
rem -=len(partial)
446446
data+=partial
@@ -825,6 +825,13 @@ def send16_burst(self, value):
825825
except Exception as ex:
826826
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)
827827

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

829836
class DACCHAN:
830837
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy