74 lines
1.7 KiB
Python
74 lines
1.7 KiB
Python
#!/usr/bin/env python
|
|
import pigpio
|
|
import time
|
|
import datetime
|
|
import asyncio
|
|
import os
|
|
|
|
PTT_IS_ON = 0
|
|
|
|
# init GPIO
|
|
pi = pigpio.pi()
|
|
# button pin
|
|
pi.set_mode(10, pigpio.INPUT)
|
|
|
|
async def run_command(*args):
|
|
"""Run command in subprocess.
|
|
|
|
Example from:
|
|
http://asyncio.readthedocs.io/en/latest/subprocess.html
|
|
"""
|
|
# Create subprocess
|
|
process = await asyncio.create_subprocess_exec(
|
|
*args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
|
|
)
|
|
|
|
# Status
|
|
print("Started: %s, pid=%s" % (args, process.pid), flush=True)
|
|
|
|
# Wait for the subprocess to finish
|
|
stdout, stderr = asyncio.get_event_loop().create_task(process.communicate())
|
|
|
|
# Progress
|
|
if process.returncode == 0:
|
|
print(
|
|
"Done: %s, pid=%s, result: %s"
|
|
% (args, process.pid, stdout.decode().strip()),
|
|
flush=True,
|
|
)
|
|
else:
|
|
print(
|
|
"Failed: %s, pid=%s, result: %s"
|
|
% (args, process.pid, stderr.decode().strip()),
|
|
flush=True,
|
|
)
|
|
|
|
# Result
|
|
result = stdout.decode().strip()
|
|
asyncio.sleep(5)
|
|
|
|
# Return stdout
|
|
return result
|
|
|
|
async def button_press(gpio:int, level) -> bool:
|
|
global PTT_IS_ON
|
|
while True:
|
|
if(pi.read(gpio)!=0):
|
|
PTT_IS_ON = 1
|
|
if(pi.read(gpio)==0):
|
|
PTT_IS_ON = 0
|
|
await asyncio.sleep(.5)
|
|
|
|
async def main():
|
|
asyncio.create_task(button_press(10, pigpio.EITHER_EDGE))
|
|
global PTT_IS_ON
|
|
while True:
|
|
if PTT_IS_ON == 1:
|
|
while PTT_IS_ON == 1:
|
|
await run_command("./ssb.sh")
|
|
else:
|
|
await run_command("./kill.sh")
|
|
await asyncio.sleep(.5)
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main()) |