QMK/keyboards/betalupi_ergodox/features/beta_rawhid.c

187 lines
3.7 KiB
C
Raw Normal View History

#include "betalupi_ergodox.h"
2022-07-21 17:49:35 -07:00
#include "extra_mappings.h"
#include "features/beta_rawhid.h"
2022-07-21 17:49:35 -07:00
#include "features/rawhid_commands.h"
2022-07-07 17:12:13 -07:00
extern uint8_t layer_layouts[];
2022-07-21 17:49:35 -07:00
2022-11-20 20:45:13 -08:00
#ifdef ENABLE_HID_SPELLCHECK
#include "features/hid_spellcheck.h"
2022-07-21 17:49:35 -07:00
#endif
2022-07-20 21:19:13 -07:00
// See features/beta_rawhid.h for prococol documentation
2022-07-07 17:12:13 -07:00
void raw_hid_receive(uint8_t *data, uint8_t length) {
uint8_t cmd = data[0];
switch (cmd) {
2022-07-08 10:50:17 -07:00
case CMD_HELLO:
ergodox_right_led_1_on();
_delay_ms(50);
ergodox_right_led_1_off();
hid_send_state();
break;
2022-07-21 17:49:35 -07:00
#ifdef ENABLE_RGB_MATRIX_FFT_ANIM
2022-07-08 10:50:17 -07:00
case CMD_ANIM_DATA:
2022-07-07 17:12:13 -07:00
cmd_animation(data, length);
break;
#endif
2022-11-20 20:45:13 -08:00
#ifdef ENABLE_HID_SPELLCHECK
2022-07-20 21:19:13 -07:00
case CMD_SPELLCHECK_WORD:
if (data[1]) {
ergodox_right_led_1_on();
_delay_ms(50);
ergodox_right_led_1_off();
}
break;
2022-07-21 17:49:35 -07:00
#endif
2022-07-20 21:19:13 -07:00
2022-07-07 17:12:13 -07:00
default:
break;
}
}
uint8_t nth_byte(size_t n, uint32_t data) {
return (data >> (8 * n)) & 0xFF;
}
// state: layer state.
// If this is 0, we'll use global layer_state instead.
void _hid_send_state(_hid_send_state_args args) {
2022-07-07 17:12:13 -07:00
uint32_t state = args.state ? args.state : layer_state;
2022-07-07 17:12:13 -07:00
2022-07-08 10:50:17 -07:00
uint8_t packet[RAW_EPSIZE] = {
CMD_SEND_STATE,
// Animation state, set later
0x00,
// Layer state
nth_byte(0, state),
nth_byte(1, state),
nth_byte(2, state),
nth_byte(3, state),
// Keymap for active layer
layer_layouts[biton32(state)]
2022-07-08 10:50:17 -07:00
};
// Set second byte
2022-07-08 10:50:17 -07:00
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) {
2022-07-21 17:49:35 -07:00
#ifdef ENABLE_RGB_MATRIX_FFT_ANIM
2022-07-08 10:50:17 -07:00
case RGB_MATRIX_CUSTOM_FFT_ANIM:
// FFT Animation is active
packet[1] = 0x02;
break;
#endif
2022-07-08 10:50:17 -07:00
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);
}
2022-07-11 10:39:06 -07:00
2022-08-10 09:11:09 -07:00
void hid_send_special_char(uint16_t char_id) {
uint8_t packet[RAW_EPSIZE] = {
CMD_SPECIAL_CHAR,
(char_id >> 0) & 0xFF,
(char_id >> 8) & 0xFF
};
raw_hid_send(packet, RAW_EPSIZE);
}
2022-11-20 20:45:13 -08:00
#ifdef ENABLE_HID_SPELLCHECK
2022-07-20 21:19:13 -07:00
void hid_send_word() {
uint8_t packet[RAW_EPSIZE] = {
CMD_SPELLCHECK_WORD,
spellcheck_buffer_size
};
for (int i = 0; i < 2 + spellcheck_buffer_size; i++) {
packet[i + 2] = spellcheck_buffer[i];
}
raw_hid_send(packet, RAW_EPSIZE);
}
2022-07-21 17:49:35 -07:00
#endif
2022-07-20 21:19:13 -07:00
2022-07-21 17:49:35 -07:00
#ifdef ENABLE_RGB_MATRIX_FFT_ANIM
2022-07-09 20:34:43 -07:00
#define FFT_PER_KEY 50
2022-07-08 10:50:17 -07:00
void cmd_animation(uint8_t *data, uint8_t length) {
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) {
2022-07-09 20:34:43 -07:00
// Data should be a pointer to 10 uint8_ts, each representing
// the height of a bar on the display.
for (uint8_t bin = 0; bin < 10; bin++) {
// Scale data values
uint8_t d = data[bin + 2];
for (uint8_t i = 0; i < 5; i++) {
// Ignore negative indices.
// See fft_col_to_array definition.
if (fft_col_to_array[bin][i] < 0) {
if (d >= FFT_PER_KEY) {
d -= FFT_PER_KEY;
} else {
d = 0;
}
continue;
}
uint8_t row = fft_col_to_array[bin][i] % MATRIX_ROWS;
uint8_t col = fft_col_to_array[bin][i] / MATRIX_ROWS;
if (d >= FFT_PER_KEY) {
g_rgb_frame_buffer[row][col] = 0xFF;
d -= FFT_PER_KEY;
} else if (d > 0) {
g_rgb_frame_buffer[row][col] = ((double) d / FFT_PER_KEY) * 0xFF;
d = 0;
} else {
g_rgb_frame_buffer[row][col] = 0;
}
/*
rgb_matrix_set_color(
fft_col_to_array[bin][i],
last_brightness, 0x00, last_brightness
);
*/
}
}
2022-07-08 10:50:17 -07:00
} else {
// If not in fft mode and we receive fft data, send a state packet so host stops sending data.
hid_send_state();
2022-07-07 17:12:13 -07:00
}
break;
}
}
#endif