Skip to content

Commit 80281ec

Browse files
author
Martin O'Hanlon
authored
Merge pull request #36 from RaspberryPiFoundation/dev
added docstrings and modified PWMBuzzer constructor
2 parents 2c76930 + 929c849 commit 80281ec

File tree

1 file changed

+68
-4
lines changed

1 file changed

+68
-4
lines changed

picozero/picozero.py

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ def _get_value(self):
8282
return next(self._gen)
8383

8484
def stop(self):
85+
"""
86+
Stops the ValueChange object running.
87+
"""
8588
self._running = False
8689
self._timer.deinit()
8790

@@ -259,6 +262,28 @@ class Buzzer(DigitalOutputDevice):
259262
Buzzer.beep = Buzzer.blink
260263

261264
class PWMOutputDevice(OutputDevice):
265+
"""
266+
Represents a device driven by a PWM pin.
267+
268+
:param int pin:
269+
The pin that the device is connected to.
270+
271+
:param int freq:
272+
The frequency of the PWM signal in Hertz. Defaults to 100.
273+
274+
:param int duty_factor:
275+
The duty factor of the PWM signal. This is a value between 0 and 65535.
276+
Defaults to 65535.
277+
278+
:param bool active_high:
279+
If :data:`True` (the default), the :meth:`on` method will set the Pin
280+
to HIGH. If :data:`False`, the :meth:`on` method will set the Pin to
281+
LOW (the :meth:`off` method always does the opposite).
282+
283+
:param bool initial_value:
284+
If :data:`False` (the default), the LED will be off initially. If
285+
:data:`True`, the LED will be switched on initially.
286+
"""
262287

263288
PIN_TO_PWM_CHANNEL = ["0A","0B","1A","1B","2A","2B","3A","3B","4A","4B","5A","5B","6A","6B","7A","7B","0A","0B","1A","1B","2A","2B","3A","3B","4A","4B","5A","5B","6A","6B"]
264289
_channels_used = {}
@@ -418,6 +443,13 @@ class PWMLED(PWMOutputDevice):
418443
:param int pin:
419444
The pin that the device is connected to.
420445
446+
:param int freq:
447+
The frequency of the PWM signal in Hertz. Defaults to 100.
448+
449+
:param int duty_factor:
450+
The duty factor of the PWM signal. This is a value between 0 and 65535.
451+
Defaults to 65535.
452+
421453
:param bool active_high:
422454
If :data:`True` (the default), the :meth:`on` method will set the Pin
423455
to HIGH. If :data:`False`, the :meth:`on` method will set the Pin to
@@ -478,21 +510,53 @@ class PWMBuzzer(PWMOutputDevice):
478510
Represents a passive buzzer driven by a PWM pin whose volume can be changed.
479511
480512
:param int pin:
481-
The pin that the device is connected to.
513+
The pin that the buzzer is connected to.
514+
515+
:param int freq:
516+
The frequency of the PWM signal in Hertz. Defaults to 440.
517+
518+
:param int duty_factor:
519+
The duty factor of the PWM signal. This is a value between 0 and 65535.
520+
Defaults to 1023.
482521
483522
:param bool active_high:
484523
If :data:`True` (the default), the :meth:`on` method will set the Pin
485524
to HIGH. If :data:`False`, the :meth:`on` method will set the Pin to
486525
LOW (the :meth:`off` method always does the opposite).
487526
488527
:param bool initial_value:
489-
If :data:`False` (the default), the buzzer will be off initially. If
490-
:data:`True`, the buzzer will be switched on initially.
491-
"""
528+
If :data:`False` (the default), the Buzzer will be off initially. If
529+
:data:`True`, the Buzzer will be switched on initially.
530+
"""
531+
def __init__(self, pin, freq=440, duty_factor=1023, active_high=True, initial_value=False):
532+
super().__init__(pin, freq, duty_factor, active_high, initial_value)
533+
492534
PWMBuzzer.volume = PWMBuzzer.value
493535
PWMBuzzer.beep = PWMBuzzer.blink
494536

495537
class Speaker(OutputDevice):
538+
"""
539+
Represents a speaker driven by a PWM pin.
540+
541+
:param int pin:
542+
The pin that the speaker is connected to.
543+
544+
:param int initial_freq:
545+
The initial frequency of the PWM signal in Hertz. Defaults to 440.
546+
547+
:param int initial_volume:
548+
The initial volume of the PWM signal. This is a value between 0 and
549+
1. Defaults to 0.
550+
551+
:param int duty_factor:
552+
The duty factor of the PWM signal. This is a value between 0 and 65535.
553+
Defaults to 1023.
554+
555+
:param bool active_high:
556+
If :data:`True` (the default), the :meth:`on` method will set the Pin
557+
to HIGH. If :data:`False`, the :meth:`on` method will set the Pin to
558+
LOW (the :meth:`off` method always does the opposite).
559+
"""
496560
NOTES = {
497561
'b0': 31, 'c1': 33, 'c#1': 35, 'd1': 37, 'd#1': 39, 'e1': 41, 'f1': 44, 'f#1': 46, 'g1': 49,'g#1': 52, 'a1': 55,
498562
'a#1': 58, 'b1': 62, 'c2': 65, 'c#2': 69, 'd2': 73, 'd#2': 78,

0 commit comments

Comments
 (0)