2022-07-08 10:50:17 -07:00
|
|
|
#include "rawhid.h"
|
2022-07-11 10:39:06 -07:00
|
|
|
#include "extra_mappings.h"
|
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) {
|
2022-07-20 20:08:03 -07:00
|
|
|
|
|
|
|
#ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
|
2022-07-08 10:50:17 -07:00
|
|
|
case RGB_MATRIX_CUSTOM_FFT_ANIM:
|
|
|
|
// FFT Animation is active
|
|
|
|
packet[1] = 0x02;
|
|
|
|
break;
|
2022-07-20 20:08:03 -07:00
|
|
|
#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-07-09 20:34:43 -07:00
|
|
|
#ifdef RGB_MATRIX_FRAMEBUFFER_EFFECTS
|
|
|
|
|
|
|
|
|
|
|
|
#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
|