Skip to content

Commit

Permalink
Add new "single" attribut for FTDI devices.
Browse files Browse the repository at this point in the history
FTDI devices could be selected with a module index (read value of
some pins) or a serial. In some setups there is only one module
present which can be identified with the VID and PID only. The
new attribute "single" checks for exactly one device with the
VID and PID. It throws an error if 0 or more than 1 devices with
this VID and PID are found.

Example for the syntax:
                <FTDI vendor="0x0403" product="0x6010">
                       <Device single="true"/>
                </FTDI>
  • Loading branch information
docbacardi committed Nov 11, 2021
1 parent 9e07ac7 commit 464b3f4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
54 changes: 33 additions & 21 deletions iomatrix/templates/io_matrix/ftdi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -150,31 +150,43 @@ function IoMatrix_FTDI:__find_ftdi_devices(atAllDevices, atDeviceAttr)
if aucPinMask~=nil then
self.tLog.debug('Pin mask: %s', table.concat(aucPinMask, ', '))
end
local fSingle = tDeviceAttr.single

-- Search the complete list of devices.
local tFoundDevice
for _, tDevice in pairs(atAllDevices) do
-- Is the device already part of the identified devices?
for _, tIdentDev in pairs(self.atDeviceAttrs) do
if tIdentDev.strPortNumbers==tDevice.strPortNumbers then
self.tLog.debug('Not considering device at port [%s] again.', tIdentDev.strPrettyPortNumbers)
tDevice = nil
break
end
if fSingle==true then
local sizAllDevices = #atAllDevices
if sizAllDevices==1 then
tFoundDevice = atAllDevices[1]
elseif sizAllDevices==0 then
self.tLog.debug('No device found.')
else
self.tLog.debug('With the "single" attribute there must be exactly one device, but here are %d of them.', sizAllDevices)
end
if tDevice~=nil then
-- Should a port number be checked and does it differ?
if strPN~=nil and tDevice.strPortNumbers~=strPN then
-- Do not use this device.
self.tLog.debug('The port of device [%s] does not match.', tDevice.strPrettyPortNumbers)
elseif strSerial~=nil and tDevice.strFtdiSerial~=strSerial then
self.tLog.debug('The serial of device [%s] does not match.', tDevice.strPrettyPortNumbers)
elseif ulModuleIndex~=nil and tDevice.ulModuleIndex~=ulModuleIndex then
self.tLog.debug('The module index of device [%s] does not match.', tDevice.strPrettyPortNumbers)
else
self.tLog.debug('Device [%s] matches.', tDevice.strPrettyPortNumbers)
tFoundDevice = tDevice
break
else
for _, tDevice in pairs(atAllDevices) do
-- Is the device already part of the identified devices?
for _, tIdentDev in pairs(self.atDeviceAttrs) do
if tIdentDev.strPortNumbers==tDevice.strPortNumbers then
self.tLog.debug('Not considering device at port [%s] again.', tIdentDev.strPrettyPortNumbers)
tDevice = nil
break
end
end
if tDevice~=nil then
-- Should a port number be checked and does it differ?
if strPN~=nil and tDevice.strPortNumbers~=strPN then
-- Do not use this device.
self.tLog.debug('The port of device [%s] does not match.', tDevice.strPrettyPortNumbers)
elseif strSerial~=nil and tDevice.strFtdiSerial~=strSerial then
self.tLog.debug('The serial of device [%s] does not match.', tDevice.strPrettyPortNumbers)
elseif ulModuleIndex~=nil and tDevice.ulModuleIndex~=ulModuleIndex then
self.tLog.debug('The module index of device [%s] does not match.', tDevice.strPrettyPortNumbers)
else
self.tLog.debug('Device [%s] matches.', tDevice.strPrettyPortNumbers)
tFoundDevice = tDevice
break
end
end
end
end
Expand Down
14 changes: 12 additions & 2 deletions lua/test_class_iomatrix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,27 @@ function TestClassIoMatrix.parseCfg_StartElement(tParser, strName, atAttributes)
end
end

local fSingle
local strSingle = atAttributes['single']
if strSingle~=nil then
strSingle = string.lower(strSingle)
if strSingle=='true' or strSingle=='yes' or strSingle=='1' then
fSingle = true
end
end

if fOk~=true then
aLxpAttr.tResult = nil
elseif auiPort==nil and strSerial==nil and uiModuleIndex==nil then
elseif auiPort==nil and strSerial==nil and uiModuleIndex==nil and fSingle==nil then
aLxpAttr.tResult = nil
aLxpAttr.tLog.error('Error in line %d, col %d: No way to identify device as no port, serial or moduleIndex specified.', iPosLine, iPosColumn)
else
local atDevice = {
port = auiPort,
serial = strSerial,
moduleidx = uiModuleIndex,
pinmask = aucPinMask
pinmask = aucPinMask,
single = fSingle
}
table.insert(aLxpAttr.tCurrentFTDIDevice, atDevice)
end
Expand Down

0 comments on commit 464b3f4

Please sign in to comment.