Added commands to change radio parameters while running

This commit is contained in:
Logan Cusano
2021-12-28 01:49:43 -05:00
parent 08d1863a4b
commit 1a930594b3

87
bot.py
View File

@@ -1,4 +1,5 @@
import os
import re
import time
import discord
import sound
@@ -20,6 +21,17 @@ class Bot(commands.Bot):
self.Default_Mention_Group = kwargs['Mention_Group']
self.Devices_List = sound.query_devices().items()
# Init radio parameters
self.freq = "104.7M"
self.freq_re = re.compile('(\d+\.?\d*M)')
self.mode = "wbfm"
self.gain = 40
self.squelch = 0
self.sample_rate = ""
self.sample_rate_re = re.compile('(\d+\.?\d*k)')
# Init SDR Variables
self.sdr_process = None
self.system_os_type = None
self.sdr_output_process = None
@@ -71,6 +83,59 @@ class Bot(commands.Bot):
await ctx.voice_client.disconnect()
await self.change_presence(activity=discord.Game(name=f"@me"), status=discord.Status.idle)
@self.command(name='chfreq', help="Use this command to change the frequency the bot is listening to. Note: 'M'"
"is required\nExmple command: '@ chfreq <['fm', 'wbfm']> <xxx.xxxM>",
brief="Changes radio frequency")
async def chfreq(ctx, mode: str, freq: str, member: discord.Member = None):
possible_modes = ['wbfm', 'fm']
member = member or ctx.author.display_name
if self.freq_re.search(freq):
self.freq = freq
if mode in possible_modes:
self.mode = mode
await ctx.send(f"Ok {member}, I have changed the mode to {self.mode} and frequency to {self.freq}")
if self.sdr_started:
self.start_sdr()
else:
await ctx.send(f"{member}, {mode} is not valid. You may only enter 'fm' or 'wbfm'")
else:
await ctx.send(f"{member}, {freq} is not valid. please refer to the help page '@ help chfreq'")
@self.command(name='chsquelch', help="Use this command to change the squelch for the frequency"
"the bot is listening to",
brief="Changes radio squelch")
async def chsquelch(ctx, squelch: int, member: discord.Member = None):
member = member or ctx.author.display_name
self.squelch = squelch
await ctx.send(f"Ok {member}, I have changed the squelch to {self.squelch}")
if self.sdr_started:
self.start_sdr()
@self.command(name='chgain', help="Use this command to change the radio gain for the bot",
brief="Changes radio gain")
async def chgain(ctx, gain: int, member: discord.Member = None):
member = member or ctx.author.display_name
self.gain = gain
await ctx.send(f"Ok {member}, I have changed the gain to {self.gain}")
if self.sdr_started:
self.start_sdr()
@self.command(name='chbw', help="Use this command to change the center frequency bandwidth for the bot."
"Note: 'k' is required\nExample Command: '@ chbw <xxx.xxk'",
brief="Changes radio bandwidth")
async def chbw(ctx, sample_rate: str, member: discord.Member = None):
member = member or ctx.author.display_name
if self.sample_rate_re.search(sample_rate):
self.sample_rate = sample_rate
await ctx.send(f"Ok {member}, I have changed the sample rate to {self.sample_rate}")
if self.sdr_started:
self.start_sdr()
else:
await ctx.send(f"{member}, {sample_rate} is not valid. please refer to the help page '@ help chbw'")
@self.command(name='reload', hidden=True)
async def _reload(ctx, module: str, member: discord.Member = None):
"""Reloads a module."""
@@ -83,7 +148,7 @@ class Bot(commands.Bot):
@self.command(name='startsdr', hidden=True)
async def _startsdr(ctx, input_freq: str, member: discord.Member = None):
member = member or ctx.author.display_name
self.start_sdr(freq=input_freq)
self.start_sdr()
def check_device(self):
for device, index in self.Devices_List:
@@ -123,17 +188,12 @@ class Bot(commands.Bot):
else:
self.system_os_type = 'Linux'
def start_sdr(self, freq="104.7M", gain=40, squelch=0, sample_rate="200k", mode="wbfm"):
possible_modes = ['wbfm', 'fm']
if type(freq) == str:
def start_sdr(self):
if type(self.freq) == str:
# Single freq sent
print(f"started: {self.sdr_started}, poll: {self.sdr_process}")
# Wait for the running processes to close
if self.sdr_started:
print(f"SDR Poll: {self.sdr_process.poll()}")
print(f"SDR Out Poll: {self.sdr_output_process.poll()}")
while self.sdr_process.poll() is None and self.sdr_output_process.poll() is None:
print(f"SDR Loop Poll: {self.sdr_process.poll()}")
self.sdr_process.terminate()
self.sdr_process.kill()
time.sleep(1)
@@ -142,11 +202,12 @@ class Bot(commands.Bot):
time.sleep(1)
self.sdr_started = False
print(f"freq: {freq}")
self.sdr_process = Popen(["rtl_fm", "-M", str(mode), "-f", str(freq), "-g", str(gain), "-l", str(squelch), "-s", str(sample_rate)],
# Start the radio
print(f"Starting freq: {self.freq}")
self.sdr_process = Popen(["rtl_fm", "-M", str(self.mode), "-f", str(self.freq), "-g", str(self.gain),
"-l", str(self.squelch), "-s", str(self.sample_rate)],
stdout=PIPE)
self.sdr_output_process = Popen(["play", "-t", "raw", "-r", "32k", "-es", "-b", "16", "-c", "1", "-V1", "-"],
stdin=self.sdr_process.stdout)
self.sdr_started = True
self.sdr_started = True