質問編集履歴
2
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -179,4 +179,17 @@
|
|
179
179
|
Usb.Task();
|
180
180
|
}
|
181
181
|
|
182
|
+
```
|
183
|
+
|
184
|
+
###追記
|
185
|
+
・12/20 一度変数に格納することでコンパイル成功、どのキーを押しても169と表示されるので、1byte×8個の表記にするにはどうすればいいか、どこに問題があるのか模索中。
|
186
|
+
```Arduino
|
187
|
+
Serial.println(&keyReport);//コンパイル時にエラー
|
188
|
+
|
189
|
+
//↓一度変数に格納するとコンパイル成功
|
190
|
+
|
191
|
+
uint8_t a =&keyReport;
|
192
|
+
Serial.println(&keyReport);//169 の数値が固定でログに表示される。
|
193
|
+
|
194
|
+
|
182
195
|
```
|
1
参考とさせて頂いてるコードを追記。
title
CHANGED
File without changes
|
body
CHANGED
@@ -33,4 +33,150 @@
|
|
33
33
|
|
34
34
|
###質問者のプログラミング経験
|
35
35
|
VBA,GAS,UWSCと操作の自動化でしか取り扱いの経験がなく、
|
36
|
-
Arduino(C/C++)言語は初めてまだ日が浅いため、修練中の身です。
|
36
|
+
Arduino(C/C++)言語は初めてまだ日が浅いため、修練中の身です。
|
37
|
+
###参考とさせて頂いてるコード
|
38
|
+
```Arduino
|
39
|
+
#include "HID.h"
|
40
|
+
|
41
|
+
#include <hidboot.h>
|
42
|
+
#include <usbhub.h>
|
43
|
+
#include <SPI.h>
|
44
|
+
|
45
|
+
static const uint8_t _hidReportDescriptor[] PROGMEM = {
|
46
|
+
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
|
47
|
+
0x09, 0x06, // USAGE (Keyboard)
|
48
|
+
0xa1, 0x01, // COLLECTION (Application)
|
49
|
+
0x85, 0x02, // REPORT_ID (2) --- Mouse.cppがID==1。
|
50
|
+
|
51
|
+
0x05, 0x07, // USAGE_PAGE (usage = keyboard page)
|
52
|
+
// モデファイヤキー(修飾キー)
|
53
|
+
0x19, 0xe0, // USAGE_MINIMUM (左CTRLが0xe0)
|
54
|
+
0x29, 0xe7, // USAGE_MAXIMUM (右GUIが0xe7)
|
55
|
+
0x15, 0x00, // LOGICAL_MINIMUM (0)
|
56
|
+
0x25, 0x01, // LOGICAL_MAXIMUM (1)
|
57
|
+
0x95, 0x08, // REPORT_COUNT (8) 全部で8つ(左右4つずつ)。
|
58
|
+
0x75, 0x01, // REPORT_SIZE (1) 各修飾キーにつき1ビット
|
59
|
+
0x81, 0x02, // INPUT (Data,Var,Abs) 8ビット長のInputフィールド(変数)が1つ。
|
60
|
+
// 予約フィールド
|
61
|
+
0x95, 0x01, // REPORT_COUNT (1)
|
62
|
+
0x75, 0x08, // REPORT_SIZE (8) 1ビットが8つ。
|
63
|
+
0x81, 0x01, // INPUT (Cnst,Var,Abs)
|
64
|
+
// LED状態のアウトプット
|
65
|
+
0x95, 0x05, // REPORT_COUNT (5) 全部で5つ。
|
66
|
+
0x75, 0x01, // REPORT_SIZE (1) 各LEDにつき1ビット
|
67
|
+
0x05, 0x08, // USAGE_PAGE (LEDs)
|
68
|
+
0x19, 0x01, // USAGE_MINIMUM (1) (NumLock LEDが1)
|
69
|
+
0x29, 0x05, // USAGE_MAXIMUM (5) (KANA LEDが5)
|
70
|
+
0x91, 0x02, // OUTPUT (Data,Var,Abs) // LED report
|
71
|
+
// LEDレポートのパディング
|
72
|
+
0x95, 0x01, // REPORT_COUNT (1)
|
73
|
+
0x75, 0x03, // REPORT_SIZE (3) 残りの3ビットを埋める。
|
74
|
+
0x91, 0x01, // OUTPUT (Cnst,Var,Abs) // padding
|
75
|
+
// 押下情報のインプット
|
76
|
+
0x95, 0x06, // REPORT_COUNT (6) 全部で6つ。
|
77
|
+
0x75, 0x08, // REPORT_SIZE (8) おのおの8ビットで表現
|
78
|
+
0x15, 0x00, // LOGICAL_MINIMUM (0) キーコードの範囲は、
|
79
|
+
0x25, 0xdd, // LOGICAL_MAXIMUM (221) 0~221(0xdd)まで
|
80
|
+
|
81
|
+
0x05, 0x07, // USAGE_PAGE (Keyboard)
|
82
|
+
0x19, 0x00, // USAGE_MINIMUM (0はキーコードではない)
|
83
|
+
0x29, 0xdd, // USAGE_MAXIMUM (Keypad Hexadecimalまで)
|
84
|
+
0x81, 0x00, // INPUT (Data,Ary,Abs)
|
85
|
+
0xc0 // END_COLLECTION
|
86
|
+
};
|
87
|
+
|
88
|
+
#define REPORT_KEYS 6
|
89
|
+
typedef struct {
|
90
|
+
uint8_t modifiers;
|
91
|
+
uint8_t reserved;
|
92
|
+
uint8_t keys[REPORT_KEYS];
|
93
|
+
} KeyReport;
|
94
|
+
|
95
|
+
KeyReport keyReport;
|
96
|
+
|
97
|
+
void sendReport() {
|
98
|
+
HID().SendReport(2, &keyReport ,sizeof(KeyReport));
|
99
|
+
}
|
100
|
+
|
101
|
+
void releaseAll() {
|
102
|
+
memset(&keyReport, 0, sizeof(KeyReport));
|
103
|
+
sendReport();
|
104
|
+
}
|
105
|
+
|
106
|
+
void report_press(uint8_t key, uint8_t mod) {
|
107
|
+
if (key != 0) {
|
108
|
+
bool already = false;
|
109
|
+
int empty_slot = -1;
|
110
|
+
for(int i = 0; i < REPORT_KEYS; i++) {
|
111
|
+
if (keyReport.keys[i] == key)
|
112
|
+
already = true;
|
113
|
+
if (keyReport.keys[i] == 0 && empty_slot < 0)
|
114
|
+
empty_slot = i;
|
115
|
+
}
|
116
|
+
if (empty_slot < 0) // error condition.
|
117
|
+
return;
|
118
|
+
if (!already)
|
119
|
+
keyReport.keys[empty_slot] = key;
|
120
|
+
}
|
121
|
+
keyReport.modifiers = mod;
|
122
|
+
sendReport();
|
123
|
+
}
|
124
|
+
|
125
|
+
void report_release(uint8_t key, uint8_t mod) {
|
126
|
+
if (key != 0) {
|
127
|
+
for(int i = 0; i < REPORT_KEYS; i++) {
|
128
|
+
if (keyReport.keys[i] == key) {
|
129
|
+
keyReport.keys[i] = 0;
|
130
|
+
break;
|
131
|
+
}
|
132
|
+
}
|
133
|
+
}
|
134
|
+
keyReport.modifiers = mod;
|
135
|
+
sendReport();
|
136
|
+
}
|
137
|
+
|
138
|
+
class KeyboardEvent : public KeyboardReportParser {
|
139
|
+
protected:
|
140
|
+
void OnControlKeysChanged(uint8_t before, uint8_t after);
|
141
|
+
void OnKeyPressed(uint8_t key) {};
|
142
|
+
void OnKeyDown (uint8_t mod, uint8_t key) { report_press(key, mod); };
|
143
|
+
void OnKeyUp (uint8_t mod, uint8_t key) { report_release(key, mod); };
|
144
|
+
};
|
145
|
+
|
146
|
+
void KeyboardEvent::OnControlKeysChanged(uint8_t before, uint8_t after) {
|
147
|
+
uint8_t change = before ^ after;
|
148
|
+
if (change != 0) {
|
149
|
+
if (change & after)
|
150
|
+
report_press(0, after);
|
151
|
+
else
|
152
|
+
report_release(0, after);
|
153
|
+
}
|
154
|
+
}
|
155
|
+
|
156
|
+
USB Usb;
|
157
|
+
HIDBoot<USB_HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);
|
158
|
+
KeyboardEvent kbd;
|
159
|
+
|
160
|
+
void error_blink(int period) {
|
161
|
+
for(;;) {
|
162
|
+
TXLED1; delay(period); TXLED0; delay(period);
|
163
|
+
RXLED1; delay(period); RXLED0; delay(period);
|
164
|
+
}
|
165
|
+
}
|
166
|
+
|
167
|
+
void setup() {
|
168
|
+
static HIDSubDescriptor node(_hidReportDescriptor, sizeof(_hidReportDescriptor));
|
169
|
+
HID().AppendDescriptor(&node);
|
170
|
+
delay(200);
|
171
|
+
releaseAll();
|
172
|
+
if (Usb.Init() == -1)
|
173
|
+
error_blink(400);
|
174
|
+
delay( 200 );
|
175
|
+
HidKeyboard.SetReportParser(0, &kbd);
|
176
|
+
}
|
177
|
+
|
178
|
+
void loop() {
|
179
|
+
Usb.Task();
|
180
|
+
}
|
181
|
+
|
182
|
+
```
|