idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
console.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Chris Leishman
3
4#pragma once
5
25#include <idfxx/cpu>
26#include <idfxx/error>
27#include <idfxx/gpio>
28#include <idfxx/uart>
29
30#include <esp_console.h>
31#include <filesystem>
32#include <functional>
33#include <optional>
34#include <string_view>
35#include <utility>
36
37namespace idfxx::console {
38
39// =============================================================================
40// Command types
41// =============================================================================
42
51using handler = std::move_only_function<int(int argc, char** argv)>;
52
60struct command {
61 std::string_view name;
62 std::string_view help = {};
63 std::string_view hint = {};
64 void* argtable = nullptr;
65};
66
67// =============================================================================
68// Command registration (free functions)
69// =============================================================================
70
71#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
96void register_command(const command& cmd, handler callback);
97
105void deregister_command(std::string_view name);
106
116
124#endif
125
139[[nodiscard]] result<void> try_register_command(const command& cmd, handler callback);
140
149
159
166
167// =============================================================================
168// Low-level console (non-REPL use)
169// =============================================================================
170
177struct config {
178 size_t max_cmdline_length = 256;
179 size_t max_cmdline_args = 32;
180 int hint_color = 39;
181 bool hint_bold = false;
182};
183
184#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
195void init(const config& cfg = {});
196
203void deinit();
204
225int run(std::string_view cmdline);
226#endif
227
239[[nodiscard]] result<void> try_init(const config& cfg = {});
240
248
261[[nodiscard]] result<int> try_run(std::string_view cmdline);
262
263// =============================================================================
264// REPL
265// =============================================================================
266
289class repl {
290public:
294 struct config {
295 size_t max_history_len = 32;
296 std::filesystem::path history_save_path = {};
297 size_t task_stack_size = 4096;
299 std::optional<core_id> core_affinity = std::nullopt;
300 std::string_view prompt = {};
302 };
303
304 // =========================================================================
305 // UART backend
306 // =========================================================================
307
308#if CONFIG_ESP_CONSOLE_UART_DEFAULT || CONFIG_ESP_CONSOLE_UART_CUSTOM || defined(__DOXYGEN__)
310 static constexpr uart_port default_uart_port = static_cast<uart_port>(CONFIG_ESP_CONSOLE_UART_NUM);
311
315 struct uart_config {
316 uart_port port = default_uart_port;
317 int baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE;
318 gpio tx_gpio = gpio::nc();
319 gpio rx_gpio = gpio::nc();
320 };
321
322#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
337 [[nodiscard]] explicit repl(const config& cfg, const uart_config& uart);
338#endif
339
349 [[nodiscard]] static result<repl> make(const config& cfg, const uart_config& uart);
350#endif // UART
351
352 // =========================================================================
353 // USB CDC backend
354 // =========================================================================
355
356#if CONFIG_ESP_CONSOLE_USB_CDC || (defined(__DOXYGEN__) && SOC_USB_OTG_SUPPORTED)
360 struct usb_cdc_config {};
361
362#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
373 [[nodiscard]] explicit repl(const config& cfg, const usb_cdc_config& usb_cdc);
374#endif
375
385 [[nodiscard]] static result<repl> make(const config& cfg, const usb_cdc_config& usb_cdc);
386#endif // USB CDC
387
388 // =========================================================================
389 // USB Serial JTAG backend
390 // =========================================================================
391
392#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG || (defined(__DOXYGEN__) && SOC_USB_SERIAL_JTAG_SUPPORTED)
396 struct usb_serial_jtag_config {};
397
398#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
409 [[nodiscard]] explicit repl(const config& cfg, const usb_serial_jtag_config& usb_jtag);
410#endif
411
421 [[nodiscard]] static result<repl> make(const config& cfg, const usb_serial_jtag_config& usb_jtag);
422#endif // USB Serial JTAG
423
424 // =========================================================================
425 // Lifecycle
426 // =========================================================================
427
434
435 repl(const repl&) = delete;
436 repl& operator=(const repl&) = delete;
437
439 repl(repl&& other) noexcept;
440
442 repl& operator=(repl&& other) noexcept;
443
449
450private:
451 explicit repl(esp_console_repl_t* handle)
452 : _handle(handle) {}
453
454 void _stop_and_delete() noexcept;
455
456 static esp_console_repl_config_t _translate_config(const config& cfg);
457
458 esp_console_repl_t* _handle = nullptr;
459};
460
// end of idfxx_console
462
463} // namespace idfxx::console
Interactive REPL (Read-Eval-Print Loop) console.
Definition console.hpp:289
esp_console_repl_t * idf_handle() const noexcept
Returns the underlying ESP-IDF REPL handle.
Definition console.hpp:448
repl(repl &&other) noexcept
Move constructor.
repl(const repl &)=delete
~repl()
Destroys the REPL.
repl & operator=(const repl &)=delete
repl & operator=(repl &&other) noexcept
Move assignment.
A GPIO pin.
Definition gpio.hpp:61
Type-safe wrapper for FreeRTOS task priority values.
Definition cpu.hpp:84
result< void > try_deinit()
Deinitializes the console module.
result< void > try_deregister_command(std::string_view name)
Deregisters a previously registered command.
void deinit()
Deinitializes the console module.
std::move_only_function< int(int argc, char **argv)> handler
Command handler callback type.
Definition console.hpp:51
result< int > try_run(std::string_view cmdline)
Runs a command line string.
result< void > try_register_help_command()
Registers the built-in 'help' command.
void register_command(const command &cmd, handler callback)
Registers a command with a callback handler.
int run(std::string_view cmdline)
Runs a command line string.
void register_help_command()
Registers the built-in 'help' command.
void init(const config &cfg={})
Initializes the console module.
void deregister_help_command()
Deregisters the built-in 'help' command.
result< void > try_init(const config &cfg={})
Initializes the console module.
result< void > try_deregister_help_command()
Deregisters the built-in 'help' command.
void deregister_command(std::string_view name)
Deregisters a previously registered command.
result< void > try_register_command(const command &cmd, handler callback)
Registers a command with a callback handler.
uart_port
Identifies a UART port.
Definition uart.hpp:33
std::expected< T, std::error_code > result
result type wrapping a value or error code.
Definition error.hpp:120
Command descriptor for registration.
Definition console.hpp:60
std::string_view hint
Hint text (empty = auto-generate from argtable)
Definition console.hpp:63
void * argtable
Optional argtable3 pointer for argument parsing.
Definition console.hpp:64
std::string_view help
Help text (empty = hidden from help)
Definition console.hpp:62
std::string_view name
Command name (required, no spaces)
Definition console.hpp:61
Configuration for low-level console initialization.
Definition console.hpp:177
size_t max_cmdline_length
Maximum command line length in bytes.
Definition console.hpp:178
int hint_color
ASCII color code for hint text.
Definition console.hpp:180
bool hint_bold
Whether to display hints in bold.
Definition console.hpp:181
size_t max_cmdline_args
Maximum number of command line arguments.
Definition console.hpp:179
REPL configuration parameters.
Definition console.hpp:294
std::string_view prompt
Prompt string (empty = default "esp> ")
Definition console.hpp:300
std::optional< core_id > core_affinity
Core affinity (nullopt = any core)
Definition console.hpp:299
size_t max_history_len
Maximum command history length.
Definition console.hpp:295
size_t max_cmdline_length
Maximum command line length (0 = default 256)
Definition console.hpp:301
size_t task_stack_size
REPL task stack size in bytes.
Definition console.hpp:297
task_priority priority
REPL task priority.
Definition console.hpp:298
std::filesystem::path history_save_path
File path for history persistence (empty = no save)
Definition console.hpp:296