LilyGo T-Keyboard
The TKeyboard component provides a simple interface to the T-Keyboard keypad. It allows you to read which key is currently pressed.
API Reference
Header File
Classes
-
class TKeyboard : public espp::BasePeripheral<>
Class for interacting with the LilyGo T-Keyboard.
This class is used to interact with the LilyGo T-Keyboard. It is designed as a peripheral component for use with a serial interface such as I2C. On The T-Keyboard, you can press Alt+B to toggle the keyboard backlight.
Example
// make the I2C that we'll use to communicate espp::I2c i2c({ .port = I2C_NUM_0, .sda_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SDA_GPIO, .scl_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SCL_GPIO, }); // now make the tkeyboard which decodes the data espp::TKeyboard tkeyboard( {.write = std::bind(&espp::I2c::write, &i2c, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), .read = std::bind(&espp::I2c::read, &i2c, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), .key_cb = [](uint8_t key) { fmt::print("'{}' Pressed!\n", (char)key); }, .auto_start = false, // can't auto start since we need to provide power .log_level = espp::Logger::Verbosity::WARN}); // on the LilyGo T-Deck, the peripheral power control pin must be set high // to enable peripheral power auto power_ctrl = GPIO_NUM_10; gpio_set_direction(power_ctrl, GPIO_MODE_OUTPUT); gpio_set_level(power_ctrl, 1); do { fmt::print("Waiting for tkeyboard to boot up...\n"); std::this_thread::sleep_for(250ms); } while (!i2c.probe_device(espp::TKeyboard::DEFAULT_ADDRESS)); fmt::print("Tkeyboard ready!\n"); tkeyboard.start();
Public Types
-
typedef std::function<void(uint8_t)> key_cb_fn
The function signature for the key callback function.
This function is called when a key is pressed.
- Param key
The key that was pressed.
-
typedef std::function<bool(uint8_t)> probe_fn
Function to probe the peripheral
- Param address
The address to probe
- Return
True if the peripheral is found at the given address
Public Functions
-
inline explicit TKeyboard(const Config &config)
Constructor for the TKeyboard class.
- Parameters
config – The configuration to use.
-
inline uint8_t get_key() const
Get the currently pressed key.
This function returns the currently pressed key.
Note
This function will return 0 if no key has been pressed.
- Returns
The currently pressed key.
-
inline uint8_t read_key(std::error_code &ec)
Read a key from the keyboard.
This function reads a key from the keyboard. It will return 0 and set the error code if an error occurs. If the keyboard task is running, it will return 0 and set the error code. If the keyboard task is not running, it will return the key that was read, and update the currently pressed key.
See also
Note
This function will return 0 if no key was read.
Note
This function will return 0 if an error occurs.
Note
This function will set the error code if the keyboard task is running.
- Parameters
ec – The error code to set if an error occurs.
- Returns
The key that was read.
-
inline bool start()
Start the keyboard task.
This function starts the keyboard task. It should be called after the keyboard has been initialized.
- Returns
True if the task was started, false otherwise.
-
inline bool stop()
Stop the keyboard task.
This function stops the keyboard task.
- Returns
True if the task was stopped, false otherwise.
- inline bool probe (std::error_code &ec) requires(UseAddress)
Probe the peripheral
Note
This function is thread safe
Note
If the probe function is not set, this function will return false and set the error code to operation_not_supported
Note
This function is only available if UseAddress is true
- Parameters
ec – The error code to set if there is an error
- Returns
True if the peripheral is found
- inline void set_address (uint8_t address) requires(UseAddress)
Set the address of the peripheral
Note
This function is thread safe
Note
This function is only available if UseAddress is true
- Parameters
address – The address of the peripheral
- inline void set_probe (const probe_fn &probe) requires(UseAddress)
Set the probe function
Note
This function is thread safe
Note
This should rarely be used, as the probe function is usually set in the constructor. If you need to change the probe function, consider using the set_config function instead.
Note
This function is only available if UseAddress is true
- Parameters
probe – The probe function
-
inline void set_write(const write_fn &write)
Set the write function
Note
This function is thread safe
Note
This should rarely be used, as the write function is usually set in the constructor. If you need to change the write function, consider using the set_config function instead.
- Parameters
write – The write function
-
inline void set_read(const read_fn &read)
Set the read function
Note
This function is thread safe
Note
This should rarely be used, as the read function is usually set in the constructor. If you need to change the read function, consider using the set_config function instead.
- Parameters
read – The read function
-
inline void set_read_register(const read_register_fn &read_register)
Set the read register function
Note
This function is thread safe
Note
This should rarely be used, as the read register function is usually set in the constructor. If you need to change the read register function, consider using the set_config function instead.
- Parameters
read_register – The read register function
-
inline void set_write_then_read(const write_then_read_fn &write_then_read)
Set the write then read function
Note
This function is thread safe
Note
This should rarely be used, as the write then read function is usually set in the constructor. If you need to change the write then
- Parameters
write_then_read – The write then read function
-
inline void set_separate_write_then_read_delay(const std::chrono::milliseconds &delay)
Set the delay between the write and read operations in write_then_read
Note
This function is thread safe
Note
This should rarely be used, as the delay is usually set in the constructor. If you need to change the delay, consider using the set_config function instead.
Note
This delay is only used if the write_then_read function is not set to a custom function and the write and read functions are separate functions.
- Parameters
delay – The delay between the write and read operations in write_then_read
-
inline void set_config(const Config &config)
Set the configuration for the peripheral
Note
This function is thread safe
Note
The configuration should normally be set in the constructor, but this function can be used to change the configuration after the peripheral has been created - for instance if the peripheral could be found on different communications buses.
- Parameters
config – The configuration for the peripheral
-
inline void set_config(Config &&config)
Set the configuration for the peripheral
Note
This function is thread safe
Note
The configuration should normally be set in the constructor, but this function can be used to change the configuration after the peripheral has been created - for instance if the peripheral could be found on different communications buses.
- Parameters
config – The configuration for the peripheral
-
inline const std::string &get_name() const
Get the name of the component
Note
This is the tag of the logger
- Returns
A const reference to the name of the component
-
inline void set_log_tag(const std::string_view &tag)
Set the tag for the logger
- Parameters
tag – The tag to use for the logger
-
inline espp::Logger::Verbosity get_log_level() const
Get the log level for the logger
See also
See also
- Returns
The verbosity level of the logger
-
inline void set_log_level(espp::Logger::Verbosity level)
Set the log level for the logger
See also
See also
- Parameters
level – The verbosity level to use for the logger
-
inline void set_log_verbosity(espp::Logger::Verbosity level)
Set the log verbosity for the logger
See also
See also
See also
Note
This is a convenience method that calls set_log_level
- Parameters
level – The verbosity level to use for the logger
-
inline espp::Logger::Verbosity get_log_verbosity() const
Get the log verbosity for the logger
See also
See also
See also
Note
This is a convenience method that calls get_log_level
- Returns
The verbosity level of the logger
-
inline void set_log_rate_limit(std::chrono::duration<float> rate_limit)
Set the rate limit for the logger
See also
Note
Only calls to the logger that have _rate_limit suffix will be rate limited
- Parameters
rate_limit – The rate limit to use for the logger
Public Static Attributes
-
static constexpr uint8_t DEFAULT_ADDRESS = 0x55
The default address of the keyboard.
-
struct Config
The configuration structure for the keyboard.
Public Members
-
BasePeripheral::write_fn write
The write function to use.
-
BasePeripheral::read_fn read
The read function to use.
-
key_cb_fn key_cb
The key callback function to use. This function will be called when a key is pressed if it is not null and the keyboard task is running.
-
uint8_t address = DEFAULT_ADDRESS
The address of the keyboard.
-
std::chrono::milliseconds polling_interval = std::chrono::milliseconds(10)
The polling interval for the keyboard.
-
bool auto_start = true
Whether or not to automatically start the keyboard task.
-
BasePeripheral::write_fn write
-
typedef std::function<void(uint8_t)> key_cb_fn