Skip to content

Commit

Permalink
Save "Best Difficulty" scores into NVS #20
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy committed Oct 19, 2023
1 parent 0a51a9c commit bf91212
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
40 changes: 40 additions & 0 deletions main/nvs_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions main/nvs_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 9 additions & 3 deletions main/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit bf91212

Please sign in to comment.