|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2019 Brent Rubell for Adafruit Industries |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | +""" |
| 23 | +`PWMOut` |
| 24 | +============================== |
| 25 | +PWMOut CircuitPython API for ESP32SPI. |
| 26 | +
|
| 27 | +* Author(s): Brent Rubell |
| 28 | +""" |
| 29 | + |
| 30 | +class PWMOut(): |
| 31 | + ESP32_PWM_PINS = set([0, 1, 2, 4, 5, |
| 32 | + 12, 13, 14, 15, |
| 33 | + 16, 17, 18, 19, |
| 34 | + 21, 22, 23, 25, |
| 35 | + 26, 27, 32, 33]) |
| 36 | + """ |
| 37 | + Implementation of CircuitPython PWMOut for ESP32SPI. |
| 38 | +
|
| 39 | + :param int esp_pin: Valid ESP32 GPIO Pin, predefined in ESP32_GPIO_PINS. |
| 40 | + :param ESP_SPIcontrol esp: The ESP object we are using. |
| 41 | + :param int duty_cycle: The fraction of each pulse which is high, 16-bit. |
| 42 | + :param int frequency: The target frequency in Hertz (32-bit). |
| 43 | + :param bool variable_frequency: True if the frequency will change over time. |
| 44 | + """ |
| 45 | + def __init__(self, esp, pwm_pin, *, frequency=500, duty_cycle=0, variable_frequency=False): |
| 46 | + if pwm_pin in self.ESP32_PWM_PINS: |
| 47 | + self._pwm_pin = pwm_pin |
| 48 | + else: |
| 49 | + raise AttributeError("Pin %d is not a valid ESP32 GPIO Pin."%esp_pin) |
| 50 | + self._esp = esp |
| 51 | + self._duty_cycle = duty_cycle |
| 52 | + self._freq = frequency |
| 53 | + self._var_freq = variable_frequency |
| 54 | + |
| 55 | + def __enter__(self): |
| 56 | + return self |
| 57 | + |
| 58 | + def __exit__(self, exc_type, exc_value, exc_traceback): |
| 59 | + self.deinit() |
| 60 | + |
| 61 | + def deinit(self): |
| 62 | + """De-initalize the PWMOut object.""" |
| 63 | + self._duty_cycle = 0 |
| 64 | + self._freq = 0 |
| 65 | + self._pwm_pin = None |
| 66 | + |
| 67 | + def _is_deinited(self): |
| 68 | + if self._pwm_pin is None: |
| 69 | + raise ValueError("PWMOut Object has been deinitialized and can no longer " |
| 70 | + "be used. Create a new PWMOut object.") |
| 71 | + |
| 72 | + @property |
| 73 | + def duty_cycle(self): |
| 74 | + """Returns the PWMOut object's duty cycle as a |
| 75 | + ratio from 0.0 to 1.0.""" |
| 76 | + self._is_deinited() |
| 77 | + return self._duty_cycle |
| 78 | + |
| 79 | + @duty_cycle.setter |
| 80 | + def duty_cycle(self, duty_cycle): |
| 81 | + """Sets the PWMOut duty cycle. |
| 82 | + :param float duty_cycle: Between 0.0 (low) and 1.0 (high). |
| 83 | + :param int duty_cycle: Between 0 (low) and 1 (high). |
| 84 | + """ |
| 85 | + self._is_deinited() |
| 86 | + if not isinstance(duty_cycle, (int, float)): |
| 87 | + raise TypeError("Invalid duty_cycle, should be int or float.") |
| 88 | + |
| 89 | + duty_cycle /= 65535.0 |
| 90 | + if not 0.0 <= duty_cycle <= 1.0: |
| 91 | + raise ValueError("Invalid duty_cycle, should be between 0.0 and 1.0") |
| 92 | + self._esp.set_analog_write(self._pwm_pin, duty_cycle) |
| 93 | + |
| 94 | + @property |
| 95 | + def frequency(self): |
| 96 | + """Returns the PWMOut object's frequency value.""" |
| 97 | + self._is_deinited() |
| 98 | + raise NotImplementedError("PWMOut Frequency not implemented in ESP32SPI") |
| 99 | + |
| 100 | + @frequency.setter |
| 101 | + def frequency(self, freq): |
| 102 | + """Sets the PWMOut object's frequency value. |
| 103 | + :param int freq: 32-bit value that dictates the PWM frequency in Hertz. |
| 104 | + NOTE: Only writeable when constructed with variable_Frequency=True. |
| 105 | + """ |
| 106 | + self._is_deinited() |
| 107 | + raise NotImplementedError("PWMOut Frequency not implemented in ESP32SPI") |
| 108 | + |
| 109 | + |
0 commit comments