screen is working now, TODO fix wiring diagram

This commit is contained in:
PiFestim 2022-07-30 18:31:03 +01:00
parent 8abf5ba17d
commit 2d055ef04f
3 changed files with 127 additions and 55 deletions

BIN
display Executable file

Binary file not shown.

90
main.py
View File

@ -3,12 +3,9 @@ import pigpio
import time import time
import datetime import datetime
import asyncio import asyncio
import os
from itertools import cycle
from RPi import GPIO from RPi import GPIO
from time import sleep
from itertools import cycle from itertools import cycle
from rotary import main_loop as rotary
bands = { bands = {
@ -22,27 +19,78 @@ bands = {
# Setup for encoder # Setup for encoder
band = cycle(bands) band = cycle(bands)
CURRENT_BAND = None CURRENT_BAND:str = None
step = 0.025
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
FREQUENCY = next_band()
clk=17 clk=17
dt=27 dt=27
sw=22 sw=22
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 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)
counter = 0
clkLastState = GPIO.input(clk) clkLastState = GPIO.input(clk)
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()
# 1. Hook up the screen and buttons # 1. Hook up the screen and buttons
# * the screen should show the frequency we're at # * the screen should show the frequency we're at
# * read the buttons # * read the buttons
# 2. Encoder [x] # 2. Encoder [x]
# * Change frequency in the scope of a given band # * Change frequency in the scope of a given band
PTT_IS_ON:bool = False PTT_IS_ON:bool = False
FREQUENCY:float = float(430)
modulations = cycle(["./modulations/ssb.sh", "./modulations/fmrds.sh"]) modulations = cycle(["./modulations/ssb.sh", "./modulations/fmrds.sh"])
@ -51,7 +99,6 @@ pi = pigpio.pi()
# button pin # button pin
pi.set_mode(10, pigpio.INPUT) pi.set_mode(10, pigpio.INPUT)
# modulation change pin(change this) # modulation change pin(change this)
pi.set_mode(11)
async def run_command(*args): async def run_command(*args):
process = await asyncio.create_subprocess_exec(*args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE) process = await asyncio.create_subprocess_exec(*args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
@ -66,24 +113,27 @@ async def button_press(gpio:int, level):
PTT_IS_ON = True PTT_IS_ON = True
if(pi.read(gpio)==0): if(pi.read(gpio)==0):
PTT_IS_ON = False PTT_IS_ON = False
await asyncio.sleep(.5) await asyncio.sleep(.01)
# async def frequency(gpio): # async def frequency(gpio):
# # TODO write this function after hooking up the encoder # # TODO write this function after hooking up the encoder
async def main(freq:float): async def main():
# TODO change after figuring out the logic # TODO change after figuring out the logic
task_created = False task_created = False
asyncio.create_task(rotary())
asyncio.create_task(rotary)
print("Ready") print("Ready")
cached_frequency = FREQUENCY
while True: while True:
await button_press(gpio, level) 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)
if (PTT_IS_ON and not task_created): if (PTT_IS_ON and not task_created):
asyncio.create_task(run_command(MODULATION, str(freq))) asyncio.create_task(run_command(next(modulations), str(FREQUENCY)))
task_created = True task_created = True
elif (task_created and not PTT_IS_ON): elif (task_created and not PTT_IS_ON):
await asyncio.sleep(1) await asyncio.sleep(1)
@ -94,6 +144,6 @@ async def main(freq:float):
if __name__ == "__main__": if __name__ == "__main__":
try: try:
asyncio.run(main("", 430)) asyncio.run(main())
except KeyboardInterrupt: except KeyboardInterrupt:
print("\n") print("\n")

View File

@ -1,42 +1,64 @@
def next_band(): from functools import wraps, partial
global CURRENT_BAND from RPi import GPIO
CURRENT_BAND = next(band) from time import sleep
counter = bands[CURRENT_BAND][0] from itertools import cycle
print(counter)
print(CURRENT_BAND)
return counter clkLastState = GPIO.input(clk)
counter = next_band()
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 = None
step = 0.025
def next_band():
global CURRENT_BAND
CURRENT_BAND = next(band)
counter = bands[CURRENT_BAND][0]
print(counter)
print(CURRENT_BAND)
return counter
def prev_band(): def prev_band():
global CURRENT_BAND global CURRENT_BAND
CURRENT_BAND = list(bands.keys())[list(bands.keys()).index(CURRENT_BAND)-1] CURRENT_BAND = list(bands.keys())[list(bands.keys()).index(CURRENT_BAND)-1]
print(CURRENT_BAND) print(CURRENT_BAND)
counter = bands[CURRENT_BAND][0] + 1 counter = bands[CURRENT_BAND][0] + 1
return counter return counter
def main_loop(): async def main_loop(clk, dt, sw):
try: try:
counter = next_band() clkState = GPIO.input(clk)
while True: dtState = GPIO.input(dt)
clkState = GPIO.input(clk) swState = GPIO.input(sw)
dtState = GPIO.input(dt) if swState == GPIO.HIGH:
swState = GPIO.input(sw) print("yeet")
if swState == GPIO.HIGH: if clkState != clkLastState:
print("yeet") if counter > bands[CURRENT_BAND][1]:
if clkState != clkLastState: counter = next_band()
if counter > bands[CURRENT_BAND][1]: if counter < bands[CURRENT_BAND][0]:
counter = next_band() counter = prev_band()
if counter < bands[CURRENT_BAND][0]: if dtState != clkState:
counter = prev_band() counter += step
if dtState != clkState: else:
counter += step counter -= step
else: # print(round(counter, 3))
counter -= step clkLastState = clkState
print(round(counter, 3)) sleep(0.01)
clkLastState = clkState
sleep(0.01)
except KeyboardInterrupt: except KeyboardInterrupt:
print("\nBye") print("\nBye")
finally: finally:
print("AAAAA") print("AAAAA")
GPIO.cleanup() GPIO.cleanup()