diff --git a/tunerlistd/buttondecoder.py b/tunerlistd/buttondecoder.py index eb06c90..80a21fb 100644 --- a/tunerlistd/buttondecoder.py +++ b/tunerlistd/buttondecoder.py @@ -12,6 +12,11 @@ BUTTONS = [ ["SRC_DOWN", "PAUSE", "SRC_UP"] ] +class ButtonEvent: + def __init__(self, btn, state=None): + self.button = btn + self.state = state + class ButtonDecoder: def __init__(self): self.pi = pi() @@ -29,14 +34,13 @@ class ButtonDecoder: def _process_line(self, line): if len(line) == 1: if line[0] == 7: - self.queue.put_nowait("WHEEL_UP") + self.queue.put_nowait(ButtonEvent("WHEEL_UP")) elif line[0] == 6: - self.queue.put_nowait("WHEEL_DOWN") + self.queue.put_nowait(ButtonEvent("WHEEL_DOWN")) elif len(line) == 3: btn_name = BUTTONS[line[0]][line[1]] action = line[2] - if action == PRESSED: - self.queue.put_nowait(btn_name) + self.queue.put_nowait(ButtonEvent(btn_name, action)) async def run(self): buffer = b'' diff --git a/tunerlistd/main.py b/tunerlistd/main.py index b7579c2..c0342dd 100644 --- a/tunerlistd/main.py +++ b/tunerlistd/main.py @@ -2,8 +2,7 @@ # BUTTON MATRIX: OK, VOL_UP, VOL_DOWN, SRC_UP, SRC_DOWN, PAUSE, WHEEL_UP, WHEEL_DOWN # CD CHANGER: NEXT, PREV, FAST_FORW, PLAY, PAUSE, RESUME, STOP, FAST_BACK, CD_(1-6), STALK, RAND_ON, RAND_OFF # RADIO: VOL_UP, VOL_DOWN, PAUSE -# ANDROID AUTO: Play, Pause, Toggle play, Next track, Previous track, Home, Phone, Call end, -# Voice command, Left, Right, Up, Down, Scroll wheel, Back, Enter +# ANDROID AUTO: ENTER, LEFT, RIGHT, UP, DOWN, BACK, HOME, PHONE, CALL_END, PLAY, PAUSE, PREV_TRACK, NEXT_TRACK, TOGGLE_PLAY, VOICE, WHEEL_LEFT, WHEEL_RIGHT def send_to_radio(btn): tunerlist.send_button(btn) @@ -25,11 +24,14 @@ from buttondecoder import ButtonDecoder from json import dumps from fastapi import FastAPI, WebSocket, Request -from fastapi.responses import PlainTextResponse, JSONResponse -from asyncio import create_task, CancelledError, Queue +from fastapi.responses import PlainTextResponse, JSONResponse, HTMLResponse +from fastapi.staticfiles import StaticFiles +from asyncio import create_task, CancelledError, Queue, sleep from uvicorn import run +import os app = FastAPI() +app.mount("/static", StaticFiles(directory="static"), name="static") background_tasks = [] tunerlist = TunerList() @@ -99,6 +101,12 @@ async def shutdown_event(): for task in background_tasks: task.cancel() +@app.get("/", response_class=HTMLResponse) +async def root(): + html_file_path = os.path.join(os.getcwd(), "static", "index.html") + with open(html_file_path, "r") as file: + return file.read() + @app.websocket("/ws") async def websocket_endpoint(websocket: WebSocket): global state_to_send @@ -120,5 +128,26 @@ async def emulated_button(request: Request): print("CD Changer button: ", btn) buttons.queue.put_nowait(btn) +@app.get("/settext", response_class=JSONResponse) +async def set_text(request: Request): + text = request.query_params.get("txt") + tunerlist.set_text(text) + return dumps(state_to_send) + +@app.get("/connect", response_class=PlainTextResponse) +async def connect_aa(request: Request): + ip = request.query_params.get("ip") + queue.put_nowait({"wireless_aa_ip": ip}) + return ip + +@app.get("/aabtn", response_class=PlainTextResponse) +async def aabtn(request: Request): + btn = request.query_params.get("btn") + queue.put_nowait({"button": {"btn": btn, "state": 0}}) + await sleep(0.2) + queue.put_nowait({"button": {"btn": btn, "state": 1}}) + return btn + + if __name__ == "__main__": run(app=app, host="0.0.0.0", port=5959, log_level="info") diff --git a/tunerlistd/static/index.html b/tunerlistd/static/index.html new file mode 100644 index 0000000..b47fd32 --- /dev/null +++ b/tunerlistd/static/index.html @@ -0,0 +1,60 @@ + + + + + AA Button Panel + + + + +

Android Auto Button Panel

+
+ +
+ + + + + \ No newline at end of file diff --git a/tunerlistd/tunerlist.py b/tunerlistd/tunerlist.py index ea99d37..318bee7 100644 --- a/tunerlistd/tunerlist.py +++ b/tunerlistd/tunerlist.py @@ -108,5 +108,8 @@ class TunerList: except CancelledError: return + def set_text(self, text): + self.queue.put_nowait(TunerListState(text, self.preset)) + async def run(self): self.future = await get_event_loop().run_in_executor(self.executor, self._run) \ No newline at end of file