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

Commit f7453f7

Browse files
committed
* Moving get_pin function to be defined at the UOSDevice level.
This is a client-facing function and low level devices aren't client-facing. Also set ``pin_index`` argument to be ``pin`` so this is more consistent with the other API functions. * ``Device._pins`` un-protected as this was purely to promote use of get_pin. * ``Device.get_pins`` removed as this is not required if pins is not protected.
1 parent 4dc2c2c commit f7453f7

6 files changed

Lines changed: 47 additions & 31 deletions

File tree

CHANGES.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
Version 0.6.0
2+
-------------
3+
4+
:Date: TBC
5+
6+
* Moving get_pin function to be defined at the UOSDevice level.
7+
This is a client-facing function and low level devices aren't client-facing.
8+
Also set ``pin_index`` argument to be ``pin`` so this is more consistent
9+
with the other API functions.
10+
* ``Device._pins`` un-protected as this was purely to promote use of get_pin.
11+
* ``Device.get_pins`` removed as this is not required if pins is not protected.
12+
113
Version 0.5.0
214
-------------
315

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "uos-hardware"
3-
version = "0.5.0"
3+
version = "0.6.0"
44
description = "A hardware abstraction layer for microcontrollers running UOS compliant firmware."
55
authors = ["nulltek <steve.public@nulltek.xyz>"]
66
license = "MIT"

tests/test_devices.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_get_compatible_pins(uos_device: UOSDevice):
1818
pins_indices = uos_device.device.get_compatible_pins(function)
1919
assert len(pins_indices) > 0
2020
for pin_index in pins_indices:
21-
pin = uos_device.device.get_pin(pin_index)
21+
pin = uos_device.get_pin(pin_index)
2222
assert isinstance(pin, Pin)
2323
if function == UOSFunctions.get_adc_input:
2424
assert pin.adc_in

uoshardware/abstractions.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -332,25 +332,9 @@ class Device:
332332
name: str
333333
interfaces: list
334334
functions_enabled: dict
335-
_pins: dict[int, Pin] = field(default_factory=dict)
335+
pins: dict[int, Pin] = field(default_factory=dict)
336336
aux_params: dict = field(default_factory=dict)
337337

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-
354338
def get_compatible_pins(self, function: UOSFunction) -> set:
355339
"""Get pins suitable for use with a particular UOS Function.
356340
@@ -366,7 +350,7 @@ def get_compatible_pins(self, function: UOSFunction) -> set:
366350
return set()
367351
return {
368352
pin_index
369-
for pin_index, pin in self._pins.items()
353+
for pin_index, pin in self.pins.items()
370354
if all(
371355
getattr(pin, requirement) for requirement in function.pin_requirements
372356
)
@@ -386,11 +370,11 @@ def update_adc_samples(self, result: ComResult):
386370
sample_values = result.get_rx_payload(0)
387371
logger.debug("Device returned sampled adc values %s", sample_values)
388372
for sample_index, pin in enumerate(result.tx_packet.payload):
389-
if pin not in self._pins:
373+
if pin not in self.pins:
390374
raise UOSRuntimeError(
391375
f"Can't update ADC samples on pin {pin} as it's invalid for {self.name}."
392376
)
393-
self._pins[pin].adc_reading = ADCSample(
377+
self.pins[pin].adc_reading = ADCSample(
394378
sample_values[sample_index * 2 : sample_index * 2 + 2],
395379
steps=pow(2, self.aux_params["adc_resolution"]),
396380
reference=self.aux_params["adc_reference"],
@@ -399,7 +383,7 @@ def update_adc_samples(self, result: ComResult):
399383
"Setting pin %s adc reading to %s",
400384
pin,
401385
# This is a false call as it can't be None here.
402-
self._pins[pin].adc_reading.value, # type: ignore
386+
self.pins[pin].adc_reading.value, # type: ignore
403387
)
404388

405389
def update_gpio_samples(self, result: ComResult):
@@ -412,14 +396,14 @@ def update_gpio_samples(self, result: ComResult):
412396
logger.debug("Device returned sampled gpio values %s", sample_values)
413397
for sample_index, pin in enumerate(sample_values):
414398
pin = result.tx_packet.payload[2 * sample_index]
415-
if pin not in self._pins:
399+
if pin not in self.pins:
416400
raise UOSRuntimeError(
417401
f"Can't update GPIO samples on pin {pin} as it's invalid for {self.name}."
418402
)
419-
self._pins[pin].gpio_reading = DigitalSample(sample_values[sample_index])
403+
self.pins[pin].gpio_reading = DigitalSample(sample_values[sample_index])
420404
logger.debug(
421405
"Setting pin %s gpio reading to %s",
422406
pin,
423407
# This is a false call as it can't be None here.
424-
self._pins[pin].gpio_reading.value, # type: ignore
408+
self.pins[pin].gpio_reading.value, # type: ignore
425409
)

uoshardware/api.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
"""Provides the HAL layer for communicating with the hardware."""
2-
from uoshardware import Loading, Persistence, UOSUnsupportedError, logger
2+
from uoshardware import (
3+
Loading,
4+
Persistence,
5+
UOSRuntimeError,
6+
UOSUnsupportedError,
7+
logger,
8+
)
39
from uoshardware.abstractions import (
410
ComResult,
511
Device,
612
InstructionArguments,
713
NPCPacket,
14+
Pin,
815
UOSFunction,
916
UOSFunctions,
1017
UOSInterface,
@@ -318,6 +325,19 @@ def is_active(self) -> bool:
318325
"""
319326
return self.__device_interface.is_active()
320327

328+
# False positive as this is a client-facing function.
329+
def get_pin(self, pin: int) -> Pin: # dead: disable
330+
"""Return a pin object corresponding to index.
331+
332+
:param pin: The index of the pin to return.
333+
:return: Pin object for provided index.
334+
"""
335+
if pin not in self.device.pins:
336+
raise UOSRuntimeError(
337+
f"Pin index {pin} doesn't exist for device {self.device.name}"
338+
)
339+
return self.device.pins[pin]
340+
321341
def __repr__(self):
322342
"""Representation of the UOS device.
323343

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 additional mux'd ADCs.
98-
pin_index: _ARDUINO_NANO_3.get_pin(pin_index)
99-
for pin_index in _ARDUINO_NANO_3.get_pins()
97+
pins={ # Doesn't expose the additional mux'd ADCs.
98+
pin_index: pin
99+
for pin_index, pin in _ARDUINO_NANO_3.pins.items()
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)