Skip to content

Instantly share code, notes, and snippets.

@raspberrypisig
Last active July 13, 2019 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raspberrypisig/e7063404468b8db3b7afb875db24f13e to your computer and use it in GitHub Desktop.
Save raspberrypisig/e7063404468b8db3b7afb875db24f13e to your computer and use it in GitHub Desktop.
create waves with pigpio on raspberry pi
#!/usr/bin/env bash
# eg. bash generic-wave-pigpio.sh 25 3000 10
#
# Creates 3kHz 10% duty cycle on GPIO 25
# 3kHz 10% duty cycle -> 30microseconds on, 300 microseconds off
# If accuracy problems, /boot/config.txt dtparam=audio=off
#set -x
G="$1"
freq="$2"
duty="$3"
usage() {
echo "$0 GPIO freq duty"
exit 1
}
[ $G ] || usage
[ $freq ] || usage
[ $duty ] || usage
period=$(python -c "print(1000000/$freq)")
on=$(python -c "print(int($period *$duty/100.0))")
off=$((period - on))
pigs m $G w
pigs wvag $((1<<$G)) 0 $on 0 $(( 1<<$G)) $off
wid=$(pigs wvcre)
pigs wvtxr $wid
# stop waveform by running:
# pigs wvhlt
#!/usr/bin/env bash
# GPIO 18 30kHz 20% duty cycle
pigs 18 30000 200000
#!/usr/bin/env python3
import sys
import time
import pigpio
last = [None]*32
cb=[]
lastdiff=0
def cbf(GPIO, level, tick):
global lastdiff
if last[GPIO] is not None:
diff = pigpio.tickDiff(last[GPIO], tick)
twosamplesaverage = (diff + lastdiff)/2
#print("G={} l={} d={}".format(GPIO, level, int(1000000.0/twosamplesaverage)))
print("GPIO={} freq={}Hz".format(GPIO, int(1000000.0/twosamplesaverage)))
lastdiff=diff
last[GPIO] = tick
pi = pigpio.pi()
if not pi.connected:
sys.exit()
g=int(sys.argv[1]) if len(sys.argv) == 2 else 25
c = pi.callback(g, pigpio.RISING_EDGE, cbf)
try:
while True:
time.sleep(60)
except KeyboardInterrupt:
c.cancel()
#!/usr/bin/env python3
import sys
import time
import pigpio
last = [None]*32
cb=[]
def cbf(GPIO, level, tick):
if last[GPIO] is not None:
diff = pigpio.tickDiff(last[GPIO], tick)
print("G={} l={} d={}".format(GPIO, level, diff))
last[GPIO] = tick
pi = pigpio.pi()
if not pi.connected:
sys.exit()
g=int(sys.argv[1]) if len(sys.argv) == 2 else 25
c = pi.callback(g, pigpio.EITHER_EDGE, cbf)
try:
while True:
time.sleep(60)
except KeyboardInterrupt:
c.cancel()
[Unit]
Description=Daemon required to control GPIO pins via pigpio
[Service]
ExecStart=/usr/bin/pigpiod -l -n 127.0.0.1
ExecStop=/bin/systemctl kill pigpiod
Type=forking
[Install]
WantedBy=multi-user.target
#!/usr/bin/env bash
sudo systemctl start pigpiod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment