Skip to content

Commit e72a9eb

Browse files
authored
Merge pull request #3280 from HiFiPhile/hid_stylus
Fix HID stylus descriptor and hid_composite example
2 parents f8ef71d + afdfb08 commit e72a9eb

File tree

2 files changed

+26
-45
lines changed

2 files changed

+26
-45
lines changed

examples/device/hid_composite/src/main.c

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -195,41 +195,30 @@ static void send_hid_report(uint8_t report_id, uint32_t btn)
195195
}
196196
}
197197
break;
198-
default: break;
199-
}
200-
}
201-
202-
/* use this to send stylus touch signal through USB. */
203-
static void send_stylus_touch(uint16_t x, uint16_t y, bool state)
204-
{
205-
// skip if hid is not ready yet
206-
if ( !tud_hid_ready() ) return;
207-
208-
static bool has_stylus_pen = false;
209-
210-
hid_stylus_report_t report =
211-
{
212-
.attr = 0,
213-
.x = 0,
214-
.y = 0
215-
};
216-
217-
report.x = x;
218-
report.y = y;
219198

220-
if (state)
221-
{
222-
report.attr = STYLUS_ATTR_TIP_SWITCH | STYLUS_ATTR_IN_RANGE;
223-
tud_hid_report(REPORT_ID_STYLUS_PEN, &report, sizeof(report));
199+
case REPORT_ID_STYLUS_PEN: {
200+
static bool touch_state = false;
201+
hid_stylus_report_t report = {
202+
.attr = 0,
203+
.x = 0,
204+
.y = 0
205+
};
224206

225-
has_stylus_pen = true;
226-
}else
227-
{
228-
report.attr = 0;
229-
if (has_stylus_pen) tud_hid_report(REPORT_ID_STYLUS_PEN, &report, sizeof(report));
230-
has_stylus_pen = false;
207+
if (btn) {
208+
report.attr = STYLUS_ATTR_TIP_SWITCH | STYLUS_ATTR_IN_RANGE;
209+
report.x = 100;
210+
report.y = 100;
211+
tud_hid_report(REPORT_ID_STYLUS_PEN, &report, sizeof(report));
212+
touch_state = true;
213+
} else {
214+
report.attr = 0;
215+
if (touch_state) tud_hid_report(REPORT_ID_STYLUS_PEN, &report, sizeof(report));
216+
touch_state = false;
217+
}
218+
}
219+
break;
220+
default: break;
231221
}
232-
233222
}
234223

235224
// Every 10ms, we will sent 1 report for each HID profile (keyboard, mouse etc ..)
@@ -239,14 +228,6 @@ void hid_task(void)
239228
// Poll every 10ms
240229
const uint32_t interval_ms = 10;
241230
static uint32_t start_ms = 0;
242-
static uint32_t touch_ms = 0;
243-
static bool touch_state = false;
244-
245-
if (board_millis() - touch_ms < 100) {
246-
touch_ms = board_millis();
247-
send_stylus_touch(0, 0, touch_state = !touch_state);
248-
return;
249-
}
250231

251232
if ( board_millis() - start_ms < interval_ms) return; // not enough time
252233
start_ms += interval_ms;

src/class/hid/hid_device.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,14 @@ void tud_hid_report_failed_cb(uint8_t instance, hid_report_type_t report_type, u
267267
// Stylus Pen Report Descriptor Template
268268
#define TUD_HID_REPORT_DESC_STYLUS_PEN(...) \
269269
HID_USAGE_PAGE ( HID_USAGE_PAGE_DIGITIZER ) , \
270-
HID_USAGE ( HID_USAGE_DIGITIZER_TOUCH_SCREEN ) , \
270+
HID_USAGE ( HID_USAGE_DIGITIZER_PEN ) , \
271271
HID_COLLECTION ( HID_COLLECTION_APPLICATION ) , \
272272
/* Report ID if any */\
273273
__VA_ARGS__ \
274-
HID_USAGE ( HID_USAGE_DIGITIZER_STYLUS ) , \
275-
HID_COLLECTION ( HID_COLLECTION_PHYSICAL ) , \
276-
HID_USAGE_PAGE ( HID_USAGE_DIGITIZER_TIP_SWITCH ) , \
277-
HID_USAGE_PAGE ( HID_USAGE_DIGITIZER_IN_RANGE ) , \
274+
HID_USAGE ( HID_USAGE_DIGITIZER_STYLUS ), \
275+
HID_COLLECTION ( HID_COLLECTION_PHYSICAL ), \
276+
HID_USAGE ( HID_USAGE_DIGITIZER_TIP_SWITCH ), \
277+
HID_USAGE ( HID_USAGE_DIGITIZER_IN_RANGE ), \
278278
HID_LOGICAL_MIN ( 0 ), \
279279
HID_LOGICAL_MAX ( 1 ), \
280280
HID_REPORT_SIZE ( 1 ), \

0 commit comments

Comments
 (0)