Skip to content

Commit

Permalink
Added Blur Slider
Browse files Browse the repository at this point in the history
  • Loading branch information
connornishijima committed May 24, 2024
1 parent 6cbc29d commit e00214a
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ void parse_command(uint32_t t_now_ms, command com) {

update_ui(UI_NEEDLE_EVENT, configuration.speed);
}
else if (fastcmp(substring, "blur")) {
// Get blur value
fetch_substring(com.command, '|', 2);
float setting_value = atof(substring);
configuration.blur = setting_value;

//update_ui(UI_NEEDLE_EVENT, configuration.blur);
}
else if (fastcmp(substring, "color")) {
// Get color value
fetch_substring(com.command, '|', 2);
Expand Down
9 changes: 9 additions & 0 deletions src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ void load_config(){

// Auto Color Cycling
configuration.auto_color_cycle = preferences.getBool("auto_color", false);

// Blur
configuration.blur = preferences.getFloat("blur", 0.00);
}

void sync_configuration_to_client() {
Expand Down Expand Up @@ -146,6 +149,11 @@ void sync_configuration_to_client() {
snprintf(config_item_buffer, 120, "new_config|auto_color_cycle|int|%d", configuration.auto_color_cycle);
websocket_handler.sendAll(config_item_buffer);

// blur
memset(config_item_buffer, 0, 120);
snprintf(config_item_buffer, 120, "new_config|blur|float|%.3f", configuration.blur);
websocket_handler.sendAll(config_item_buffer);

websocket_handler.sendAll("config_ready");
}

Expand All @@ -165,6 +173,7 @@ bool save_config() {
preferences.putBool("dithering", configuration.temporal_dithering);
preferences.putBool("reverse_color", configuration.reverse_color_range);
preferences.putBool("auto_color", configuration.auto_color_cycle);
preferences.putFloat("blur", configuration.blur);

return true;
}
Expand Down
6 changes: 6 additions & 0 deletions src/gpu_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ void run_gpu() {

apply_background(configuration.background);

//scramble_image( configuration.blur * 50.0 );

apply_fast_blur( configuration.blur * 8.0 );

draw_ui_overlay(); // (ui.h)

if( EMOTISCOPE_ACTIVE == true && configuration.screensaver == true){
Expand Down Expand Up @@ -89,6 +93,8 @@ void run_gpu() {
//clip_leds();
apply_tonemapping();

//apply_fractional_blur( leds, NUM_LEDS, configuration.blur * 10.0 );

//apply_frame_blending( configuration.softness );
//apply_phosphor_decay( configuration.softness );

Expand Down
85 changes: 83 additions & 2 deletions src/leds.h
Original file line number Diff line number Diff line change
Expand Up @@ -544,11 +544,55 @@ void apply_frame_blending(float blend_amount){
memcpy(previous_frame, leds, sizeof(CRGBF) * NUM_LEDS);
}

void apply_fractional_blur(CRGBF* pixels, uint16_t num_pixels, float kernel_size) {
if(kernel_size >= 0.001){
// Ensure kernel_size is positive
if (kernel_size <= 0) {
printf("Kernel size must be positive.\n");
return;
}

// Calculate the effective range of influence for the kernel size
int range = (int)ceil(kernel_size * 3); // Typically 3 standard deviations are considered
CRGBF temp_pixels[num_pixels];
memset(temp_pixels, 0, sizeof(temp_pixels));

for (int i = 0; i < num_pixels; ++i) {
float total_weight = 0;
CRGBF weighted_sum = {0.0f, 0.0f, 0.0f};

// Apply weights to the pixels in the range
for (int k = -range; k <= range; ++k) {
int pixel_index = i + k;
if (pixel_index < 0) pixel_index = 0;
if (pixel_index >= num_pixels) pixel_index = num_pixels - 1;

// Calculate the weight using a Gaussian-like function
float distance = fabs(k);
float weight = exp(-0.5 * (distance / kernel_size) * (distance / kernel_size));

weighted_sum.r += pixels[pixel_index].r * weight;
weighted_sum.g += pixels[pixel_index].g * weight;
weighted_sum.b += pixels[pixel_index].b * weight;

total_weight += weight;
}

// Normalize the weighted sum by the total weight
temp_pixels[i].r = weighted_sum.r / total_weight;
temp_pixels[i].g = weighted_sum.g / total_weight;
temp_pixels[i].b = weighted_sum.b / total_weight;
}

// Copy the blurred values back to the original array
memcpy(pixels, temp_pixels, sizeof(CRGBF) * num_pixels);
}
}

void apply_box_blur(CRGBF* pixels, uint16_t num_pixels, int kernel_size) {
// Ensure kernel size is odd for symmetry around the central pixel
if (kernel_size % 2 == 0) {
printf("Kernel size must be odd.\n");
return;
kernel_size -= 1;
}

int half_kernel = kernel_size / 2;
Expand Down Expand Up @@ -752,4 +796,41 @@ void apply_tonemapping() {
leds[i].g = soft_clip_hdr(leds[i].g);
leds[i].b = soft_clip_hdr(leds[i].b);
}
}

void scramble_image( float distance ){
memset(leds_temp, 0, sizeof(CRGBF) * NUM_LEDS);

// Scramble the image
for(uint16_t i = 0; i < NUM_LEDS; i++){
// Use ESP32-S3 hardware RNG to get a float between 0.0 and 1.0
float random_value = get_random_float();

float splat_position = (random_value - 0.5) * 2.0;
float splat_opacity = 1.0 - fabs(splat_position);

splat_position *= distance;

// Calculate the new index for the pixel (signed integer)
int16_t new_index = i + (int16_t)(splat_position);

if(new_index > 0 && new_index < NUM_LEDS){
// Copy the pixel to the new index
leds_temp[new_index] = add(leds_temp[new_index], leds[i], splat_opacity);
}
}

memcpy(leds, leds_temp, sizeof(CRGBF) * NUM_LEDS);
}

void apply_fast_blur( float kernel_size ){
if(kernel_size == 0.0){
return;
}

apply_box_blur( leds, NUM_LEDS, 3.0 + kernel_size );

if(kernel_size > 1.0){
apply_box_blur( leds, NUM_LEDS, 3.0 + kernel_size );
}
}
1 change: 1 addition & 0 deletions src/sliders.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ void load_sliders_relevant_to_mode(int16_t mode_index) {
register_slider("saturation", 0.000, 1.000, 0.001);
register_slider("warmth", 0.000, 1.000, 0.001);
register_slider("background", 0.000, 1.000, 0.001);
register_slider("blur", 0.000, 1.000, 0.001);
}
1 change: 1 addition & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,5 @@ struct config {
bool temporal_dithering;
bool auto_color_cycle;
bool reverse_color_range;
float blur;
};
26 changes: 26 additions & 0 deletions src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ uint8_t noise_samples[TOTAL_NOISE_SAMPLES];
float num_leds_float_lookup[NUM_LEDS];
float num_freqs_float_lookup[NUM_FREQS];
float num_tempi_float_lookup[NUM_TEMPI];
float random_float_lookup[1024];

char substring[128];

Expand All @@ -42,10 +43,17 @@ void init_num_tempi_float_lookup(){
}
}

void init_random_float_lookup(){
for(uint16_t i = 0; i < 1024; i++){
random_float_lookup[i] = esp_random() / (float)UINT32_MAX;
}
}

void init_floating_point_lookups(){
init_num_leds_float_lookup();
init_num_freqs_float_lookup();
init_num_tempi_float_lookup();
init_random_float_lookup();
}

void init_noise_samples(){
Expand All @@ -54,6 +62,24 @@ void init_noise_samples(){
}
}

float get_random_float(){
static float position = 0.0;

uint32_t index = uint32_t(position * 1023);
float push = random_float_lookup[index];
if(push > 0.5){
push += push*0.1;
}

position += (push*0.01);

while(position >= 1.0){
position -= 1.0;
}

return push;
}

void fetch_substring(char* input_buffer, char delimiter, uint8_t fetch_index){
memset(substring, 0, 128);
int16_t input_length = strlen(input_buffer);
Expand Down

0 comments on commit e00214a

Please sign in to comment.