|
| 1 | +/****************************************************************************** |
| 2 | +* longpress_combined.ino |
| 3 | +* BC6xxx & BC759x Key Scan Interface Library Example Code |
| 4 | +* |
| 5 | +* This code uses Serial1 to connect a BC6xxx or BC759x chip. Besides |
| 6 | +* normal single key pressing, it can also detect the long-press of particular |
| 7 | +* keys or the combination of multiple keys. |
| 8 | +* Serial is used to print the key detection results. Use the IDE built-in |
| 9 | +* Serial Monitor to see outputs. |
| 10 | +* This software prints the key value on Serial when there is a key event. |
| 11 | +* When key 1 or key 5 is pressed for more than 3s, a new key event with user |
| 12 | +* defined key value of 120(0x78) and 121(0x79) is generated. |
| 13 | +* When key 0 and key 1 are pressed simutaneously, a new key event for this |
| 14 | +* combined key with key value of 122(0x7a) is generated. Also for a combination |
| 15 | +* of key 8 and key 12, with key value of 123(0x79). |
| 16 | +* This code runs on any Arduino compatible boards. |
| 17 | +* |
| 18 | +* Dependencies: |
| 19 | +* This code depends on the following libraries: |
| 20 | +* Arduino Software Serial Library |
| 21 | +* UART_Keyboard_Driver Library |
| 22 | +* |
| 23 | +* Author: |
| 24 | +* This software is written by BitCode. https://bitcode.com.cn |
| 25 | +* |
| 26 | +* Version: |
| 27 | +* V1.0 March 2021 |
| 28 | +* V2.0 May 2021, changed print output to use built-in Serial. |
| 29 | +* V3.0 October 2024. Changed default keyboard port to Serial1 |
| 30 | +* |
| 31 | +* License: |
| 32 | +* MIT license. It can be used for both open source and commercial projects. |
| 33 | +******************************************************************************/ |
| 34 | +#include <UART_Keyboard.h> |
| 35 | + |
| 36 | +#define KEYBOARD_SERIAL Serial1 // By default this code uses Serial1 as keyboard port |
| 37 | +// If you are using Arduino with only 1 Serial (such as UNO), you may want to disable |
| 38 | +// the above line and use the following setting: |
| 39 | +// #include <SoftwareSerial.h> |
| 40 | +// #define KEYBOARD_SERIAL swSerial |
| 41 | +// SoftwareSerial swSerial(11, 12); // creating SoftwareSerial instance, using pin 11 as Rx, 12 as Tx (Tx not used in this example) |
| 42 | + |
| 43 | +UartKeyboard Keypad(KEYBOARD_SERIAL); // creating UartKeyboard instance |
| 44 | + |
| 45 | + |
| 46 | +// Definition of long-press keys |
| 47 | +const unsigned char lp1[2] = { 1, 120 }; // Longpress key 1, detecting long-pressing of key 1 and use 120 as the user defined key value |
| 48 | +const unsigned char lp2[2] = { 5, 121 }; // Longpress key 2, detecting long-pressing of key 5 and use 121 as the user defined key value |
| 49 | +const unsigned char* LPList[2] = { lp1, lp2 }; // Longpress key list |
| 50 | + |
| 51 | +// Definition of combined keys |
| 52 | +const unsigned char cb1[4] = { 2, 122, 0, 1 }; // Combined key 1, combination of 2 keys: key0 and key1, use 122 as user defined key value |
| 53 | +const unsigned char cb2[4] = { 2, 123, 8, 12 }; // Combined key 2, combination of 2 keys: key8 and key12, use 123 as user defined key value |
| 54 | +const unsigned char* CBList[2] = { cb1, cb2 }; // Combined key list |
| 55 | + |
| 56 | +void setup() { |
| 57 | + Serial.begin(115200); // Initialize Serial for monitoring |
| 58 | + KEYBOARD_SERIAL.begin(9600); // Initialize software serial swSerial |
| 59 | + Keypad.defLongpressKey(LPList, 2); // Define long-press keys |
| 60 | + Keypad.setLongpressCount(60); // Set long-press time to 3s (50ms * 60) |
| 61 | + Keypad.defCombinedKey(CBList, 2); // Define combined keys |
| 62 | +} |
| 63 | + |
| 64 | +void loop() { |
| 65 | + // put your main code here, to run repeatedly: |
| 66 | + Keypad.checkChanges(); // let the key scan library to update key status |
| 67 | + if (Keypad.isKeyChanged()) // if there is any detectable key change |
| 68 | + { |
| 69 | + Serial.println(Keypad.getKeyValue()); // print key value on Serial (use Serial Monitor to see it!) |
| 70 | + } |
| 71 | + Keypad.longpressTick(); // Tick the long-press detector in BC_key_scan library |
| 72 | + delay(50); // delay 50ms |
| 73 | +} |
0 commit comments