
Arduino入門

G12 (SDA) と G11 (SCL)、割り込みピンは G10 (INT) です。#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);
}Arduino IDE で CoreS3 に対応するボードとポートを選択し、「検証」をクリックしてプログラムをコンパイルします。コンパイルに成功したら「書き込み」をクリックして CoreS3 に書き込み、完了後にシリアルモニターで実行結果を確認できます。