From bf91212c3e8086c580069c748208601de4303681 Mon Sep 17 00:00:00 2001 From: tommy Date: Thu, 19 Oct 2023 16:40:33 -0400 Subject: [PATCH] Save "Best Difficulty" scores into NVS #20 --- main/nvs_config.c | 40 ++++++++++++++++++++++++++++++++++++++++ main/nvs_config.h | 3 +++ main/system.c | 12 +++++++++--- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/main/nvs_config.c b/main/nvs_config.c index 6b2816ea6..b86b5e544 100644 --- a/main/nvs_config.c +++ b/main/nvs_config.c @@ -96,3 +96,43 @@ void nvs_config_set_u16(const char * key, const uint16_t value) nvs_close(handle); return; } + +uint64_t nvs_config_get_u64(const char * key, const uint64_t default_value) +{ + nvs_handle handle; + esp_err_t err; + err = nvs_open(NVS_CONFIG_NAMESPACE, NVS_READONLY, &handle); + if (err != ESP_OK) { + return default_value; + } + + uint64_t out; + err = nvs_get_u64(handle, key, &out); + + if (err != ESP_OK) { + return default_value; + } + + nvs_close(handle); + return out; +} + +void nvs_config_set_u64(const char * key, const uint64_t value) +{ + + nvs_handle handle; + esp_err_t err; + err = nvs_open(NVS_CONFIG_NAMESPACE, NVS_READWRITE, &handle); + if (err != ESP_OK) { + ESP_LOGW(TAG, "Could not open nvs"); + return; + } + + err = nvs_set_u64(handle, key, value); + if (err != ESP_OK) { + ESP_LOGW(TAG, "Could not write nvs key: %s, value: %llu", key, value); + return; + } + nvs_close(handle); + return; +} diff --git a/main/nvs_config.h b/main/nvs_config.h index 9d43fc0d2..90a088581 100644 --- a/main/nvs_config.h +++ b/main/nvs_config.h @@ -16,10 +16,13 @@ #define NVS_CONFIG_BOARD_VERSION "boardversion" #define NVS_CONFIG_FLIP_SCREEN "flipscreen" #define NVS_CONFIG_INVERT_SCREEN "invertscreen" +#define NVS_CONFIG_BEST_DIFF "bestdiff" char * nvs_config_get_string(const char * key, const char * default_value); void nvs_config_set_string(const char * key, const char * default_value); uint16_t nvs_config_get_u16(const char * key, const uint16_t default_value); void nvs_config_set_u16(const char * key, const uint16_t value); +uint64_t nvs_config_get_u64(const char * key, const uint64_t default_value); +void nvs_config_set_u64(const char * key, const uint64_t value); #endif // MAIN_NVS_CONFIG_H diff --git a/main/system.c b/main/system.c index b65fbdbf9..e4ac987e6 100644 --- a/main/system.c +++ b/main/system.c @@ -43,14 +43,14 @@ static void _init_system(GlobalState * global_state, SystemModule * module) module->screen_page = 0; module->shares_accepted = 0; module->shares_rejected = 0; - module->best_nonce_diff = 0; + module->best_nonce_diff = nvs_config_get_u64(NVS_CONFIG_BEST_DIFF, 0); module->start_time = esp_timer_get_time(); module->lastClockSync = 0; module->FOUND_BLOCK = false; module->startup_done = false; - // set the best diff string to 0 - _suffix_string(0, module->best_diff_string, DIFF_STRING_SIZE, 0); + // set the best diff string + _suffix_string(module->best_nonce_diff, module->best_diff_string, DIFF_STRING_SIZE, 0); // set the ssid string to blank memset(module->ssid, 0, 20); @@ -275,7 +275,13 @@ static void _check_for_best_diff(SystemModule * module, double diff, uint32_t nb if (diff < module->best_nonce_diff) { return; } + + uint64_t old = module->best_nonce_diff; module->best_nonce_diff = diff; + // only write to flash if new > old + if(module->best_nonce_diff > old) { + nvs_config_set_u64(NVS_CONFIG_BEST_DIFF, module->best_nonce_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);