RaspiQRP/main.py

150 lines
3.8 KiB
Python
Raw Permalink Normal View History

2022-07-20 17:22:05 +03:00
#!/usr/bin/env python
import pigpio
import time
import datetime
2022-07-20 19:28:27 +03:00
import asyncio
2022-07-29 17:50:32 +01:00
from RPi import GPIO
from itertools import cycle
2022-07-29 17:50:32 +01:00
bands = {
"80m":[3.5, 4],
"40m":[7, 7.3],
"20m":[14, 14.35],
"10m":[28, 29.7],
"2m":[144,146],
"70cm":[430,440]
}
# Setup for encoder
band = cycle(bands)
CURRENT_BAND:str = None
def next_band():
global CURRENT_BAND
CURRENT_BAND = next(band)
FREQUENCY = bands[CURRENT_BAND][0]
print(FREQUENCY)
print(CURRENT_BAND)
return FREQUENCY
def prev_band():
global CURRENT_BAND
CURRENT_BAND = list(bands.keys())[list(bands.keys()).index(CURRENT_BAND)-1]
print(CURRENT_BAND)
FREQUENCY = bands[CURRENT_BAND][0] + 1
return FREQUENCY
2022-07-29 17:50:32 +01:00
FREQUENCY = next_band()
2022-07-29 17:50:32 +01:00
clk=17
dt=27
sw=22
GPIO.setmode(GPIO.BCM)
GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
2022-07-29 17:50:32 +01:00
clkLastState = GPIO.input(clk)
2022-07-20 19:22:13 +03:00
async def rotary(clk=clk, dt=dt, sw=sw, step=0.025):
GPIO.setmode(GPIO.BCM)
GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(sw, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
global FREQUENCY, clkLastState
try:
while True:
clkState = GPIO.input(clk)
dtState = GPIO.input(dt)
swState = GPIO.input(sw)
if swState == GPIO.HIGH:
print("yeet")
if clkState != clkLastState:
if FREQUENCY > bands[CURRENT_BAND][1]:
FREQUENCY = next_band()
print(FREQUENCY)
if FREQUENCY < bands[CURRENT_BAND][0]:
FREQUENCY = prev_band()
print(FREQUENCY)
if dtState != clkState:
FREQUENCY += step
else:
FREQUENCY -= step
# print(round(FREQUENCY, 3))
clkLastState = clkState
await asyncio.sleep(.01)
except KeyboardInterrupt:
print("\nBye")
finally:
print("AAAAA")
GPIO.cleanup()
2022-07-29 17:10:43 +03:00
# 1. Hook up the screen and buttons
# * the screen should show the frequency we're at
# * read the buttons
2022-07-29 17:50:32 +01:00
# 2. Encoder [x]
2022-07-29 17:10:43 +03:00
# * Change frequency in the scope of a given band
PTT_IS_ON:bool = False
modulations = cycle(["./modulations/ssb.sh", "./modulations/fmrds.sh"])
2022-07-20 17:29:35 +03:00
# init GPIO
pi = pigpio.pi()
# button pin
pi.set_mode(10, pigpio.INPUT)
# modulation change pin(change this)
2022-07-20 17:02:17 +03:00
2022-07-20 19:03:17 +01:00
async def run_command(*args):
process = await asyncio.create_subprocess_exec(*args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
2022-07-20 19:03:17 +01:00
print("Started: %s, pid=%s" % (args, process.pid), flush=True)
2022-07-25 23:11:33 +03:00
async def button_press(gpio:int, level):
2022-07-25 23:11:33 +03:00
# TODO: light an LED
2022-07-20 19:03:17 +01:00
global PTT_IS_ON
2022-07-25 23:11:33 +03:00
if(pi.read(gpio)!=0):
PTT_IS_ON = True
if(pi.read(gpio)==0):
PTT_IS_ON = False
await asyncio.sleep(.01)
2022-07-29 17:50:32 +01:00
# async def frequency(gpio):
# # TODO write this function after hooking up the encoder
2022-07-29 17:10:43 +03:00
async def main():
# TODO change after figuring out the logic
task_created = False
asyncio.create_task(rotary())
print("Ready")
cached_frequency = FREQUENCY
2022-07-20 19:18:34 +03:00
while True:
if FREQUENCY != cached_frequency:
cached_frequency = FREQUENCY
asyncio.create_task(run_command("./display", str(format(round(FREQUENCY,3), "7f"))))
await button_press(10, pigpio.EITHER_EDGE)
2022-07-29 17:50:32 +01:00
if (PTT_IS_ON and not task_created):
asyncio.create_task(run_command(next(modulations), str(FREQUENCY)))
task_created = True
elif (task_created and not PTT_IS_ON):
await asyncio.sleep(1)
asyncio.create_task(run_command("./kill.sh"))
task_created = False
2022-07-20 19:03:17 +01:00
await asyncio.sleep(.5)
2022-07-20 19:28:27 +03:00
2022-07-20 17:29:35 +03:00
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\n")