Skip to content

Instantly share code, notes, and snippets.

@VeggieVampire
Created January 17, 2023 10:21
Show Gist options
  • Save VeggieVampire/8a2555fe25cf96a852b87e22fc085ad9 to your computer and use it in GitHub Desktop.
Save VeggieVampire/8a2555fe25cf96a852b87e22fc085ad9 to your computer and use it in GitHub Desktop.
import pygame
import json
import subprocess
import os
def run_script(button):
os.system("pkill rtl_fm")
command = "rtl_fm -A lut -E deemp -f {} -M {} -s {} -r {} - | ffmpeg -nostats -loglevel quiet -r 200k -f s16le -ar {} -ac 1 -i - -f wav - | aplay -q -t wav > /dev/null 2>&1".format(
button["frequency"], button["modulation_mode"], button["sample_rate"], button["output_rate"], button["output_rate"])
subprocess.Popen(command, shell=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT)
pygame.init()
# Read config file
with open("config.json") as config_file:
config = json.load(config_file)
# Create a window with buttons
window = pygame.display.set_mode((400, 400))
buttons = []
labels = []
# Create labels for the buttons
font = pygame.font.Font(None, 30)
for button_config in config["buttons"]:
text = button_config["name"]
label = font.render(text, True, (255, 255, 255))
text_rect = label.get_rect()
button = pygame.Rect(button_config["x"], button_config["y"], text_rect.width, button_config["height"])
buttons.append(button)
labels.append(label)
clicked_button = ""
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
for i, button in enumerate(buttons):
if button.collidepoint(event.pos):
run_script(config["buttons"][i])
clicked_button = config["buttons"][i]["name"]
break
# Draw the buttons and labels
for i, button in enumerate(buttons):
if config["buttons"][i]["name"] == clicked_button:
pygame.draw.rect(window, (0, 255, 0), button)
else:
pygame.draw.rect(window, (255, 0, 0), button)
window.blit(labels[i], (button.x + (button.width - labels[i].get_width()) / 2, button.y + (button.height - labels[i].get_height()) / 2))
pygame.display.update()
pygame.quit()
@VeggieVampire
Copy link
Author

Here's an example of the config.json file

{
"buttons": [
{
"name": "KMOD",
"x": 50,
"y": 50,
"height": 50,
"frequency": "97.5e6",
"modulation_mode": "wbfm",
"sample_rate": "200000",
"output_rate": "44100"
},
{
"name": "KJSR",
"x": 150,
"y": 50,
"height": 50,
"frequency": "105.5e6",
"modulation_mode": "wbfm",
"sample_rate": "240000",
"output_rate": "48000"
},
{
"name": "Button 3",
"x": 50,
"y": 150,
"height": 50,
"frequency": "107.7e6",
"modulation_mode": "nbfm",
"sample_rate": "180000",
"output_rate": "44100"
},
{
"name": "Button 4",
"x": 150,
"y": 150,
"height": 50,
"frequency": "109.9e6",
"modulation_mode": "wbfm",
"sample_rate": "220000",
"output_rate": "48000"
}
]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment