Last active
January 20, 2019 14:55
-
-
Save kentwait/37fd8f7d70f1c8f2c0a969ed6bcd5c36 to your computer and use it in GitHub Desktop.
carrier generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def carrier_square_wave_generator(gpio, frequency, signal_length): | |
""" | |
Generate carrier square wave. | |
""" | |
waveform = [] | |
# frequency is number of cycles per second, usually 38 kHz | |
micro_per_cycle = 1000.0 / frequency # 1 / kHz is millisecond, * 1000 is microsecond | |
# number of cycles during the signal | |
# signal_length is in microseconds | |
num_cycles = int(round(signal_length / micro_per_cycle)) | |
on = int(round(micro_per_cycle / 2.0)) # signal length is in microseconds | |
sofar = 0 | |
# from zero cycles to target cycles | |
for c in range(num_cycles): | |
target = int(round((c+1) * micro_per_cycle)) # target is time in ms | |
sofar += on | |
off = target - sofar | |
sofar += off | |
# pigpio.pulse(gpio_on, gpio_off, delay) | |
# gpio_on - the GPIO to switch on at the start of the pulse. | |
# gpio_off - the GPIO to switch off at the start of the pulse. | |
# delay - the delay in microseconds before the next pulse. | |
waveform.append(pigpio.pulse(1<<gpio, 0, on)) # bitwise shift? | |
waveform.append(pigpio.pulse(0, 1<<gpio, off)) | |
return waveform | |
# ORIGINAL FUNCTION | |
def carrier(gpio, frequency, micros): | |
""" | |
Generate carrier square wave. | |
""" | |
wf = [] | |
cycle = 1000.0 / frequency | |
cycles = int(round(micros/cycle)) | |
on = int(round(cycle / 2.0)) | |
sofar = 0 | |
for c in range(cycles): | |
target = int(round((c+1)*cycle)) | |
sofar += on | |
off = target - sofar | |
sofar += off | |
wf.append(pigpio.pulse(1<<gpio, 0, on)) | |
wf.append(pigpio.pulse(0, 1<<gpio, off)) | |
return wf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment