Skip to content

Commit

Permalink
Add docs to multi device funcs & tweak name (#55)
Browse files Browse the repository at this point in the history
- Formatting
  • Loading branch information
simon-wh authored Jan 3, 2023
1 parent 5152e45 commit 1729f05
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
59 changes: 35 additions & 24 deletions src/wooting-usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ static uint16_t getCrc16ccitt(const uint8_t *buffer, uint16_t size) {
}

typedef void (*set_meta_func)();
void walk_hid_devices(struct hid_device_info* hid_info_walker, set_meta_func meta_func);
void walk_hid_devices(struct hid_device_info *hid_info_walker,
set_meta_func meta_func);

static void reset_meta(WOOTING_USB_META *device_meta) {
device_meta->connected = false;
Expand Down Expand Up @@ -229,7 +230,8 @@ bool wooting_usb_find_keyboard() {
// }

// catch handle being empty despite having found keyboards
if (!keyboard_handle) wooting_usb_select_device(0);
if (!keyboard_handle)
wooting_usb_select_device(0);
return true;
} else {
#ifdef DEBUG_LOG
Expand All @@ -254,29 +256,29 @@ bool wooting_usb_find_keyboard() {

#define PID_ALT_CHECK(base_pid) \
(hid_info = hid_enumerate(WOOTING_VID2, base_pid | V2_ALT_PID_0)) != NULL || \
(hid_info = hid_enumerate(WOOTING_VID2, base_pid | V2_ALT_PID_1)) != \
NULL || \
(hid_info = hid_enumerate(WOOTING_VID2, base_pid | V2_ALT_PID_2)) != \
NULL
(hid_info = hid_enumerate(WOOTING_VID2, base_pid | V2_ALT_PID_1)) != \
NULL || \
(hid_info = hid_enumerate(WOOTING_VID2, base_pid | V2_ALT_PID_2)) != \
NULL

if ((hid_info = hid_enumerate(WOOTING_VID, WOOTING_ONE_PID)) != NULL) {
#ifdef DEBUG_LOG
printf("Enumerate on Wooting One Successful\n");
#endif
walk_hid_devices(hid_info, set_meta_wooting_one);
}
}
if (PID_ALT_CHECK(WOOTING_ONE_V2_PID)) {
#ifdef DEBUG_LOG
printf("Enumerate on Wooting One (V2) Successful\n");
#endif
walk_hid_devices(hid_info, set_meta_wooting_one_v2);
}
}
if ((hid_info = hid_enumerate(WOOTING_VID, WOOTING_TWO_PID)) != NULL) {
#ifdef DEBUG_LOG
printf("Enumerate on Wooting Two Successful\n");
#endif
walk_hid_devices(hid_info, set_meta_wooting_two);
}
}
if (PID_ALT_CHECK(WOOTING_TWO_V2_PID)) {
#ifdef DEBUG_LOG
printf("Enumerate on Wooting Two (V2) Successful\n");
Expand Down Expand Up @@ -308,7 +310,7 @@ bool wooting_usb_find_keyboard() {
}

enumerating = false;

if (connected_keyboards == 0) {
#ifdef DEBUG_LOG
printf("Enumerate failed\n");
Expand All @@ -325,11 +327,13 @@ bool wooting_usb_find_keyboard() {
return connected_keyboards > 0;
}

void walk_hid_devices(struct hid_device_info *hid_info_walker, set_meta_func meta_func) {
void walk_hid_devices(struct hid_device_info *hid_info_walker,
set_meta_func meta_func) {
// We can just search for the interface with matching custom Wooting Cfg usage
// page
while (hid_info_walker) {
if (connected_keyboards == WOOTING_MAX_RGB_DEVICES) break;
if (connected_keyboards == WOOTING_MAX_RGB_DEVICES)
break;
#ifdef DEBUG_LOG
printf("Found interface No: %d\n", hid_info_walker->interface_number);
printf("Found usage page: %d\n", hid_info_walker->usage_page);
Expand All @@ -350,24 +354,25 @@ void walk_hid_devices(struct hid_device_info *hid_info_walker, set_meta_func met
meta_func(&wooting_usb_meta_array[connected_keyboards]);
(&wooting_usb_meta_array[connected_keyboards])->connected = true;

// Any feature sends need to be done after the meta is set so the correct value
// for v2_interface is set
// Any feature sends need to be done after the meta is set so the
// correct value for v2_interface is set

// Once the keyboard is found send an init command
#ifdef DEBUG_LOG
bool result =
#endif
wooting_usb_send_feature(WOOTING_COLOR_INIT_COMMAND, 0, 0, 0, 0);
wooting_usb_send_feature(WOOTING_COLOR_INIT_COMMAND, 0, 0, 0, 0);
#ifdef DEBUG_LOG
printf("Color init result: %d\n", result);
#endif

(&wooting_usb_meta_array[connected_keyboards])->layout = wooting_usb_get_layout();
(&wooting_usb_meta_array[connected_keyboards])->layout =
wooting_usb_get_layout();

// Increment found keyboard count and switch to the next element in the array
// Increment found keyboard count and switch to the next element in the
// array
connected_keyboards++;
}
else {
} else {
#ifdef DEBUG_LOG
printf("No Keyboard handle: %S\n", hid_error(NULL));
#endif
Expand All @@ -381,30 +386,36 @@ void walk_hid_devices(struct hid_device_info *hid_info_walker, set_meta_func met

bool wooting_usb_select_device(uint8_t device_index) {
// Only change device if the given index is valid
if (device_index < 0 || device_index >= WOOTING_MAX_RGB_DEVICES || (device_index >= connected_keyboards && !enumerating)) return false;
if (device_index < 0 || device_index >= WOOTING_MAX_RGB_DEVICES ||
(device_index >= connected_keyboards && !enumerating))
return false;

// Fetch pointer and meta data from arrays
keyboard_handle = keyboard_handle_array[device_index];
wooting_usb_meta = &wooting_usb_meta_array[device_index];
// Initilize meta data should it somehow be empty
if (wooting_usb_meta->model == NULL) reset_meta(wooting_usb_meta);
if (wooting_usb_meta->model == NULL)
reset_meta(wooting_usb_meta);

#ifdef DEBUG_LOG
printf("Keyboard handle: %p | Model: %s\n", keyboard_handle, wooting_usb_meta->model);
printf("Keyboard handle: %p | Model: %s\n", keyboard_handle,
wooting_usb_meta->model);
#endif

return true;
}

WOOTING_USB_META *wooting_usb_get_device_meta(uint8_t device_index) {
// Only change device if the given index is valid
if (device_index < 0 || device_index >= WOOTING_MAX_RGB_DEVICES || (device_index >= connected_keyboards && !enumerating)) return NULL;
if (device_index < 0 || device_index >= WOOTING_MAX_RGB_DEVICES ||
(device_index >= connected_keyboards && !enumerating))
return NULL;

// Fetch pointer and meta data from arrays
return &wooting_usb_meta_array[device_index];
}

uint8_t wooting_usb_keyboard_count() { return connected_keyboards; }
uint8_t wooting_usb_device_count() { return connected_keyboards; }

bool wooting_usb_send_buffer_v1(RGB_PARTS part_number, uint8_t rgb_buffer[]) {
if (!wooting_usb_find_keyboard()) {
Expand Down
17 changes: 15 additions & 2 deletions src/wooting-usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,23 @@ void wooting_usb_disconnect(bool trigger_cb);
bool wooting_usb_find_keyboard(void);

WOOTING_USB_META *wooting_usb_get_meta(void);
WOOTINGRGBSDK_API WOOTING_USB_META *wooting_usb_get_device_meta(uint8_t device_index);

WOOTINGRGBSDK_API uint8_t wooting_usb_keyboard_count(void);
/// @brief Gets the meta struct of a particular device
/// @param device_index Index of the device you want the meta of
/// @return Pointer to the meta struct of the device, NULL if out of range
WOOTINGRGBSDK_API WOOTING_USB_META *
wooting_usb_get_device_meta(uint8_t device_index);

/// @brief Returns the number of devices connected
/// @return The number of devices connected
WOOTINGRGBSDK_API uint8_t wooting_usb_device_count(void);

/// @brief Selects a particular device as the receiver of following commands
/// @param device_index The index of the device to select
/// @return true if the device was selected, false if the device index was out
/// of range
WOOTINGRGBSDK_API bool wooting_usb_select_device(uint8_t);

WOOTINGRGBSDK_API bool wooting_usb_use_v2_interface(void);
WOOTINGRGBSDK_API size_t wooting_usb_get_response_size(void);

Expand Down

0 comments on commit 1729f05

Please sign in to comment.