From e06373035d13979050026c7a043e691948959132 Mon Sep 17 00:00:00 2001 From: Milokita Date: Sun, 23 Nov 2014 18:34:29 +0800 Subject: [PATCH] Square wave generator example code added It generates 7.5 10 12 16 Hz square waves on pin 21 26 12 6 simultaneously --- .../square wave generator (4 channels).py | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Example Code/square wave generator (4 channels).py diff --git a/Example Code/square wave generator (4 channels).py b/Example Code/square wave generator (4 channels).py new file mode 100644 index 0000000..2c34c71 --- /dev/null +++ b/Example Code/square wave generator (4 channels).py @@ -0,0 +1,94 @@ +#!/usr/bin/env python + +# 2014-11-15 +# tarte.py +# Public Domain + +import time + +import pigpio + +LED1=21 +LED2=26 +LED3=12 +LED4=6 + +""" +7.5 10 12 16 + +Find the number of cycles needed for an integral switch on/off for each LED. + +That's 2*7.5 which is 2 seconds worth. + +In 2 seconds there will be this many cycles of on/off + +15 20 24 32 + +How many micros for each cycle? + +15 66666 on 66667 off = 1999995 +20 50000 on 50000 off = 2000000 +24 41666 on 41667 off = 1999992 +32 31250 on 31250 off = 2000000 + +There will be a slight error which will not be detectable by most means. +""" + +def wave(pi, gpio, hz, secs, on=1, offset=0): + """ + Generate a hz cycles per second square wave on gpio for + secs seconds. The first transition is to level on at + offset microseconds from the start. + """ + micros_left = int(secs * 1000000) + transitions = int(2 * hz * secs) + micros = micros_left / transitions + + if (offset < 0) or (offset > micros): + print("Illegal offset {} for hz {}".format(offset, hz)) + exit() + + wf = [] # Empty waveform. + + if offset: + wf.append(pigpio.pulse(0, 0, offset)) + micros_left -= micros + last_micros = micros - offset + transitions -= 1 + + for t in range(transitions, 0, -1): + micros = micros_left / t + if (t & 1) == (on & 1): + wf.append(pigpio.pulse(0, 1<