Skip to content

Commit

Permalink
Show a friendlier error when SPIMode is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaberez committed Jan 20, 2015
1 parent 9d80f2d commit 3a15cd5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/u12.py
Expand Up @@ -1681,7 +1681,6 @@ def rawAsynch(self, Data, AddDelay = False, TimeoutActive = False, SetTransmitEn

return returnDict

SPIModes = ['A', 'B', 'C', 'D']
def rawSPI(self, Data, AddMsDelay = False, AddHundredUsDelay = False, SPIMode = 'A', NumberOfBytesToWriteRead = 0, ControlCS = False, StateOfActiveCS = False, CSLineNumber = 0):
"""
Name: U12.rawSPI( Data, AddMsDelay = False, AddHundredUsDelay = False,
Expand Down Expand Up @@ -1740,7 +1739,11 @@ def rawSPI(self, Data, AddMsDelay = False, AddHundredUsDelay = False, SPIMode =
bf.bit7 = int(bool(AddMsDelay))
bf.bit6 = int(bool(AddHundredUsDelay))

modeIndex = self.SPIModes.index(SPIMode)
spiModes = ('A', 'B', 'C', 'D')
try:
modeIndex = spiModes.index(SPIMode)
except ValueError:
raise U12Exception("Invalid SPIMode %r, valid modes are: %r" % (SPIMode, spiModes))
bf[7-modeIndex] = 1

command[4] = int(bf)
Expand Down
8 changes: 6 additions & 2 deletions src/u3.py
Expand Up @@ -1203,7 +1203,6 @@ def watchdog(self, ResetOnTimeout = False, SetDIOStateOnTimeout = False, Timeout
return watchdogStatus
watchdog.section = 2

SPIModes = { 'A' : 0, 'B' : 1, 'C' : 2, 'D' : 3 }
def spi(self, SPIBytes, AutoCS=True, DisableDirConfig = False, SPIMode = 'A', SPIClockFactor = 0, CSPINNum = 4, CLKPinNum = 5, MISOPinNum = 6, MOSIPinNum = 7):
"""
Name: U3.spi(SPIBytes, AutoCS=True, DisableDirConfig = False,
Expand Down Expand Up @@ -1245,7 +1244,12 @@ def spi(self, SPIBytes, AutoCS=True, DisableDirConfig = False, SPIMode = 'A', SP
if DisableDirConfig:
command[6] |= (1 << 6)

command[6] |= ( self.SPIModes[SPIMode] & 3 )
spiModes = ('A', 'B', 'C', 'D')
try:
modeIndex = spiModes.index(SPIMode)
except ValueError:
raise LabJackException("Invalid SPIMode %r, valid modes are: %r" % (SPIMode, spiModes))
command[6] |= modeIndex

command[7] = SPIClockFactor
#command[8] = Reserved
Expand Down
8 changes: 6 additions & 2 deletions src/u6.py
Expand Up @@ -803,7 +803,6 @@ def watchdog(self, Write = False, ResetOnTimeout = False, SetDIOStateOnTimeout =

return watchdogStatus

SPIModes = { 'A' : 0, 'B' : 1, 'C' : 2, 'D' : 3 }
def spi(self, SPIBytes, AutoCS=True, DisableDirConfig = False, SPIMode = 'A', SPIClockFactor = 0, CSPINNum = 0, CLKPinNum = 1, MISOPinNum = 2, MOSIPinNum = 3):
"""
Name: U6.spi(SPIBytes, AutoCS=True, DisableDirConfig = False,
Expand Down Expand Up @@ -851,7 +850,12 @@ def spi(self, SPIBytes, AutoCS=True, DisableDirConfig = False, SPIMode = 'A', SP
if DisableDirConfig:
command[6] |= (1 << 6)

command[6] |= ( self.SPIModes[SPIMode] & 3 )
spiModes = ('A', 'B', 'C', 'D')
try:
modeIndex = spiModes.index(SPIMode)
except ValueError:
raise LabJackException("Invalid SPIMode %r, valid modes are: %r" % (SPIMode, spiModes))
command[6] |= modeIndex

command[7] = SPIClockFactor
#command[8] = Reserved
Expand Down

0 comments on commit 3a15cd5

Please sign in to comment.