Skip to content

Commit b613353

Browse files
committed
added cli commands
1 parent c08250b commit b613353

File tree

2 files changed

+111
-6
lines changed

2 files changed

+111
-6
lines changed

openlch/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = "0.2.1"
1+
__version__ = "0.3.0"
22

33
from .grpc import Servo

openlch/cli.py

Lines changed: 110 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
"""Defines the CLI for the OpenLCH project."""
22

33
import subprocess
4-
54
import click
5+
from .grpc import Servo
66

7+
DEFAULT_IP = "192.168.42.1"
78

89
@click.group()
910
def cli() -> None:
10-
"""OpenLCH CLI tool for interacting with MilkV boards."""
11-
raise NotImplementedError("The CLI is not yet implemented")
11+
"""OpenLCH CLI tool for interacting with MilkV boards.
12+
13+
Available commands:
14+
- ping: Ping the MilkV board
15+
- get-positions: Get current positions of all servos
16+
- set-position: Set position for a specific servo
17+
- set-wifi: Set WiFi credentials for the MilkV board
18+
- get-servo-info: Get information about a specific servo
19+
- scan-servos: Scan for connected servos
20+
- change-servo-id: Change the ID of a servo
1221
22+
Use 'openlch COMMAND --help' for more information on a specific command.
23+
"""
24+
pass
1325

1426
@cli.command()
15-
@click.option("--ip", required=True, help="IP address of the MilkV board")
27+
@click.argument("ip", default=DEFAULT_IP)
1628
def ping(ip: str) -> None:
1729
"""Ping the MilkV board at the specified IP address."""
1830
try:
19-
result = subprocess.run(["ping", "-c", "4", ip], capture_output=True, text=True, check=False)
31+
result = subprocess.run(["ping", "-c", "1", ip], capture_output=True, text=True, check=False)
2032
if result.returncode == 0:
2133
click.echo(f"Successfully pinged {ip}")
2234
click.echo(result.stdout)
@@ -26,6 +38,99 @@ def ping(ip: str) -> None:
2638
except Exception as e:
2739
click.echo(f"An error occurred: {str(e)}")
2840

41+
@cli.command()
42+
@click.argument("ip", default=DEFAULT_IP)
43+
def get_positions(ip: str) -> None:
44+
"""Get current positions of all servos."""
45+
client = Servo(ip)
46+
try:
47+
positions = client.get_positions()
48+
click.echo("Current positions:")
49+
for id, position in positions:
50+
click.echo(f"Servo {id}: {position}")
51+
except Exception as e:
52+
click.echo(f"An error occurred: {str(e)}")
53+
finally:
54+
client.close()
55+
56+
@cli.command()
57+
@click.argument("id", type=int)
58+
@click.argument("position", type=float)
59+
@click.argument("ip", default=DEFAULT_IP)
60+
def set_position(id: int, position: float, ip: str) -> None:
61+
"""Set position for a specific servo."""
62+
client = Servo(ip)
63+
try:
64+
client.set_positions([(id, position)])
65+
click.echo(f"Position set for servo {id} to {position}")
66+
except Exception as e:
67+
click.echo(f"An error occurred: {str(e)}")
68+
finally:
69+
client.close()
70+
71+
@cli.command()
72+
@click.argument("ssid")
73+
@click.argument("password")
74+
@click.argument("ip", default=DEFAULT_IP)
75+
def set_wifi(ssid: str, password: str, ip: str) -> None:
76+
"""Set WiFi credentials for the MilkV board."""
77+
client = Servo(ip)
78+
try:
79+
client.set_wifi_info(ssid, password)
80+
click.echo("WiFi credentials set successfully")
81+
except Exception as e:
82+
click.echo(f"An error occurred: {str(e)}")
83+
finally:
84+
client.close()
85+
86+
@cli.command()
87+
@click.argument("id", type=int)
88+
@click.argument("ip", default=DEFAULT_IP)
89+
def get_servo_info(id: int, ip: str) -> None:
90+
"""Get information about a specific servo."""
91+
client = Servo(ip)
92+
try:
93+
info = client.get_servo_info(id)
94+
click.echo(f"Servo {id} info:")
95+
for key, value in info.items():
96+
click.echo(f"{key}: {value}")
97+
except Exception as e:
98+
click.echo(f"An error occurred: {str(e)}")
99+
finally:
100+
client.close()
101+
102+
@cli.command()
103+
@click.argument("ip", default=DEFAULT_IP)
104+
def scan_servos(ip: str) -> None:
105+
"""Scan for connected servos."""
106+
client = Servo(ip)
107+
try:
108+
servo_ids = client.scan()
109+
click.echo("Found servo IDs:")
110+
for id in servo_ids:
111+
click.echo(id)
112+
except Exception as e:
113+
click.echo(f"An error occurred: {str(e)}")
114+
finally:
115+
client.close()
116+
117+
@cli.command()
118+
@click.argument("old_id", type=int)
119+
@click.argument("new_id", type=int)
120+
@click.argument("ip", default=DEFAULT_IP)
121+
def change_servo_id(old_id: int, new_id: int, ip: str) -> None:
122+
"""Change the ID of a servo."""
123+
client = Servo(ip)
124+
try:
125+
success = client.change_id(old_id, new_id)
126+
if success:
127+
click.echo(f"Successfully changed servo ID from {old_id} to {new_id}")
128+
else:
129+
click.echo("Failed to change servo ID")
130+
except Exception as e:
131+
click.echo(f"An error occurred: {str(e)}")
132+
finally:
133+
client.close()
29134

30135
if __name__ == "__main__":
31136
# python -m openlch.cli

0 commit comments

Comments
 (0)