Replaced monitor_flags with independent project configuration options: monitor_parity, monitor_eol, monitor_raw, monitor_echo

This commit is contained in:
Ivan Kravets
2022-06-15 15:36:17 +03:00
parent 6f9985125d
commit 30709fd0b3
5 changed files with 103 additions and 71 deletions

View File

@ -21,6 +21,8 @@ PlatformIO Core 6
- Automatically reconnect if a connection fails - Automatically reconnect if a connection fails
- Added new `pio device monitor --no-reconnect <https://docs.platformio.org/en/latest/core/userguide/device/cmd_monitor.html#cmdoption-pio-device-monitor-no-reconnect>`__ option to disable automatic reconnection - Added new `pio device monitor --no-reconnect <https://docs.platformio.org/en/latest/core/userguide/device/cmd_monitor.html#cmdoption-pio-device-monitor-no-reconnect>`__ option to disable automatic reconnection
- Handle disconnects more gracefully (`issue #3939 <https://github.com/platformio/platformio-core/issues/3939>`_) - Handle disconnects more gracefully (`issue #3939 <https://github.com/platformio/platformio-core/issues/3939>`_)
- Replaced ``monitor_flags`` with independent project configuration options: `monitor_parity <https://docs.platformio.org/en/latest/projectconf/section_env_monitor.html#monitor-parity>`__, `monitor_eol <https://docs.platformio.org/en/latest/projectconf/section_env_monitor.html#monitor-eol>`__, `monitor_raw <https://docs.platformio.org/en/latest/projectconf/section_env_monitor.html#monitor-raw>`__, `monitor_echo <https://docs.platformio.org/en/latest/projectconf/section_env_monitor.html#monitor-echo>`__
- Fixed an issue when the monitor filters were not applied in their order (`issue #4320 <https://github.com/platformio/platformio-core/issues/4320>`_)
* Improved a serial port finder for a board with predefined HWIDs * Improved a serial port finder for a board with predefined HWIDs
* Merged the |UNITTESTING| "building" stage with "uploading" for the embedded target (`issue #4307 <https://github.com/platformio/platformio-core/issues/4307>`_) * Merged the |UNITTESTING| "building" stage with "uploading" for the embedded target (`issue #4307 <https://github.com/platformio/platformio-core/issues/4307>`_)

2
docs

Submodule docs updated: f0acc3040f...a3c98fe6f3

View File

@ -13,6 +13,7 @@
# limitations under the License. # limitations under the License.
import os import os
import sys
import click import click
@ -31,24 +32,26 @@ from platformio.project.options import ProjectOptions
@click.option( @click.option(
"-b", "-b",
"--baud", "--baud",
type=int, type=ProjectOptions["env.monitor_speed"].type,
show_default=True,
help="Set baud/speed [default=%d]" % ProjectOptions["env.monitor_speed"].default, help="Set baud/speed [default=%d]" % ProjectOptions["env.monitor_speed"].default,
) )
@click.option( @click.option(
"--parity", "--parity",
default="N", type=ProjectOptions["env.monitor_parity"].type,
show_default=True, help="Enable parity checking [default=%s]"
type=click.Choice(["N", "E", "O", "S", "M"]), % ProjectOptions["env.monitor_parity"].default,
help="Set parity",
) )
@click.option("--rtscts", is_flag=True, help="Enable RTS/CTS flow control") @click.option("--rtscts", is_flag=True, help="Enable RTS/CTS flow control")
@click.option("--xonxoff", is_flag=True, help="Enable software flow control") @click.option("--xonxoff", is_flag=True, help="Enable software flow control")
@click.option( @click.option(
"--rts", default=None, type=click.IntRange(0, 1), help="Set initial RTS line state" "--rts",
type=ProjectOptions["env.monitor_rts"].type,
help="Set initial RTS line state",
) )
@click.option( @click.option(
"--dtr", default=None, type=click.IntRange(0, 1), help="Set initial DTR line state" "--dtr",
type=ProjectOptions["env.monitor_dtr"].type,
help="Set initial DTR line state",
) )
@click.option("--echo", is_flag=True, help="Enable local echo") @click.option("--echo", is_flag=True, help="Enable local echo")
@click.option( @click.option(
@ -58,16 +61,18 @@ from platformio.project.options import ProjectOptions
help="Set the encoding for the serial port (e.g. hexlify, Latin1, UTF-8)", help="Set the encoding for the serial port (e.g. hexlify, Latin1, UTF-8)",
) )
@click.option( @click.option(
"-f", "--filter", "filters", multiple=True, help="Add filters/text transformations" "-f",
"--filter",
"filters",
multiple=True,
help="Apply filters/text transformations",
) )
@click.option( @click.option(
"--eol", "--eol",
default="CRLF", type=ProjectOptions["env.monitor_eol"].type,
show_default=True, help="End of line mode [default=%s]" % ProjectOptions["env.monitor_eol"].default,
type=click.Choice(["CR", "LF", "CRLF"]),
help="End of line mode",
) )
@click.option("--raw", is_flag=True, help="Do not apply any encodings/transformations") @click.option("--raw", is_flag=True, help=ProjectOptions["env.monitor_raw"].description)
@click.option( @click.option(
"--exit-char", "--exit-char",
type=int, type=int,
@ -105,16 +110,17 @@ from platformio.project.options import ProjectOptions
help="Load configuration from `platformio.ini` and the specified environment", help="Load configuration from `platformio.ini` and the specified environment",
) )
def device_monitor_cmd(**options): def device_monitor_cmd(**options):
platform = None
project_options = {}
with fs.cd(options["project_dir"]): with fs.cd(options["project_dir"]):
platform = None
project_options = {}
try: try:
project_options = get_project_options(options["environment"]) project_options = get_project_options(options["environment"])
options = apply_project_monitor_options(options, project_options)
if "platform" in project_options: if "platform" in project_options:
platform = PlatformFactory.new(project_options["platform"]) platform = PlatformFactory.new(project_options["platform"])
except NotPlatformIOProjectError: except NotPlatformIOProjectError:
pass pass
options = apply_project_monitor_options(options, project_options)
register_filters(platform=platform, options=options) register_filters(platform=platform, options=options)
options["port"] = find_serial_port( options["port"] = find_serial_port(
initial_port=options["port"], initial_port=options["port"],
@ -125,8 +131,6 @@ def device_monitor_cmd(**options):
ensure_ready=True, ensure_ready=True,
) )
options["baud"] = options["baud"] or ProjectOptions["env.monitor_speed"].default
if options["menu_char"] == options["exit_char"]: if options["menu_char"] == options["exit_char"]:
raise exception.UserSideException( raise exception.UserSideException(
"--exit-char can not be the same as --menu-char" "--exit-char can not be the same as --menu-char"
@ -143,12 +147,18 @@ def get_project_options(environment=None):
def apply_project_monitor_options(initial_options, project_options): def apply_project_monitor_options(initial_options, project_options):
for k in ("port", "speed", "rts", "dtr"): for option_meta in ProjectOptions.values():
k2 = "monitor_%s" % k if option_meta.group != "monitor":
if k == "speed": continue
k = "baud" cli_key = option_meta.name.split("_", 1)[1]
if initial_options[k] is None and k2 in project_options: if cli_key == "speed":
initial_options[k] = project_options[k2] cli_key = "baud"
if k != "port": # value set from CLI, skip overriding
initial_options[k] = int(initial_options[k]) if initial_options[cli_key] not in (None, (), []) and (
option_meta.type != click.BOOL or f"--{cli_key}" in sys.argv[1:]
):
continue
initial_options[cli_key] = project_options.get(
option_meta.name, option_meta.default
)
return initial_options return initial_options

View File

@ -519,6 +519,13 @@ ProjectOptions = OrderedDict(
oldnames=["monitor_baud"], oldnames=["monitor_baud"],
default=9600, default=9600,
), ),
ConfigEnvOption(
group="monitor",
name="monitor_parity",
description="A monitor parity checking",
type=click.Choice(["N", "E", "O", "S", "M"]),
default="N",
),
ConfigEnvOption( ConfigEnvOption(
group="monitor", group="monitor",
name="monitor_filters", name="monitor_filters",
@ -541,12 +548,24 @@ ProjectOptions = OrderedDict(
), ),
ConfigEnvOption( ConfigEnvOption(
group="monitor", group="monitor",
name="monitor_flags", name="monitor_eol",
description=( description="A monitor end of line mode",
"The extra flags and options for `platformio device monitor` " type=click.Choice(["CR", "LF", "CRLF"]),
"command" default="CRLF",
), ),
multiple=True, ConfigEnvOption(
group="monitor",
name="monitor_raw",
description="Disable encodings/transformations of device output",
type=click.BOOL,
default=False,
),
ConfigEnvOption(
group="monitor",
name="monitor_echo",
description="Enable a monitor local echo",
type=click.BOOL,
default=False,
), ),
# Library # Library
ConfigEnvOption( ConfigEnvOption(

View File

@ -270,60 +270,67 @@ def device_list(agents, json_output):
@remote_device.command("monitor", short_help="Monitor remote device") @remote_device.command("monitor", short_help="Monitor remote device")
@click.option("--port", "-p", help="Port, a number or a device name") @click.option("--port", "-p", help="Port, a number or a device name")
@click.option( @click.option(
"--baud",
"-b", "-b",
type=int, "--baud",
help="Set baud rate, default=%d" % ProjectOptions["env.monitor_speed"].default, type=ProjectOptions["env.monitor_speed"].type,
help="Set baud/speed [default=%d]" % ProjectOptions["env.monitor_speed"].default,
) )
@click.option( @click.option(
"--parity", "--parity",
default="N", type=ProjectOptions["env.monitor_parity"].type,
type=click.Choice(["N", "E", "O", "S", "M"]), help="Set parity [default=%s]" % ProjectOptions["env.monitor_parity"].default,
help="Set parity, default=N",
) )
@click.option("--rtscts", is_flag=True, help="Enable RTS/CTS flow control, default=Off") @click.option("--rtscts", is_flag=True, help="Enable RTS/CTS flow control")
@click.option("--xonxoff", is_flag=True, help="Enable software flow control")
@click.option( @click.option(
"--xonxoff", is_flag=True, help="Enable software flow control, default=Off" "--rts",
type=ProjectOptions["env.monitor_rts"].type,
help="Set initial RTS line state",
) )
@click.option( @click.option(
"--rts", default=None, type=click.IntRange(0, 1), help="Set initial RTS line state" "--dtr",
type=ProjectOptions["env.monitor_dtr"].type,
help="Set initial DTR line state",
) )
@click.option( @click.option("--echo", is_flag=True, help="Enable local echo")
"--dtr", default=None, type=click.IntRange(0, 1), help="Set initial DTR line state"
)
@click.option("--echo", is_flag=True, help="Enable local echo, default=Off")
@click.option( @click.option(
"--encoding", "--encoding",
default="UTF-8", default="UTF-8",
help="Set the encoding for the serial port (e.g. hexlify, " show_default=True,
"Latin1, UTF-8), default: UTF-8", help="Set the encoding for the serial port (e.g. hexlify, Latin1, UTF-8)",
)
@click.option(
"-f",
"--filter",
"filters",
multiple=True,
help="Apply filters/text transformations",
) )
@click.option("--filter", "-f", multiple=True, help="Add text transformation")
@click.option( @click.option(
"--eol", "--eol",
default="CRLF", type=ProjectOptions["env.monitor_eol"].type,
type=click.Choice(["CR", "LF", "CRLF"]), help="End of line mode [default=%s]" % ProjectOptions["env.monitor_eol"].default,
help="End of line mode, default=CRLF",
) )
@click.option("--raw", is_flag=True, help="Do not apply any encodings/transformations") @click.option("--raw", is_flag=True, help=ProjectOptions["env.monitor_raw"].description)
@click.option( @click.option(
"--exit-char", "--exit-char",
type=int, type=int,
default=3, default=3,
show_default=True,
help="ASCII code of special character that is used to exit " help="ASCII code of special character that is used to exit "
"the application, default=3 (Ctrl+C)", "the application [default=3 (Ctrl+C)]",
) )
@click.option( @click.option(
"--menu-char", "--menu-char",
type=int, type=int,
default=20, default=20,
help="ASCII code of special character that is used to " help="ASCII code of special character that is used to "
"control miniterm (menu), default=20 (DEC)", "control terminal (menu) [default=20 (DEC)]",
) )
@click.option( @click.option(
"--quiet", "--quiet",
is_flag=True, is_flag=True,
help="Diagnostics: suppress non-error messages, default=Off", help="Diagnostics: suppress non-error messages",
) )
@click.option( @click.option(
"-d", "-d",
@ -354,19 +361,17 @@ def device_monitor(ctx, agents, **kwargs):
try: try:
with fs.cd(kwargs["project_dir"]): with fs.cd(kwargs["project_dir"]):
project_options = get_project_options(kwargs["environment"]) project_options = get_project_options(kwargs["environment"])
kwargs = apply_project_monitor_options(kwargs, project_options)
except NotPlatformIOProjectError: except NotPlatformIOProjectError:
pass pass
kwargs["baud"] = kwargs["baud"] or ProjectOptions["env.monitor_speed"].default kwargs = apply_project_monitor_options(kwargs, project_options)
kwargs["reconnect"] = False
def _tx_target(sock_dir): def _tx_target(sock_dir):
subcmd_argv = ["remote"] subcmd_argv = ["remote"]
for agent in agents: for agent in agents:
subcmd_argv.extend(["--agent", agent]) subcmd_argv.extend(["--agent", agent])
subcmd_argv.extend(["device", "monitor"]) subcmd_argv.extend(["device", "monitor"])
subcmd_argv.extend(project_options_to_monitor_argv(kwargs, project_options)) subcmd_argv.extend(project_options_to_monitor_argv(kwargs))
subcmd_argv.extend(["--sock", sock_dir]) subcmd_argv.extend(["--sock", sock_dir])
subprocess.call([proc.where_is_program("platformio")] + subcmd_argv) subprocess.call([proc.where_is_program("platformio")] + subcmd_argv)
@ -381,6 +386,7 @@ def device_monitor(ctx, agents, **kwargs):
return return
with open(sock_file, encoding="utf8") as fp: with open(sock_file, encoding="utf8") as fp:
kwargs["port"] = fp.read() kwargs["port"] = fp.read()
kwargs["no_reconnect"] = True
ctx.invoke(device_monitor_cmd, **kwargs) ctx.invoke(device_monitor_cmd, **kwargs)
t.join(2) t.join(2)
finally: finally:
@ -389,19 +395,14 @@ def device_monitor(ctx, agents, **kwargs):
return True return True
def project_options_to_monitor_argv(cli_options, project_options, ignore=None): def project_options_to_monitor_argv(cli_options):
confmon_flags = project_options.get("monitor_flags", []) result = []
result = confmon_flags[::] for item in cli_options["filters"] or []:
result.extend(["--filter", item])
for f in project_options.get("monitor_filters", []):
result.extend(["--filter", f])
for k, v in cli_options.items(): for k, v in cli_options.items():
if v is None or (ignore and k in ignore): if v is None or k == "filters":
continue continue
k = "--" + k.replace("_", "-") k = "--" + k.replace("_", "-")
if k in confmon_flags:
continue
if isinstance(v, bool): if isinstance(v, bool):
if v: if v:
result.append(k) result.append(k)