Added fft visualizer

master
Mark 2022-07-07 17:12:13 -07:00
parent 66e7100d4b
commit ac7423c1f3
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
7 changed files with 180 additions and 98 deletions

110
animations/fft.h Normal file
View File

@ -0,0 +1,110 @@
#ifdef RGB_MATRIX_DATAPOINTER_ENABLED
#ifndef DISABLE_RGB_MATRIX_FFT_ANIM
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.
// 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] = {
{ 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 }
};
// TODO:
// Dynamic color settings
bool FFT_ANIM(effect_params_t* params) {
RGB_MATRIX_USE_LIMITS(led_min, led_max);
// REQUIRED for any animation that uses this pointer.
// Clears any previous data.
if (params->init) {
rgb_matrix_anim_data = NULL;
return led_max < DRIVER_LED_TOTAL;
};
if (rgb_matrix_anim_data == NULL) {
// If animation data is empty, turn off all leds and exit.
for (uint8_t i = led_min; i < led_max; i++) {
rgb_matrix_set_color(
i,
0x00, 0x00, 0x00
);
}
} else {
// Data should be a pointer to 10 uint8_ts, each representing
// the height of a bar on the display.
uint8_t* data = (uint8_t*) rgb_matrix_anim_data;
for (uint8_t bin = 0; bin < 10; bin++) {
// Scale data values
uint8_t d = data[bin];
uint8_t bin_height = d / 50;
// Brightness of topmost key
uint8_t last_brightness = ((d % 50)/50.0) * 0xFF;
// Turn off leds that should be off.
// 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) {
continue;
}
rgb_matrix_set_color(
col_to_array[bin][i],
0x00, 0x00, 0x00
);
}
// 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) {
continue;
}
// If this is the topmost lit key, its
// brightness depends on the height of the bar.
if (i == bin_height - 1) {
rgb_matrix_set_color(
col_to_array[bin][i],
last_brightness, 0x00, last_brightness
);
// If this isn't the topmost key,
// it has a plain full-brightness color.
} else {
rgb_matrix_set_color(
col_to_array[bin][i],
0x00, 0x00, 0xFF
);
}
}
}
}
return led_max < DRIVER_LED_TOTAL;
}
#endif
#endif
#endif

View File

@ -12,6 +12,9 @@
//#define RGBLIGHT_EFFECT_TWINKLE //#define RGBLIGHT_EFFECT_TWINKLE
// Custom effects
#define RGB_MATRIX_DATAPOINTER_ENABLED
// Normal matrix effects // Normal matrix effects
//#define DISABLE_RGB_MATRIX_SOLID_COLOR //#define DISABLE_RGB_MATRIX_SOLID_COLOR

View File

@ -1,92 +1,6 @@
#include "keymap.h" #include "keymap.h"
// -1 => skip
int8_t 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 }
};
void cmd_animation(uint8_t *data, uint8_t length) {
uint8_t subcmd = data[1];
switch (subcmd) {
case 0x02: // Data input (fft)
for (uint8_t i = 0; i < DRIVER_LED_TOTAL; i++) {
// Turn off all LEDs
rgb_matrix_set_color(
i,
0x00, 0x00, 0x00
);
}
for (uint8_t bin = 0; bin < 10; bin++) {
uint8_t d = data[bin + 2];
uint8_t bin_height = d / 20;
uint8_t last_brightness = ((d % 20)/20.0) * 0xFF;
if (bin_height == 0) {
continue;
}
for (uint8_t i = 0; i < bin_height; i++) {
if (col_to_array[bin][i] < 0) {
continue;
}
if (i == bin_height - 1) {
rgb_matrix_set_color(
col_to_array[bin][i],
last_brightness, 0x00, last_brightness
);
} else {
rgb_matrix_set_color(
col_to_array[bin][i],
0x00, 0x00, 0xFF
);
}
}
}
break;
}
}
void raw_hid_receive(uint8_t *data, uint8_t length) {
uint8_t cmd = data[0];
switch (cmd) {
case 0x01: // animation
cmd_animation(data, length);
break;
default:
break;
}
/*
raw_hid_send(data, length);
ergodox_right_led_1_on();
_delay_ms(10);
ergodox_right_led_1_off();
i2c_start(0x14, ERGODOX_EZ_I2C_TIMEOUT);
i2c_write(0, ERGODOX_EZ_I2C_TIMEOUT);
i2c_stop();
*/
}
// Define custom keys // Define custom keys
// (Must be done BEFORE keymaps) // (Must be done BEFORE keymaps)
enum custom_keycodes { enum custom_keycodes {
@ -157,10 +71,7 @@ const uint8_t PROGMEM ledmap[][DRIVER_LED_TOTAL][3] = {
int current_lang = LANG_EN; int current_lang = LANG_EN;
void keyboard_post_init_user(void) { void keyboard_post_init_user(void) {}
//set_lang(LANG_EN);
}

View File

@ -1,9 +1,7 @@
# Set any rules.mk overrides for your specific keymap here. # rules.mk overrides
# See rules at https://docs.qmk.fm/#/config_options?id=the-rulesmk-file
CONSOLE_ENABLE = no
COMMAND_ENABLE = no
TAP_DANCE_ENABLE = yes TAP_DANCE_ENABLE = yes
SRC = \
matrix.c \ SRC += \
tapdance/wmlayout.c \ tapdance/wmlayout.c \
tapdance/tapdance.c tapdance/tapdance.c

53
rawhid.c Normal file
View File

@ -0,0 +1,53 @@
#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
void raw_hid_receive(uint8_t *data, uint8_t length) {
uint8_t cmd = data[0];
switch (cmd) {
#ifdef RGB_MATRIX_DATAPOINTER_ENABLED
case 0x01: // Animation
cmd_animation(data, length);
break;
#endif
default:
break;
}
// raw_hid_send(data, length);
}
#ifdef RGB_MATRIX_DATAPOINTER_ENABLED
void cmd_animation(uint8_t *data, uint8_t length) {
uint8_t subcmd = data[1];
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];
}
break;
}
}
#endif

1
rgb_matrix_kb.inc Normal file
View File

@ -0,0 +1 @@
#include "animations/fft.h"

View File

@ -35,8 +35,11 @@ MAGIC_ENABLE = no
DEBOUNCE_TYPE = eager_pr DEBOUNCE_TYPE = eager_pr
# project specific files SRC += \
SRC += matrix.c led_i2c.c matrix.c \
led_i2c.c \
rawhid.c
QUANTUM_LIB_SRC += i2c_master.c QUANTUM_LIB_SRC += i2c_master.c
LAYOUTS = ergodox LAYOUTS = ergodox
@ -45,6 +48,9 @@ LAYOUTS = ergodox
# Significantly reduces firmware size. # Significantly reduces firmware size.
LTO_ENABLE = yes LTO_ENABLE = yes
# Enable keyboard-specific effects
RGB_MATRIX_CUSTOM_KB = yes
MOUSE_SHARED_EP = no MOUSE_SHARED_EP = no
# FROM glow dir # FROM glow dir