]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/bcm283x/pulseio/PWMOut.py
Merge pull request #317 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         if not 0 <= duty_cycle <= 65535:
102             raise ValueError("Invalid duty cycle value, should be between 0 and 65535")
103
104         # convert from 16-bit
105         duty_cycle /= 65535.0
106
107         self._duty_cycle = duty_cycle
108         self._pwmpin.ChangeDutyCycle(round(self._duty_cycle * 100))
109
110     @property
111     def frequency(self):
112         """Get or set the PWM's output frequency in Hertz.
113
114         Raises:
115             PWMError: if an I/O or OS error occurs.
116             TypeError: if value type is not int or float.
117
118         :type: int, float
119         """
120
121         return self._frequency
122
123     @frequency.setter
124     def frequency(self, frequency):
125         if not isinstance(frequency, (int, float)):
126             raise TypeError("Invalid frequency type, should be int or float.")
127
128         self._pwmpin.ChangeFrequency(round(frequency))
129         self._frequency = frequency
130
131     @property
132     def enabled(self):
133         """Get or set the PWM's output enabled state.
134
135         Raises:
136             PWMError: if an I/O or OS error occurs.
137             TypeError: if value type is not bool.
138
139         :type: bool
140         """
141         return self._enabled
142
143     @enabled.setter
144     def enabled(self, value):
145         if not isinstance(value, bool):
146             raise TypeError("Invalid enabled type, should be string.")
147
148         if value:
149             self._pwmpin.start(round(self._duty_cycle * 100))
150         else:
151             self._pwmpin.stop()
152
153         self._enabled = value
154
155     # String representation
156     def __str__(self):
157         return "pin %s (freq=%f Hz, duty_cycle=%f%%)" % (
158             self._pin,
159             self.frequency,
160             self.duty_cycle,
161         )