|
31 | 31 | """ |
32 | 32 |
|
33 | 33 | import digitalio |
| 34 | +import pulseio |
34 | 35 | import math |
| 36 | +import time |
| 37 | +import audioio |
| 38 | +import array |
| 39 | +import math |
| 40 | + |
35 | 41 | from neopixel_write import neopixel_write |
36 | 42 |
|
| 43 | + |
| 44 | +def tone(pin, frequency, duration=0, tone_analog = False): |
| 45 | + """ |
| 46 | + Generates a square wave of the specified frequency (50% duty cycle) |
| 47 | + on a pin |
| 48 | +
|
| 49 | + :param pin: the pin on which to generate the tone |
| 50 | + :param frequency: frequency of tone in Hz |
| 51 | + :param duration: duration of tone in millis (optional) |
| 52 | + :param tone_analog: uses analog pins instead of digital |
| 53 | + """ |
| 54 | + if(tone_analog == False): |
| 55 | + pwm = pulseio.PWMOut(pin, frequency = frequency, variable_frequency = False) |
| 56 | + print(pwm.duty_cycle) |
| 57 | + for i in range(0, duration): |
| 58 | + pwm.duty_cycle = 2 ** 15 |
| 59 | + print(pwm.duty_cycle) |
| 60 | + time.sleep(0.001) |
| 61 | + pwm.deinit() |
| 62 | + else: |
| 63 | + length = 5000 // frequency |
| 64 | + square_wave = array.array("H", [0] * length) |
| 65 | + for i in range(length): |
| 66 | + if(i%2 == 0): |
| 67 | + square_wave.append((2 ** 16) - 1) |
| 68 | + else: |
| 69 | + square_wave.append(0x00) |
| 70 | + sound = audioio.AudioOut(pin, square_wave) |
| 71 | + sound.play(loop=True) |
| 72 | + time.sleep(duration) |
| 73 | + sound.stop() |
| 74 | + sound.deinit() |
| 75 | + |
| 76 | +def shiftIn(dataPin, clock, msb_first=True): |
| 77 | + """ |
| 78 | + Shifts in a byte of data one bit at a time. Starts from either the LSB or |
| 79 | + MSB. |
| 80 | +
|
| 81 | + :param dataPin: pin on which to input each bit |
| 82 | + :param clock: toggles to signal dataPin reads |
| 83 | + :param msb_first: order to shift bits (least significant or most significant bit first) |
| 84 | + :return: returns the value read |
| 85 | + :rtype: int |
| 86 | + """ |
| 87 | + |
| 88 | + value = 0 |
| 89 | + i = 0 |
| 90 | + |
| 91 | + for i in range(0, 8): |
| 92 | + clock.value = True |
| 93 | + if msb_first == False: |
| 94 | + value |= ((dataPin.value) << i) |
| 95 | + print(value) |
| 96 | + else: |
| 97 | + value |= ((dataPin.value) << (7-i)) |
| 98 | + print(value) |
| 99 | + clock.value = False |
| 100 | + i+=1 |
| 101 | + return value |
| 102 | + |
| 103 | +def shiftOut(dataPin, clock, value, msb_first=True): |
| 104 | + """ |
| 105 | + Shifts out a byte of data one bit at a time. Data gets written to a data |
| 106 | + pin. Then, the clock pulses hi then low |
| 107 | +
|
| 108 | + :param dataPin: value bits get output on this pin |
| 109 | + :param clock: toggled once the data pin is set |
| 110 | + :param msb_first: order to shift bits (least significant or most significant bit first) |
| 111 | + :param value: byte to be shifted |
| 112 | +
|
| 113 | + Example for Metro Express: |
| 114 | +
|
| 115 | + .. code-block:: python |
| 116 | +
|
| 117 | + import digitalio |
| 118 | + import simpleio |
| 119 | + from board import * |
| 120 | + clock = digitalio.DigitalInOut(D12) |
| 121 | + dataPin = digitalio.DigitalInOut(D11) |
| 122 | + clock.direction = digitalio.Direction.OUTPUT |
| 123 | + dataPin.direction = digitalio.Direction.OUTPUT |
| 124 | +
|
| 125 | + while True: |
| 126 | + valueSend = 500 |
| 127 | + shiftOut(dataPin, clock, 'LSBFIRST', (valueSend>>8)) |
| 128 | + shiftOut(dataPin, clock, 'LSBFIRST', valueSend) |
| 129 | + shiftOut(dataPin, clock, 'MSBFIRST', (valueSend>>8)) |
| 130 | + shiftOut(dataPin, clock, 'MSBFIRST', valueSend) |
| 131 | + """ |
| 132 | + value = value&0xFF |
| 133 | + for i in range(0, 8): |
| 134 | + if msb_first == False: |
| 135 | + tmpval = bool((value & (1 << i))) |
| 136 | + dataPin.value = tmpval |
| 137 | + else: |
| 138 | + tmpval = bool(value & (1 << (7-i))) |
| 139 | + dataPin.value = tmpval |
| 140 | + print (tmpval) |
| 141 | + |
37 | 142 | class DigitalOut: |
38 | 143 | """ |
39 | 144 | Simple digital output that is valid until soft reset. |
|
0 commit comments