@@ -309,6 +309,8 @@ class Pin:
309309 # pylint: disable=too-many-instance-attributes
310310 # Due to the nature of embedded pin complexity.
311311
312+ index : int = - 1
313+
312314 gpio_out : bool = False
313315 gpio_in : bool = False
314316 dac_out : bool = False
@@ -330,9 +332,25 @@ class Device:
330332 name : str
331333 interfaces : list
332334 functions_enabled : dict
333- pins : dict [int , Pin ] = field (default_factory = dict )
335+ _pins : dict [int , Pin ] = field (default_factory = dict )
334336 aux_params : dict = field (default_factory = dict )
335337
338+ def get_pin (self , pin_index ) -> Pin :
339+ """Return a pin object corresponding to index.
340+
341+ :param pin_index: The index of the pin to return.
342+ :return: Pin object for provided index.
343+ """
344+ if pin_index not in self ._pins :
345+ raise UOSRuntimeError (
346+ f"Pin index { pin_index } doesn't exist for device { self .name } "
347+ )
348+ return self ._pins [pin_index ]
349+
350+ def get_pins (self ) -> set :
351+ """Return a set of pin indices known by this device."""
352+ return set (self ._pins .keys ())
353+
336354 def get_compatible_pins (self , function : UOSFunction ) -> dict :
337355 """Return a dict of pin objects that are suitable for a function.
338356
@@ -348,7 +366,7 @@ def get_compatible_pins(self, function: UOSFunction) -> dict:
348366 return {}
349367 return {
350368 pin_index : pin
351- for pin_index , pin in self .pins .items ()
369+ for pin_index , pin in self ._pins .items ()
352370 if all (
353371 getattr (pin , requirement ) for requirement in function .pin_requirements
354372 )
@@ -368,11 +386,11 @@ def update_adc_samples(self, result: ComResult):
368386 sample_values = result .get_rx_payload (0 )
369387 logger .debug ("Device returned sampled adc values %s" , sample_values )
370388 for sample_index , pin in enumerate (result .tx_packet .payload ):
371- if pin not in self .pins :
389+ if pin not in self ._pins :
372390 raise UOSRuntimeError (
373391 f"Can't update ADC samples on pin { pin } as it's invalid for { self .name } ."
374392 )
375- self .pins [pin ].adc_reading = ADCSample (
393+ self ._pins [pin ].adc_reading = ADCSample (
376394 sample_values [sample_index * 2 : sample_index * 2 + 2 ],
377395 steps = pow (2 , self .aux_params ["adc_resolution" ]),
378396 reference = self .aux_params ["adc_reference" ],
@@ -381,7 +399,7 @@ def update_adc_samples(self, result: ComResult):
381399 "Setting pin %s adc reading to %s" ,
382400 pin ,
383401 # This is a false call as it can't be None here.
384- self .pins [pin ].adc_reading .value , # type: ignore
402+ self ._pins [pin ].adc_reading .value , # type: ignore
385403 )
386404
387405 def update_gpio_samples (self , result : ComResult ):
@@ -394,14 +412,14 @@ def update_gpio_samples(self, result: ComResult):
394412 logger .debug ("Device returned sampled gpio values %s" , sample_values )
395413 for sample_index , pin in enumerate (sample_values ):
396414 pin = result .tx_packet .payload [2 * sample_index ]
397- if pin not in self .pins :
415+ if pin not in self ._pins :
398416 raise UOSRuntimeError (
399417 f"Can't update GPIO samples on pin { pin } as it's invalid for { self .name } ."
400418 )
401- self .pins [pin ].gpio_reading = DigitalSample (sample_values [sample_index ])
419+ self ._pins [pin ].gpio_reading = DigitalSample (sample_values [sample_index ])
402420 logger .debug (
403421 "Setting pin %s gpio reading to %s" ,
404422 pin ,
405423 # This is a false call as it can't be None here.
406- self .pins [pin ].gpio_reading .value , # type: ignore
424+ self ._pins [pin ].gpio_reading .value , # type: ignore
407425 )
0 commit comments