This library and the CH9329 chip allows an Arduino board without USB capability to act as a keyboard. Supports serial communication mode - mode 0.
For more information about the original Keyboard library please visit https://www.arduino.cc/reference/en/language/functions/usb/keyboard/
#include "CH9329_Keyboard.h" void setup() { Serial.begin(CH9329_DEFAULT_BAUDRATE); CH9329_Keyboard.begin(Serial); }
SoftwareSerial can also be used.
#include "CH9329_Keyboard.h" #include <SoftwareSerial.h> const byte rxPin = 2; const byte txPin = 3; SoftwareSerial mySerial (rxPin, txPin); void setup() { mySerial.begin(CH9329_DEFAULT_BAUDRATE); CH9329_Keyboard.begin(mySerial); }
For more information about the original Keyboard library please visit https://www.arduino.cc/reference/en/language/functions/usb/keyboard/
The latest release is available in the Library Manager in the Arduino IDE. Search for "CH9329_Keyboard". Click install.
manual installation
-
Download the source code by clicking on "Source code(zip)" in the latest Releases.
-
Start the Arduino IDE.
-
Select "Sketch" → "Include Library" → "Add .ZIP Library…".
-
Select the downloaded zip file and click "Open".
For microcontrollers with a flash size of 2KB or less, such as the ATtiny202, the flash usage is reduced by disabling the print function.
This is automatically determined by using the FLASHEND
macro.
As mentioned above, if the flash size is less than 2KB, the print function cannot be used.
While testing with the ATtiny202, I found that the Serial object was taking up the majority of the flash. So I prepared a method that does not use Serial.
CH9329_Keyboard.begin();
In this way, the write
and print
functions are disabled. Instead, use press
release
releaseAll
getReportData
to get the data that should be sent to the UART.
The important part is the getReportData
function. It converts the key press information registered by the press
release
releaseAll
function into the CH9329 protocol and returns it.
The code for pressing and releasing the "W" key is as follows
//global variable uint8_t reportData[REPORT_DATA_LENGTH] = {}; //setup or loop int length = 0; CH9329_Keyboard.press("w"); length = CH9329_Keyboard.getReportData(reportData, REPORT_DATA_LENGTH); USART0_sendValue(reportData, length); //UART send function. This is not included. CH9329_Keyboard.release("w"); length = CH9329_Keyboard.getReportData(reportData, REPORT_DATA_LENGTH); USART0_sendValue(reportData, length); //UART send function. This is not included.
All boards with Serial available that inherit Stream.
Or all boards capable of serial communication.
いちかわ(ICHI) (Twitter) さんが公開している 和訳したデータシートはとても役に立ちました。
Copyright (c) Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA