Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions resources/arduino/install-arduino-cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ if command -v arduino-cli &> /dev/null; then
exit 0
fi

# Make sure we are at home.
cd
# Make sure we are at home or give a warning and exit.
cd "$HOME" || { echo "Could not change to home directory."; exit 1; }

# Get the arduino-cli tool and install.
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh
Expand All @@ -16,10 +16,3 @@ curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.
"${HOME}/bin/arduino-cli" core update-index
"${HOME}/bin/arduino-cli" core install arduino:avr
"${HOME}/bin/arduino-cli" lib install ArduinoJson

# Ask if the user wants to install the power board software.
read -p "Do you want to install the power board software? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
source ./install-power-board.sh
fi
8 changes: 7 additions & 1 deletion resources/arduino/install-power-board.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
FQBN="${FQBN:-arduino:avr:uno}"
SKETCH_LOCATION="${SKETCH_LOCATION:-PowerBoard}"
ARDUINO_PORT="${ARDUINO_PORT:-/dev/ttyACM0}"
DO_UPLOAD="${DO_UPLOAD:-true}"

# Change to the directory of this script.
cd "$(dirname "${BASH_SOURCE[0]}")" || { echo "Could not change to script directory."; exit 1; }

arduino-cli compile -b "${FQBN}" "${SKETCH_LOCATION}"
arduino-cli upload -p "${ARDUINO_PORT}" -b "${FQBN}" "${SKETCH_LOCATION}"
if [ "${DO_UPLOAD}" = "true" ]; then
arduino-cli upload -p "${ARDUINO_PORT}" -b "${FQBN}" "${SKETCH_LOCATION}"
fi
79 changes: 51 additions & 28 deletions src/panoptes/pocs/utils/cli/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
import requests
import typer
from rich import print
from rich.console import Console
from sparklines import sparklines

from panoptes.pocs.utils.service.power import RelayCommand

console = Console()


@dataclass
class HostInfo:
Expand Down Expand Up @@ -200,25 +203,13 @@ def setup_power(
help="Confirm power board setup.",
),
] = False,
do_install: Annotated[
bool,
typer.Option(
...,
"--install",
prompt="Would you like to install the arduino script?",
help="Install the arduino script.",
),
] = False,
install_script: Path = typer.Option(
"resources/arduino/install-power-board.sh", help="Path to the power monitor script."
),
arduino_device: str = typer.Option("/dev/ttyACM0", help="Path to the Arduino device."),
):
"""Set up the power board port and labels; optionally install Arduino sketch.

Args:
confirm: Confirmation flag to proceed with setup.
do_install: If True, run the Arduino install script before setup.
install_script: Path to the Arduino install script.
arduino_device: Path to the Arduino device for uploading the sketch.

Returns:
None
Expand All @@ -227,20 +218,52 @@ def setup_power(
print("[red]Cancelled.[/red]")
return typer.Abort()

if do_install:
if not install_script.exists():
print(f"[red]Cannot find install script at {install_script}[/red]")
return typer.Abort()
# Change directory to the arduino script.
os.chdir(install_script.parent)
cmd = f"bash {install_script.name}"
print(f"Running: {cmd}")
try:
subprocess.run(cmd, shell=True)
except subprocess.CalledProcessError as e:
print(f"[red]Error running install script: {e}[/red]")
else:
print("[green]Arduino script installed.[/green]")
arduino_cli_script = Path("~/resources/arduino/install-arduino-cli.sh").expanduser()
power_board_script = Path("~/resources/arduino/install-power-board.sh").expanduser()

if not arduino_cli_script.exists() and not power_board_script.exists():
print(
f"[red]Error: Neither Arduino CLI setup script nor power board setup script found at "
f"{arduino_cli_script} and {power_board_script}. Cannot proceed with setup.[/red]"
)
return None

with console.status(f"Running Arduino CLI setup script: {arduino_cli_script}", spinner="dots"):
result = subprocess.run(f"bash {arduino_cli_script}", shell=True, capture_output=True, text=True)
if result.returncode != 0:
print(f"[red]{result.stdout}\n{result.stderr}[/red]")
return None
else:
print("[green]✅ Arduino CLI setup complete.[/green]")

# Check if the Arduino device is available
arduino_device_path = Path(arduino_device)
do_upload = "true" if arduino_device_path.exists() else "false"

env = os.environ.copy()
env["DO_UPLOAD"] = do_upload

upload_msg = "with upload" if do_upload == "true" else "without upload (device not found)"
with console.status(
f"Running power board setup script: {power_board_script} ({upload_msg})", spinner="dots"
):
result = subprocess.run(
f"bash {power_board_script}", shell=True, capture_output=True, text=True, env=env
)
if result.returncode != 0:
print(f"[red]{result.stdout}\n{result.stderr}[/red]")
return None
else:
print("[green]✅ Power board script complete.[/green]")
if do_upload == "0":
print(
f"[yellow]⚠️ Sketch was compiled but not uploaded because "
f"device {arduino_device} was not found. Please connect the "
f"device and run this command again to upload the sketch."
f"[/yellow]"
)

return None


if __name__ == "__main__":
Expand Down
Loading