diff --git a/animations/fft.h b/animations/fft.h index f9bf1c1..e11c649 100644 --- a/animations/fft.h +++ b/animations/fft.h @@ -5,12 +5,12 @@ RGB_MATRIX_EFFECT(FFT_ANIM) #ifdef RGB_MATRIX_CUSTOM_EFFECT_IMPLS // Maps fft columns to leds. -// col_to_array[i] returns an array of leds in the ith bin of the fft. +// fft_col_to_array[i] returns an array of leds in the ith bin of the fft. // Negative indices are "invisible" leds. // Use them when your keyboard has gaps. // // This layout is for the Ergodox EZ. -static int8_t col_to_array[10][5] = { +static int8_t fft_col_to_array[10][5] = { { 47, 43, 38, 33, 28 }, { 46, 42, 37, 32, 27 }, { 45, 41, 36, 31, 26 }, @@ -62,13 +62,13 @@ bool FFT_ANIM(effect_params_t* params) { // There are 5 keys in each column. for (uint8_t i = bin_height; i < 6; i++) { // Ignore negative indices. - // See col_to_array definition. - if (col_to_array[bin][i] < 0) { + // See fft_col_to_array definition. + if (fft_col_to_array[bin][i] < 0) { continue; } rgb_matrix_set_color( - col_to_array[bin][i], + fft_col_to_array[bin][i], 0x00, 0x00, 0x00 ); } @@ -77,8 +77,8 @@ bool FFT_ANIM(effect_params_t* params) { // Turn on leds that should be on. for (uint8_t i = 0; i < bin_height; i++) { // Ignore negative indices. - // See col_to_array definition. - if (col_to_array[bin][i] < 0) { + // See fft_col_to_array definition. + if (fft_col_to_array[bin][i] < 0) { continue; } @@ -86,7 +86,7 @@ bool FFT_ANIM(effect_params_t* params) { // brightness depends on the height of the bar. if (i == bin_height - 1) { rgb_matrix_set_color( - col_to_array[bin][i], + fft_col_to_array[bin][i], last_brightness, 0x00, last_brightness ); @@ -94,7 +94,7 @@ bool FFT_ANIM(effect_params_t* params) { // it has a plain full-brightness color. } else { rgb_matrix_set_color( - col_to_array[bin][i], + fft_col_to_array[bin][i], 0x00, 0x00, 0xFF ); } diff --git a/betalupi_ergodox.c b/betalupi_ergodox.c index 098d888..40d6802 100644 --- a/betalupi_ergodox.c +++ b/betalupi_ergodox.c @@ -19,6 +19,7 @@ along with this program. If not, see . */ #include "betalupi_ergodox.h" +#include "rawhid.h" extern inline void ergodox_board_led_on(void); extern inline void ergodox_right_led_1_on(void); @@ -336,7 +337,14 @@ bool process_record_kb(uint16_t keycode, keyrecord_t *record) { } eeconfig_update_kb(keyboard_config.raw); } + + hid_send_state(); return false; + + case RGB_MOD: + // Tell the host whenever we change animation mode. + hid_send_state(); + return true; #endif // Custom RGBLIGHT macros diff --git a/config.h b/config.h index ffebbf6..c2b97f6 100644 --- a/config.h +++ b/config.h @@ -13,6 +13,11 @@ #define USB_MAX_POWER_CONSUMPTION 500 #define USB_SUSPEND_WAKEUP_DELAY 0 +// raw HID params +#define RAW_USAGE_PAGE 0xFF60 +#define RAW_USAGE_ID 0x61 + + // Key matrix info #define MATRIX_ROWS 14 #define MATRIX_ROWS_PER_SIDE (MATRIX_ROWS / 2) diff --git a/keymaps/default/keymap.h b/keymaps/default/keymap.h index 300a5ec..cb52875 100644 --- a/keymaps/default/keymap.h +++ b/keymaps/default/keymap.h @@ -32,7 +32,6 @@ #include "layers.h" #include "tapdance.h" -#include "raw_hid.h" #define KC_MAC_UNDO LGUI(KC_Z) diff --git a/rawhid.c b/rawhid.c index 6a4a8b6..edb3369 100644 --- a/rawhid.c +++ b/rawhid.c @@ -1,28 +1,22 @@ -#include "betalupi_ergodox.h" - -void raw_hid_receive(uint8_t *data, uint8_t length); - -#ifdef RGB_MATRIX_DATAPOINTER_ENABLED - void cmd_animation(uint8_t *data, uint8_t length); - - // Animation data. - // Data received from host is saved here, - // and rgb_matrix_anim_data points to this array when necessary. - uint8_t hid_anim_data[32]; - - // Datapointer - extern void* rgb_matrix_anim_data; -#endif - -// END HEADERS +#include "rawhid.h" +uint8_t hid_anim_data[32]; +// See rawhid.h for prococol documentation void raw_hid_receive(uint8_t *data, uint8_t length) { uint8_t cmd = data[0]; switch (cmd) { + case CMD_HELLO: + ergodox_right_led_1_on(); + _delay_ms(50); + ergodox_right_led_1_off(); + + hid_send_state(); + break; + #ifdef RGB_MATRIX_DATAPOINTER_ENABLED - case 0x01: // Animation + case CMD_ANIM_DATA: cmd_animation(data, length); break; #endif @@ -31,21 +25,59 @@ void raw_hid_receive(uint8_t *data, uint8_t length) { break; } - // raw_hid_send(data, length); } + + +void hid_send_state() { + uint8_t packet[RAW_EPSIZE] = { + CMD_SEND_STATE + }; + + + if (rgb_matrix_get_flags() != LED_FLAG_ALL) { + // RGB matrix is disabled + packet[1] = 0x00; + } else { + uint8_t mode = rgb_matrix_get_mode(); + switch (mode) { + case RGB_MATRIX_CUSTOM_FFT_ANIM: + // FFT Animation is active + packet[1] = 0x02; + break; + + default: + // Normal animation is active + packet[1] = 0x01; + break; + } + } + + // Note that all sent packets MUST be + // RAW_EPSIZE long. + raw_hid_send(packet, RAW_EPSIZE); +} + + + #ifdef RGB_MATRIX_DATAPOINTER_ENABLED void cmd_animation(uint8_t *data, uint8_t length) { - uint8_t subcmd = data[1]; + switch (data[1]) { + case CMD_ANIM_DATA_fft: + // Only read data if animation is in fft mode + if (rgb_matrix_get_mode() == RGB_MATRIX_CUSTOM_FFT_ANIM) { + // rgb_matrix_anim_data is set to NULL at animation init. + if (rgb_matrix_anim_data == NULL) { rgb_matrix_anim_data = hid_anim_data; } - switch (subcmd) { - case 0x02: // Data input (fft) - - // TODO: assign pointer smartly. - rgb_matrix_anim_data = hid_anim_data; - - for (uint8_t bin = 0; bin < 10; bin++) { - hid_anim_data[bin] = data[bin + 2]; + // Copy data into rgb matrix array + memcpy( + hid_anim_data, + data + 2, + sizeof(uint8_t) + 10 + ); + } else { + // If not in fft mode and we receive fft data, send a state packet so host stops sending data. + hid_send_state(); } break; } diff --git a/rawhid.h b/rawhid.h new file mode 100644 index 0000000..6dcaca1 --- /dev/null +++ b/rawhid.h @@ -0,0 +1,55 @@ +#pragma once +#include "betalupi_ergodox.h" +#include "raw_hid.h" + + +void raw_hid_receive(uint8_t *data, uint8_t length); +void hid_send_state(void); + +#ifdef RGB_MATRIX_DATAPOINTER_ENABLED + void cmd_animation(uint8_t *data, uint8_t length); + + // Animation data. + // Data received from host is saved here, + // and rgb_matrix_anim_data points to this array when necessary. + extern uint8_t hid_anim_data[32]; + + // Datapointer + extern void* rgb_matrix_anim_data; +#endif + + + +// Sent by host when connection is initiated. +#define CMD_HELLO 0x00 + +// Send keyboard state to host. +// +// Packet structure: +// Data: | cmd | anim state | +// # of Bytes: | 1 | 1 | +// +// anim state: +// 0x00: RGBMatrix disabled +// 0x01: normal animation, no HID data. +// 0x02: FFT Animation +#define CMD_SEND_STATE 0x01 + + +// Animation data. Sent by host. +// +// Packet structure: +// Data: | cmd | data type | data | +// # of Bytes: | 1 | 1 | ? | +// +// data type: +// Which animation this data is for. These are defined below. +// +// data: +// Animation data. Content depends on data type. +#define CMD_ANIM_DATA 0x02 + +// Data for FFT animation. +// Data segment consists of 10 bits, each representing the height of a column. +// Minimum height is 0, maximum is 250. +#define CMD_ANIM_DATA_fft 0x00