Overview of M5Cardputer library
masterm5cardputer library provides the basic software interface for the M5Stack M5Cardputer and M5Cardputer-ADV boards. It is designed to facilitate development on these specific hardware platforms.repository·master·Indexed 43 days ago
https://github.com/m5stack/m5cardputerCore library for programming M5Stack M5Cardputer and M5Cardputer-ADV hardware. Provides a software interface to manage the display, power management, speaker, microphone, and built-in keyboard, including the TCA8418KeyboardReader for I2C keyboard input and the KeyboardReader base class for key detection.
m5cardputer library provides the basic software interface for the M5Stack M5Cardputer and M5Cardputer-ADV boards. It is designed to facilitate development on these specific hardware platforms.The KeyboardReader class provides an abstract interface for implementing keyboard reading logic on the M5Cardputer. Developers can implement this interface to handle hardware-specific key detection.
Key methods:
begin(): Initializes the keyboard reader hardware or software state.update(): Must be called periodically (typically in a main loop) to refresh the keyboard state and populate the internal key list.keyList(): Returns a constant reference to a std::vector<Point2D_t> containing the currently active keys.The Point2D_t struct represents a key position using x and y coordinates.
// Example of how a consumer would interact with a KeyboardReader implementation
void loop() {
reader.update(); // Refresh key states
const auto& keys = reader.keyList(); // Get currently pressed keys
for (const auto& key : keys) {
// Process key at position {key.x, key.y}
}
}To start using the M5Cardputer, call the begin() method on the global M5Cardputer instance. You can initialize it with default settings or provide a custom m5::M5Unified::config_t object to configure the underlying M5Unified hardware settings. You can also choose whether or not to enable the keyboard functionality via the enableKeyboard boolean parameter.
#include <M5Cardputer.h>
void setup() {
// Initialize with default settings and enabled keyboard
M5Cardputer.begin();
// OR initialize with custom M5Unified configuration
// m5::M5Unified::config_t cfg = { ... };
// M5Cardputer.begin(cfg, true);
}
void loop() {
M5Cardputer.update();
}Call M5Cardputer.update() within your main loop to process hardware events, such as button presses or keyboard input, ensuring the internal state of the device remains synchronized with the physical hardware.
void loop() {
M5Cardputer.update();
// Handle inputs/events here
}The TCA8418KeyboardReader class is an implementation of the KeyboardReader interface specifically designed for the TCA8418 I2C keyboard chip used in M5Cardputer hardware. It allows you to read keyboard events by polling or handling interrupts.
To use it, instantiate the class (optionally providing an interrupt pin), call begin() to initialize the I2C communication, and call update() within your main loop to process key events.
// Example usage pattern
TCA8418KeyboardReader keyboard(-1); // Use -1 if not using an interrupt pin
void setup() {
keyboard.begin();
}
void loop() {
keyboard.update();
// Process keyboard events here
}The M5_CARDPUTER class provides direct access to the device's hardware components through public member references. These include the display, power management, speaker, microphone, and the built-in keyboard. It also provides access to I2C buses for both internal and external devices.
// Display access
M5Cardputer.Display.print("Hello");
M5Cardputer.Speaker.volume(128);
M5Cardputer.Mic.getSample();
// Keyboard access
if (M5Cardputer.Keyboard.isPressed()) { ... }
// I2C access
M5Cardputer.In_I2C.begin(); // Internal I2C
M5Cardputer.Ex_I2C.begin(); // External I2C (Port.A)
// Button access
if (M5Cardputer.BtnA.isPressed()) { ... }The Point2D_t struct is used to represent the coordinates of a key in the keyboard matrix.
Fields:
int x: The X-coordinate of the key.int y: The Y-coordinate of the key.It supports equality comparison using the == operator.
struct Point2D_t {
int x = 0;
int y = 0;
bool operator==(const Point2D_t& other) const {
return x == other.x && y == other.y;
}
};The TCA8418KeyboardReader constructor accepts an optional interrupt_pin integer. If your hardware configuration utilizes an interrupt line from the TCA8418 chip to signal key presses, pass the corresponding GPIO pin number. Otherwise, pass -1 to disable interrupt-based signaling.
TCA8418KeyboardReader(int interrupt_pin = -1);