From 2b086083cd9a28808e31fb4392a491f5365a49aa Mon Sep 17 00:00:00 2001 From: Boyan Date: Mon, 25 Jul 2022 23:11:33 +0300 Subject: [PATCH] tried some dry optimization(w/o raspi) --- main.py | 29 ++++++++++++----------- main_classes.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 main_classes.py diff --git a/main.py b/main.py index 179ba15..2be61f7 100644 --- a/main.py +++ b/main.py @@ -24,35 +24,36 @@ async def run_command(*args): print("Started: %s, pid=%s" % (args, process.pid), flush=True) + async def button_press(gpio:int, level): + # TODO: light an LED global PTT_IS_ON - while True: - if(pi.read(gpio)!=0): - PTT_IS_ON = True - if(pi.read(gpio)==0): - PTT_IS_ON = False - await asyncio.sleep(.5) + if(pi.read(gpio)!=0): + PTT_IS_ON = True + if(pi.read(gpio)==0): + PTT_IS_ON = False + await asyncio.sleep(.5) async def moduation_change(gpio:int, level): global MODULATION - while True: - # given that the logic is the same - # if it isn't: TODO: rewrite this func - if(pi.read(gpio)!=0): - MODULATION = next(modulations) - await asyncio.sleep(.5) + + # given that the logic is the same + # if it isn't: TODO: rewrite this func + if(pi.read(gpio)!=0): + MODULATION = next(modulations) + await asyncio.sleep(.5) async def frequency(gpio): # TODO write this function after hooking up the encoder async def main(freq:float): - asyncio.create_task(button_press(10, pigpio.EITHER_EDGE)) # TODO change after figuring out the logic - asyncio.create_task(modulation_change(11, pigpio.EITHER_EDGE)) task_created = False print("Ready") while True: + await moduation_change(gpio, level) + await button_press(gpio, level) if (PTT_IS_ON and not task_created): asyncio.create_task(run_command(MODULATION, str(freq))) task_created = True diff --git a/main_classes.py b/main_classes.py new file mode 100644 index 0000000..7fb2934 --- /dev/null +++ b/main_classes.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +import pigpio +import time +import datetime +import asyncio +import os +from itertools import cycle + +PTT_IS_ON:bool = False +MODULATION:str = None +FREQUENCY:float = float(430) +modulations = cycle(["./modulations/ssb.sh", "./modulations/fmrds.sh"]) + +pi = pigpio.pi() # init GPIO + + +async def run_command(*args): + process = await asyncio.create_subprocess_exec(*args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE) + print("Started: %s, pid=%s" % (args, process.pid), flush=True) + +class GPIO: + def __init__(self, pin:int, level, led): + self.pin = pin + pi.set_mode(pin, pigpio.INPUT) + self.level = level + self.led = led + + async def is_clicked(self) -> bool: + if(pi.read(self.pin)!=0): + clicked = True + self.led = "ON" + if(pi.read(self.pin)==0): + clicked = False + await asyncio.sleep(.5) + return clicked + +async def frequency(gpio): + # TODO write this function after hooking up the encoder + pass + +async def main(freq:float): + task_created = False + + print("Ready") + while True: + ptt = await GPIO(10, pigpio.EITHER_EDGE, 13).is_clicked() # 13 is not an actual pin # + # TODO: if works, do the modulation change trigger + if (ptt and not task_created): + asyncio.create_task(run_command(MODULATION, str(freq))) + task_created = True + elif (task_created and not ptt): + await asyncio.sleep(1) + asyncio.create_task(run_command("./kill.sh")) + task_created = False + await asyncio.sleep(.5) + + +if __name__ == "__main__": + try: + asyncio.run(main("", 430)) + except KeyboardInterrupt: + print("\n")