Skip to content

Commit

Permalink
Merge pull request fossasia#51 from viveksb007/development
Browse files Browse the repository at this point in the history
added reference for values in MCP4728.py and fixed some warnings
  • Loading branch information
mariobehling authored Apr 18, 2017
2 parents 83710f4 + ee92d0d commit 2cddcf2
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 48 deletions.
42 changes: 26 additions & 16 deletions PSL/Peripherals.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,29 +276,32 @@ def read_repeat(self):
self.H.__sendByte__(CP.I2C_READ_MORE)
val = self.H.__getByte__()
self.H.__get_ack__()
return val
except Exception as ex:
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)
return val


def read_end(self):
try:
self.H.__sendByte__(CP.I2C_HEADER)
self.H.__sendByte__(CP.I2C_READ_END)
val = self.H.__getByte__()
self.H.__get_ack__()
return val
except Exception as ex:
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)
return val


def read_status(self):
try:
self.H.__sendByte__(CP.I2C_HEADER)
self.H.__sendByte__(CP.I2C_STATUS)
val = self.H.__getInt__()
self.H.__get_ack__()
return val
except Exception as ex:
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)
return val


def readBulk(self, device_address, register_address, bytes_to_read):
try:
Expand Down Expand Up @@ -626,9 +629,10 @@ def send8(self, value):
self.H.__sendByte__(value) # value byte
v = self.H.__getByte__()
self.H.__get_ack__()
return v
except Exception as ex:
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)
return v


def send16(self, value):
"""
Expand All @@ -651,9 +655,10 @@ def send16(self, value):
self.H.__sendInt__(value) # value byte
v = self.H.__getInt__()
self.H.__get_ack__()
return v
except Exception as ex:
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)
return v


def send8_burst(self, value):
"""
Expand Down Expand Up @@ -762,6 +767,7 @@ def __init__(self, H, vref=3.3, devid=0):
self.CHANS = {'PCS': DACCHAN('PCS', [0, 3.3e-3], 0), 'PV3': DACCHAN('PV3', [0, 3.3], 1),
'PV2': DACCHAN('PV2', [-3.3, 3.3], 2), 'PV1': DACCHAN('PV1', [-5., 5.], 3)}
self.CHANNEL_MAP = {0: 'PCS', 1: 'PV3', 2: 'PV2', 3: 'PV1'}
self.values = {'PV1': 0, 'PV2': 0, 'PV3': 0, 'PCS': 0}

def __ignoreCalibration__(self, name):
self.CHANS[name].calibration_enabled = False
Expand Down Expand Up @@ -798,7 +804,8 @@ def __setRawVoltage__(self, name, v):
'''
val = self.CHANS[name].apply_calibration(v)
self.I2C.writeBulk(self.addr, [64 | (CHAN.channum << 1), (val >> 8) & 0x0F, val & 0xFF])
return CHAN.CodeToV(v)
self.values[name] = CHAN.CodeToV(v)
return self.values[name]

def __writeall__(self, v1, v2, v3, v4):
self.I2C.start(self.addr, 0)
Expand Down Expand Up @@ -871,7 +878,7 @@ class NRF24L01():
I2C_COMMANDS = 2
I2C_TRANSACTION = 0 << 4
I2C_WRITE = 1 << 4
SCAN_I2C = 2 << 4
I2C_SCAN = 2 << 4
PULL_SCL_LOW = 3 << 4
I2C_CONFIG = 4 << 4
I2C_READ = 5 << 4
Expand Down Expand Up @@ -964,9 +971,10 @@ def rxchar(self):
self.H.__sendByte__(CP.NRF_RXCHAR)
value = self.H.__getByte__()
self.H.__get_ack__()
return value
except Exception as ex:
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)
return value


def txchar(self, char):
'''
Expand All @@ -989,9 +997,10 @@ def hasData(self):
self.H.__sendByte__(CP.NRF_HASDATA)
value = self.H.__getByte__()
self.H.__get_ack__()
return value
except Exception as ex:
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)
return value


def flush(self):
'''
Expand Down Expand Up @@ -1031,9 +1040,10 @@ def read_register(self, address):
self.H.__sendByte__(address)
val = self.H.__getByte__()
self.H.__get_ack__()
return val
except Exception as ex:
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)
return val


def get_status(self):
'''
Expand Down Expand Up @@ -1070,9 +1080,9 @@ def write_address(self, register, address):
self.H.__sendByte__(CP.NRFL01)
self.H.__sendByte__(CP.NRF_WRITEADDRESS)
self.H.__sendByte__(register)
self.H.__sendByte__(address & 0xFF);
self.H.__sendByte__((address >> 8) & 0xFF);
self.H.__sendByte__((address >> 16) & 0xFF);
self.H.__sendByte__(address & 0xFF)
self.H.__sendByte__((address >> 8) & 0xFF)
self.H.__sendByte__((address >> 16) & 0xFF)
self.H.__get_ack__()
except Exception as ex:
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)
Expand All @@ -1085,9 +1095,9 @@ def selectAddress(self, address):
try:
self.H.__sendByte__(CP.NRFL01)
self.H.__sendByte__(CP.NRF_WRITEADDRESSES)
self.H.__sendByte__(address & 0xFF);
self.H.__sendByte__((address >> 8) & 0xFF);
self.H.__sendByte__((address >> 16) & 0xFF);
self.H.__sendByte__(address & 0xFF)
self.H.__sendByte__((address >> 8) & 0xFF)
self.H.__sendByte__((address >> 16) & 0xFF)
self.H.__get_ack__()
self.CURRENT_ADDRESS = address
if address not in self.sigs:
Expand Down
32 changes: 18 additions & 14 deletions PSL/packet_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,19 @@ def listPorts(self):
return glob.glob('/dev/ttyACM*') + glob.glob('/dev/ttyUSB*')

def connectToPort(self, portname):
try:
import platform
if platform.system() not in ["Windows", "Darwin"]:
import socket
self.blockingSocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.blockingSocket.bind('\0PSghhhLab%s' % portname)
self.blockingSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except socket.error as e:
self.occupiedPorts.add(portname)
raise RuntimeError("Another program is using %s (%d)" % (portname))
try:
self.blockingSocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.blockingSocket.bind('\0PSghhhLab%s' % portname)
self.blockingSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except socket.error as e:
self.occupiedPorts.add(portname)
raise RuntimeError("Another program is using %s (%d)" % (portname))

fd = serial.Serial(portname, 9600, stopbits=1, timeout=0.02)
fd.read(100);
fd.read(100)
fd.close()
fd = serial.Serial(portname, self.BAUD, stopbits=1, timeout=1.0)
if (fd.inWaiting()):
Expand Down Expand Up @@ -172,9 +174,10 @@ def __getByte__(self):
reads a byte from the serial port and returns it
"""
ss = self.fd.read(1)
if len(ss):
return CP.Byte.unpack(ss)[0]
else:
try:
if len(ss):
return CP.Byte.unpack(ss)[0]
except Exception as ex:
print('byte communication error.', time.ctime())
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)
# sys.exit(1)
Expand All @@ -185,9 +188,10 @@ def __getInt__(self):
returns an integer after combining them
"""
ss = self.fd.read(2)
if len(ss) == 2:
return CP.ShortInt.unpack(ss)[0]
else:
try:
if len(ss) == 2:
return CP.ShortInt.unpack(ss)[0]
except Exception as ex:
print('int communication error.', time.ctime())
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)
# sys.exit(1)
Expand Down
35 changes: 17 additions & 18 deletions PSL/sciencelab.py
Original file line number Diff line number Diff line change
Expand Up @@ -1938,7 +1938,7 @@ def start_one_channel_LA_backup__(self, trigger=1, channel='ID1', maximum_time=6
"""
try:
self.clear_buffer(0, self.MAX_SAMPLES / 2);
self.clear_buffer(0, self.MAX_SAMPLES / 2)
self.H.__sendByte__(CP.TIMING)
self.H.__sendByte__(CP.START_ONE_CHAN_LA)
self.H.__sendInt__(self.MAX_SAMPLES / 4)
Expand Down Expand Up @@ -2085,7 +2085,7 @@ def start_one_channel_LA(self, **args):
# trigger_mode same as channel_mode.
# default_value : 3
try:
self.clear_buffer(0, self.MAX_SAMPLES / 2);
self.clear_buffer(0, self.MAX_SAMPLES / 2)
self.H.__sendByte__(CP.TIMING)
self.H.__sendByte__(CP.START_ALTERNATE_ONE_CHAN_LA)
self.H.__sendInt__(self.MAX_SAMPLES / 4)
Expand Down Expand Up @@ -2167,7 +2167,7 @@ def start_two_channel_LA(self, **args):
trigger = 0

try:
self.clear_buffer(0, self.MAX_SAMPLES);
self.clear_buffer(0, self.MAX_SAMPLES)
self.H.__sendByte__(CP.TIMING)
self.H.__sendByte__(CP.START_TWO_CHAN_LA)
self.H.__sendInt__(self.MAX_SAMPLES / 4)
Expand All @@ -2176,13 +2176,13 @@ def start_two_channel_LA(self, **args):
self.H.__sendByte__((modes[1] << 4) | modes[0]) # Modes. four bits each
self.H.__sendByte__((chans[1] << 4) | chans[0]) # Channels. four bits each
self.H.__get_ack__()
n = 0;
n = 0
for a in self.dchans[:2]:
a.prescaler = 0;
a.length = self.MAX_SAMPLES / 4;
a.datatype = 'long';
a.prescaler = 0
a.length = self.MAX_SAMPLES / 4
a.datatype = 'long'
a.maximum_time = maximum_time * 1e6 # conversion to uS
a.mode = modes[n];
a.mode = modes[n]
a.channel_number = chans[n]
a.name = strchans[n]
n += 1
Expand Down Expand Up @@ -2221,7 +2221,7 @@ def start_three_channel_LA(self, **args):
"""
try:
self.clear_buffer(0, self.MAX_SAMPLES);
self.clear_buffer(0, self.MAX_SAMPLES)
self.H.__sendByte__(CP.TIMING)
self.H.__sendByte__(CP.START_THREE_CHAN_LA)
self.H.__sendInt__(self.MAX_SAMPLES / 4)
Expand Down Expand Up @@ -2289,7 +2289,7 @@ def start_four_channel_LA(self, trigger=1, maximum_time=0.001, mode=[1, 1, 1, 1]
Use :func:`fetch_long_data_from_LA` (points to read,x) to get data acquired from channel x.
The read data can be accessed from :class:`~ScienceLab.dchans` [x-1]
"""
self.clear_buffer(0, self.MAX_SAMPLES);
self.clear_buffer(0, self.MAX_SAMPLES)
prescale = 0
"""
if(maximum_time > 0.26):
Expand Down Expand Up @@ -2403,7 +2403,7 @@ def fetch_int_data_from_LA(self, bytes, chan=1):
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)

t = np.trim_zeros(t)
b = 1;
b = 1
rollovers = 0
while b < len(t):
if (t[b] < t[b - 1] and t[b] != 0):
Expand Down Expand Up @@ -3277,7 +3277,7 @@ def load_equation(self, chan, function, span=None, **kwargs):
'''
if function == 'sine' or function == np.sin:
function = np.sin;
function = np.sin
span = [0, 2 * np.pi]
self.WType[chan] = 'sine'
elif function == 'tria':
Expand Down Expand Up @@ -3759,11 +3759,11 @@ def WS2812B(self, cols, output='CS1'):
self.H.__sendByte__(len(cols) * 3)
for col in cols:
# R=reverse_bits(int(col[0]));G=reverse_bits(int(col[1]));B=reverse_bits(int(col[2]))
R = col[0];
G = col[1];
B = col[2];
self.H.__sendByte__(G);
self.H.__sendByte__(R);
R = col[0]
G = col[1]
B = col[2]
self.H.__sendByte__(G)
self.H.__sendByte__(R)
self.H.__sendByte__(B)
# print(col)
self.H.__get_ack__()
Expand Down Expand Up @@ -3892,7 +3892,6 @@ def __stepperMotor__(self, steps, delay, direction):
except Exception as ex:
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)

t = time.time()
time.sleep(steps * delay * 1e-3) # convert mS to S

def stepForward(self, steps, delay):
Expand Down

0 comments on commit 2cddcf2

Please sign in to comment.