0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

71歳の... ESP32-S3へUSBキーボードから文字列を直接入力。「¥」「|」「_」の文字も。

Posted at

 先回、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.cppEspUsbHost.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モジュールと合わせて使おうと思います。最後まで見ていただきありがとうございました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?