Skip to content
This repository was archived by the owner on Nov 23, 2025. It is now read-only.

Commit 338321f

Browse files
committed
Protected pins dict and instead exposed a get_pin function on device.
This allows introspection on pin internals to making usage easier.
1 parent dddd2b1 commit 338321f

3 files changed

Lines changed: 32 additions & 12 deletions

File tree

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Version 0.5.0
3232
This allows us more freedom to build higher level decoding functions.
3333
* Adding functionality to include ADC and GPIO responses as objects stored
3434
in the device pins.
35+
* Protected pins dict and instead exposed a get_pin function on device.
36+
This allows introspection on pin internals to making usage easier.
3537

3638
Version 0.4.0
3739
-------------

uoshardware/abstractions.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
)

uoshardware/devices/_arduino.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
UOSFunctions.hard_reset.name: [Persistence.NONE],
1515
UOSFunctions.get_system_info.name: [Persistence.NONE],
1616
},
17-
pins={
17+
_pins={
1818
2: Pin(gpio_out=True, gpio_in=True, pull_up=True),
1919
3: Pin(
2020
gpio_out=True,
@@ -94,9 +94,9 @@
9494
name="Arduino Uno 3",
9595
interfaces=_ARDUINO_NANO_3.interfaces,
9696
functions_enabled=_ARDUINO_NANO_3.functions_enabled,
97-
pins={ # Doesn't expose the addition mux'd ADCs.
98-
pin_index: value
99-
for pin_index, value in _ARDUINO_NANO_3.pins.items()
97+
_pins={ # Doesn't expose the additional mux'd ADCs.
98+
pin_index: _ARDUINO_NANO_3.get_pin(pin_index)
99+
for pin_index in _ARDUINO_NANO_3.get_pins()
100100
if pin_index not in {20, 21}
101101
},
102102
aux_params={"default_baudrate": 115200, "adc_reference": 5, "adc_resolution": 10},

0 commit comments

Comments
 (0)