先回、ESP32-S3の「UsbHost」機能を用い文字の直接入力ができたので、文字列入力もやってみました。
環境等は同じです。
1.Windows 11 Pro
2.Arduino IDE 2.3.6
3.ボードは、FREENOVE ESP32-S3 WROOM N8R8 (8MB Flash)
Arduino IDE上では「ESP32S3 Dev Module」
4.ライブラリマネージャーで「EspUsbHost」をインストール
コンパイルエラー対処としてスケッチフォルダー内に
"EspUsbHost.h"と"EspUsbHost.cpp"をコピー
5.外付けした「USB Type-A メスコネクタ」にUSBレシーバーを接続
「ELECOM ワイアレスキーボード TK-FDM105T」使用できました
このままでは記号「¥」「|」「_」が使えませんでした。そこで、次の記事
のスケッチを参考に。それでキーコードを調べました。そして、EspUsbHost.h
内のHID_KEYCODE_TO_ASCII_JA
の最後に以下の行を追加することにより出来るようにしました。
EspUsbHost.h
\
{0 , 0 }, /* 0x68 */ \
{0 , 0 }, /* 0x69 */ \
{0 , 0 }, /* 0x6a */ \
{0 , 0 }, /* 0x6b */ \
{0 , 0 }, /* 0x6c */ \
{0 , 0 }, /* 0x6d */ \
{0 , 0 }, /* 0x6e */ \
{0 , 0 }, /* 0x6f */ \
{0 , 0 }, /* 0x70 */ \
{0 , 0 }, /* 0x71 */ \
{0 , 0 }, /* 0x72 */ \
{0 , 0 }, /* 0x73 */ \
{0 , 0 }, /* 0x74 */ \
{0 , 0 }, /* 0x75 */ \
{0 , 0 }, /* 0x76 */ \
{0 , 0 }, /* 0x77 */ \
{0 , 0 }, /* 0x78 */ \
{0 , 0 }, /* 0x79 */ \
{0 , 0 }, /* 0x7a */ \
{0 , 0 }, /* 0x7b */ \
{0 , 0 }, /* 0x7c */ \
{0 , 0 }, /* 0x7d */ \
{0 , 0 }, /* 0x7e */ \
{0 , 0 }, /* 0x7f */ \
{0 , 0 }, /* 0x80 */ \
{0 , 0 }, /* 0x81 */ \
{0 , 0 }, /* 0x82 */ \
{0 , 0 }, /* 0x83 */ \
{0 , 0 }, /* 0x84 */ \
{0 , 0 }, /* 0x85 */ \
{0 , 0 }, /* 0x86 */ \
{'\\' , '_' }, /* 0x87 */ \
{0 , 0 }, /* 0x88 */ \
{'\\' , '|' }, /* 0x89 */ \
{0 , 0 }, /* 0x8a */ \
#endif
同時にEspUsbHost.cpp
の 709,710行の配列要素数を「128」から余裕を持たせた「160」にしました。
static uint8_t const keyboard_conv_table[160][2] = { HID_KEYCODE_TO_ASCII };
static uint8_t const keyboard_conv_table_ja[160][2] = { HID_KEYCODE_TO_ASCII_JA };
さらに、ここ
(有難うございます。よく参考にさせていただいてます。)
を見つけたので、さっそくxTaskCreateUniversal
も試してみました。usbHost.task()
は「Core0」に移動。
何故かコンパイルエラーになるので、スケッチフォルダーにEspUsbHost.cpp
とEspUsbHost.h
をコピーしておきます。スケッチ本体は次のように。
EspUsbHost_KB_String.ino
#include "EspUsbHost.h"
#include <string>
String line = "";
class MyEspUsbHost : public EspUsbHost {
void onKeyboardKey(uint8_t ascii, uint8_t keycode, uint8_t modifier) {
if (31 < ascii && ascii < 127) {
line = line + (char)ascii;
Serial.printf("%c", ascii);
} else if (ascii == 8) {
if (line.length() > 0) {
line = line.substring(0, line.length() - 1);
Serial.println();
Serial.println(line);
}
} else if (ascii == 10 || ascii == 13) {
Serial.println();
Serial.println(line);
line = "";
}
};
};
MyEspUsbHost usbHost;
TaskHandle_t thp[1];
void setup() {
Serial.begin(115200);
delay(500);
usbHost.begin();
usbHost.setHIDLocal(HID_LOCAL_Japan_Katakana);
//xTaskCreatePinnedToCore(Core0, "Core0", 4096 * 2, NULL, 3, &thp[0], 0);
xTaskCreateUniversal(Core0, "Core0", 4096 * 2, NULL, 3, &thp[0], CONFIG_ARDUINO_RUNNING_CORE);
Serial.println("ESP32-S3 Keyboard Test Start");
}
void loop() {
}
void Core0(void *args) {
while (1) {
delay(1);
usbHost.task();
}
}
キーコードとか複雑でよくわかっておりません(エヘヘ)。次はTFT-LCDモジュールと合わせて使おうと思います。最後まで見ていただきありがとうございました。