From 705b38f66a0cd7f744fec47a25157047a42b3c69 Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 12 Jun 2024 16:30:05 -0400 Subject: [PATCH 1/5] add pwmout to generic --- .../generic_agnostic_board/PWMOut.py | 144 ++++++++++++++++++ src/pwmio.py | 2 + 2 files changed, 146 insertions(+) create mode 100644 src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py diff --git a/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py b/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py new file mode 100644 index 00000000..9f6e1624 --- /dev/null +++ b/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py @@ -0,0 +1,144 @@ +# SPDX-FileCopyrightText: 2024 Brent Rubell for Adafruit Industries +# +# SPDX-License-Identifier: MIT +"""Mock PWMOut Wrapper for Generic Agnostic Board""" + +class PWMError(IOError): + """Base class for PWM errors.""" + + +class PWMOut: + """Pulse Width Modulation Output Class""" + + def __init__(self, pin, *, frequency=500, duty_cycle=0, variable_frequency=False): + self._pwmpin = None + self._period = 0 + self._open(pin, duty_cycle, frequency, variable_frequency) + + def __del__(self): + self.deinit() + + def __enter__(self): + return self + + def __exit__(self, t, value, traceback): + self.deinit() + + def _open(self, pin, duty=0, freq=500, variable_frequency=False): + self._pin = pin + self._pwmpin = GPIO.PWM(pin.id, freq) + + if variable_frequency: + print("Variable Frequency is not supported, continuing without it...") + + # set frequency + self.frequency = freq + # set duty + self.duty_cycle = duty + + self.enabled = True + + def deinit(self): + """Deinit the PWM.""" + if self._pwmpin is not None: + self._pwmpin = None + + def _is_deinited(self): + if self._pwmpin is None: + raise ValueError( + "Object has been deinitialize and can no longer " + "be used. Create a new object." + ) + + @property + def period(self): + """Get or set the PWM's output period in seconds. + + Raises: + PWMError: if an I/O or OS error occurs. + TypeError: if value type is not int or float. + + :type: int, float + """ + return 1.0 / self.frequency + + @period.setter + def period(self, period): + if not isinstance(period, (int, float)): + raise TypeError("Invalid period type, should be int or float.") + + self.frequency = 1.0 / period + + @property + def duty_cycle(self): + """Get or set the PWM's output duty cycle which is the fraction of + each pulse which is high. 16-bit + + Raises: + PWMError: if an I/O or OS error occurs. + TypeError: if value type is not int or float. + ValueError: if value is out of bounds of 0.0 to 1.0. + + :type: int, float + """ + return int(self._duty_cycle * 65535) + + @duty_cycle.setter + def duty_cycle(self, duty_cycle): + if not isinstance(duty_cycle, (int, float)): + raise TypeError("Invalid duty cycle type, should be int or float.") + + if not 0 <= duty_cycle <= 65535: + raise ValueError("Invalid duty cycle value, should be between 0 and 65535") + + # convert from 16-bit + duty_cycle /= 65535.0 + + self._duty_cycle = duty_cycle + + @property + def frequency(self): + """Get or set the PWM's output frequency in Hertz. + + Raises: + PWMError: if an I/O or OS error occurs. + TypeError: if value type is not int or float. + + :type: int, float + """ + + return self._frequency + + @frequency.setter + def frequency(self, frequency): + if not isinstance(frequency, (int, float)): + raise TypeError("Invalid frequency type, should be int or float.") + + self._frequency = frequency + + @property + def enabled(self): + """Get or set the PWM's output enabled state. + + Raises: + PWMError: if an I/O or OS error occurs. + TypeError: if value type is not bool. + + :type: bool + """ + return self._enabled + + @enabled.setter + def enabled(self, value): + if not isinstance(value, bool): + raise TypeError("Invalid enabled type, should be string.") + + self._enabled = value + + # String representation + def __str__(self): + return "pin %s (freq=%f Hz, duty_cycle=%f%%)" % ( + self._pin, + self.frequency, + self.duty_cycle, + ) diff --git a/src/pwmio.py b/src/pwmio.py index 3039c940..729d41b7 100644 --- a/src/pwmio.py +++ b/src/pwmio.py @@ -46,6 +46,8 @@ from adafruit_blinka.microcontroller.tegra.PWMOut import PWMOut elif detector.board.any_luckfox_pico_board: from adafruit_blinka.microcontroller.generic_linux.sysfs_pwmout import PWMOut +elif detector.board.OS_AGNOSTIC_BOARD: + from adafruit_blinka.microcontroller.generic_agnostic_board.PWMOut import PWMOut elif ( detector.board.feather_u2if or detector.board.feather_can_u2if From e3f753660c32faf7ad11d361504eeb6a8e876d3e Mon Sep 17 00:00:00 2001 From: brentru Date: Wed, 12 Jun 2024 16:46:07 -0400 Subject: [PATCH 2/5] rm GPIO undef --- .../microcontroller/generic_agnostic_board/PWMOut.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py b/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py index 9f6e1624..13877749 100644 --- a/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py +++ b/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py @@ -26,7 +26,6 @@ def __exit__(self, t, value, traceback): def _open(self, pin, duty=0, freq=500, variable_frequency=False): self._pin = pin - self._pwmpin = GPIO.PWM(pin.id, freq) if variable_frequency: print("Variable Frequency is not supported, continuing without it...") From 9918df54467106dae7c302fb4fa90480568bbf7a Mon Sep 17 00:00:00 2001 From: brentru Date: Thu, 13 Jun 2024 16:33:46 -0400 Subject: [PATCH 3/5] push pwmout --- .../microcontroller/generic_agnostic_board/PWMOut.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py b/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py index 13877749..8af5ead5 100644 --- a/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py +++ b/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py @@ -27,9 +27,6 @@ def __exit__(self, t, value, traceback): def _open(self, pin, duty=0, freq=500, variable_frequency=False): self._pin = pin - if variable_frequency: - print("Variable Frequency is not supported, continuing without it...") - # set frequency self.frequency = freq # set duty @@ -112,7 +109,6 @@ def frequency(self): def frequency(self, frequency): if not isinstance(frequency, (int, float)): raise TypeError("Invalid frequency type, should be int or float.") - self._frequency = frequency @property From 1dee40190887e373b008112efa1c8706855cb03f Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 14 Jun 2024 10:34:48 -0400 Subject: [PATCH 4/5] fix freq --- .../microcontroller/generic_agnostic_board/PWMOut.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py b/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py index 8af5ead5..00194e32 100644 --- a/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py +++ b/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py @@ -29,6 +29,7 @@ def _open(self, pin, duty=0, freq=500, variable_frequency=False): # set frequency self.frequency = freq + self._variable_frequency = variable_frequency # set duty self.duty_cycle = duty From 1e5b11b39c9fad6cfa7ff4869693c1cad5afa27c Mon Sep 17 00:00:00 2001 From: brentru Date: Fri, 14 Jun 2024 10:36:53 -0400 Subject: [PATCH 5/5] blacken --- .../microcontroller/generic_agnostic_board/PWMOut.py | 1 + src/pwmio.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py b/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py index 00194e32..a8d83a03 100644 --- a/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py +++ b/src/adafruit_blinka/microcontroller/generic_agnostic_board/PWMOut.py @@ -3,6 +3,7 @@ # SPDX-License-Identifier: MIT """Mock PWMOut Wrapper for Generic Agnostic Board""" + class PWMError(IOError): """Base class for PWM errors.""" diff --git a/src/pwmio.py b/src/pwmio.py index 729d41b7..54d00911 100644 --- a/src/pwmio.py +++ b/src/pwmio.py @@ -47,7 +47,7 @@ elif detector.board.any_luckfox_pico_board: from adafruit_blinka.microcontroller.generic_linux.sysfs_pwmout import PWMOut elif detector.board.OS_AGNOSTIC_BOARD: - from adafruit_blinka.microcontroller.generic_agnostic_board.PWMOut import PWMOut + from adafruit_blinka.microcontroller.generic_agnostic_board.PWMOut import PWMOut elif ( detector.board.feather_u2if or detector.board.feather_can_u2if