diff --git a/OS/01/device.py b/OS/01/device.py index eb03cdd..b584536 100644 --- a/OS/01/device.py +++ b/OS/01/device.py @@ -23,6 +23,7 @@ import tempfile from datetime import datetime from interpreter import interpreter # Just for code execution. Maybe we should let people do from interpreter.computer import run? from utils.kernel import put_kernel_messages_into_queue +from utils.get_system_info import get_system_info from stt import stt_wav from utils.logs import setup_logging @@ -42,6 +43,9 @@ WS_URL = os.getenv('SERVER_URL') if not WS_URL: raise ValueError("The environment variable SERVER_URL is not set. Please set it to proceed.") +# Specify OS +current_platform = get_system_info() + # Initialize PyAudio p = pyaudio.PyAudio() @@ -190,6 +194,7 @@ async def websocket_communication(WS_URL): result = interpreter.computer.run(language, code) send_queue.put(result) + except: # traceback.print_exc() logger.info(f"Connecting to `{WS_URL}`...") @@ -205,9 +210,30 @@ if __name__ == "__main__": if os.getenv('CODE_RUNNER') == "device": asyncio.create_task(put_kernel_messages_into_queue(send_queue)) - # Keyboard listener for spacebar press/release - listener = keyboard.Listener(on_press=on_press, on_release=on_release) - listener.start() + + #If Raspberry Pi, add the button listener, otherwise use the spacebar + if current_platform.startswith("raspberry-pi"): + logger.info("Raspberry Pi detected, using button on GPIO pin 15") + # Use GPIO pin 15 + pindef = ["gpiochip4", "15"] # gpiofind PIN15 + print("PINDEF", pindef) + + # HACK: needs passwordless sudo + process = await asyncio.create_subprocess_exec("sudo", "gpiomon", "-brf", *pindef, stdout=asyncio.subprocess.PIPE) + while True: + line = await process.stdout.readline() + if line: + line = line.decode().strip() + if "FALLING" in line: + toggle_recording(False) + elif "RISING" in line: + toggle_recording(True) + else: + break + else: + # Keyboard listener for spacebar press/release + listener = keyboard.Listener(on_press=on_press, on_release=on_release) + listener.start() asyncio.run(main()) p.terminate() \ No newline at end of file diff --git a/OS/01/start.sh b/OS/01/start.sh index 95ce705..a5d7d67 100755 --- a/OS/01/start.sh +++ b/OS/01/start.sh @@ -1,3 +1,5 @@ +#!/usr/bin/env bash + ### Import Environment Variables from .env if [ ! -f ".env" ]; then echo "Error: .env file does not exist. To create one, see .env.example for an example." @@ -5,11 +7,12 @@ if [ ! -f ".env" ]; then fi set -a; source .env; set +a - ### SETUP # if using local models, install the models / executables + if [[ "$ALL_LOCAL" == "True" ]]; then + curl -OL "${WHISPER_MODEL_URL}${WHISPER_MODEL_NAME}" --output-dir ${WHISPER_RUST_PATH} OS=$(uname -s) ARCH=$(uname -m) if [ "$OS" = "Darwin" ]; then @@ -52,7 +55,7 @@ fi SERVER_PORT=$(echo $SERVER_URL | grep -oE "[0-9]+") if [ -n "$SERVER_PORT" ]; then - lsof -ti tcp:$SERVER_PORT | xargs kill + lsof -ti tcp:$SERVER_PORT | xargs kill 2>/dev/null || true fi ### START diff --git a/OS/01/utils/get_system_info.py b/OS/01/utils/get_system_info.py new file mode 100644 index 0000000..eeca56a --- /dev/null +++ b/OS/01/utils/get_system_info.py @@ -0,0 +1,38 @@ +import os +import platform + + +def get_system_info(): + system = platform.system() + + if system == "Linux": + # Attempt to identify specific Linux distribution + distro = "linux" # Default to generic 'linux' + try: + with open("/etc/os-release") as f: + os_release_info = f.read().lower() + if "ubuntu" in os_release_info: + return "raspberry-pi-ubuntu" + elif "raspbian" in os_release_info: + return "raspberry-pi-os" + except FileNotFoundError: + pass + + # Check for Raspberry Pi hardware + try: + with open("/proc/device-tree/model") as f: + model_info = f.read() + if "Raspberry Pi" in model_info: + if distro == "ubuntu": + return "raspberry-pi-ubuntu" + return "raspberry-pi" + except FileNotFoundError: + pass + + return distro + elif system == "Darwin": + return "darwin" + elif system == "Windows": + return "windows" + else: + return "unknown" diff --git a/OS/01/utils/kernel.py b/OS/01/utils/kernel.py index 994e6fb..8deb06c 100644 --- a/OS/01/utils/kernel.py +++ b/OS/01/utils/kernel.py @@ -32,11 +32,12 @@ def custom_filter(message): end = message.find('}TO_INTERPRETER}', start) return message[start:end] # Check for USB mention - elif 'USB' in message: - return message - # Check for network related keywords - elif any(keyword in message for keyword in ['network', 'IP', 'internet', 'LAN', 'WAN', 'router', 'switch']) and "networkStatusForFlags" not in message: - return message + # elif 'USB' in message: + # return message + # # Check for network related keywords + # elif any(keyword in message for keyword in ['network', 'IP', 'internet', 'LAN', 'WAN', 'router', 'switch']) and "networkStatusForFlags" not in message: + + # return message else: return None