English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

Faces Keyboard3 Arduino Tutorial

1. Preparation

2. Notes

Pin Compatibility
Faces Keyboard3 connects to the host device through the Faces Bottom3 M5-Bus. M5-Bus pin definitions vary between host devices, so check the pin compatibility table below and update the example for your actual pin connections before use.

3. Example Program

  • This tutorial uses a CoreS3 as the host device. Insert Faces Keyboard3 into the panel connector on Faces Bottom3, then connect Faces Bottom3 to CoreS3 through M5-Bus. Faces Keyboard3 communicates with the host over I2C; after connection, the I2C IO pins are G12 (SDA) and G11 (SCL), and the interrupt pin is G10 (INT).
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include <Arduino.h>
#include <M5Faces.h>
#include <M5Unified.h>

M5Faces_Keyboard3 faces;
String screen_text;

void redraw_screen()
{
    M5.Display.fillScreen(TFT_BLACK);
    M5.Display.setCursor(0, 0);
    M5.Display.print(">");
    M5.Display.print(screen_text);
}

void setup()
{
    Serial.begin(115200);
    screen_text.reserve(256);

    auto config           = M5.config();
    config.fallback_board = m5::board_t::board_M5StackCoreS3;
    M5.begin(config);

    const int i2c_sda = M5.getPin(m5::pin_name_t::in_i2c_sda);
    const int i2c_scl = M5.getPin(m5::pin_name_t::in_i2c_scl);
    if (!M5.In_I2C.isEnabled()) {
        M5.In_I2C.begin(I2C_NUM_1, i2c_sda, i2c_scl);
    }

    M5.Display.setRotation(1);
    M5.Display.setTextColor(TFT_GREEN, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeMonoBold12pt7b);
    M5.Display.setTextSize(1);
    M5.Display.setTextWrap(true);
    M5.Display.setTextScroll(true);
    redraw_screen();

    if (faces.begin(&M5.In_I2C) != M5FACES_OK) {
        Serial.println("Keyboard3 init failed");
        M5.Display.print(" init failed");
        return;
    }

    Serial.println("Keyboard3 ready");
}

void loop()
{
    if (faces.update()) {
        const uint8_t raw = faces.getKey();
        const char value  = faces.getChar();
        const char* name  = M5Faces_Keyboard3::keyboard3_code_parse(raw);

        if (faces.isPrintable()) {
            Serial.printf("raw=0x%02X key='%c'\n", raw, value);
            screen_text += value;
            redraw_screen();
        } else {
            Serial.printf("raw=0x%02X key=%s\n", raw, name ? name : "UNKNOWN");
            if ((value == '\b' || raw == KEYBOARD3_KEY_DEL) && screen_text.length() > 0) {
                screen_text.remove(screen_text.length() - 1);
                redraw_screen();
            } else if (value == '\n') {
                screen_text += '\n';
                redraw_screen();
            } else if (value == '\t') {
                screen_text += "    ";
                redraw_screen();
            }
        }
    }
    delay(5);
}

4. Compile and Upload

In Arduino IDE, select the board and port for CoreS3, then click "Verify" to compile the program. After compilation succeeds, click "Upload" to flash it to CoreS3. When the upload is complete, open the Serial Monitor to view the output.

Page Tools
PDF
On This Page