-
Notifications
You must be signed in to change notification settings - Fork 20
/
Emoji_Button.ino
92 lines (66 loc) · 1.97 KB
/
Emoji_Button.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
Emoji Button
This example sends an emoji character over USB HID when the button is pressed.
Note: Only macOS and Linux as supported at this time, and the use of
#define is generally discouraged in Arduino examples
The circuit:
- Arduino Nano 33 BLE or Arduino Nano 33 BLE Sense board.
- Button connected to pin 3 and GND.
Created by Don Coleman, Sandeep Mistry
This example code is in the public domain.
*/
#include <PluggableUSBHID.h>
#include <USBKeyboard.h>
// Select an OS:
//#define MACOS // You'll need to enable and select the unicode keyboard: System Preferences -> Input Sources -> + -> Others -> Unicode Hex Input
//#define LINUX
#if !defined(MACOS) && !defined(LINUX)
#error "Please select an OS!"
#endif
// use table: https://apps.timwhitlock.info/emoji/tables/unicode
const int bicep = 0x1f4aa;
const int punch = 0x1f44a;
const int buttonPin = 3;
USBKeyboard keyboard;
int previousButtonState = HIGH;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState != previousButtonState) {
if (buttonState == LOW) {
// pressed
sentUtf8(bicep);
} else {
// released
}
previousButtonState = buttonState;
}
}
void sentUtf8(unsigned long c) {
String s;
#if defined(MACOS)
// https://apple.stackexchange.com/questions/183045/how-can-i-type-unicode-characters-without-using-the-mouse
s = String(utf8ToUtf16(c), HEX);
for (int i = 0; i < s.length(); i++) {
keyboard.key_code(s[i], KEY_ALT);
}
#elif defined(LINUX)
s = String(c, HEX);
keyboard.key_code('u', KEY_CTRL | KEY_SHIFT);
for (int i = 0; i < s.length(); i++) {
keyboard.key_code(s[i]);
}
#endif
keyboard.key_code(' ');
}
// based on https://stackoverflow.com/a/6240819/2020087
unsigned long utf8ToUtf16(unsigned long in) {
unsigned long result;
in -= 0x10000;
result |= (in & 0x3ff);
result |= (in << 6) & 0x03ff0000;
result |= 0xd800dc00;
return result;
}