Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2 cool new features + some refractor #241

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
265 changes: 265 additions & 0 deletions KeyboardVisualizer.pro.user

Large diffs are not rendered by default.

264 changes: 64 additions & 200 deletions KeyboardVisualizerCommon/Visualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ void Visualizer::Initialize()
frgd_mode = VISUALIZER_PATTERN_STATIC_GREEN_YELLOW_RED;
single_color_mode = VISUALIZER_SINGLE_COLOR_FOLLOW_FOREGROUND;
reactive_bkgd = false;
start_from_bot_inv = false;
start_from_bottom = false;
audio_device_idx = 0;
filter_constant = 1.0f;

Expand Down Expand Up @@ -378,6 +380,14 @@ void Visualizer::SaveSettings()
snprintf(out_str, 1024, "silent_bkgd=%d\r\n", silent_bkgd);
outfile.write(out_str, strlen(out_str));

//Save Silent Background Flag
snprintf(out_str, 1024, "start_from_bottom=%d\r\n", start_from_bottom);
outfile.write(out_str, strlen(out_str));

//Save Silent Background Flag
snprintf(out_str, 1024, "start_from_bot_inv=%d\r\n", start_from_bot_inv);
outfile.write(out_str, strlen(out_str));

//Save Background Timeout
snprintf(out_str, 1024, "background_timeout=%d\r\n", background_timeout);
outfile.write(out_str, strlen(out_str));
Expand Down Expand Up @@ -441,6 +451,8 @@ void Visualizer::SendSettings()
settings.frgd_mode = frgd_mode;
settings.reactive_bkgd = reactive_bkgd;
settings.silent_bkgd = silent_bkgd;
settings.start_from_bot_inv = start_from_bot_inv;
settings.start_from_bottom = start_from_bottom;
settings.background_timeout = background_timeout;
port->tcp_write((char *)&settings, sizeof(settings));
}
Expand Down Expand Up @@ -653,9 +665,11 @@ void Visualizer::Update()
fft[i] = sum / (2 * avg_size + 1);
}
}

for(int i = 0; i < 256; i++)
{
fft_fltr[i] = fft_fltr[i] + (filter_constant * (fft[i] - fft_fltr[i]));
float current = fft[i];
fft_fltr[i] = fft_fltr[i] + (filter_constant * (current - fft_fltr[i]));
}
}

Expand Down Expand Up @@ -891,84 +905,6 @@ void Visualizer::DrawPattern(VISUALIZER_PATTERN pattern, int bright, vis_pixels
{
switch (pattern)
{
case VISUALIZER_PATTERN_SOLID_BLACK:
DrawSolidColor(bright, 0x00000000, pixels);
break;

case VISUALIZER_PATTERN_SOLID_WHITE:
DrawSolidColor(bright, 0x00FFFFFF, pixels);
break;

case VISUALIZER_PATTERN_SOLID_RED:
DrawSolidColor(bright, 0x000000FF, pixels);
break;

case VISUALIZER_PATTERN_SOLID_ORANGE:
DrawSolidColor(bright, 0x000040FF, pixels);
break;

case VISUALIZER_PATTERN_SOLID_YELLOW:
DrawSolidColor(bright, 0x0000FFFF, pixels);
break;

case VISUALIZER_PATTERN_SOLID_GREEN:
DrawSolidColor(bright, 0x0000FF00, pixels);
break;

case VISUALIZER_PATTERN_SOLID_CYAN:
DrawSolidColor(bright, 0x00FFFF00, pixels);
break;

case VISUALIZER_PATTERN_SOLID_BLUE:
DrawSolidColor(bright, 0x00FF0000, pixels);
break;

case VISUALIZER_PATTERN_SOLID_PURPLE:
DrawSolidColor(bright, 0x00FF0040, pixels);
break;

case VISUALIZER_PATTERN_STATIC_GREEN_YELLOW_RED:
{
RGBColor colors[] = { 0x0000FF00, 0x0000FFFF, 0x000000FF };
DrawHorizontalBars(bright, colors, 3, pixels);
}
break;

case VISUALIZER_PATTERN_STATIC_GREEN_WHITE_RED:
{
RGBColor colors[] = { 0x0000FF00, 0x00FFFFFF, 0x000000FF };
DrawHorizontalBars(bright, colors, 3, pixels);
}
break;

case VISUALIZER_PATTERN_STATIC_BLUE_CYAN_WHITE:
{
RGBColor colors[] = { 0x00FF0000, 0x00FFFF00, 0x00FFFFFF };
DrawHorizontalBars(bright, colors, 3, pixels);
}
break;

case VISUALIZER_PATTERN_STATIC_RED_WHITE_BLUE:
{
RGBColor colors[] = { 0x000000FF, 0x00FFFFFF, 0x00FF0000 };
DrawHorizontalBars(bright, colors, 3, pixels);
}
break;

case VISUALIZER_PATTERN_STATIC_RAINBOW:
{
RGBColor colors[] = { 0x000000FF, 0x0000FFFF, 0x0000FF00, 0x00FFFF00, 0x00FF0000, 0x00FF00FF };
DrawHorizontalBars(bright, colors, 6, pixels);
}
break;

case VISUALIZER_PATTERN_STATIC_RAINBOW_INVERSE:
{
RGBColor colors[] = { 0x00FF00FF, 0x00FF0000, 0x00FFFF00, 0x0000FF00, 0x0000FFFF, 0x000000FF };
DrawHorizontalBars(bright, colors, 6, pixels);
}
break;

case VISUALIZER_PATTERN_ANIM_RAINBOW_SINUSOIDAL:
DrawRainbowSinusoidal(bright, bkgd_step, pixels);
break;
Expand All @@ -992,6 +928,24 @@ void Visualizer::DrawPattern(VISUALIZER_PATTERN pattern, int bright, vis_pixels
case VISUALIZER_PATTERN_ANIM_SINUSOIDAL_CYCLE:
DrawSinusoidalCycle(bright, bkgd_step, pixels);
break;
default:
if(pattern <= VISUALIZER_SINGLE_COLOR_PURPLE)
DrawSolidColor(bright, colors[pattern], pixels);
else
{
std::vector<std::vector<RGBColor>> colors=
{
{ 0x0000FF00, 0x0000FFFF, 0x000000FF },
{ 0x0000FF00, 0x00FFFFFF, 0x000000FF },
{ 0x00FF0000, 0x00FFFF00, 0x00FFFFFF },
{ 0x000000FF, 0x00FFFFFF, 0x00FF0000 },
{ 0x000000FF, 0x0000FFFF, 0x0000FF00, 0x00FFFF00, 0x00FF0000, 0x00FF00FF },
{ 0x00FF00FF, 0x00FF0000, 0x00FFFF00, 0x0000FF00, 0x0000FFFF, 0x000000FF }
};
std::vector<RGBColor>& current = colors[pattern-VISUALIZER_PATTERN_STATIC_GREEN_YELLOW_RED];
DrawHorizontalBars(bright, (RGBColor*)current.data(), current.size(), pixels);
}
break;
}
}

Expand Down Expand Up @@ -1082,6 +1036,8 @@ void Visualizer::NetUpdateThreadFunction()
frgd_mode = ((settings_pkt_type *)buf)->frgd_mode;
reactive_bkgd = ((settings_pkt_type *)buf)->reactive_bkgd;
silent_bkgd = ((settings_pkt_type *)buf)->silent_bkgd;
start_from_bottom = ((settings_pkt_type *)buf)->start_from_bottom;
start_from_bot_inv = ((settings_pkt_type *)buf)->start_from_bot_inv;
background_timeout = ((settings_pkt_type *)buf)->background_timeout;
SetNormalization(nrml_ofst, nrml_scl);

Expand Down Expand Up @@ -1200,74 +1156,29 @@ void Visualizer::VisThreadFunction()
//Draw Bar Graph Foreground
if (y == ROW_IDX_BAR_GRAPH)
{
if (x < 128)
{
if ((fft_fltr[5] - 0.05f) >((1 / 128.0f)*(127-x)))
{
if (shutdown_flag == true)
{
int in_color = pixels_fg.pixels[y][x];
pixels_render->pixels[y][x] = RGB(((brightness * GetRValue(in_color))), ((brightness * GetGValue(in_color))), ((brightness * GetBValue(in_color))));
}
else
{
pixels_render->pixels[y][x] = pixels_fg.pixels[y][x];
}
}
else

bool A = start_from_bottom;
bool B = fft_fltr[5] > (start_from_bot_inv ? 256 - x : x) / 256.0f;
bool C = x < 128;
bool D = fft_fltr[5] - 0.05f > 1 / 128.0f*(127-x);
bool E = fft_fltr[5] - 0.05f > 1 / 128.0f*(x-128);

if([&]() -> bool { if(A) return B; if(C) return D; return E; }())
if (shutdown_flag == true)
{
if (reactive_bkgd || silent_bkgd)
{
if (!silent_bkgd || ((background_timer >= background_timeout) && (background_timeout > 0)))
{
int in_color = pixels_bg.pixels[y][x];
pixels_render->pixels[y][x] = RGB(((brightness * GetRValue(in_color))), ((brightness * GetGValue(in_color))), ((brightness * GetBValue(in_color))));
}
else
{
pixels_render->pixels[y][x] = RGB(0, 0, 0);
}
}
else
{
pixels_render->pixels[y][x] = pixels_bg.pixels[y][x];
}
int in_color = pixels_fg.pixels[y][x];
pixels_render->pixels[y][x] = RGB(((brightness * GetRValue(in_color))), ((brightness * GetGValue(in_color))), ((brightness * GetBValue(in_color))));
}
}
else
{
if ((fft_fltr[5] - 0.05f) >((1 / 128.0f)*((x-128))))
else pixels_render->pixels[y][x] = pixels_fg.pixels[y][x];
else if (reactive_bkgd || silent_bkgd)
if (!silent_bkgd || ((background_timer >= background_timeout) && (background_timeout > 0)))
{
if (shutdown_flag == true)
{
int in_color = pixels_fg.pixels[y][x];
pixels_render->pixels[y][x] = RGB(((brightness * GetRValue(in_color))), ((brightness * GetGValue(in_color))), ((brightness * GetBValue(in_color))));
}
else
{
pixels_render->pixels[y][x] = pixels_fg.pixels[y][x];
}
int in_color = pixels_bg.pixels[y][x];
pixels_render->pixels[y][x] = RGB(((brightness * GetRValue(in_color))), ((brightness * GetGValue(in_color))), ((brightness * GetBValue(in_color))));
}
else
{
if (reactive_bkgd || silent_bkgd)
{
if (!silent_bkgd || (background_timer >= background_timeout))
{
int in_color = pixels_bg.pixels[y][x];
pixels_render->pixels[y][x] = RGB(((brightness * GetRValue(in_color))), ((brightness * GetGValue(in_color))), ((brightness * GetBValue(in_color))));
}
else
{
pixels_render->pixels[y][x] = RGB(0, 0, 0);
}
}
else
{
pixels_render->pixels[y][x] = pixels_bg.pixels[y][x];
}
}
}
pixels_render->pixels[y][x] = RGB(0, 0, 0);
else pixels_render->pixels[y][x] = pixels_bg.pixels[y][x];
}
}
}
Expand All @@ -1280,69 +1191,21 @@ void Visualizer::VisThreadFunction()
if ((background_timeout <= 0 ) || (background_timer < background_timeout))
{
//Draw brightness based visualizer for single LED devices
switch (single_color_mode)
if(single_color_mode <= VISUALIZER_SINGLE_COLOR_PURPLE)
DrawSingleColorStatic(brightness, colors[single_color_mode], pixels_render);
else if(single_color_mode != VISUALIZER_SINGLE_COLOR_BACKGROUND)
{
case VISUALIZER_SINGLE_COLOR_BLACK:
DrawSingleColorStatic(brightness, 0x00000000, pixels_render);
break;

case VISUALIZER_SINGLE_COLOR_WHITE:
DrawSingleColorStatic(brightness, 0x00FFFFFF, pixels_render);
break;

case VISUALIZER_SINGLE_COLOR_RED:
DrawSingleColorStatic(brightness, 0x000000FF, pixels_render);
break;

case VISUALIZER_SINGLE_COLOR_ORANGE:
DrawSingleColorStatic(brightness, 0x000080FF, pixels_render);
break;

case VISUALIZER_SINGLE_COLOR_YELLOW:
DrawSingleColorStatic(brightness, 0x0000FFFF, pixels_render);
break;

case VISUALIZER_SINGLE_COLOR_GREEN:
DrawSingleColorStatic(brightness, 0x0000FF00, pixels_render);
break;

case VISUALIZER_SINGLE_COLOR_CYAN:
DrawSingleColorStatic(brightness, 0x00FFFF00, pixels_render);
break;

case VISUALIZER_SINGLE_COLOR_BLUE:
DrawSingleColorStatic(brightness, 0x00FF0000, pixels_render);
break;

case VISUALIZER_SINGLE_COLOR_PURPLE:
DrawSingleColorStatic(brightness, 0x00FF00FF, pixels_render);
break;

case VISUALIZER_SINGLE_COLOR_BACKGROUND:
//Intentionally do nothing, leave the background unmodified
break;

case VISUALIZER_SINGLE_COLOR_FOLLOW_BACKGROUND:
DrawSingleColorBackground(brightness, &pixels_bg, pixels_render);
break;

case VISUALIZER_SINGLE_COLOR_FOLLOW_FOREGROUND:
DrawSingleColorForeground(brightness, &pixels_fg, pixels_render);
break;
if(single_color_mode == VISUALIZER_SINGLE_COLOR_FOLLOW_BACKGROUND)
DrawSingleColorBackground(brightness, &pixels_bg, pixels_render);
else
DrawSingleColorForeground(brightness, &pixels_fg, pixels_render);
}
}


//Swap buffers
if (pixels_render == &pixels_vs1)
{
pixels_render = &pixels_vs2;
pixels_out = &pixels_vs1;
}
else
{
pixels_render = &pixels_vs1;
pixels_out = &pixels_vs2;
}
pixels_render = (pixels_render == &pixels_vs1) ? &pixels_vs2 : &pixels_vs1;
pixels_out = (pixels_out == &pixels_vs1) ? &pixels_vs2 : &pixels_vs1;

//Increment background step
bkgd_step = bkgd_step += (anim_speed / 100.0f);
Expand Down Expand Up @@ -1797,6 +1660,7 @@ void Visualizer::LEDUpdateThreadFunction()
break;

case ZONE_TYPE_LINEAR:

for (int x = 0; x < x_count; x++)
{
controller->zones[zone_idx].colors[x] = pixels_out->pixels[ROW_IDX_BAR_GRAPH][zone_index_map->x_index[x]];
Expand Down
4 changes: 4 additions & 0 deletions KeyboardVisualizerCommon/Visualizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ typedef struct
int frgd_mode;
bool reactive_bkgd;
bool silent_bkgd;
bool start_from_bottom;
bool start_from_bot_inv;
unsigned int background_timeout;
} settings_pkt_type;

Expand Down Expand Up @@ -199,6 +201,8 @@ class Visualizer
int bkgd_mode;
bool reactive_bkgd;
bool silent_bkgd;
bool start_from_bottom;
bool start_from_bot_inv;
unsigned int background_timeout;
unsigned int background_timer;

Expand Down
2 changes: 2 additions & 0 deletions KeyboardVisualizerCommon/VisualizerDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ enum
VISUALIZER_NUM_SINGLE_COLOR
};

constexpr int colors[] = {0x00000000, 0x00FFFFFF, 0x000000FF, 0x000080FF, 0x0000FFFF, 0x0000FF00, 0x00FFFF00, 0x00FF0000, 0x00FF00FF};

const char * const visualizer_single_color_labels[VISUALIZER_NUM_SINGLE_COLOR] =
{
"Black",
Expand Down
Loading