QMK/keyboards/betalupi_ergodox/rawhid.c

137 lines
2.9 KiB
C
Raw Normal View History

2022-07-08 10:50:17 -07:00
#include "rawhid.h"
2022-07-07 17:12:13 -07:00
2022-07-08 10:50:17 -07:00
uint8_t hid_anim_data[32];
2022-07-07 17:12:13 -07:00
2022-07-08 10:50:17 -07:00
// See 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-09 20:34:43 -07:00
#ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
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
default:
break;
}
}
2022-07-08 10:50:17 -07:00
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);
}
2022-07-09 20:34:43 -07:00
#ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
// Maps fft columns to leds.
// 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 fft_col_to_array[10][5] = {
{ 47, 43, 38, 33, 28 },
{ 46, 42, 37, 32, 27 },
{ 45, 41, 36, 31, 26 },
{ 44, 40, 35, 30, 25 },
{ -1, 39, 34, 29, 24 },
{ -1, 15, 10, 5, 0 },
{ 20, 16, 11, 6, 1 },
{ 21, 17, 12, 7, 2 },
{ 22, 18, 13, 8, 3 },
{ 23, 19, 14, 9, 4 }
};
#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