File tree Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Expand file tree Collapse file tree 1 file changed +28
-1
lines changed Original file line number Diff line number Diff line change 25
25
26
26
The `simpleio` module contains classes to provide simple access to IO.
27
27
"""
28
-
28
+ import audioio
29
+ import array
29
30
import digitalio
30
31
import pulseio
31
32
import math
32
33
import time
33
34
35
+ def tone (pin , frequency , duration = 1 ):
36
+ """
37
+ Generates a square wave of the specified frequency (50% duty cycle)
38
+ on a pin
39
+
40
+ :param ~microcontroller.Pin Pin: Pin on which to output the tone
41
+ :param int frequency: Frequency of tone in Hz
42
+ :param int duration: Duration of tone in seconds (optional)
43
+ """
44
+ try :
45
+ length = 4000 // frequency
46
+ square_wave = array .array ("H" , [0 ] * length )
47
+ for i in range (length ):
48
+ if i < length / 2 :
49
+ square_wave .append (0xFFFF )
50
+ else :
51
+ square_wave .append (0x00 )
52
+ with audioio .AudioOut (pin , square_wave ) as waveform :
53
+ waveform .play (loop = True )
54
+ time .sleep (duration )
55
+ waveform .stop ()
56
+ except ValueError :
57
+ with pulseio .PWMOut (pin , frequency = frequency , variable_frequency = False ) as pwm :
58
+ pwm .duty_cycle = 0x8000
59
+ time .sleep (duration )
60
+
34
61
def shift_in (dataPin , clock , msb_first = True ):
35
62
"""
36
63
Shifts in a byte of data one bit at a time. Starts from either the LSB or
You can’t perform that action at this time.
0 commit comments