-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathesp_utils.py
More file actions
92 lines (76 loc) · 3.02 KB
/
esp_utils.py
File metadata and controls
92 lines (76 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Utility functions for ESP-IDF tools
"""
import os
import asyncio
from typing import Tuple
async def run_command_async(command: str) -> Tuple[int, str, str]:
"""Run a command asynchronously and capture output
Args:
command: The command to run
Returns:
Tuple[int, str, str]: Return code, stdout, stderr
"""
try:
process = await asyncio.create_subprocess_shell(
command,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
return process.returncode, stdout.decode(), stderr.decode()
except Exception as e:
return 1, "", f"Error executing command: {str(e)}"
def get_esp_idf_dir(idf_path: str = None) -> str:
"""Get the ESP-IDF directory path
Args:
idf_path: Optional path to ESP-IDF directory. If None or empty, uses IDF_PATH environment variable.
Returns:
str: Path to the ESP-IDF directory
Raises:
ValueError: If idf_path is not provided and IDF_PATH environment variable is not set
"""
if idf_path:
return idf_path
if "IDF_PATH" in os.environ:
return os.environ["IDF_PATH"]
raise ValueError("IDF_PATH must be provided either as parameter or environment variable")
def get_export_script(idf_path: str = None) -> str:
"""Get the path to the ESP-IDF export script
Args:
idf_path: Optional path to ESP-IDF directory. If None or empty, uses IDF_PATH environment variable.
Returns:
str: Path to the export script
"""
return os.path.join(get_esp_idf_dir(idf_path), "export.sh")
def check_esp_idf_installed(idf_path: str = None) -> bool:
"""Check if ESP-IDF is installed
Args:
idf_path: Optional path to ESP-IDF directory. If None or empty, uses IDF_PATH environment variable.
Returns:
bool: True if ESP-IDF is installed, False otherwise
"""
try:
return os.path.exists(get_esp_idf_dir(idf_path))
except ValueError:
return False
async def list_serial_ports() -> Tuple[int, str, str]:
"""List available serial ports for ESP devices
Returns:
Tuple[int, str, str]: Return code, stdout with port list, stderr
"""
try:
# Try to use idf.py to list ports (if available)
process = await asyncio.create_subprocess_shell(
"python -m serial.tools.list_ports",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
return process.returncode, stdout.decode(), stderr.decode()
except Exception as e:
# Fallback: try common port patterns
common_ports = ["/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyACM0", "/dev/ttyACM1",
"/dev/cu.usbserial-*", "/dev/cu.SLAB_USBtoUART", "COM1", "COM2", "COM3"]
port_info = "Common ESP device ports to try:\n" + "\n".join(common_ports)
return 0, port_info, f"Note: Could not auto-detect ports. Error: {str(e)}"