]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/bcm283x/pulseio/PWMOut.py
Merge pull request #314 from makermelissa/master
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / bcm283x / pulseio / PWMOut.py
1 """Custom PWMOut Wrapper for Rpi.GPIO PWM Class"""
2 import RPi.GPIO as GPIO
3
4 GPIO.setmode(GPIO.BCM)  # Use BCM pins D4 = GPIO #4
5 GPIO.setwarnings(False)  # shh!
6
7
8 # pylint: disable=unnecessary-pass
9 class PWMError(IOError):
10     """Base class for PWM errors."""
11
12     pass
13
14
15 # pylint: enable=unnecessary-pass
16
17
18 class PWMOut:
19     """Pulse Width Modulation Output Class"""
20
21     def __init__(self, pin, *, frequency=500, duty_cycle=0, variable_frequency=False):
22         self._pwmpin = None
23         self._period = 0
24         self._open(pin, duty_cycle, frequency, variable_frequency)
25
26     def __del__(self):
27         self.deinit()
28
29     def __enter__(self):
30         return self
31
32     def __exit__(self, t, value, traceback):
33         self.deinit()
34
35     def _open(self, pin, duty=0, freq=500, variable_frequency=False):
36         self._pin = pin
37         GPIO.setup(pin.id, GPIO.OUT)
38         self._pwmpin = GPIO.PWM(pin.id, freq)
39
40         if variable_frequency:
41             print("Variable Frequency is not supported, continuing without it...")
42
43         # set frequency
44         self.frequency = freq
45         # set duty
46         self.duty_cycle = duty
47
48         self.enabled = True
49
50     def deinit(self):
51         """Deinit the PWM."""
52         self._pwmpin.stop()
53         GPIO.cleanup(self._pin.id)
54         self._pwmpin = None
55
56     def _is_deinited(self):
57         if self._pwmpin is None:
58             raise ValueError(
59                 "Object has been deinitialize and can no longer "
60                 "be used. Create a new object."
61             )
62
63     @property
64     def period(self):
65         """Get or set the PWM's output period in seconds.
66
67         Raises:
68             PWMError: if an I/O or OS error occurs.
69             TypeError: if value type is not int or float.
70
71         :type: int, float
72         """
73         return 1.0 / self.frequency
74
75     @period.setter
76     def period(self, period):
77         if not isinstance(period, (int, float)):
78             raise TypeError("Invalid period type, should be int or float.")
79
80         self.frequency = 1.0 / period
81
82     @property
83     def duty_cycle(self):
84         """Get or set the PWM's output duty cycle which is the fraction of
85         each pulse which is high. 16-bit
86
87         Raises:
88             PWMError: if an I/O or OS error occurs.
89             TypeError: if value type is not int or float.
90             ValueError: if value is out of bounds of 0.0 to 1.0.
91
92         :type: int, float
93         """
94         return int(self._duty_cycle * 65535)
95
96     @duty_cycle.setter
97     def duty_cycle(self, duty_cycle):
98         if not isinstance(duty_cycle, (int, float)):
99             raise TypeError("Invalid duty cycle type, should be int or float.")
100
101         # convert from 16-bit
102         duty_cycle /= 65535.0
103         if not 0.0 <= duty_cycle <= 1.0:
104             raise ValueError("Invalid duty cycle value, should be between 0.0 and 1.0.")
105
106         self._pwmpin.ChangeDutyCycle(round(duty_cycle * 100))
107         self._duty_cycle = duty_cycle
108
109     @property
110     def frequency(self):
111         """Get or set the PWM's output frequency in Hertz.
112
113         Raises:
114             PWMError: if an I/O or OS error occurs.
115             TypeError: if value type is not int or float.
116
117         :type: int, float
118         """
119
120         return self._frequency
121
122     @frequency.setter
123     def frequency(self, frequency):
124         if not isinstance(frequency, (int, float)):
125             raise TypeError("Invalid frequency type, should be int or float.")
126
127         self._pwmpin.ChangeFrequency(round(frequency))
128         self._frequency = frequency
129
130     @property
131     def enabled(self):
132         """Get or set the PWM's output enabled state.
133
134         Raises:
135             PWMError: if an I/O or OS error occurs.
136             TypeError: if value type is not bool.
137
138         :type: bool
139         """
140         return self._enabled
141
142     @enabled.setter
143     def enabled(self, value):
144         if not isinstance(value, bool):
145             raise TypeError("Invalid enabled type, should be string.")
146
147         if value:
148             self._pwmpin.start(self.duty_cycle)
149         else:
150             self._pwmpin.stop()
151
152         self._enabled = value
153
154     # String representation
155     def __str__(self):
156         return "pin %s (freq=%f Hz, duty_cycle=%f%%)" % (
157             self._pin,
158             self.frequency,
159             self.duty_cycle * 100,
160         )