From 80fcc06d3c4cacf0372b277d032606229bb186c8 Mon Sep 17 00:00:00 2001 From: simon-wh Date: Thu, 21 Oct 2021 11:20:03 +0100 Subject: [PATCH] Handle 2 alt modes & format files with clang-format --- src/wooting-rgb-sdk.c | 558 +++++++++++----------- src/wooting-rgb-sdk.h | 98 ++-- src/wooting-usb.c | 1024 +++++++++++++++++++++-------------------- src/wooting-usb.h | 4 +- 4 files changed, 867 insertions(+), 817 deletions(-) diff --git a/src/wooting-rgb-sdk.c b/src/wooting-rgb-sdk.c index 8e8c48c..7ce662c 100644 --- a/src/wooting-rgb-sdk.c +++ b/src/wooting-rgb-sdk.c @@ -1,12 +1,10 @@ /* -* Copyright 2018 Wooting Technologies B.V. -* -* This Source Code Form is subject to the terms of the Mozilla Public -* License, v. 2.0. If a copy of the MPL was not distributed with this -* file, You can obtain one at http://mozilla.org/MPL/2.0/. -*/ -#include "stdbool.h" -#include "stdint.h" + * Copyright 2018 Wooting Technologies B.V. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ #include "wooting-rgb-sdk.h" #define NOLED 255 @@ -19,11 +17,11 @@ static bool wooting_rgb_auto_update = false; // Each rgb buffer is able to hold RGB values for 24 keys // There is some overhead because of the memory layout of the LED drivers -static uint8_t rgb_buffer0[RGB_RAW_BUFFER_SIZE] = { 0 }; -static uint8_t rgb_buffer1[RGB_RAW_BUFFER_SIZE] = { 0 }; -static uint8_t rgb_buffer2[RGB_RAW_BUFFER_SIZE] = { 0 }; -static uint8_t rgb_buffer3[RGB_RAW_BUFFER_SIZE] = { 0 }; -static uint8_t rgb_buffer4[RGB_RAW_BUFFER_SIZE] = { 0 }; +static uint8_t rgb_buffer0[RGB_RAW_BUFFER_SIZE] = {0}; +static uint8_t rgb_buffer1[RGB_RAW_BUFFER_SIZE] = {0}; +static uint8_t rgb_buffer2[RGB_RAW_BUFFER_SIZE] = {0}; +static uint8_t rgb_buffer3[RGB_RAW_BUFFER_SIZE] = {0}; +static uint8_t rgb_buffer4[RGB_RAW_BUFFER_SIZE] = {0}; static bool rgb_buffer0_changed = false; static bool rgb_buffer1_changed = false; @@ -56,305 +54,309 @@ static bool rgb_buffer_matrix_changed = false; // Converts the array index to a memory location in the RGB buffers static uint8_t get_safe_led_idex(uint8_t row, uint8_t column) { - const uint8_t rgb_led_index[WOOTING_RGB_ROWS][WOOTING_RGB_COLS] = { - { 0, NOLED, 11, 12, 23, 24, 36, 47, 85, 84, 49, 48, 59, 61, 73, 81, 80, 113, 114, 115, 116 }, - { 2, 1, 14, 13, 26, 25, 35, 38, 37, 87, 86, 95, 51, 63, 75, 72, 74, 96, 97, 98, 99 }, - { 3, 4, 15, 16, 27, 28, 39, 42, 40, 88, 89, 52, 53, 71, 76, 83, 77, 102, 103, 104, 100 }, - { 5, 6, 17, 18, 29, 30, 41, 46, 44, 90, 93, 54, 57, 65, NOLED, NOLED, NOLED, 105, 106, 107, NOLED }, - { 9, 8, 19, 20, 31, 34, 32, 45, 43, 91, 92, 55, NOLED, 66, NOLED, 78, NOLED, 108, 109, 110, 101 }, - { 10, 22, 21, NOLED, NOLED, NOLED, 33, NOLED, NOLED, NOLED, 94, 58, 67, 68, 70, 79, 82, NOLED, 111, 112, NOLED } - }; - - WOOTING_USB_META* meta = wooting_usb_get_meta(); - if (row < meta->max_rows && column < meta->max_columns) { - return rgb_led_index[row][column]; - } else { - return NOLED; - } + const uint8_t rgb_led_index[WOOTING_RGB_ROWS][WOOTING_RGB_COLS] = { + {0, NOLED, 11, 12, 23, 24, 36, 47, 85, 84, 49, + 48, 59, 61, 73, 81, 80, 113, 114, 115, 116}, + {2, 1, 14, 13, 26, 25, 35, 38, 37, 87, 86, + 95, 51, 63, 75, 72, 74, 96, 97, 98, 99}, + {3, 4, 15, 16, 27, 28, 39, 42, 40, 88, 89, + 52, 53, 71, 76, 83, 77, 102, 103, 104, 100}, + {5, 6, 17, 18, 29, 30, 41, 46, 44, 90, 93, + 54, 57, 65, NOLED, NOLED, NOLED, 105, 106, 107, NOLED}, + {9, 8, 19, 20, 31, 34, 32, 45, 43, 91, 92, + 55, NOLED, 66, NOLED, 78, NOLED, 108, 109, 110, 101}, + {10, 22, 21, NOLED, NOLED, NOLED, 33, NOLED, NOLED, NOLED, 94, + 58, 67, 68, 70, 79, 82, NOLED, 111, 112, NOLED}}; + + WOOTING_USB_META *meta = wooting_usb_get_meta(); + if (row < meta->max_rows && column < meta->max_columns) { + return rgb_led_index[row][column]; + } else { + return NOLED; + } } -static inline uint16_t encodeColor(uint8_t red, uint8_t green, uint8_t blue) -{ - uint16_t encode = 0; +static inline uint16_t encodeColor(uint8_t red, uint8_t green, uint8_t blue) { + uint16_t encode = 0; - encode |= (red & 0xf8) << 8; - encode |= (green & 0xfc) << 3; - encode |= (blue & 0xf8) >> 3; + encode |= (red & 0xf8) << 8; + encode |= (green & 0xfc) << 3; + encode |= (blue & 0xf8) >> 3; - return encode; + return encode; } -bool wooting_rgb_kbd_connected() { - return wooting_usb_find_keyboard(); -} +bool wooting_rgb_kbd_connected() { return wooting_usb_find_keyboard(); } void wooting_rgb_set_disconnected_cb(void_cb cb) { - wooting_usb_set_disconnected_cb(cb); + wooting_usb_set_disconnected_cb(cb); } bool wooting_rgb_reset_rgb() { - return wooting_usb_send_feature(WOOTING_RESET_ALL_COMMAND, 0, 0, 0, 0); + return wooting_usb_send_feature(WOOTING_RESET_ALL_COMMAND, 0, 0, 0, 0); } - bool wooting_rgb_close() { - if (wooting_rgb_reset_rgb()) { - wooting_usb_disconnect(false); - return true; - } - else { - return false; - } -} - -bool wooting_rgb_reset() { - return wooting_rgb_close(); + if (wooting_rgb_reset_rgb()) { + wooting_usb_disconnect(false); + return true; + } else { + return false; + } } -bool wooting_rgb_direct_set_key(uint8_t row, uint8_t column, uint8_t red, uint8_t green, uint8_t blue) { - // We don't need to call this here as the wooting_usb_send_feature calls will perform the check again and there's no need to - // run this multiple times, especially when each call will attempt to ensure connection is still available - // if (!wooting_rgb_kbd_connected()) { - // return false; - // } - if (wooting_usb_use_v2_interface()){ - KeyboardMatrixID id = {.row = row, .column = column}; - return wooting_usb_send_feature(WOOTING_SINGLE_COLOR_COMMAND, *(uint8_t*)&id, red, green, blue); - } else { - uint8_t keyCode = get_safe_led_idex(row, column); - - if (keyCode == NOLED || keyCode > wooting_usb_get_meta()->led_index_max) { - return false; - } - else if (keyCode == LED_LEFT_SHIFT_ANSI) { - bool update_ansi = wooting_usb_send_feature(WOOTING_SINGLE_COLOR_COMMAND, LED_LEFT_SHIFT_ANSI, red, green, blue); - bool update_iso = wooting_usb_send_feature(WOOTING_SINGLE_COLOR_COMMAND, LED_LEFT_SHIFT_ISO, red, green, blue); - - return update_ansi && update_iso; - } - else if (keyCode == LED_ENTER_ANSI) { - bool update_ansi = wooting_usb_send_feature(WOOTING_SINGLE_COLOR_COMMAND, LED_ENTER_ANSI, red, green, blue); - bool update_iso = wooting_usb_send_feature(WOOTING_SINGLE_COLOR_COMMAND, LED_ENTER_ISO, red, green, blue); - - return update_ansi && update_iso; - } - else { - return wooting_usb_send_feature(WOOTING_SINGLE_COLOR_COMMAND, keyCode, red, green, blue); - } - } +bool wooting_rgb_reset() { return wooting_rgb_close(); } + +bool wooting_rgb_direct_set_key(uint8_t row, uint8_t column, uint8_t red, + uint8_t green, uint8_t blue) { + // We don't need to call this here as the wooting_usb_send_feature calls will + // perform the check again and there's no need to run this multiple times, + // especially when each call will attempt to ensure connection is still + // available if (!wooting_rgb_kbd_connected()) { return false; + // } + if (wooting_usb_use_v2_interface()) { + KeyboardMatrixID id = {.row = row, .column = column}; + return wooting_usb_send_feature(WOOTING_SINGLE_COLOR_COMMAND, + *(uint8_t *)&id, red, green, blue); + } else { + uint8_t keyCode = get_safe_led_idex(row, column); + + if (keyCode == NOLED || keyCode > wooting_usb_get_meta()->led_index_max) { + return false; + } else if (keyCode == LED_LEFT_SHIFT_ANSI) { + bool update_ansi = wooting_usb_send_feature( + WOOTING_SINGLE_COLOR_COMMAND, LED_LEFT_SHIFT_ANSI, red, green, blue); + bool update_iso = wooting_usb_send_feature( + WOOTING_SINGLE_COLOR_COMMAND, LED_LEFT_SHIFT_ISO, red, green, blue); + + return update_ansi && update_iso; + } else if (keyCode == LED_ENTER_ANSI) { + bool update_ansi = wooting_usb_send_feature( + WOOTING_SINGLE_COLOR_COMMAND, LED_ENTER_ANSI, red, green, blue); + bool update_iso = wooting_usb_send_feature( + WOOTING_SINGLE_COLOR_COMMAND, LED_ENTER_ISO, red, green, blue); + + return update_ansi && update_iso; + } else { + return wooting_usb_send_feature(WOOTING_SINGLE_COLOR_COMMAND, keyCode, + red, green, blue); + } + } } bool wooting_rgb_direct_reset_key(uint8_t row, uint8_t column) { - if (!wooting_rgb_kbd_connected()) { - return false; - } - - if (wooting_usb_use_v2_interface()){ - KeyboardMatrixID id = {.row = row, .column = column}; - return wooting_usb_send_feature(WOOTING_SINGLE_RESET_COMMAND, 0, 0, 0, *(uint8_t*)&id); - } else { - uint8_t keyCode = get_safe_led_idex(row, column); - - if (keyCode == NOLED || keyCode > wooting_usb_get_meta()->led_index_max) { - return false; - } - else if (keyCode == LED_LEFT_SHIFT_ANSI) { - bool update_ansi = wooting_usb_send_feature(WOOTING_SINGLE_RESET_COMMAND, 0, 0, 0, LED_LEFT_SHIFT_ANSI); - bool update_iso = wooting_usb_send_feature(WOOTING_SINGLE_RESET_COMMAND, 0, 0, 0, LED_LEFT_SHIFT_ISO); - - return update_ansi && update_iso; - } - else if (keyCode == LED_ENTER_ANSI) { - bool update_ansi = wooting_usb_send_feature(WOOTING_SINGLE_RESET_COMMAND, 0, 0, 0, LED_ENTER_ANSI); - bool update_iso = wooting_usb_send_feature(WOOTING_SINGLE_RESET_COMMAND, 0, 0, 0, LED_ENTER_ISO); - - return update_ansi && update_iso; - } - else { - return wooting_usb_send_feature(WOOTING_SINGLE_RESET_COMMAND, 0, 0, 0, keyCode); - } - } + if (!wooting_rgb_kbd_connected()) { + return false; + } + + if (wooting_usb_use_v2_interface()) { + KeyboardMatrixID id = {.row = row, .column = column}; + return wooting_usb_send_feature(WOOTING_SINGLE_RESET_COMMAND, 0, 0, 0, + *(uint8_t *)&id); + } else { + uint8_t keyCode = get_safe_led_idex(row, column); + + if (keyCode == NOLED || keyCode > wooting_usb_get_meta()->led_index_max) { + return false; + } else if (keyCode == LED_LEFT_SHIFT_ANSI) { + bool update_ansi = wooting_usb_send_feature(WOOTING_SINGLE_RESET_COMMAND, + 0, 0, 0, LED_LEFT_SHIFT_ANSI); + bool update_iso = wooting_usb_send_feature(WOOTING_SINGLE_RESET_COMMAND, + 0, 0, 0, LED_LEFT_SHIFT_ISO); + + return update_ansi && update_iso; + } else if (keyCode == LED_ENTER_ANSI) { + bool update_ansi = wooting_usb_send_feature(WOOTING_SINGLE_RESET_COMMAND, + 0, 0, 0, LED_ENTER_ANSI); + bool update_iso = wooting_usb_send_feature(WOOTING_SINGLE_RESET_COMMAND, + 0, 0, 0, LED_ENTER_ISO); + + return update_ansi && update_iso; + } else { + return wooting_usb_send_feature(WOOTING_SINGLE_RESET_COMMAND, 0, 0, 0, + keyCode); + } + } } void wooting_rgb_array_auto_update(bool auto_update) { - wooting_rgb_auto_update = auto_update; + wooting_rgb_auto_update = auto_update; } bool wooting_rgb_array_update_keyboard() { - if (!wooting_rgb_kbd_connected()) { - return false; - } - - if (wooting_usb_use_v2_interface()) { - if (rgb_buffer_matrix_changed) { - if (!wooting_usb_send_buffer_v2(rgb_buffer_matrix)) { - return false; - } - rgb_buffer_matrix_changed = false; - } - } else { - - if (rgb_buffer0_changed) { - if (!wooting_usb_send_buffer_v1(PART0, rgb_buffer0)) { - return false; - } - rgb_buffer0_changed = false; - } - - if (rgb_buffer1_changed) { - if (!wooting_usb_send_buffer_v1(PART1, rgb_buffer1)) { - return false; - } - rgb_buffer1_changed = false; - } - - if (rgb_buffer2_changed) { - if (!wooting_usb_send_buffer_v1(PART2, rgb_buffer2)) { - return false; - } - rgb_buffer2_changed = false; - } - - if (rgb_buffer3_changed) { - if (!wooting_usb_send_buffer_v1(PART3, rgb_buffer3)) { - return false; - } - rgb_buffer3_changed = false; - } - - if (rgb_buffer4_changed && wooting_usb_get_meta()->device_type == DEVICE_KEYBOARD) { - if (!wooting_usb_send_buffer_v1(PART4, rgb_buffer4)) { - return false; - } - rgb_buffer4_changed = false; - } - } - return true; + if (!wooting_rgb_kbd_connected()) { + return false; + } + + if (wooting_usb_use_v2_interface()) { + if (rgb_buffer_matrix_changed) { + if (!wooting_usb_send_buffer_v2(rgb_buffer_matrix)) { + return false; + } + rgb_buffer_matrix_changed = false; + } + } else { + + if (rgb_buffer0_changed) { + if (!wooting_usb_send_buffer_v1(PART0, rgb_buffer0)) { + return false; + } + rgb_buffer0_changed = false; + } + + if (rgb_buffer1_changed) { + if (!wooting_usb_send_buffer_v1(PART1, rgb_buffer1)) { + return false; + } + rgb_buffer1_changed = false; + } + + if (rgb_buffer2_changed) { + if (!wooting_usb_send_buffer_v1(PART2, rgb_buffer2)) { + return false; + } + rgb_buffer2_changed = false; + } + + if (rgb_buffer3_changed) { + if (!wooting_usb_send_buffer_v1(PART3, rgb_buffer3)) { + return false; + } + rgb_buffer3_changed = false; + } + + if (rgb_buffer4_changed && + wooting_usb_get_meta()->device_type == DEVICE_KEYBOARD) { + if (!wooting_usb_send_buffer_v1(PART4, rgb_buffer4)) { + return false; + } + rgb_buffer4_changed = false; + } + } + return true; } -static bool wooting_rgb_array_change_single(uint8_t row, uint8_t column, uint8_t red, uint8_t green, uint8_t blue) { - if (wooting_usb_use_v2_interface()){ - uint16_t prevValue = rgb_buffer_matrix[row][column]; - uint16_t newValue = encodeColor(red, green, blue); - if (newValue != prevValue) { - rgb_buffer_matrix[row][column] =newValue; - rgb_buffer_matrix_changed = true; - } - - return true; - } else { - - const uint8_t pwm_mem_map[48] = - { - 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, - 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, - 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d - }; - - uint8_t led_index = get_safe_led_idex(row, column); - uint8_t *buffer_pointer; - - // prevent assigning led's that don't exist - if (led_index > wooting_usb_get_meta()->led_index_max) { - return false; - } - if (led_index >= 96) { - buffer_pointer = rgb_buffer4; - rgb_buffer4_changed = true; - } - else if (led_index >= 72) { - buffer_pointer = rgb_buffer3; - rgb_buffer3_changed = true; - } - else if (led_index >= 48) { - buffer_pointer = rgb_buffer2; - rgb_buffer2_changed = true; - } - else if (led_index >= 24) { - buffer_pointer = rgb_buffer1; - rgb_buffer1_changed = true; - } - else { - buffer_pointer = rgb_buffer0; - rgb_buffer0_changed = true; - } - - uint8_t buffer_index = pwm_mem_map[led_index % 24]; - buffer_pointer[buffer_index] = gammaFilter[red]; - buffer_pointer[buffer_index + 0x10] = gammaFilter[green]; - buffer_pointer[buffer_index + 0x20] = gammaFilter[blue]; - - if (led_index == LED_ENTER_ANSI) { - uint8_t iso_enter_index = pwm_mem_map[LED_ENTER_ISO - 48]; - rgb_buffer2[iso_enter_index] = rgb_buffer2[buffer_index]; - rgb_buffer2[iso_enter_index + 0x10] = rgb_buffer2[buffer_index + 0x10]; - rgb_buffer2[iso_enter_index + 0x20] = rgb_buffer2[buffer_index + 0x20]; - } - else if (led_index == LED_LEFT_SHIFT_ANSI) { - uint8_t iso_shift_index = pwm_mem_map[LED_LEFT_SHIFT_ISO]; - rgb_buffer0[iso_shift_index] = rgb_buffer0[buffer_index]; - rgb_buffer0[iso_shift_index + 0x10] = rgb_buffer0[buffer_index + 0x10]; - rgb_buffer0[iso_shift_index + 0x20] = rgb_buffer0[buffer_index + 0x20]; - } - - return true; - } +static bool wooting_rgb_array_change_single(uint8_t row, uint8_t column, + uint8_t red, uint8_t green, + uint8_t blue) { + if (wooting_usb_use_v2_interface()) { + uint16_t prevValue = rgb_buffer_matrix[row][column]; + uint16_t newValue = encodeColor(red, green, blue); + if (newValue != prevValue) { + rgb_buffer_matrix[row][column] = newValue; + rgb_buffer_matrix_changed = true; + } + + return true; + } else { + + const uint8_t pwm_mem_map[48] = { + 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d}; + + uint8_t led_index = get_safe_led_idex(row, column); + uint8_t *buffer_pointer; + + // prevent assigning led's that don't exist + if (led_index > wooting_usb_get_meta()->led_index_max) { + return false; + } + if (led_index >= 96) { + buffer_pointer = rgb_buffer4; + rgb_buffer4_changed = true; + } else if (led_index >= 72) { + buffer_pointer = rgb_buffer3; + rgb_buffer3_changed = true; + } else if (led_index >= 48) { + buffer_pointer = rgb_buffer2; + rgb_buffer2_changed = true; + } else if (led_index >= 24) { + buffer_pointer = rgb_buffer1; + rgb_buffer1_changed = true; + } else { + buffer_pointer = rgb_buffer0; + rgb_buffer0_changed = true; + } + + uint8_t buffer_index = pwm_mem_map[led_index % 24]; + buffer_pointer[buffer_index] = gammaFilter[red]; + buffer_pointer[buffer_index + 0x10] = gammaFilter[green]; + buffer_pointer[buffer_index + 0x20] = gammaFilter[blue]; + + if (led_index == LED_ENTER_ANSI) { + uint8_t iso_enter_index = pwm_mem_map[LED_ENTER_ISO - 48]; + rgb_buffer2[iso_enter_index] = rgb_buffer2[buffer_index]; + rgb_buffer2[iso_enter_index + 0x10] = rgb_buffer2[buffer_index + 0x10]; + rgb_buffer2[iso_enter_index + 0x20] = rgb_buffer2[buffer_index + 0x20]; + } else if (led_index == LED_LEFT_SHIFT_ANSI) { + uint8_t iso_shift_index = pwm_mem_map[LED_LEFT_SHIFT_ISO]; + rgb_buffer0[iso_shift_index] = rgb_buffer0[buffer_index]; + rgb_buffer0[iso_shift_index + 0x10] = rgb_buffer0[buffer_index + 0x10]; + rgb_buffer0[iso_shift_index + 0x20] = rgb_buffer0[buffer_index + 0x20]; + } + + return true; + } } -bool wooting_rgb_array_set_single(uint8_t row, uint8_t column, uint8_t red, uint8_t green, uint8_t blue) { - //We just check to see if we believe the keyboard to be connected here as this call may just be updating the array and not actually communicating with the keyboard - //If auto update is on then the update_keyboard method will ping the keyboard before communicating with the keyboard - if (!wooting_usb_get_meta()->connected) { - return false; - } - - if (!wooting_rgb_array_change_single(row, column, red, green, blue)) { - return false; - } - - if (wooting_rgb_auto_update) { - return wooting_rgb_array_update_keyboard(); - } - else { - return true; - } +bool wooting_rgb_array_set_single(uint8_t row, uint8_t column, uint8_t red, + uint8_t green, uint8_t blue) { + // We just check to see if we believe the keyboard to be connected here as + // this call may just be updating the array and not actually communicating + // with the keyboard If auto update is on then the update_keyboard method will + // ping the keyboard before communicating with the keyboard + if (!wooting_usb_get_meta()->connected) { + return false; + } + + if (!wooting_rgb_array_change_single(row, column, red, green, blue)) { + return false; + } + + if (wooting_rgb_auto_update) { + return wooting_rgb_array_update_keyboard(); + } else { + return true; + } } -bool wooting_rgb_array_set_full(const uint8_t* colors_buffer) { - //Just need to check if we believe it is connected, the update_keyboard call will ping the keyboard if it is necessary - if (!wooting_usb_get_meta()->connected) { - return false; - } - - const uint8_t columns = wooting_usb_get_meta()->max_columns; - - for (uint8_t row = 0; row < WOOTING_RGB_ROWS; row++) { - const uint8_t* color = colors_buffer + row * WOOTING_RGB_COLS * 3; - for (uint8_t col = 0; col < columns; col++) { - uint8_t red = color[0]; - uint8_t green = color[1]; - uint8_t blue = color[2]; - - wooting_rgb_array_change_single(row, col, red, green, blue); - - color += 3; - } - } - - if (wooting_rgb_auto_update) { - return wooting_rgb_array_update_keyboard(); - } - else { - return true; - } +bool wooting_rgb_array_set_full(const uint8_t *colors_buffer) { + // Just need to check if we believe it is connected, the update_keyboard call + // will ping the keyboard if it is necessary + if (!wooting_usb_get_meta()->connected) { + return false; + } + + const uint8_t columns = wooting_usb_get_meta()->max_columns; + + for (uint8_t row = 0; row < WOOTING_RGB_ROWS; row++) { + const uint8_t *color = colors_buffer + row * WOOTING_RGB_COLS * 3; + for (uint8_t col = 0; col < columns; col++) { + uint8_t red = color[0]; + uint8_t green = color[1]; + uint8_t blue = color[2]; + + wooting_rgb_array_change_single(row, col, red, green, blue); + + color += 3; + } + } + + if (wooting_rgb_auto_update) { + return wooting_rgb_array_update_keyboard(); + } else { + return true; + } } -const WOOTING_USB_META* wooting_rgb_device_info() { - if (!wooting_usb_get_meta()->connected) wooting_usb_find_keyboard(); - return wooting_usb_get_meta(); +const WOOTING_USB_META *wooting_rgb_device_info() { + if (!wooting_usb_get_meta()->connected) + wooting_usb_find_keyboard(); + return wooting_usb_get_meta(); } WOOTING_DEVICE_LAYOUT wooting_rgb_device_layout(void) { - return wooting_usb_get_meta()->layout; + return wooting_usb_get_meta()->layout; } \ No newline at end of file diff --git a/src/wooting-rgb-sdk.h b/src/wooting-rgb-sdk.h index 34630e2..6e0c6a7 100644 --- a/src/wooting-rgb-sdk.h +++ b/src/wooting-rgb-sdk.h @@ -1,10 +1,10 @@ /* -* Copyright 2018 Wooting Technologies B.V. -* -* This Source Code Form is subject to the terms of the Mozilla Public -* License, v. 2.0. If a copy of the MPL was not distributed with this -* file, You can obtain one at http://mozilla.org/MPL/2.0/. -*/ + * Copyright 2018 Wooting Technologies B.V. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ #pragma once #ifdef __cplusplus @@ -12,18 +12,18 @@ extern "C" { #endif #ifdef _WIN32 -#ifdef WOOTINGRGBSDK_EXPORTS -#define WOOTINGRGBSDK_API __declspec(dllexport) -#else -#define WOOTINGRGBSDK_API __declspec(dllimport) +#ifdef WOOTINGRGBSDK_EXPORTS +#define WOOTINGRGBSDK_API __declspec(dllexport) +#else +#define WOOTINGRGBSDK_API __declspec(dllimport) #endif #else // __declspec is win32 only #define WOOTINGRGBSDK_API #endif -#include "stdint.h" #include "stdbool.h" +#include "stdint.h" #include "wooting-usb.h" /** @brief Check if keyboard connected. @@ -40,7 +40,8 @@ WOOTINGRGBSDK_API bool wooting_rgb_kbd_connected(void); /** @brief Set callback for when a keyboard disconnects. -The callback will be called when a Wooting keyboard disconnects. The callback will only trigger after a failed color change. +The callback will be called when a Wooting keyboard disconnects. The callback +will only trigger after a failed color change. @ingroup API @param cb The function pointer of the callback @@ -78,9 +79,11 @@ WOOTINGRGBSDK_API bool wooting_rgb_reset(void); /** @brief Directly set and update 1 key on the keyboard. -This function will directly change the color of 1 key on the keyboard. This will not influence the keyboard color array. +This function will directly change the color of 1 key on the keyboard. This will +not influence the keyboard color array. -Use this function for simple applifications, like a notification. Use the array functions if you want to change the entire keyboard. +Use this function for simple applifications, like a notification. Use the array +functions if you want to change the entire keyboard. @ingroup API @param row The horizontal index of the key @@ -92,13 +95,17 @@ Use this function for simple applifications, like a notification. Use the array @returns This functions return true (1) if the colour is set. */ -WOOTINGRGBSDK_API bool wooting_rgb_direct_set_key(uint8_t row, uint8_t column, uint8_t red, uint8_t green, uint8_t blue); +WOOTINGRGBSDK_API bool wooting_rgb_direct_set_key(uint8_t row, uint8_t column, + uint8_t red, uint8_t green, + uint8_t blue); /** @brief Directly reset 1 key on the keyboard to the original color. -This function will directly reset the color of 1 key on the keyboard. This will not influence the keyboard color array. +This function will directly reset the color of 1 key on the keyboard. This will +not influence the keyboard color array. -Use this function for simple applifications, like a notification. Use the array functions if you want to change the entire keyboard. +Use this function for simple applifications, like a notification. Use the array +functions if you want to change the entire keyboard. @ingroup API @param row The horizontal index of the key @@ -107,11 +114,13 @@ Use this function for simple applifications, like a notification. Use the array @returns This functions return true (1) if the colour is reset. */ -WOOTINGRGBSDK_API bool wooting_rgb_direct_reset_key(uint8_t row, uint8_t column); +WOOTINGRGBSDK_API bool wooting_rgb_direct_reset_key(uint8_t row, + uint8_t column); /** @brief Send the colors from the color array to the keyboard. -This function will send the changes made with the wooting_rgb_array single and full functions to the keyboard. +This function will send the changes made with the wooting_rgb_array single and +full functions to the keyboard. @ingroup API @@ -120,9 +129,11 @@ This functions return true (1) if the colours are updated. */ WOOTINGRGBSDK_API bool wooting_rgb_array_update_keyboard(void); -/** @brief Change the auto update flag for the wooting_rgb_array single and full functions functions. +/** @brief Change the auto update flag for the wooting_rgb_array single and full +functions functions. -This function can be used to set a auto update trigger after every change with the wooting_rgb_array single and full functions function. +This function can be used to set a auto update trigger after every change with +the wooting_rgb_array single and full functions function. Standard is set to false. @@ -136,9 +147,10 @@ WOOTINGRGBSDK_API void wooting_rgb_array_auto_update(bool auto_update); /** @brief Set a single color in the colour array. -This function will set a single color in the colour array. This will not directly update the keyboard (unless the flag is set), so -it can be called frequently. For example in a loop that updates the entire keyboard. This way you can avoid dealing with C arrays from -different languages. +This function will set a single color in the colour array. This will not +directly update the keyboard (unless the flag is set), so it can be called +frequently. For example in a loop that updates the entire keyboard. This way you +can avoid dealing with C arrays from different languages. @ingroup API @param row The horizontal index of the key @@ -148,41 +160,54 @@ different languages. @param blue 0-255 value of the blue color @returns -This functions return true (1) if the colours are changed (if auto update flag: updated). +This functions return true (1) if the colours are changed (if auto update flag: +updated). */ -WOOTINGRGBSDK_API bool wooting_rgb_array_set_single(uint8_t row, uint8_t column, uint8_t red, uint8_t green, uint8_t blue); +WOOTINGRGBSDK_API bool wooting_rgb_array_set_single(uint8_t row, uint8_t column, + uint8_t red, uint8_t green, + uint8_t blue); /** @brief Set a full colour array. -This function will set a complete color array. This will not directly update the keyboard (unless the flag is set). +This function will set a complete color array. This will not directly update the +keyboard (unless the flag is set). -If you use a non-C language it is recommended to use the wooting_rgb_array_set_single function to change the colors to avoid compatibility issues. +If you use a non-C language it is recommended to use the +wooting_rgb_array_set_single function to change the colors to avoid +compatibility issues. -Buffer should be layout out as [Row0Col0Red, Row0Col0Green, Row0Col0Blue, Row0Col1Red, Row0Col1Green, Row0Col1Blue, ... Row5Row20Red, Row5Row20Green, Row5Row20Blue]. -Expected size is 6 row * 21 columns * 3 colors per key = 378 bytes. +Buffer should be layout out as [Row0Col0Red, Row0Col0Green, Row0Col0Blue, +Row0Col1Red, Row0Col1Green, Row0Col1Blue, ... Row5Row20Red, Row5Row20Green, +Row5Row20Blue]. Expected size is 6 row * 21 columns * 3 colors per key = 378 +bytes. @ingroup API -@param colors_buffer Pointer to a buffer of a full color array +@param colors_buffer Pointer to a buffer of a full color array @returns -This functions return true (1) if the colours are changed (if auto update flag: updated). +This functions return true (1) if the colours are changed (if auto update flag: +updated). */ WOOTINGRGBSDK_API bool wooting_rgb_array_set_full(const uint8_t *colors_buffer); /** @brief Retrieve information about the connected Device -This function returns a pointer to a struct which provides various relevant details about the currently connected device. E.g. max rgb rows, columns, etc +This function returns a pointer to a struct which provides various relevant +details about the currently connected device. E.g. max rgb rows, columns, etc @ingroup API @returns -This functions returns a pointer to a `WOOTING_USB_META` struct which contains relevant Device Information +This functions returns a pointer to a `WOOTING_USB_META` struct which contains +relevant Device Information */ -WOOTINGRGBSDK_API const WOOTING_USB_META* wooting_rgb_device_info(void); +WOOTINGRGBSDK_API const WOOTING_USB_META *wooting_rgb_device_info(void); /** @brief Retrieve layout of the connected device -This function returns an enum flag indicating the layout, e.g. ISO. See WOOTING_DEVICE_LAYOUT for options. It will return LAYOUT_UNKNOWN if no device is connected or it failed to get the layout info from the device +This function returns an enum flag indicating the layout, e.g. ISO. See +WOOTING_DEVICE_LAYOUT for options. It will return LAYOUT_UNKNOWN if no device is +connected or it failed to get the layout info from the device @ingroup API @@ -194,4 +219,3 @@ WOOTINGRGBSDK_API WOOTING_DEVICE_LAYOUT wooting_rgb_device_layout(void); #ifdef __cplusplus } #endif - diff --git a/src/wooting-usb.c b/src/wooting-usb.c index fa59ef7..b766bc8 100644 --- a/src/wooting-usb.c +++ b/src/wooting-usb.c @@ -1,20 +1,18 @@ /* -* Copyright 2018 Wooting Technologies B.V. -* -* This Source Code Form is subject to the terms of the Mozilla Public -* License, v. 2.0. If a copy of the MPL was not distributed with this -* file, You can obtain one at http://mozilla.org/MPL/2.0/. -*/ -#include "stdint.h" -#include "stdbool.h" -#include "string.h" + * Copyright 2018 Wooting Technologies B.V. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ #include "wooting-usb.h" #include "hidapi.h" #include "stdlib.h" +#include "string.h" #define WOOTING_COMMAND_SIZE 8 -#define WOOTING_REPORT_SIZE 128+1 -#define WOOTING_V2_REPORT_SIZE 256+1 +#define WOOTING_REPORT_SIZE 128 + 1 +#define WOOTING_V2_REPORT_SIZE 256 + 1 #define WOOTING_V1_RESPONSE_SIZE 128 #define WOOTING_V2_RESPONSE_SIZE 256 @@ -23,574 +21,598 @@ #define WOOTING_ONE_PID 0xFF01 #define WOOTING_ONE_V2_PID 0x1100 -// Every keyboard can have an alternative PID for gamepad driver compatibility (indicated by X) -#define WOOTING_ONE_V2_PIDX 0x1101 +// Every keyboard can have an alternative PID for gamepad driver compatibility +// (indicated by X) +#define V2_ALT_PID_0 0x0 +#define V2_ALT_PID_1 0x1 +#define V2_ALT_PID_2 0x2 #define WOOTING_TWO_PID 0xFF02 #define WOOTING_TWO_V2_PID 0x1200 -#define WOOTING_TWO_V2_PIDX 0x1201 #define WOOTING_TWO_LE_PID 0x1210 -#define WOOTING_TWO_LE_PIDX 0x1211 #define WOOTING_TWO_HE_PID 0x1220 -#define WOOTING_TWO_HE_PIDX 0x1221 #define CFG_USAGE_PAGE 0x1337 static WOOTING_USB_META wooting_usb_meta; - static void_cb disconnected_callback = NULL; -static hid_device* keyboard_handle = NULL; +static hid_device *keyboard_handle = NULL; static void debug_print_buffer(uint8_t *buff, size_t len); -static uint16_t getCrc16ccitt(const uint8_t* buffer, uint16_t size) -{ - uint16_t crc = 0; +static uint16_t getCrc16ccitt(const uint8_t *buffer, uint16_t size) { + uint16_t crc = 0; - while (size--) { - crc ^= (*buffer++ << 8); + while (size--) { + crc ^= (*buffer++ << 8); - for (uint8_t i = 0; i < 8; ++i) { - if (crc & 0x8000) { - crc = (crc << 1) ^ 0x1021; - } - else { - crc = crc << 1; - } - } - } + for (uint8_t i = 0; i < 8; ++i) { + if (crc & 0x8000) { + crc = (crc << 1) ^ 0x1021; + } else { + crc = crc << 1; + } + } + } - return crc; + return crc; } - typedef void (*set_meta_func)(); static void reset_meta() { - wooting_usb_meta.connected = false; - - wooting_usb_meta.model = "N/A"; - wooting_usb_meta.device_type = -1; - wooting_usb_meta.max_rows = 0; - wooting_usb_meta.max_columns = 0; - wooting_usb_meta.led_index_max = 0; - wooting_usb_meta.v2_interface = false; - wooting_usb_meta.layout = LAYOUT_UNKNOWN; + wooting_usb_meta.connected = false; + + wooting_usb_meta.model = "N/A"; + wooting_usb_meta.device_type = -1; + wooting_usb_meta.max_rows = 0; + wooting_usb_meta.max_columns = 0; + wooting_usb_meta.led_index_max = 0; + wooting_usb_meta.v2_interface = false; + wooting_usb_meta.layout = LAYOUT_UNKNOWN; } - static void set_meta_wooting_one() { - wooting_usb_meta.model = "Wooting One"; - wooting_usb_meta.device_type = DEVICE_KEYBOARD_TKL; - wooting_usb_meta.max_rows = WOOTING_RGB_ROWS; - wooting_usb_meta.max_columns = WOOTING_ONE_RGB_COLS; - wooting_usb_meta.led_index_max = WOOTING_ONE_KEY_CODE_LIMIT; - wooting_usb_meta.v2_interface = false; + wooting_usb_meta.model = "Wooting One"; + wooting_usb_meta.device_type = DEVICE_KEYBOARD_TKL; + wooting_usb_meta.max_rows = WOOTING_RGB_ROWS; + wooting_usb_meta.max_columns = WOOTING_ONE_RGB_COLS; + wooting_usb_meta.led_index_max = WOOTING_ONE_KEY_CODE_LIMIT; + wooting_usb_meta.v2_interface = false; } static void set_meta_wooting_one_v2() { - set_meta_wooting_one(); - wooting_usb_meta.v2_interface = true; + set_meta_wooting_one(); + wooting_usb_meta.v2_interface = true; } static void set_meta_wooting_two() { - wooting_usb_meta.model = "Wooting Two"; - wooting_usb_meta.device_type = DEVICE_KEYBOARD; - wooting_usb_meta.max_rows = WOOTING_RGB_ROWS; - wooting_usb_meta.max_columns = WOOTING_TWO_RGB_COLS; - wooting_usb_meta.led_index_max = WOOTING_TWO_KEY_CODE_LIMIT; - wooting_usb_meta.v2_interface = false; + wooting_usb_meta.model = "Wooting Two"; + wooting_usb_meta.device_type = DEVICE_KEYBOARD; + wooting_usb_meta.max_rows = WOOTING_RGB_ROWS; + wooting_usb_meta.max_columns = WOOTING_TWO_RGB_COLS; + wooting_usb_meta.led_index_max = WOOTING_TWO_KEY_CODE_LIMIT; + wooting_usb_meta.v2_interface = false; } static void set_meta_wooting_two_v2() { - set_meta_wooting_two(); - wooting_usb_meta.v2_interface = true; + set_meta_wooting_two(); + wooting_usb_meta.v2_interface = true; } static void set_meta_wooting_two_le() { - wooting_usb_meta.model = "Wooting Two Lekker Edition"; - wooting_usb_meta.device_type = DEVICE_KEYBOARD; - wooting_usb_meta.led_index_max = WOOTING_TWO_KEY_CODE_LIMIT; - wooting_usb_meta.max_rows = WOOTING_RGB_ROWS; - wooting_usb_meta.max_columns = WOOTING_TWO_RGB_COLS; - wooting_usb_meta.led_index_max = WOOTING_TWO_KEY_CODE_LIMIT; - wooting_usb_meta.v2_interface = true; + wooting_usb_meta.model = "Wooting Two Lekker Edition"; + wooting_usb_meta.device_type = DEVICE_KEYBOARD; + wooting_usb_meta.led_index_max = WOOTING_TWO_KEY_CODE_LIMIT; + wooting_usb_meta.max_rows = WOOTING_RGB_ROWS; + wooting_usb_meta.max_columns = WOOTING_TWO_RGB_COLS; + wooting_usb_meta.led_index_max = WOOTING_TWO_KEY_CODE_LIMIT; + wooting_usb_meta.v2_interface = true; } static void set_meta_wooting_two_he() { - wooting_usb_meta.model = "Wooting Two HE"; - wooting_usb_meta.device_type = DEVICE_KEYBOARD; - wooting_usb_meta.led_index_max = WOOTING_TWO_KEY_CODE_LIMIT; - wooting_usb_meta.max_rows = WOOTING_RGB_ROWS; - wooting_usb_meta.max_columns = WOOTING_TWO_RGB_COLS; - wooting_usb_meta.led_index_max = WOOTING_TWO_KEY_CODE_LIMIT; - wooting_usb_meta.v2_interface = true; + wooting_usb_meta.model = "Wooting Two HE"; + wooting_usb_meta.device_type = DEVICE_KEYBOARD; + wooting_usb_meta.led_index_max = WOOTING_TWO_KEY_CODE_LIMIT; + wooting_usb_meta.max_rows = WOOTING_RGB_ROWS; + wooting_usb_meta.max_columns = WOOTING_TWO_RGB_COLS; + wooting_usb_meta.led_index_max = WOOTING_TWO_KEY_CODE_LIMIT; + wooting_usb_meta.v2_interface = true; } -WOOTING_USB_META* wooting_usb_get_meta() { - //We want to initialise the struct to the default values if it hasn't been set - if (wooting_usb_meta.model == NULL){ - reset_meta(); - } +WOOTING_USB_META *wooting_usb_get_meta() { + // We want to initialise the struct to the default values if it hasn't been + // set + if (wooting_usb_meta.model == NULL) { + reset_meta(); + } - return &wooting_usb_meta; + return &wooting_usb_meta; } bool wooting_usb_use_v2_interface(void) { - return wooting_usb_meta.v2_interface; + return wooting_usb_meta.v2_interface; } - void wooting_usb_disconnect(bool trigger_cb) { - #ifdef DEBUG_LOG - printf("Keyboard disconnected\n"); - #endif - reset_meta(); - hid_close(keyboard_handle); - keyboard_handle = NULL; - - if (trigger_cb && disconnected_callback) { - disconnected_callback(); - } +#ifdef DEBUG_LOG + printf("Keyboard disconnected\n"); +#endif + reset_meta(); + hid_close(keyboard_handle); + keyboard_handle = NULL; + + if (trigger_cb && disconnected_callback) { + disconnected_callback(); + } } -void wooting_usb_set_disconnected_cb(void_cb cb) { - disconnected_callback = cb; -} +void wooting_usb_set_disconnected_cb(void_cb cb) { disconnected_callback = cb; } WOOTING_DEVICE_LAYOUT wooting_usb_get_layout() { - uint8_t buff[20]; - int result = wooting_usb_send_feature_with_response(buff, sizeof(buff), WOOTING_DEVICE_CONFIG_COMMAND, 0, 0, 0, 0); - if (result != -1) { - uint8_t index = wooting_usb_use_v2_interface() ? 10 : 9; - uint8_t layout = buff[index]; - #ifdef DEBUG_LOG - printf("Layout result: %d, %d\n", layout, index); - #endif - if (layout <= LAYOUT_ISO) - return (WOOTING_DEVICE_LAYOUT)layout; - else { - printf("Unknown device layout found %d\n", layout); - return LAYOUT_UNKNOWN; - } - }else { - printf("Failed to get device config info for layout detection, result: %d\n", result); - } - - return LAYOUT_UNKNOWN; + uint8_t buff[20]; + int result = wooting_usb_send_feature_with_response( + buff, sizeof(buff), WOOTING_DEVICE_CONFIG_COMMAND, 0, 0, 0, 0); + if (result != -1) { + uint8_t index = wooting_usb_use_v2_interface() ? 10 : 9; + uint8_t layout = buff[index]; +#ifdef DEBUG_LOG + printf("Layout result: %d, %d\n", layout, index); +#endif + if (layout <= LAYOUT_ISO) + return (WOOTING_DEVICE_LAYOUT)layout; + else { + printf("Unknown device layout found %d\n", layout); + return LAYOUT_UNKNOWN; + } + } else { + printf( + "Failed to get device config info for layout detection, result: %d\n", + result); + } + + return LAYOUT_UNKNOWN; } bool wooting_usb_find_keyboard() { - if (keyboard_handle) { - // #ifdef DEBUG_LOG - // printf("Got keyboard handle already\n"); - // #endif - // If keyboard is disconnected read will return -1 - // https://github.com/signal11/hidapi/issues/55#issuecomment-5307209 - // unsigned char stub = 0; - // if (hid_read_timeout(keyboard_handle, &stub, 0, 0) != -1) { - // #ifdef DEBUG_LOG - // printf("Keyboard succeeded test read\n"); - // #endif - // return true; - // } else { - // #ifdef DEBUG_LOG - // printf("Keyboard failed test read, disconnecting...\n"); - // #endif - // wooting_usb_disconnect(true); - // } - return true; - } else { - #ifdef DEBUG_LOG - printf("No keyboard handle already\n"); - #endif - } - - struct hid_device_info* hid_info; - - reset_meta(); - set_meta_func meta_func; - if ((hid_info = hid_enumerate(WOOTING_VID, WOOTING_ONE_PID)) != NULL) { - #ifdef DEBUG_LOG - printf("Enumerate on Wooting One Successful\n"); - #endif - meta_func = set_meta_wooting_one; - } - else if ((hid_info = hid_enumerate(WOOTING_VID2, WOOTING_ONE_V2_PID)) != NULL - || (hid_info = hid_enumerate(WOOTING_VID2, WOOTING_ONE_V2_PIDX)) != NULL) { - #ifdef DEBUG_LOG - printf("Enumerate on Wooting One (V2) Successful\n"); - #endif - meta_func = set_meta_wooting_one_v2; - } - else if ((hid_info = hid_enumerate(WOOTING_VID, WOOTING_TWO_PID)) != NULL) { - #ifdef DEBUG_LOG - printf("Enumerate on Wooting Two Successful\n"); - #endif - meta_func = set_meta_wooting_two; - } - else if ((hid_info = hid_enumerate(WOOTING_VID2, WOOTING_TWO_V2_PID)) != NULL - || (hid_info = hid_enumerate(WOOTING_VID2, WOOTING_TWO_V2_PIDX)) != NULL) { - #ifdef DEBUG_LOG - printf("Enumerate on Wooting Two (V2) Successful\n"); - #endif - meta_func = set_meta_wooting_two_v2; - } - else if ((hid_info = hid_enumerate(WOOTING_VID2, WOOTING_TWO_LE_PID)) != NULL - || (hid_info = hid_enumerate(WOOTING_VID2, WOOTING_TWO_LE_PIDX)) != NULL) { - #ifdef DEBUG_LOG - printf("Enumerate on Wooting Two Lekker Edition Successful\n"); - #endif - meta_func = set_meta_wooting_two_le; - } - else if ((hid_info = hid_enumerate(WOOTING_VID2, WOOTING_TWO_HE_PID)) != NULL - || (hid_info = hid_enumerate(WOOTING_VID2, WOOTING_TWO_HE_PIDX)) != NULL) { - #ifdef DEBUG_LOG - printf("Enumerate on Wooting Two HE Successful\n"); - #endif - meta_func = set_meta_wooting_two_he; - } - else { - #ifdef DEBUG_LOG - printf("Enumerate failed\n"); - #endif - return false; - } - - bool keyboard_found = false; + if (keyboard_handle) { + // #ifdef DEBUG_LOG + // printf("Got keyboard handle already\n"); + // #endif + // If keyboard is disconnected read will return -1 + // https://github.com/signal11/hidapi/issues/55#issuecomment-5307209 + // unsigned char stub = 0; + // if (hid_read_timeout(keyboard_handle, &stub, 0, 0) != -1) { + // #ifdef DEBUG_LOG + // printf("Keyboard succeeded test read\n"); + // #endif + // return true; + // } else { + // #ifdef DEBUG_LOG + // printf("Keyboard failed test read, disconnecting...\n"); + // #endif + // wooting_usb_disconnect(true); + // } + return true; + } else { +#ifdef DEBUG_LOG + printf("No keyboard handle already\n"); +#endif + } + + struct hid_device_info *hid_info; + + reset_meta(); + set_meta_func meta_func; + if ((hid_info = hid_enumerate(WOOTING_VID, WOOTING_ONE_PID)) != NULL) { +#ifdef DEBUG_LOG + printf("Enumerate on Wooting One Successful\n"); +#endif + meta_func = set_meta_wooting_one; + } else if ((hid_info = hid_enumerate( + WOOTING_VID2, WOOTING_ONE_V2_PID | V2_ALT_PID_0)) != NULL || + (hid_info = hid_enumerate( + WOOTING_VID2, WOOTING_ONE_V2_PID | V2_ALT_PID_1)) != NULL || + (hid_info = hid_enumerate( + WOOTING_VID2, WOOTING_ONE_V2_PID | V2_ALT_PID_2)) != NULL) { +#ifdef DEBUG_LOG + printf("Enumerate on Wooting One (V2) Successful\n"); +#endif + meta_func = set_meta_wooting_one_v2; + } else if ((hid_info = hid_enumerate(WOOTING_VID, WOOTING_TWO_PID)) != NULL) { +#ifdef DEBUG_LOG + printf("Enumerate on Wooting Two Successful\n"); +#endif + meta_func = set_meta_wooting_two; + } else if ((hid_info = hid_enumerate( + WOOTING_VID2, WOOTING_TWO_V2_PID | V2_ALT_PID_0)) != NULL || + (hid_info = hid_enumerate( + WOOTING_VID2, WOOTING_TWO_V2_PID | V2_ALT_PID_1)) != NULL || + (hid_info = hid_enumerate( + WOOTING_VID2, WOOTING_TWO_V2_PID | V2_ALT_PID_2)) != NULL) { +#ifdef DEBUG_LOG + printf("Enumerate on Wooting Two (V2) Successful\n"); +#endif + meta_func = set_meta_wooting_two_v2; + } else if ((hid_info = hid_enumerate( + WOOTING_VID2, WOOTING_TWO_LE_PID | V2_ALT_PID_0)) != NULL || + (hid_info = hid_enumerate( + WOOTING_VID2, WOOTING_TWO_LE_PID | V2_ALT_PID_1)) != NULL || + (hid_info = hid_enumerate( + WOOTING_VID2, WOOTING_TWO_LE_PID | V2_ALT_PID_2)) != NULL) { +#ifdef DEBUG_LOG + printf("Enumerate on Wooting Two Lekker Edition Successful\n"); +#endif + meta_func = set_meta_wooting_two_le; + } else if ((hid_info = hid_enumerate( + WOOTING_VID2, WOOTING_TWO_HE_PID | V2_ALT_PID_0)) != NULL || + (hid_info = hid_enumerate( + WOOTING_VID2, WOOTING_TWO_HE_PID | V2_ALT_PID_1)) != NULL || + (hid_info = hid_enumerate( + WOOTING_VID2, WOOTING_TWO_HE_PID | V2_ALT_PID_2)) != NULL) { +#ifdef DEBUG_LOG + printf("Enumerate on Wooting Two HE Successful\n"); +#endif + meta_func = set_meta_wooting_two_he; + } else { +#ifdef DEBUG_LOG + printf("Enumerate failed\n"); +#endif + return false; + } + + bool keyboard_found = false; #ifdef LEGACY_DETECTION - // The amount of interfaces is variable, so we need to look for the configuration interface - // In the Wooting one keyboard the configuration interface is always 4 lower than the highest number - struct hid_device_info* hid_info_walker = hid_info; - uint8_t highestInterfaceNr = 0; - while (hid_info_walker) { - if (hid_info_walker->interface_number > highestInterfaceNr) { - highestInterfaceNr = hid_info_walker->interface_number; - } - hid_info_walker = hid_info_walker->next; - } - - uint8_t interfaceNr = highestInterfaceNr - 4; - - #ifdef DEBUG_LOG - printf("Higest Interface No: %d, Search interface No: %d\n", highestInterfaceNr, interfaceNr); - #endif - - // Reset walker and look for the interface number - hid_info_walker = hid_info; - while (hid_info_walker) { - #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); - #endif - if (hid_info_walker->interface_number == interfaceNr) { - #ifdef DEBUG_LOG - printf("Attempting to open\n"); - #endif - keyboard_handle = hid_open_path(hid_info_walker->path); - if (keyboard_handle) { - #ifdef DEBUG_LOG - printf("Found keyboard_handle: %s\n", hid_info_walker->path); - #endif - // Once the keyboard is found send an init command and abuse two reads to make a 50 ms delay to make sure the keyboard is ready - #ifdef DEBUG_LOG - bool result = - #endif - wooting_usb_send_feature(WOOTING_COLOR_INIT_COMMAND, 0, 0, 0, 0); - #ifdef DEBUG_LOG - printf("Color init result: %d\n", result); - #endif - unsigned char stub = 0; - hid_read(keyboard_handle, &stub, 0); - hid_read_timeout(keyboard_handle, &stub, 0, 50); - - keyboard_found = true; - } else { - #ifdef DEBUG_LOG - printf("No Keyboard handle: %S\n", hid_error(NULL)); - #endif - } - break; - } - - hid_info_walker = hid_info_walker->next; - } - #else - - // We can just search for the interface with matching custom Wooting Cfg usage page - struct hid_device_info* hid_info_walker = hid_info; - while (hid_info_walker) { - #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); - #endif - if (hid_info_walker->usage_page == CFG_USAGE_PAGE) { - - #ifdef DEBUG_LOG - printf("Attempting to open\n"); - #endif - keyboard_handle = hid_open_path(hid_info_walker->path); - if (keyboard_handle) { - #ifdef DEBUG_LOG - printf("Found keyboard_handle: %s\n", hid_info_walker->path); - #endif - - keyboard_found = true; - } else { - #ifdef DEBUG_LOG - printf("No Keyboard handle: %S\n", hid_error(NULL)); - #endif - } - break; - - } - hid_info_walker = hid_info_walker->next; - } - - #endif - - hid_free_enumeration(hid_info); - if (keyboard_found){ - meta_func(); - wooting_usb_meta.connected = true; - - // 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); - #ifdef DEBUG_LOG - printf("Color init result: %d\n", result); - #endif - - wooting_usb_meta.layout = wooting_usb_get_layout(); - } - #ifdef DEBUG_LOG - printf("Finished looking for keyboard returned: %d\n", keyboard_found); - #endif - return keyboard_found; + // The amount of interfaces is variable, so we need to look for the + // configuration interface In the Wooting one keyboard the configuration + // interface is always 4 lower than the highest number + struct hid_device_info *hid_info_walker = hid_info; + uint8_t highestInterfaceNr = 0; + while (hid_info_walker) { + if (hid_info_walker->interface_number > highestInterfaceNr) { + highestInterfaceNr = hid_info_walker->interface_number; + } + hid_info_walker = hid_info_walker->next; + } + + uint8_t interfaceNr = highestInterfaceNr - 4; + +#ifdef DEBUG_LOG + printf("Higest Interface No: %d, Search interface No: %d\n", + highestInterfaceNr, interfaceNr); +#endif + + // Reset walker and look for the interface number + hid_info_walker = hid_info; + while (hid_info_walker) { +#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); +#endif + if (hid_info_walker->interface_number == interfaceNr) { +#ifdef DEBUG_LOG + printf("Attempting to open\n"); +#endif + keyboard_handle = hid_open_path(hid_info_walker->path); + if (keyboard_handle) { +#ifdef DEBUG_LOG + printf("Found keyboard_handle: %s\n", hid_info_walker->path); +#endif +// Once the keyboard is found send an init command and abuse two reads to make a +// 50 ms delay to make sure the keyboard is ready +#ifdef DEBUG_LOG + bool result = +#endif + wooting_usb_send_feature(WOOTING_COLOR_INIT_COMMAND, 0, 0, 0, 0); +#ifdef DEBUG_LOG + printf("Color init result: %d\n", result); +#endif + unsigned char stub = 0; + hid_read(keyboard_handle, &stub, 0); + hid_read_timeout(keyboard_handle, &stub, 0, 50); + + keyboard_found = true; + } else { +#ifdef DEBUG_LOG + printf("No Keyboard handle: %S\n", hid_error(NULL)); +#endif + } + break; + } + + hid_info_walker = hid_info_walker->next; + } +#else + + // We can just search for the interface with matching custom Wooting Cfg usage + // page + struct hid_device_info *hid_info_walker = hid_info; + while (hid_info_walker) { +#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); +#endif + if (hid_info_walker->usage_page == CFG_USAGE_PAGE) { + +#ifdef DEBUG_LOG + printf("Attempting to open\n"); +#endif + keyboard_handle = hid_open_path(hid_info_walker->path); + if (keyboard_handle) { +#ifdef DEBUG_LOG + printf("Found keyboard_handle: %s\n", hid_info_walker->path); +#endif + + keyboard_found = true; + } else { +#ifdef DEBUG_LOG + printf("No Keyboard handle: %S\n", hid_error(NULL)); +#endif + } + break; + } + hid_info_walker = hid_info_walker->next; + } + +#endif + + hid_free_enumeration(hid_info); + if (keyboard_found) { + meta_func(); + wooting_usb_meta.connected = true; + +// 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); +#ifdef DEBUG_LOG + printf("Color init result: %d\n", result); +#endif + + wooting_usb_meta.layout = wooting_usb_get_layout(); + } +#ifdef DEBUG_LOG + printf("Finished looking for keyboard returned: %d\n", keyboard_found); +#endif + return keyboard_found; } bool wooting_usb_send_buffer_v1(RGB_PARTS part_number, uint8_t rgb_buffer[]) { - if (!wooting_usb_find_keyboard()) { - return false; - } - - uint8_t report_buffer[WOOTING_REPORT_SIZE] = { 0 }; - - report_buffer[0] = 0; // HID report index (unused) - report_buffer[1] = 0xD0; // Magicword - report_buffer[2] = 0xDA; // Magicword - report_buffer[3] = WOOTING_RAW_COLORS_REPORT; // Report ID - switch (part_number) { - case PART0: { - report_buffer[4] = 0; // Slave nr - report_buffer[5] = 0; // Reg start address - break; - } - case PART1: { - report_buffer[4] = 0; // Slave nr - report_buffer[5] = RGB_RAW_BUFFER_SIZE; // Reg start address - break; - } - case PART2: { - report_buffer[4] = 1; // Slave nr - report_buffer[5] = 0; // Reg start address - break; - } - case PART3: { - report_buffer[4] = 1; // Slave nr - report_buffer[5] = RGB_RAW_BUFFER_SIZE; // Reg start address - break; - } - // wooting_rgb_array_update_keyboard will not run into this - case PART4: { - //Wooting One will not have this part of the report - if (wooting_usb_meta.device_type != DEVICE_KEYBOARD) { - return false; - } - report_buffer[4] = 2; // Slave nr - report_buffer[5] = 0; // Reg start address - break; - } - default: { - return false; - } - } - - memcpy(&report_buffer[6], rgb_buffer, RGB_RAW_BUFFER_SIZE); - - uint16_t crc = getCrc16ccitt((uint8_t*)&report_buffer, WOOTING_REPORT_SIZE - 2); - report_buffer[127] = (uint8_t)crc; - report_buffer[128] = crc >> 8; - int report_size = hid_write(keyboard_handle, report_buffer, WOOTING_REPORT_SIZE); - if (report_size == WOOTING_REPORT_SIZE) { - return true; - } - else { - #ifdef DEBUG_LOG - printf("Got report size: %d, expected: %d, disconnecting..\n", report_size, WOOTING_REPORT_SIZE); - #endif - wooting_usb_disconnect(true); - return false; - } + if (!wooting_usb_find_keyboard()) { + return false; + } + + uint8_t report_buffer[WOOTING_REPORT_SIZE] = {0}; + + report_buffer[0] = 0; // HID report index (unused) + report_buffer[1] = 0xD0; // Magicword + report_buffer[2] = 0xDA; // Magicword + report_buffer[3] = WOOTING_RAW_COLORS_REPORT; // Report ID + switch (part_number) { + case PART0: { + report_buffer[4] = 0; // Slave nr + report_buffer[5] = 0; // Reg start address + break; + } + case PART1: { + report_buffer[4] = 0; // Slave nr + report_buffer[5] = RGB_RAW_BUFFER_SIZE; // Reg start address + break; + } + case PART2: { + report_buffer[4] = 1; // Slave nr + report_buffer[5] = 0; // Reg start address + break; + } + case PART3: { + report_buffer[4] = 1; // Slave nr + report_buffer[5] = RGB_RAW_BUFFER_SIZE; // Reg start address + break; + } + // wooting_rgb_array_update_keyboard will not run into this + case PART4: { + // Wooting One will not have this part of the report + if (wooting_usb_meta.device_type != DEVICE_KEYBOARD) { + return false; + } + report_buffer[4] = 2; // Slave nr + report_buffer[5] = 0; // Reg start address + break; + } + default: { + return false; + } + } + + memcpy(&report_buffer[6], rgb_buffer, RGB_RAW_BUFFER_SIZE); + + uint16_t crc = + getCrc16ccitt((uint8_t *)&report_buffer, WOOTING_REPORT_SIZE - 2); + report_buffer[127] = (uint8_t)crc; + report_buffer[128] = crc >> 8; + int report_size = + hid_write(keyboard_handle, report_buffer, WOOTING_REPORT_SIZE); + if (report_size == WOOTING_REPORT_SIZE) { + return true; + } else { +#ifdef DEBUG_LOG + printf("Got report size: %d, expected: %d, disconnecting..\n", report_size, + WOOTING_REPORT_SIZE); +#endif + wooting_usb_disconnect(true); + return false; + } } -bool wooting_usb_send_buffer_v2(uint16_t rgb_buffer[WOOTING_RGB_ROWS][WOOTING_RGB_COLS]) { - if (!wooting_usb_find_keyboard()) { - return false; - } - - uint8_t report_buffer[WOOTING_V2_REPORT_SIZE] = { 0 }; - report_buffer[0] = 0; // HID report index (unused) - report_buffer[1] = 0xD0; // Magicword - report_buffer[2] = 0xDA; // Magicword - report_buffer[3] = WOOTING_RAW_COLORS_REPORT; // Report ID - memcpy(&report_buffer[4], rgb_buffer, WOOTING_RGB_ROWS * WOOTING_RGB_COLS * sizeof(uint16_t)); - - int report_size = hid_write(keyboard_handle, report_buffer, WOOTING_V2_REPORT_SIZE); - if (report_size == WOOTING_V2_REPORT_SIZE) { - #ifdef DEBUG_LOG - printf("Successfully sent V2 buffer...\n"); - #endif - return true; - } - else { - #ifdef DEBUG_LOG - printf("Got report size: %d, expected: %d, disconnecting..\n", report_size, WOOTING_V2_REPORT_SIZE); - #endif - wooting_usb_disconnect(true); - return false; - } +bool wooting_usb_send_buffer_v2( + uint16_t rgb_buffer[WOOTING_RGB_ROWS][WOOTING_RGB_COLS]) { + if (!wooting_usb_find_keyboard()) { + return false; + } + + uint8_t report_buffer[WOOTING_V2_REPORT_SIZE] = {0}; + report_buffer[0] = 0; // HID report index (unused) + report_buffer[1] = 0xD0; // Magicword + report_buffer[2] = 0xDA; // Magicword + report_buffer[3] = WOOTING_RAW_COLORS_REPORT; // Report ID + memcpy(&report_buffer[4], rgb_buffer, + WOOTING_RGB_ROWS * WOOTING_RGB_COLS * sizeof(uint16_t)); + + int report_size = + hid_write(keyboard_handle, report_buffer, WOOTING_V2_REPORT_SIZE); + if (report_size == WOOTING_V2_REPORT_SIZE) { +#ifdef DEBUG_LOG + printf("Successfully sent V2 buffer...\n"); +#endif + return true; + } else { +#ifdef DEBUG_LOG + printf("Got report size: %d, expected: %d, disconnecting..\n", report_size, + WOOTING_V2_REPORT_SIZE); +#endif + wooting_usb_disconnect(true); + return false; + } } -int wooting_usb_send_feature_buff(uint8_t commandId, uint8_t parameter0, uint8_t parameter1, uint8_t parameter2, uint8_t parameter3) { - uint8_t report_buffer[WOOTING_COMMAND_SIZE]; - - report_buffer[0] = 0; // HID report index (unused) - report_buffer[1] = 0xD0; // Magic word - report_buffer[2] = 0xDA; // Magic word - report_buffer[3] = commandId; - report_buffer[4] = parameter3; - report_buffer[5] = parameter2; - report_buffer[6] = parameter1; - report_buffer[7] = parameter0; - - return hid_send_feature_report(keyboard_handle, report_buffer, WOOTING_COMMAND_SIZE); +int wooting_usb_send_feature_buff(uint8_t commandId, uint8_t parameter0, + uint8_t parameter1, uint8_t parameter2, + uint8_t parameter3) { + uint8_t report_buffer[WOOTING_COMMAND_SIZE]; + + report_buffer[0] = 0; // HID report index (unused) + report_buffer[1] = 0xD0; // Magic word + report_buffer[2] = 0xDA; // Magic word + report_buffer[3] = commandId; + report_buffer[4] = parameter3; + report_buffer[5] = parameter2; + report_buffer[6] = parameter1; + report_buffer[7] = parameter0; + + return hid_send_feature_report(keyboard_handle, report_buffer, + WOOTING_COMMAND_SIZE); } size_t wooting_usb_get_response_size(void) { - if (wooting_usb_use_v2_interface()) { - return WOOTING_V2_RESPONSE_SIZE; - } else { - return WOOTING_V1_RESPONSE_SIZE; - } + if (wooting_usb_use_v2_interface()) { + return WOOTING_V2_RESPONSE_SIZE; + } else { + return WOOTING_V1_RESPONSE_SIZE; + } } -bool wooting_usb_send_feature(uint8_t commandId, uint8_t parameter0, uint8_t parameter1, uint8_t parameter2, uint8_t parameter3) { - if (!wooting_usb_find_keyboard()) { - return false; - } - - #ifdef DEBUG_LOG - printf("Sending feature: %d\n", commandId); - #endif - - int command_size = wooting_usb_send_feature_buff(commandId, parameter0, parameter1, parameter2, parameter3); - size_t response_size = wooting_usb_get_response_size(); - - // Just read the response and discard it - uint8_t* buff = (uint8_t*) calloc(response_size, sizeof(uint8_t)); - int result = wooting_usb_read_response(buff, response_size); - free(buff); - #ifdef DEBUG_LOG - printf("Read result %d \n", result); - #endif - - if (command_size == WOOTING_COMMAND_SIZE && result == response_size) { - return true; - } - else { - #ifdef DEBUG_LOG - printf("Got command size: %d, expected: %d, disconnecting..\n", command_size, WOOTING_COMMAND_SIZE); - #endif - - wooting_usb_disconnect(true); - return false; - } +bool wooting_usb_send_feature(uint8_t commandId, uint8_t parameter0, + uint8_t parameter1, uint8_t parameter2, + uint8_t parameter3) { + if (!wooting_usb_find_keyboard()) { + return false; + } + +#ifdef DEBUG_LOG + printf("Sending feature: %d\n", commandId); +#endif + + int command_size = wooting_usb_send_feature_buff( + commandId, parameter0, parameter1, parameter2, parameter3); + size_t response_size = wooting_usb_get_response_size(); + + // Just read the response and discard it + uint8_t *buff = (uint8_t *)calloc(response_size, sizeof(uint8_t)); + int result = wooting_usb_read_response(buff, response_size); + free(buff); +#ifdef DEBUG_LOG + printf("Read result %d \n", result); +#endif + + if (command_size == WOOTING_COMMAND_SIZE && result == response_size) { + return true; + } else { +#ifdef DEBUG_LOG + printf("Got command size: %d, expected: %d, disconnecting..\n", + command_size, WOOTING_COMMAND_SIZE); +#endif + + wooting_usb_disconnect(true); + return false; + } } -int wooting_usb_send_feature_with_response(uint8_t *buff, size_t len, uint8_t commandId, uint8_t parameter0, uint8_t parameter1, uint8_t parameter2, uint8_t parameter3) { - if (!wooting_usb_find_keyboard()) { - return -1; - } - - #ifdef DEBUG_LOG - printf("Sending feature with response: %d\n", commandId); - #endif - - int command_size = wooting_usb_send_feature_buff(commandId, parameter0, parameter1, parameter2, parameter3); - if (command_size == WOOTING_COMMAND_SIZE) { - size_t response_size = wooting_usb_get_response_size(); - uint8_t* responseBuff = (uint8_t*) calloc(response_size, sizeof(uint8_t)); - - int result = wooting_usb_read_response(responseBuff, response_size); - - if (result == response_size) { - memcpy(buff, responseBuff, len); - free(responseBuff); - return result; - } else { - #ifdef DEBUG_LOG - printf("Got response size: %d, expected: %d, disconnecting..\n", result, (int)response_size); - #endif - - free(responseBuff); - wooting_usb_disconnect(true); - return -1; - } - } else { - #ifdef DEBUG_LOG - printf("Got command size: %d, expected: %d, disconnecting..\n", command_size, WOOTING_COMMAND_SIZE); - #endif - - wooting_usb_disconnect(true); - return false; - } +int wooting_usb_send_feature_with_response( + uint8_t *buff, size_t len, uint8_t commandId, uint8_t parameter0, + uint8_t parameter1, uint8_t parameter2, uint8_t parameter3) { + if (!wooting_usb_find_keyboard()) { + return -1; + } + +#ifdef DEBUG_LOG + printf("Sending feature with response: %d\n", commandId); +#endif + + int command_size = wooting_usb_send_feature_buff( + commandId, parameter0, parameter1, parameter2, parameter3); + if (command_size == WOOTING_COMMAND_SIZE) { + size_t response_size = wooting_usb_get_response_size(); + uint8_t *responseBuff = (uint8_t *)calloc(response_size, sizeof(uint8_t)); + + int result = wooting_usb_read_response(responseBuff, response_size); + + if (result == response_size) { + memcpy(buff, responseBuff, len); + free(responseBuff); + return result; + } else { +#ifdef DEBUG_LOG + printf("Got response size: %d, expected: %d, disconnecting..\n", result, + (int)response_size); +#endif + + free(responseBuff); + wooting_usb_disconnect(true); + return -1; + } + } else { +#ifdef DEBUG_LOG + printf("Got command size: %d, expected: %d, disconnecting..\n", + command_size, WOOTING_COMMAND_SIZE); +#endif + + wooting_usb_disconnect(true); + return false; + } } static void debug_print_buffer(uint8_t *buff, size_t len) { - #ifdef DEBUG_LOG - printf("Buffer content \n"); - for(int i = 0; i < len; i++ ) - { - printf("%d%s", buff[i], i < len-1 ? ", " : ""); - } - printf("\n"); - #endif +#ifdef DEBUG_LOG + printf("Buffer content \n"); + for (int i = 0; i < len; i++) { + printf("%d%s", buff[i], i < len - 1 ? ", " : ""); + } + printf("\n"); +#endif } -int wooting_usb_read_response_timeout(uint8_t *buff, size_t len, int milliseconds) -{ - int result = hid_read_timeout(keyboard_handle, buff, len, milliseconds); - #ifdef DEBUG_LOG - printf("hid_read_timeout result code: %d\n", result); - #endif - debug_print_buffer(buff, len); - return result; +int wooting_usb_read_response_timeout(uint8_t *buff, size_t len, + int milliseconds) { + int result = hid_read_timeout(keyboard_handle, buff, len, milliseconds); +#ifdef DEBUG_LOG + printf("hid_read_timeout result code: %d\n", result); +#endif + debug_print_buffer(buff, len); + return result; } -int wooting_usb_read_response(uint8_t *buff, size_t len) -{ - int result = hid_read(keyboard_handle, buff, len); - #ifdef DEBUG_LOG - printf("hid_read result code: %d\n", result); - #endif - debug_print_buffer(buff, len); - return result; +int wooting_usb_read_response(uint8_t *buff, size_t len) { + int result = hid_read(keyboard_handle, buff, len); +#ifdef DEBUG_LOG + printf("hid_read result code: %d\n", result); +#endif + debug_print_buffer(buff, len); + return result; } diff --git a/src/wooting-usb.h b/src/wooting-usb.h index 9a8ef9e..5301dfc 100644 --- a/src/wooting-usb.h +++ b/src/wooting-usb.h @@ -22,8 +22,10 @@ extern "C" { #define WOOTINGRGBSDK_API #endif -#include +#include "stdbool.h" +#include "stdint.h" #include +#include typedef void (*void_cb)(void);