Skip to content

Commit c8553ee

Browse files
authored
Added tone()
tone helper added to generate square wave w/50% duty cycle and variable frequency
1 parent f213e1b commit c8553ee

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

simpleio.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,96 @@
3232

3333
import digitalio
3434
import math
35+
import time
3536
from neopixel_write import neopixel_write
3637

38+
39+
def tone(pin, frequency, duration=0):
40+
"""
41+
Generates a square wave of the specified frequency (50% duty cycle)
42+
on a pin
43+
44+
:param pin: the pin on which to generate the tone
45+
:param frequency: frequency of tone in Hz
46+
:param duration: duration of tone in millis (optional)
47+
"""
48+
for i in range(0, duration):
49+
pwm = pulseio.PWMOut(pin, duty_cycle = 2 ** 15, frequency, variable_frequency = False)
50+
time.sleep(0.001)
51+
52+
53+
def noTone(pin):
54+
"""
55+
Stops the generation of the square wave produced by tone()
56+
by deinit'ing the pin.
57+
58+
:param pin: pin on which to stop the tone from being played
59+
"""
60+
pwm.deinit(pin)
61+
62+
def shiftIn(dataPin, clock, bitOrder):
63+
"""
64+
Shifts in a byte of data one bit at a time. Starts from either the LSB or
65+
MSB.
66+
67+
:param dataPin: pin on which to input each bit
68+
:param clock: toggles to signal dataPin reads
69+
:param bitOrder: order to shift bits (either LSBFIRST or MSBFIRST)
70+
:param value: RETURNS the value read
71+
"""
72+
73+
value = 0
74+
i = 0
75+
76+
for i in range(0, 8):
77+
clock.value = True
78+
if bitOrder.upper() == 'LSBFIRST':
79+
value |= ((dataPin.value) << i)
80+
else:
81+
value |= ((dataPin.value) << (7-i))
82+
clock.value = False
83+
i+=1
84+
return value
85+
86+
87+
def shiftOut(dataPin, clock, bitOrder, value):
88+
"""
89+
Shifts out a byte of data one bit at a time. Data gets written to a data
90+
pin. Then, the clock pulses hi then low
91+
92+
:param dataPin: value bits get output on this pin
93+
:param clock: toggled once the data pin is set
94+
:param bitOrder: order to shift bits (either LSBFIRST or MSBFIRST)
95+
:param value: byte to be shifted
96+
97+
Example for Metro Express:
98+
99+
.. code-block:: python
100+
101+
import digitalio
102+
import simpleio
103+
from board import *
104+
clock = digitalio.DigitalInOut(D12)
105+
dataPin = digitalio.DigitalInOut(D11)
106+
clock.switch_to_output()
107+
dataPin.switch_to_output()
108+
109+
while True:
110+
valueSend = 500
111+
shiftOut(dataPin, clock, 'LSBFIRST', (valueSend>>8))
112+
shiftOut(dataPin, clock, 'LSBFIRST', valueSend)
113+
shiftOut(dataPin, clock, 'MSBFIRST', (valueSend>>8))
114+
shiftOut(dataPin, clock, 'MSBFIRST', valueSend)
115+
"""
116+
value = value&0xFF
117+
for i in range(0, 8):
118+
if bitOrder.upper() == 'LSBFIRST':
119+
tmpval = bool((value & (1 << i)))
120+
dataPin.value = tmpval
121+
else:
122+
tmpval = bool(value & (1 << (7-i)))
123+
dataPin.value = tmpval
124+
37125
class DigitalOut:
38126
"""
39127
Simple digital output that is valid until soft reset.

0 commit comments

Comments
 (0)