Skip to content

Commit

Permalink
New gamma correction method
Browse files Browse the repository at this point in the history
  • Loading branch information
connornishijima committed May 19, 2024
1 parent 09a0ea0 commit 3bcf596
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 134 deletions.
11 changes: 7 additions & 4 deletions data/js/websockets_connection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const MAX_PING_PONG_REPLY_TIME_MS = 4000;
const MAX_CONNECTION_TIME_MS = 3000;
const MAX_CONNECTION_TIME_MS = 10000;
const AUTO_RECONNECT = true;

let ws;
Expand Down Expand Up @@ -387,7 +387,7 @@ function reconnect_websockets(){
set_ui_locked_state(true);
setTimeout(function(){
window.location.reload();
}, 2000);
}, 500);
}
}
}
Expand All @@ -398,8 +398,11 @@ function open_websockets_connection_to_device(){
connection_pending = true;
setInterval(check_connection_timeout, 100);

console.log("CONNECTING TO "+device_ip);
ws = new WebSocket("ws://"+device_ip+":80/ws");
websockets_server = "ws://"+device_ip+":80/ws";

console.log("CONNECTING TO WS SERVER: "+websockets_server);
ws = new WebSocket(websockets_server);

document.getElementById("device_nickname").innerHTML = device_ip;

ws.onopen = function(e) {
Expand Down
7 changes: 3 additions & 4 deletions src/EMOTISCOPE_FIRMWARE.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
// Welcome to the Emotiscope Engine. This is firmware which:
//
// - Logs raw audio data from the microphone into buffers
// - Detects frequencies in the audio using many Goertzel filters
// - Detects frequencies in the audio using many Goertzel filters at once
// - Detects the BPM of music
// - Syncronizes to the beats of said music
// - Checks the touch sensors for input
// - Hosts an HTTP server for a web app
// - Talks to that web app over a high speed ws:// connection
// - Stores settings in flash memory
// - Draws custom light show modes to the LEDs which react to music
// in real-time with a variety of effects
// - Draws custom light show modes to the LEDs which react to
// music in real-time with a variety of effects
// - Runs the indicator light
// - Runs the screensaver
// - Applies a blue-light filter to the LEDs
Expand Down Expand Up @@ -56,7 +56,6 @@
#define SOFTWARE_VERSION_MINOR ( 2 )
#define SOFTWARE_VERSION_PATCH ( 0 )


// ############################################################################
// ## DEPENDENCIES ############################################################

Expand Down
85 changes: 0 additions & 85 deletions src/EMOTISCOPE_FIRMWARE.ino.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion src/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void parse_command(uint32_t t_now_ms, command com) {
// Get saturation value
fetch_substring(com.command, '|', 2);
float setting_value = atof(substring);
configuration.saturation = sqrt(clip_float(setting_value));
configuration.saturation = sqrt(sqrt(clip_float(setting_value)));

//update_ui(UI_NEEDLE_EVENT, configuration.saturation);
}
Expand Down
40 changes: 40 additions & 0 deletions src/led_driver.h

Large diffs are not rendered by default.

48 changes: 38 additions & 10 deletions src/leds.h
Original file line number Diff line number Diff line change
Expand Up @@ -614,16 +614,45 @@ void apply_gamma_correction_to_color(CRGBF* color, float gamma) {
}

void apply_gamma_correction() {
/*
profile_function([&]() {
dsps_mul_f32_ae32((float*)leds, (float*)leds, (float*)leds, NUM_LEDS*3, 1, 1, 1);
}, __func__);
*/

static bool first_run = true;
if(first_run == true){
first_run = false;

// Generate a lookup table for gamma correction
//printf("gamma_correction_lookup[2048] = {");

for(uint16_t i = 0; i < 2048; i++){
float gamma = 1.5;
float corrected = powf(
(float)i / 2047.0,
gamma
);

//printf("%.4f, ", corrected);
gamma_correction_lookup[i] = corrected;
}

//printf("};\n");
}

for(uint16_t i = 0; i < NUM_LEDS; i++){
leds[i].r = gamma_correction_lookup[uint16_t(leds[i].r * 2047)];
leds[i].g = gamma_correction_lookup[uint16_t(leds[i].g * 2047)];
leds[i].b = gamma_correction_lookup[uint16_t(leds[i].b * 2047)];
}
}

void apply_brightness() {
profile_function([&]() {
if(light_modes[configuration.current_mode].type == LIGHT_MODE_TYPE_SYSTEM){ return; }

float brightness_val = 0.3+configuration.brightness*0.7;
float brightness_val = 0.3 + (configuration.brightness*0.7);

scale_CRGBF_array_by_constant(leds, brightness_val, NUM_LEDS);
}, __func__);
Expand Down Expand Up @@ -686,7 +715,6 @@ void apply_background(float background_level){

// Apply background to the main buffer
scale_CRGBF_array_by_constant(leds_temp, background_level, NUM_LEDS);
//scale_CRGBF_array_by_constant(leds, 1.0 - background_level, NUM_LEDS);
add_CRGBF_arrays(leds, leds_temp, NUM_LEDS);
}
}, __func__);
Expand All @@ -708,14 +736,14 @@ void fade_display(){
}

float soft_clip_hdr(float input) {
if (input < 0.75) {
// Linear function: output is the same as input for values less than 0.75
return input;
} else {
// Non-linear function: transforms input values >= 0.75 to soft clipped values between 0.75 and 1.0
float t = (input - 0.75) * 4.0; // Scale input to enhance the soft clipping curve effect
return 0.75 + 0.25 * tanh(t); // Use hyperbolic tangent to provide a soft transition to 1.0
}
if (input < 0.9) {
// Linear function: output is the same as input for values less than 0.9
return input;
} else {
// Non-linear function: transforms input values >= 0.9 to soft clipped values between 0.9 and 1.0
float t = (input - 0.9) * 10.0; // Scale input to enhance the soft clipping curve effect
return 0.9 + 0.1 * tanh(t); // Use hyperbolic tangent to provide a soft transition to 1.0
}
}

void apply_tonemapping() {
Expand Down
2 changes: 2 additions & 0 deletions src/light_modes.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Functions for outputting computed data in beautiful fashion to the LEDs based on

// INACTIVE MODES
#include "light_modes/inactive/neutral.h"
#include "light_modes/inactive/starfield.h"

// SYSTEM MODES
#include "light_modes/system/self_test.h"
Expand All @@ -47,6 +48,7 @@ light_mode light_modes[] = {

// Inactive Modes
{ "Neutral", LIGHT_MODE_TYPE_INACTIVE, &draw_neutral },
{ "Starfield", LIGHT_MODE_TYPE_INACTIVE, &draw_starfield },

// System Modes
{ "Self Test", LIGHT_MODE_TYPE_SYSTEM, &draw_self_test },
Expand Down
72 changes: 72 additions & 0 deletions src/light_modes/inactive/starfield.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
float random_float(float min, float max) {
float random = ((float) rand()) / (float) RAND_MAX;
float diff = max - min;
float r = random * diff;
return min + r;
}

void draw_starfield(){
for(uint16_t i = 0; i < NUM_LEDS>>1; i++){
float progress = (float)i / (float)(NUM_LEDS>>1);
leds[i] = hsv(
1.0,
configuration.saturation,
progress*1.0
);
}

for(uint16_t i = 0; i < NUM_LEDS>>1; i++){
float progress = (float)i / (float)(NUM_LEDS>>1);
leds[(NUM_LEDS >> 1) + i] = hsv(
1.0,
configuration.saturation,
1.0 + (progress*3.0)
);
}
}

void draw_starfield_real(){
const uint16_t num_stars = 150;
static float star_speed[num_stars];
static float star_positions[num_stars];
static float star_brightness[num_stars];

static bool first_run = true;
if(first_run){
memset(star_speed, 0, sizeof(float)*num_stars);
memset(star_positions, 0, sizeof(float)*num_stars);
memset(star_brightness,0, sizeof(float)*num_stars);
first_run = false;
}

for(uint16_t i = 0; i < num_stars; i++){
float progress = (float)i / (float)num_stars;
CRGBF dot_color = hsv(
get_color_range_hue(progress),
configuration.saturation,
star_brightness[i]*4.0
);

if(fabsf(star_speed[i]) <= 0.01 || star_brightness[i] < 0.001){
star_speed[i] = (random_float(0.0, 1.0) - 0.5) * 0.1;
star_brightness[i] = 1.0; //random_float(0.1, 1.0);
star_positions[i] = 0.0;

dot_color = {
0.00001,
0.00001,
0.00001
};
}
else{
star_positions[i] += (star_speed[i]*0.1);
star_brightness[i] *= 0.975;

if(fabsf( star_positions[i] ) > 1.25f){
star_speed[i] = 0.0;
}
}

draw_dot(leds, NUM_RESERVED_DOTS+i, dot_color, 0.5 + (star_positions[i] * 0.5), 1.0);
}
}
49 changes: 26 additions & 23 deletions src/profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ void watch_gpu_fps() {
void print_system_info() {
static uint32_t next_print_ms = 0;
const uint16_t print_interval_ms = 2000;

bool printing_enabled = false;

if (t_now_ms >= next_print_ms) {
next_print_ms += print_interval_ms;

Expand Down Expand Up @@ -193,30 +194,32 @@ void print_system_info() {
snprintf(stat_buffer, 64, "heap|%lu", (uint32_t)free_heap);
broadcast(stat_buffer);

printf("# SYSTEM INFO ####################\n");
printf("CPU CORE USAGE --- %.2f%%\n", CPU_CORE_USAGE*100);
printf("CPU FPS ---------- %.3f\n", FPS_CPU);
printf("GPU FPS ---------- %.3f\n", FPS_GPU);
printf("Free Heap -------- %lu\n", (uint32_t)free_heap);
printf("Free Stack CPU --- %lu\n", (uint32_t)free_stack_cpu);
printf("Free Stack GPU --- %lu\n", (uint32_t)free_stack_gpu);
//printf("Total PSRAM ------ %lu\n", (uint32_t)ESP.getPsramSize());
//printf("Free PSRAM ------- %lu\n", (uint32_t)ESP.getFreePsram());
printf("IP Address ------- %s\n", WiFi.localIP().toString().c_str());
printf("MAC Address ------ %s\n", mac_str);
printf("\n");
printf("- WS CLIENTS -----------------\n");
if(web_server_ready == true){
for(uint16_t i = 0; i < MAX_WEBSOCKET_CLIENTS; i++){
PsychicWebSocketClient *client = get_client_in_slot(i);
if (client != NULL) {
printf("%s\n", client->remoteIP().toString().c_str());
if(printing_enabled == true){
printf("# SYSTEM INFO ####################\n");
printf("CPU CORE USAGE --- %.2f%%\n", CPU_CORE_USAGE*100);
printf("CPU FPS ---------- %.3f\n", FPS_CPU);
printf("GPU FPS ---------- %.3f\n", FPS_GPU);
printf("Free Heap -------- %lu\n", (uint32_t)free_heap);
printf("Free Stack CPU --- %lu\n", (uint32_t)free_stack_cpu);
printf("Free Stack GPU --- %lu\n", (uint32_t)free_stack_gpu);
//printf("Total PSRAM ------ %lu\n", (uint32_t)ESP.getPsramSize());
//printf("Free PSRAM ------- %lu\n", (uint32_t)ESP.getFreePsram());
printf("IP Address ------- %s\n", WiFi.localIP().toString().c_str());
printf("MAC Address ------ %s\n", mac_str);
printf("\n");
printf("- WS CLIENTS -----------------\n");
if(web_server_ready == true){
for(uint16_t i = 0; i < MAX_WEBSOCKET_CLIENTS; i++){
PsychicWebSocketClient *client = get_client_in_slot(i);
if (client != NULL) {
printf("%s\n", client->remoteIP().toString().c_str());
}
}
}
}
printf("------------------------------\n");
printf("------------------------------\n");

print_profiled_function_hits();
printf("##################################\n\n");
print_profiled_function_hits();
printf("##################################\n\n");
}
}
}
2 changes: 1 addition & 1 deletion src/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ void fetch_substring(char* input_buffer, char delimiter, uint8_t fetch_index){

void broadcast(const char* message){
extern PsychicWebSocketHandler websocket_handler;
printf("TX: %s\n", message);
websocket_handler.sendAll(message);
//printf("%s\n", message);
}

float linear_to_tri(float input) {
Expand Down
Loading

0 comments on commit 3bcf596

Please sign in to comment.