Update: Working on SDR RX

This commit is contained in:
Logan Cusano
2021-12-31 02:18:23 -05:00
parent 2d07ba04c3
commit 0a8af22087

35
bot.py
View File

@@ -14,8 +14,10 @@ class Bot(commands.Bot):
# If there is no custom command prefix (!help, ?help, etc.), use '>!' but also accept @ mentions
if 'command_prefix' not in kwargs.keys():
kwargs['command_prefix'] = '>!'
commands.Bot.__init__(self, command_prefix=commands.when_mentioned_or(kwargs['command_prefix']),
activity=discord.Game(name=f"@me"), status=discord.Status.idle)
commands.Bot.__init__(self, command_prefix=commands.when_mentioned_or(kwargs['command_prefix']))
# Set the initial activity of the bot
self.set_activity(connected=False)
# Init the core bot variables
self.DEVICE_ID = kwargs['Device_ID']
@@ -37,6 +39,7 @@ class Bot(commands.Bot):
self.sample_rate = "200k"
self.play_sample_rate = '32k'
self.sample_rate_re = re.compile('(\d+\.?\d*k)')
self.sdr_filters = ["dc"]
# Init SDR Variables
self.sdr_process = None
@@ -113,9 +116,10 @@ class Bot(commands.Bot):
"is required\nExample command: '@ chfreq wbfm 104.7M\n"
"Example command: '@ chfreq fm 154.785M",
brief="Changes radio frequency")
async def chfreq(ctx, mode: str, freq: str, member: discord.Member = None):
async def chfreq(ctx, mode: str, freq: str, sdr_filter: str = None, member: discord.Member = None):
# Possible band-types that can be used
possible_modes = ['wbfm', 'fm']
possible_filters = ['dc', 'deemp']
member = member or ctx.author.display_name
# Check to make sure the frequency input matches the syntax needed
@@ -124,6 +128,12 @@ class Bot(commands.Bot):
# Check to make sure the selected mode is valid
if mode in possible_modes:
self.mode = mode
if sdr_filter in possible_filters:
self.sdr_filters = [sdr_filter]
else:
self.sdr_filters = ['deemp']
await ctx.send(f"Ok {str(member).capitalize()}, I'm changing the mode to {str(self.mode).upper()} and frequency to"
f" {self.freq}")
@@ -235,8 +245,8 @@ class Bot(commands.Bot):
elif self.system_os_type == 'Windows':
discord.opus.load_opus('./opus/libopus.dll')
# Check to make sure the selected device is still available and has not changed it's index
def check_device(self):
# Check to make sure the selected device is still available and has not changed it's index
for device, index in self.Devices_List:
if int(index) == self.DEVICE_ID and str(device) == self.DEVICE_NAME:
return True
@@ -249,8 +259,8 @@ class Bot(commands.Bot):
else:
return False
# Search the ./modules folder for any modules to load
def check_for_modules(self):
# Search the ./modules folder for any modules to load
# A valid module must be built as a 'cog', refer to the docs for more information
for folder_name in os.listdir("modules"):
if str(folder_name)[0] == '.':
@@ -259,8 +269,8 @@ class Bot(commands.Bot):
print(f"Loaded extension: {folder_name}")
self.load_extension(f"modules.{folder_name}.cog")
# Reload a selected module for changes
def reload_modules(self, module):
# Reload a selected module for changes
try:
self.unload_extension(f"modules.{module}.cog")
print(f"Unloaded {module}")
@@ -271,15 +281,15 @@ class Bot(commands.Bot):
print(e)
return False
# Check and store the OS type of the system for later use
def check_os_type(self):
# Check and store the OS type of the system for later use
if os.name == 'nt':
self.system_os_type = 'Windows'
else:
self.system_os_type = 'Linux'
# Check to see if there is only one frequency
def start_sdr(self):
# Check to see if there is only one frequency
if type(self.freq) == str:
# Single freq sent
# Stop the SDR if it is running
@@ -289,8 +299,8 @@ class Bot(commands.Bot):
print(f"Starting freq: {self.freq}")
# Start the SDR receiver and pipe the output
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)
"-l", str(self.squelch), "-s", str(self.sample_rate), "-E",
str(self.sdr_filters[0]), "-A", "fast"], stdout=PIPE)
# Use the piped output of the SDR receiver to generate audio
self.sdr_output_process = Popen(["play", "-t", "raw", "-r", str(self.play_sample_rate), "-es", "-b",
"16", "-c", "1", "-V1", "-"],
@@ -298,8 +308,8 @@ class Bot(commands.Bot):
# Set the started variable for later checks
self.sdr_started = True
# Check to see if the SDR is running
def stop_sdr(self):
# Check to see if the SDR is running
if self.sdr_started:
# Wait for the running processes to close
while self.sdr_process.poll() is None and self.sdr_output_process.poll() is None:
@@ -312,6 +322,7 @@ class Bot(commands.Bot):
self.sdr_started = False
# Set the activity of the bot
async def set_activity(self, connected=True):
if connected:
if self.profile_name is None:
@@ -326,6 +337,7 @@ class Bot(commands.Bot):
elif not connected:
await self.change_presence(activity=discord.Game(name=f"@ me"), status=discord.Status.idle)
# Save the current radio settings as a profile
async def save_radio_config(self, profile_name):
config = configparser.SafeConfigParser()
@@ -349,6 +361,7 @@ class Bot(commands.Bot):
self.start_sdr()
await self.set_activity()
# Load a saved profile into the current settings
async def load_radio_config(self, profile_name):
config = configparser.ConfigParser()
if os.path.exists('./profiles.ini'):