Skip to content

Instantly share code, notes, and snippets.

@sp5wwp
Last active May 13, 2021
Embed
What would you like to do?
PTT python script for RPi
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import sys
import os
import signal
import subprocess
import pigpio
PTT_IN=2
PTT_OUT=3
MUTE=4
ACT=5
PWR_OFF=6
a=0
last_ptt=1
def signal_handler(signal, frame):
GPIO.output(PTT_OUT, 0)
GPIO.output(MUTE, 1)
GPIO.output(ACT, 0)
os.system('pkill m17-*')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(PTT_IN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(PWR_OFF, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(PTT_OUT, GPIO.OUT)
GPIO.setup(MUTE, GPIO.OUT)
GPIO.setup(ACT, GPIO.OUT)
GPIO.output(PTT_OUT, 0)
GPIO.output(MUTE, 0)
GPIO.output(ACT, 0)
os.system('sudo pigpiod')
CLK = pigpio.pi()
CLK.hardware_PWM(18, 480000, 500000) #480kHz, 50% on GPIO18 (RPi header #12)
while True:
ptt = GPIO.input(PTT_IN)
if ptt==0 and last_ptt==1:
time.sleep(0.05)
if GPIO.input(PTT_IN)==0:
print("PTT down")
last_ptt=0
GPIO.output(MUTE, 1)
os.system('pkill m17-demod')
time.sleep(0.1)
a = subprocess.Popen('arecord -D plughw:1,0 -c 1 -r 8000 -f S16_LE -t raw | sox --buffer 18 -t raw -r 8k -b 16 -e signed -L -c 1 - -r 8k -t raw - | m17-mod -S SP5WWP -D KC1AWV | aplay -D plughw:1,0 -c 1 -r 48000 -f S16_LE', shell=True, preexec_fn=os.setpgrp)
GPIO.output(PTT_OUT, 1)
time.sleep(0.5)
GPIO.output(ACT, 1)
if (ptt==1 and last_ptt==0) or a==0:
time.sleep(0.05)
if GPIO.input(PTT_IN)==1:
last_ptt=1
GPIO.output(ACT, 0)
time.sleep(0.75)
GPIO.output(PTT_OUT, 0)
os.system('pkill m17-mod')
time.sleep(0.3)
a = subprocess.Popen('arecord -D plughw:1,0 -c 1 -r 48000 -f S16_LE -t raw | m17-demod -l -d | aplay -D plughw:1,0 -c 1 -r 8000 -f S16_LE', shell=True, preexec_fn=os.setpgrp)
GPIO.output(MUTE, 0)
print("PTT up")
if GPIO.input(PWR_OFF)==0:
time.sleep(0.05)
if GPIO.input(PWR_OFF)==0:
os.system('sudo halt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment