| 
 | 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 | +    """  | 
 | 32 | +    Implementation of CircuitPython PWMOut for ESP32SPI.  | 
 | 33 | +
  | 
 | 34 | +    :param int esp_pin: Valid ESP32 GPIO Pin, predefined in ESP32_GPIO_PINS.  | 
 | 35 | +    :param ESP_SPIcontrol esp: The ESP object we are using.  | 
 | 36 | +    :param int duty_cycle: The fraction of each pulse which is high, 16-bit.  | 
 | 37 | +    :param int frequency: The target frequency in Hertz (32-bit).  | 
 | 38 | +    :param bool variable_frequency: True if the frequency will change over time.  | 
 | 39 | +    """  | 
 | 40 | +    ESP32_PWM_PINS = set([0, 1, 2, 4, 5,  | 
 | 41 | +                          12, 13, 14, 15,  | 
 | 42 | +                          16, 17, 18, 19,  | 
 | 43 | +                          21, 22, 23, 25,  | 
 | 44 | +                          26, 27, 32, 33])  | 
 | 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."%pwm_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 | +        """Checks if PWMOut object has been previously de-initalized"""  | 
 | 69 | +        if self._pwm_pin is None:  | 
 | 70 | +            raise ValueError("PWMOut Object has been deinitialized and can no longer "  | 
 | 71 | +                             "be used. Create a new PWMOut object.")  | 
 | 72 | + | 
 | 73 | +    @property  | 
 | 74 | +    def duty_cycle(self):  | 
 | 75 | +        """Returns the PWMOut object's duty cycle as a  | 
 | 76 | +        ratio from 0.0 to 1.0."""  | 
 | 77 | +        self._is_deinited()  | 
 | 78 | +        return self._duty_cycle  | 
 | 79 | + | 
 | 80 | +    @duty_cycle.setter  | 
 | 81 | +    def duty_cycle(self, duty_cycle):  | 
 | 82 | +        """Sets the PWMOut duty cycle.  | 
 | 83 | +        :param float duty_cycle: Between 0.0 (low) and 1.0 (high).  | 
 | 84 | +        :param int duty_cycle: Between 0 (low) and 1 (high).  | 
 | 85 | +        """  | 
 | 86 | +        self._is_deinited()  | 
 | 87 | +        if not isinstance(duty_cycle, (int, float)):  | 
 | 88 | +            raise TypeError("Invalid duty_cycle, should be int or float.")  | 
 | 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 | +        return self._freq  | 
 | 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 | +        self._freq = freq  | 
 | 108 | +        raise NotImplementedError("PWMOut Frequency not implemented in ESP32SPI")  | 
0 commit comments