CarPi/main.py

70 lines
2.3 KiB
Python
Raw Normal View History

2025-04-01 10:15:07 -05:00
import argparse
2025-04-03 10:22:56 -05:00
import subprocess
2025-03-31 21:05:01 -05:00
import sys
2025-04-01 10:15:07 -05:00
import bluetooth #PyBluez
2025-04-03 10:22:56 -05:00
from src.pipewire import Pipewire
2025-04-01 10:15:07 -05:00
version = '0.0.0'
2025-03-31 21:05:01 -05:00
2025-04-03 10:22:56 -05:00
def bluetooth_scan(args):
2025-03-31 21:05:01 -05:00
print('Discovering Bluetooth Devices...')
nearby_devices = bluetooth.discover_devices(duration=10, lookup_names=True, flush_cache=True, lookup_class=False)
print('Discovered {} devices:'.format(len(nearby_devices)))
for device in nearby_devices:
print(f'{device[0]}\t{device[1]}')
2025-04-03 10:22:56 -05:00
def bluetooth_connect(args):
process = subprocess.run(['bluetoothctl', 'connect', args.address], capture_output=True)
for line in iter(lambda: process.stdout.read(1), b""):
sys.stdout.buffer.write(line);
print("%s",process.stderr)
def pipewire_list(args):
2025-04-03 10:33:06 -05:00
print("ID | Description")
print("----------------")
2025-04-03 10:22:56 -05:00
sinks = Pipewire.get_sinks()
for sink in sinks:
2025-04-03 10:33:06 -05:00
print(str(sink['id']) + " | " + sink['info']['props']['node.description'])
2025-04-03 10:22:56 -05:00
def pipewire_get_default_sink(args):
print("ID | Description")
print("----------------")
sink = Pipewire.get_default_audio_sink()
print(str(sink['id']) + " | " + sink['info']['props']['node.description'])
2025-04-03 13:50:13 -05:00
def pipewire_get_volume(args):
print(Pipewire.get_volume())
2025-03-31 21:05:01 -05:00
def main():
2025-04-01 10:15:07 -05:00
argparser = argparse.ArgumentParser(description='CarPI CLI.')
# Create actions
2025-04-03 10:22:56 -05:00
actions = argparser.add_subparsers(title='actions', required=True, dest='action')
action_bt_list = actions.add_parser('bluetooth-list', help='list nearby bluetooth devices')
action_bt_list.set_defaults(func=bluetooth_scan)
action_bt_connect = actions.add_parser('bluetooth-connect', help='connect to a bluetooth device')
action_bt_connect.set_defaults(func=bluetooth_connect)
action_bt_connect.add_argument('address', help='the bluetooth address of the device to connect to.')
2025-04-03 10:22:56 -05:00
action_pw_list = actions.add_parser('pipewire-list', help='list pipewire objects')
action_pw_list.set_defaults(func=pipewire_list)
2025-04-01 10:15:07 -05:00
action_pw_get_default_sink = actions.add_parser('pipewire-get-default-sink', help='show the default audio sink')
action_pw_get_default_sink.set_defaults(func=pipewire_get_default_sink)
2025-04-03 13:50:13 -05:00
action_pw_get_volume = actions.add_parser('pipewire-get-volume', help='get volume of the specified device')
action_pw_get_volume.set_defaults(func=pipewire_get_volume)
2025-04-01 10:15:07 -05:00
args = argparser.parse_args()
2025-04-03 10:22:56 -05:00
args.func(args)
2025-03-31 21:05:01 -05:00
if (__name__ == "__main__"):
sys.exit(main())