diff --git a/adafruit_pcf8591/analog_in.py b/adafruit_pcf8591/analog_in.py index 43a4824..2c29012 100644 --- a/adafruit_pcf8591/analog_in.py +++ b/adafruit_pcf8591/analog_in.py @@ -27,53 +27,59 @@ * Author(s): Bryan Siepert, adpted from ADS1x15 by Carter Nelson """ +try: + import typing # pylint: disable=unused-import + from adafruit_pcf8591.pcf8591.PCF8591 import PCF8591 +except ImportError: + pass + class AnalogIn: """AnalogIn Mock Implementation for ADC Reads.""" - def __init__(self, pcf, pin): + def __init__(self, pcf: PCF8591, pin: int) -> None: """AnalogIn - :param ads: The PCF8591 object. - :param ~digitalio.DigitalInOut pin: Required ADC channel pin. + :param pcf: The PCF8591 object. + :param int pin: Required ADC channel pin. """ self._pcf = pcf self._channel_number = pin @property - def voltage(self): + def voltage(self) -> float: """Returns the value of an ADC channel in volts as compared to the reference voltage.""" if not self._pcf: raise RuntimeError( - "Underlying ADC does not exist, likely due to callint `deinit`" + "Underlying ADC does not exist, likely due to calling `deinit`" ) raw_reading = self._pcf.read(self._channel_number) return ((raw_reading << 8) / 65535) * self._pcf.reference_voltage @property - def value(self): + def value(self) -> int: """Returns the value of an ADC channel. The value is scaled to a 16-bit integer from the native 8-bit value.""" if not self._pcf: raise RuntimeError( - "Underlying ADC does not exist, likely due to callint `deinit`" + "Underlying ADC does not exist, likely due to calling `deinit`" ) return self._pcf.read(self._channel_number) << 8 @property - def reference_voltage(self): + def reference_voltage(self) -> float: """The maximum voltage measurable (also known as the reference voltage) as a float in Volts. Assumed to be 3.3V but can be overridden using the `PCF8591` constructor""" if not self._pcf: raise RuntimeError( - "Underlying ADC does not exist, likely due to callint `deinit`" + "Underlying ADC does not exist, likely due to calling `deinit`" ) return self._pcf.reference_voltage - def deinit(self): + def deinit(self) -> None: """Release the reference to the PCF8591. Create a new AnalogIn to use it again.""" self._pcf = None diff --git a/adafruit_pcf8591/analog_out.py b/adafruit_pcf8591/analog_out.py index b90351e..348f0a2 100644 --- a/adafruit_pcf8591/analog_out.py +++ b/adafruit_pcf8591/analog_out.py @@ -26,16 +26,21 @@ * Author(s): Bryan Siepert """ +try: + import typing # pylint: disable=unused-import + from adafruit_pcf8591.pcf8591.PCF8591 import PCF8591 +except ImportError: + pass class AnalogOut: """AnalogIn Mock Implementation for ADC Reads.""" - def __init__(self, pcf, dac_pin=0): + def __init__(self, pcf: PCF8591, dac_pin: int = 0) -> None: """AnalogIn :param pcf: The pcf object. - :param ~digitalio.DigitalInOut DAC pin: Required pin must be P4 + :param int pin: Required pin must be adafruit_pcf8591.pcf8591.A0 """ self._pcf = pcf @@ -45,24 +50,24 @@ def __init__(self, pcf, dac_pin=0): self._pcf.dac_enabled = True @property - def value(self): + def value(self) -> int: """Returns the currently set value of the DAC pin as an integer.""" return self._value @value.setter - def value(self, new_value): # this may have to scale from 16-bit + def value(self, new_value: int) -> None: # this may have to scale from 16-bit if new_value < 0 or new_value > 65535: raise ValueError("value must be a 16-bit integer from 0-65535") if not self._pcf.dac_enabled: raise RuntimeError( - "Underlying DAC is disabled, likely due to callint `deinit`" + "Underlying DAC is disabled, likely due to calling `deinit`" ) # underlying sensor is 8-bit, so scale accordingly self._pcf.write(new_value >> 8) self._value = new_value - def deinit(self): + def deinit(self) -> None: """Disable the underlying DAC and release the reference to the PCF8591. Create a new AnalogOut to use it again.""" self._pcf.dac_enabled = False diff --git a/adafruit_pcf8591/pcf8591.py b/adafruit_pcf8591/pcf8591.py index 741d429..a3aafb0 100644 --- a/adafruit_pcf8591/pcf8591.py +++ b/adafruit_pcf8591/pcf8591.py @@ -33,6 +33,13 @@ from micropython import const from adafruit_bus_device import i2c_device +try: + import typing # pylint: disable=unused-import + from typing_extensions import Literal + from busio import I2C +except ImportError: + pass + _PCF8591_DEFAULT_ADDR = const(0x48) # PCF8591 Default Address _PCF8591_ENABLE_DAC = const(0x40) # control bit for having the DAC active @@ -49,11 +56,16 @@ class PCF8591: """Driver for the PCF8591 DAC & ADC Combo breakout. :param ~busio.I2C i2c_bus: The I2C bus the PCF8591 is connected to. - :param address: The I2C device address for the sensor. Default is ``0x28``. + :param int address: The I2C device address for the sensor. Default is ``0x28``. """ - def __init__(self, i2c_bus, address=_PCF8591_DEFAULT_ADDR, reference_voltage=3.3): + def __init__( + self, + i2c_bus: I2C, + address: int = _PCF8591_DEFAULT_ADDR, + reference_voltage: float = 3.3, + ) -> None: self.i2c_device = i2c_device.I2CDevice(i2c_bus, address) self._dacval = 0 self._dac_enabled = False @@ -67,12 +79,12 @@ def __init__(self, i2c_bus, address=_PCF8591_DEFAULT_ADDR, reference_voltage=3.3 # user calls to `read` @property - def reference_voltage(self): + def reference_voltage(self) -> float: """The voltage level that ADC signals are compared to. An ADC value of 65535 will equal `reference_voltage`""" return self._reference_voltage - def _half_read(self, channel): + def _half_read(self, channel: Literal[0, 1, 2, 3]) -> None: if self._dac_enabled: self._buffer[0] = _PCF8591_ENABLE_DAC self._buffer[1] = self._dacval @@ -85,10 +97,10 @@ def _half_read(self, channel): with self.i2c_device as i2c: i2c.write_then_readinto(self._buffer, self._buffer) - def read(self, channel): + def read(self, channel: Literal[0, 1, 2, 3]) -> None: """Read an analog value from one of the four ADC inputs - param: :channel The single-ended ADC channel to read from, 0 thru 3 + :param int channel: The single-ended ADC channel to read from, 0 thru 3 """ if channel < 0 or channel > 3: raise ValueError("channel must be from 0-3") @@ -101,20 +113,20 @@ def read(self, channel): return unpack_from(">B", self._buffer[1:])[0] @property - def dac_enabled(self): + def dac_enabled(self) -> bool: """Enables the DAC when True, or sets it to tri-state / high-Z when False""" return self._dac_enabled @dac_enabled.setter - def dac_enabled(self, enable_dac): + def dac_enabled(self, enable_dac: bool) -> None: self._dac_enabled = enable_dac self.write(self._dacval) - def write(self, value): + def write(self, value: int) -> None: """Writes a uint8_t value to the DAC output - param: :output The value to write: 0 is GND and 65535 is VCC + :param int value: The value to write: 0 is GND and 65535 is VCC """ diff --git a/requirements.txt b/requirements.txt index fcef575..6b8b435 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ Adafruit-Blinka adafruit-circuitpython-register adafruit-circuitpython-busdevice +typing-extensions~=4.0