Skip to content

Commit bddca70

Browse files
committed
Improve generic HID endpoint slightly
1 parent 9d46dba commit bddca70

1 file changed

Lines changed: 30 additions & 18 deletions

File tree

xwhatsit_core/util_comm.c

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@
2424
#include "avr/eeprom.h"
2525
#include "generic_hid.h"
2626

27+
bool matrix_scan_custom(matrix_row_t current_matrix[]);
28+
2729
#if GENERIC_HID_REPORT_SIZE < 32
2830
#error "GENERIC_HID_REPORT_SIZE is too small, see util_comm.c"
2931
#endif
3032

33+
#undef RAW_EPSIZE
34+
#define RAW_EPSIZE GENERIC_HID_REPORT_SIZE
35+
3136
#define min(x, y) (((x) < (y))?(x):(y))
3237

3338
#define STRIFY(a) #a
@@ -36,7 +41,6 @@
3641
#ifdef KEYBOARD_NAME
3742
static const char PROGMEM KEYBOARD_FILENAME[] = STR(KEYBOARD_NAME)".c";
3843
#endif
39-
extern matrix_row_t raw_matrix[MATRIX_ROWS];
4044

4145
static const uint8_t magic[] = UTIL_COMM_MAGIC;
4246

@@ -74,15 +78,18 @@ uint8_t handle_generic_hid_report(uint8_t report_id, uint8_t count, uint8_t data
7478
case UTIL_COMM_GET_KEYSTATE:
7579
response[2] = UTIL_COMM_RESPONSE_OK;
7680
{
77-
char *current_matrix_ptr = (char *) raw_matrix;
81+
matrix_row_t current_matrix[MATRIX_ROWS];
82+
(void) matrix_scan_custom(current_matrix);
83+
char *current_matrix_ptr = (char *)current_matrix;
7884
int offset = 0;
79-
if ((uint8_t) sizeof(raw_matrix) > *response_length - 3) {
85+
if (sizeof(current_matrix) > *response_length - 3)
86+
{
8087
offset = data[3];
8188
current_matrix_ptr += offset;
8289
}
83-
*response_length = min(*response_length - 3, (uint8_t) sizeof(raw_matrix) - offset);
84-
memcpy(&response[3], current_matrix_ptr, *response_length);
85-
*response_length += 3;
90+
const uint8_t count = min(*response_length - 3, sizeof(current_matrix) - offset);
91+
memcpy(&response[3], current_matrix_ptr, count);
92+
*response_length = 3 + count;
8693
}
8794
break;
8895
case UTIL_COMM_GET_THRESHOLDS:
@@ -99,9 +106,9 @@ uint8_t handle_generic_hid_report(uint8_t report_id, uint8_t count, uint8_t data
99106
offset = data[4];
100107
assigned_to_threshold_ptr += offset;
101108
}
102-
*response_length = min(*response_length - 6, (uint8_t) sizeof(assigned_to_threshold[cal_bin]) - offset);
103-
memcpy(&response[6], assigned_to_threshold_ptr, *response_length);
104-
*response_length += 6;
109+
const uint8_t count = min(*response_length - 6, sizeof(assigned_to_threshold) - offset);
110+
memcpy(&response[6], assigned_to_threshold_ptr, count);
111+
*response_length = count + 6;
105112
}
106113
#else
107114
response[3] = 0;
@@ -113,16 +120,19 @@ uint8_t handle_generic_hid_report(uint8_t report_id, uint8_t count, uint8_t data
113120
case UTIL_COMM_GET_KEYBOARD_FILENAME:
114121
{
115122
int string_length = sizeof(KEYBOARD_FILENAME);
123+
const uint8_t offset = data[3];
116124
response[2] = UTIL_COMM_RESPONSE_OK;
117-
if (data[3] >= string_length) {
125+
if (offset >= string_length) {
118126
response[3] = 0;
119127
*response_length = 4;
120128
} else {
121-
const char *substring = KEYBOARD_FILENAME + data[3];
122-
string_length -= data[3];
123-
*response_length = min(*response_length - 3, string_length);
129+
const char *substring = KEYBOARD_FILENAME + offset;
130+
string_length -= offset;
131+
if (string_length > *response_length - 3) {
132+
string_length = *response_length - 3;
133+
}
124134
memcpy_P(&response[3], substring, string_length);
125-
*response_length += 3;
135+
*response_length = string_length + 3;
126136
}
127137
break;
128138
}
@@ -149,11 +159,13 @@ uint8_t handle_generic_hid_report(uint8_t report_id, uint8_t count, uint8_t data
149159
uint16_t value = measure_middle_keymap_coords(col, row, CAPSENSE_HARDCODED_SAMPLE_TIME, 8);
150160
response[3 + i*2] = value & 0xff;
151161
response[3 + i*2 + 1] = (value >> 8) & 0xff;
152-
if (++col >= MATRIX_COLS) {
162+
++col;
163+
if (col >= MATRIX_COLS) {
153164
col -= MATRIX_COLS;
154-
if (++row >= MATRIX_CAPSENSE_ROWS) {
155-
break;
156-
}
165+
++row;
166+
}
167+
if (row >= MATRIX_CAPSENSE_ROWS) {
168+
break;
157169
}
158170
}
159171
break;

0 commit comments

Comments
 (0)