M5Cardputer Library

repository·master·Indexed 43 days ago

https://github.com/m5stack/m5cardputer

Core 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.

Tokens
1.4K
Snippets
7
Records
8
Agent score
57%

What's inside M5Cardputer

  1. Use the KeyboardReader base class for keyboard input

    master

    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}
        }
    }
  2. Initialize the M5Cardputer with begin()

    master

    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();
    }
  3. Update device state with update()

    master

    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
    }
  4. Use TCA8418KeyboardReader for I2C keyboard input

    master

    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
    }
  5. Access hardware components via M5Cardputer

    master

    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()) { ... }