From 9f22c21f174f404b605e52e7e88bad61d6a71509 Mon Sep 17 00:00:00 2001 From: Skot Croshere Date: Mon, 19 Jun 2023 22:35:55 -0400 Subject: [PATCH] added suffixes to best_difficulty display --- main/system.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++- main/system.h | 3 +++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/main/system.c b/main/system.c index 585f1845..e5c50180 100644 --- a/main/system.c +++ b/main/system.c @@ -23,6 +23,8 @@ static const char *TAG = "SystemModule"; #define BM1397_VOLTAGE CONFIG_BM1397_VOLTAGE +static void _suffix_string(uint64_t, char *, size_t, int); + static void _init_system(SystemModule* module) { @@ -38,6 +40,8 @@ static void _init_system(SystemModule* module) { module->lastClockSync = 0; module->FOUND_BLOCK = false; + _suffix_string(0, module->best_diff_string, DIFF_STRING_SIZE, 0); + //test the LEDs // ESP_LOGI(TAG, "Init LEDs!"); // ledc_init(); @@ -106,7 +110,7 @@ static void _update_best_diff(SystemModule* module){ } OLED_clearLine(3); memset(module->oled_buf, 0, 20); - snprintf(module->oled_buf, 20, module->FOUND_BLOCK ? "!!! BLOCK FOUND !!!" : "BD: %u", module->best_nonce_diff); + snprintf(module->oled_buf, 20, module->FOUND_BLOCK ? "!!! BLOCK FOUND !!!" : "BD: %s", module->best_diff_string); OLED_writeString(0, 3, module->oled_buf); } @@ -208,6 +212,8 @@ static void _check_for_best_diff(SystemModule * module, double diff, uint32_t nb return; } module->best_nonce_diff = diff; + //make the best_nonce_diff into a string + _suffix_string((uint64_t)diff, module->best_diff_string, DIFF_STRING_SIZE, 0); double network_diff = _calculate_network_difficulty(nbits); if(diff > network_diff){ module->FOUND_BLOCK = true; @@ -216,6 +222,62 @@ static void _check_for_best_diff(SystemModule * module, double diff, uint32_t nb ESP_LOGI(TAG, "Network diff: %f", network_diff); } +/* Convert a uint64_t value into a truncated string for displaying with its + * associated suitable for Mega, Giga etc. Buf array needs to be long enough */ +static void _suffix_string(uint64_t val, char *buf, size_t bufsiz, int sigdigits) { + const double dkilo = 1000.0; + const uint64_t kilo = 1000ull; + const uint64_t mega = 1000000ull; + const uint64_t giga = 1000000000ull; + const uint64_t tera = 1000000000000ull; + const uint64_t peta = 1000000000000000ull; + const uint64_t exa = 1000000000000000000ull; + char suffix[2] = ""; + bool decimal = true; + double dval; + + if (val >= exa) { + val /= peta; + dval = (double)val / dkilo; + strcpy(suffix, "E"); + } else if (val >= peta) { + val /= tera; + dval = (double)val / dkilo; + strcpy(suffix, "P"); + } else if (val >= tera) { + val /= giga; + dval = (double)val / dkilo; + strcpy(suffix, "T"); + } else if (val >= giga) { + val /= mega; + dval = (double)val / dkilo; + strcpy(suffix, "G"); + } else if (val >= mega) { + val /= kilo; + dval = (double)val / dkilo; + strcpy(suffix, "M"); + } else if (val >= kilo) { + dval = (double)val / dkilo; + strcpy(suffix, "K"); + } else { + dval = val; + decimal = false; + } + + if (!sigdigits) { + if (decimal) + snprintf(buf, bufsiz, "%.3g%s", dval, suffix); + else + snprintf(buf, bufsiz, "%d%s", (unsigned int)dval, suffix); + } else { + /* Always show sigdigits + 1, padded on right with zeroes + * followed by suffix */ + int ndigits = sigdigits - 1 - (dval > 0.0 ? floor(log10(dval)) : 0); + + snprintf(buf, bufsiz, "%*.*f%s", sigdigits + 1, ndigits, dval, suffix); + } +} + void SYSTEM_task(void *pvParameters) { diff --git a/main/system.h b/main/system.h index d149cd97..d1d41da1 100644 --- a/main/system.h +++ b/main/system.h @@ -3,6 +3,8 @@ #define HISTORY_LENGTH 100 #define HISTORY_WINDOW_SIZE 5 + +#define DIFF_STRING_SIZE 10 typedef struct { double duration_start; int historical_hashrate_rolling_index; @@ -16,6 +18,7 @@ typedef struct { int screen_page; char oled_buf[20]; uint32_t best_nonce_diff; + char best_diff_string[DIFF_STRING_SIZE]; bool FOUND_BLOCK; uint32_t lastClockSync;