@@ -57,14 +57,14 @@ def get_device_definition(identity: str) -> Device | None:
5757class UOSDevice : # dead: disable
5858 """Class for high level object-orientated control of UOS devices.
5959
60- :ivar device : Device definitions as parsed from a compatible ini.
60+ :ivar __device : Device definitions as parsed from a compatible ini.
6161 :ivar identity: The type of device, this is must have a valid device in the config.
6262 :ivar address: Compliant connection string for identifying the device and interface.
6363 :ivar __device_interface: Lower level communication protocol layer.
6464 :ivar __kwargs: Connection specific / optional parameters.
6565 """
6666
67- device : Device
67+ __device : Device
6868 identity = ""
6969 address = ""
7070 __device_interface : UOSInterface
@@ -101,13 +101,16 @@ def __init__(
101101 raise UOSUnsupportedError (
102102 f"'{ self .identity } ' does not have a valid look up table"
103103 )
104- self .device = device
105- if interface == Interface .SERIAL and Interface .SERIAL in self .device .interfaces :
104+ self .__device = device
105+ if (
106+ interface == Interface .SERIAL
107+ and Interface .SERIAL in self .__device .interfaces
108+ ):
106109 self .__device_interface = Serial (
107110 address ,
108- baudrate = self .device .aux_params ["default_baudrate" ],
111+ baudrate = self .__device .aux_params ["default_baudrate" ],
109112 )
110- elif interface == Interface .STUB and Interface .STUB in self .device .interfaces :
113+ elif interface == Interface .STUB and Interface .STUB in self .__device .interfaces :
111114 self .__device_interface = Stub (
112115 connection = address ,
113116 errored = (kwargs ["errored" ] if "errored" in kwargs else False ),
@@ -173,7 +176,7 @@ def get_gpio_input(
173176 ),
174177 )
175178 if result .status :
176- self .device .update_gpio_samples (result )
179+ self .__device .update_gpio_samples (result )
177180 return result
178181
179182 def get_adc_input (
@@ -192,7 +195,7 @@ def get_adc_input(
192195 ),
193196 )
194197 if result .status : # update the samples in the device.
195- self .device .update_adc_samples (result )
198+ self .__device .update_adc_samples (result )
196199 return result
197200
198201 def get_system_info (self ) -> ComResult :
@@ -254,17 +257,16 @@ def __execute_instruction(
254257 :raises: UOSUnsupportedError if function is not possible on the loaded device.
255258 """
256259 if (
257- function .name not in self .device .functions_enabled
260+ function .name not in self .__device .functions_enabled
258261 or (
259262 instruction_data .check_pin is not None
260- and instruction_data .check_pin
261- not in self .device .get_compatible_pins (function )
263+ and instruction_data .check_pin not in self .get_compatible_pins (function )
262264 )
263265 or instruction_data .volatility
264- not in self .device .functions_enabled [function .name ]
266+ not in self .__device .functions_enabled [function .name ]
265267 ):
266268 logger .debug (
267- "Known functions %s" , str (self .device .functions_enabled .keys ())
269+ "Known functions %s" , str (self .__device .functions_enabled .keys ())
268270 )
269271 raise UOSUnsupportedError (
270272 f"{ function .name } ({ instruction_data .volatility .name } ) "
@@ -332,11 +334,40 @@ def get_pin(self, pin: int) -> Pin: # dead: disable
332334 :param pin: The index of the pin to return.
333335 :return: Pin object for provided index.
334336 """
335- if pin not in self .device .pins :
337+ if pin not in self .__device .pins :
336338 raise UOSRuntimeError (
337- f"Pin index { pin } doesn't exist for device { self .device .name } "
339+ f"Pin index { pin } doesn't exist for device { self .__device .name } "
340+ )
341+ return self .__device .pins [pin ]
342+
343+ def get_compatible_pins (self , function : UOSFunction ) -> set :
344+ """Get pins suitable for use with a particular UOS Function.
345+
346+ :param function: the string name of the UOS Schema function.
347+ :return: Set of pin indices which support the function.
348+ """
349+ if (
350+ not isinstance (function , UOSFunction )
351+ or function not in UOSFunctions .enumerate_functions ()
352+ ):
353+ raise UOSUnsupportedError (f"UOS function { function .name } doesn't exist." )
354+ if function .pin_requirements is None : # pins are not relevant to this function
355+ return set ()
356+ return {
357+ pin_index
358+ for pin_index , pin in self .__device .pins .items ()
359+ if all (
360+ getattr (pin , requirement ) for requirement in function .pin_requirements
338361 )
339- return self .device .pins [pin ]
362+ }
363+
364+ # False positive as this is a client-facing function.
365+ def get_functions_enabled (self ) -> dict : # dead: disable
366+ """Return functions enabled for the device.
367+
368+ :return: Dictionary of function names to list of Persistence levels.
369+ """
370+ return self .__device .functions_enabled
340371
341372 def __repr__ (self ):
342373 """Representation of the UOS device.
@@ -345,6 +376,6 @@ def __repr__(self):
345376 """
346377 return (
347378 f"<UOSDevice(address='{ self .address } ', identity='{ self .identity } ', "
348- f"device={ self .device } , __device_interface='{ self .__device_interface } ', "
379+ f"device={ self .__device } , __device_interface='{ self .__device_interface } ', "
349380 f"__kwargs={ self .__kwargs } )>"
350381 )
0 commit comments