Guest User

Untitled

a guest
Jan 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. def carrier_square_wave_generator(gpio, frequency, signal_length):
  2. """
  3. Generate carrier square wave.
  4. """
  5. waveform = []
  6. # frequency is number of cycles per second, usually 38 kHz
  7. micro_per_cycle = 1000.0 / frequency # 1 / kHz is millisecond, * 1000 is microsecond
  8. # number of cycles during the signal
  9. # signal_length is in microseconds
  10. num_cycles = int(round(signal_length / micro_per_cycle))
  11.  
  12. on = int(round(micro_per_cycle / 2.0)) # signal length is in microseconds
  13. sofar = 0
  14.  
  15. # from zero cycles to target cycles
  16. for c in range(num_cycles):
  17. target = int(round((c+1) * micro_per_cycle)) # target is time in ms
  18. sofar += on
  19. off = target - sofar
  20. sofar += off
  21.  
  22. # pigpio.pulse(gpio_on, gpio_off, delay)
  23. # gpio_on - the GPIO to switch on at the start of the pulse.
  24. # gpio_off - the GPIO to switch off at the start of the pulse.
  25. # delay - the delay in microseconds before the next pulse.
  26. waveform.append(pigpio.pulse(1<<gpio, 0, on)) # bitwise shift?
  27. waveform.append(pigpio.pulse(0, 1<<gpio, off))
  28.  
  29. return waveform
  30.  
  31. # ORIGINAL FUNCTION
  32. def carrier(gpio, frequency, micros):
  33. """
  34. Generate carrier square wave.
  35. """
  36. wf = []
  37. cycle = 1000.0 / frequency
  38. cycles = int(round(micros/cycle))
  39. on = int(round(cycle / 2.0))
  40. sofar = 0
  41. for c in range(cycles):
  42. target = int(round((c+1)*cycle))
  43. sofar += on
  44. off = target - sofar
  45. sofar += off
  46. wf.append(pigpio.pulse(1<<gpio, 0, on))
  47. wf.append(pigpio.pulse(0, 1<<gpio, off))
  48. return wf
Add Comment
Please, Sign In to add comment