2023-09-18 21:05:45 -04:00
|
|
|
#include "system.h"
|
2023-05-10 18:20:37 -04:00
|
|
|
|
2023-09-18 21:05:45 -04:00
|
|
|
#include "esp_log.h"
|
2023-05-10 18:20:37 -04:00
|
|
|
|
|
|
|
#include "DS4432U.h"
|
|
|
|
#include "EMC2101.h"
|
|
|
|
#include "INA260.h"
|
|
|
|
#include "adc.h"
|
2023-09-17 00:01:54 -04:00
|
|
|
#include "connect.h"
|
|
|
|
#include "global_state.h"
|
|
|
|
#include "led_controller.h"
|
|
|
|
#include "nvs_config.h"
|
2023-05-10 18:20:37 -04:00
|
|
|
#include "oled.h"
|
2023-09-18 21:05:45 -04:00
|
|
|
|
|
|
|
#include "driver/gpio.h"
|
|
|
|
#include "driver/i2c.h"
|
2023-09-18 21:57:21 -04:00
|
|
|
#include "esp_app_desc.h"
|
2023-09-18 21:05:45 -04:00
|
|
|
#include "esp_netif.h"
|
|
|
|
#include "esp_timer.h"
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "lwip/inet.h"
|
|
|
|
|
2023-06-09 19:30:28 -04:00
|
|
|
#include <inttypes.h>
|
2023-09-17 00:01:54 -04:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdint.h>
|
2023-09-18 21:05:45 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2023-09-17 00:01:54 -04:00
|
|
|
#include <sys/time.h>
|
2023-09-18 21:05:45 -04:00
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
static const char * TAG = "SystemModule";
|
2023-05-10 18:20:37 -04:00
|
|
|
|
2023-06-19 22:35:55 -04:00
|
|
|
static void _suffix_string(uint64_t, char *, size_t, int);
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
static esp_netif_t * netif;
|
|
|
|
static esp_netif_ip_info_t ip_info;
|
|
|
|
|
2023-09-21 11:18:38 -04:00
|
|
|
static void _init_system(GlobalState * global_state, SystemModule * module)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-06-06 09:06:30 -04:00
|
|
|
module->duration_start = 0;
|
2023-06-06 14:09:51 -04:00
|
|
|
module->historical_hashrate_rolling_index = 0;
|
|
|
|
module->historical_hashrate_init = 0;
|
|
|
|
module->current_hashrate = 0;
|
|
|
|
module->screen_page = 0;
|
|
|
|
module->shares_accepted = 0;
|
|
|
|
module->shares_rejected = 0;
|
2023-06-08 17:48:26 -04:00
|
|
|
module->best_nonce_diff = 0;
|
2023-06-09 14:49:56 -04:00
|
|
|
module->start_time = esp_timer_get_time();
|
|
|
|
module->lastClockSync = 0;
|
2023-06-09 19:30:28 -04:00
|
|
|
module->FOUND_BLOCK = false;
|
2023-06-21 19:00:09 -04:00
|
|
|
module->startup_done = false;
|
2023-06-03 17:02:10 -04:00
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// set the best diff string to 0
|
2023-06-19 22:35:55 -04:00
|
|
|
_suffix_string(0, module->best_diff_string, DIFF_STRING_SIZE, 0);
|
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// set the ssid string to blank
|
2023-06-21 19:00:09 -04:00
|
|
|
memset(module->ssid, 0, 20);
|
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// set the wifi_status to blank
|
2023-06-21 19:00:09 -04:00
|
|
|
memset(module->wifi_status, 0, 20);
|
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// test the LEDs
|
|
|
|
// ESP_LOGI(TAG, "Init LEDs!");
|
|
|
|
// ledc_init();
|
|
|
|
// led_set();
|
2023-05-10 18:20:37 -04:00
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// Playing with BI level
|
2023-05-10 18:20:37 -04:00
|
|
|
gpio_set_direction(GPIO_NUM_10, GPIO_MODE_OUTPUT);
|
|
|
|
gpio_set_level(GPIO_NUM_10, 0);
|
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// Init I2C
|
2023-05-10 18:20:37 -04:00
|
|
|
ESP_ERROR_CHECK(i2c_master_init());
|
|
|
|
ESP_LOGI(TAG, "I2C initialized successfully");
|
|
|
|
|
|
|
|
ADC_init();
|
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// DS4432U tests
|
2023-09-17 00:01:54 -04:00
|
|
|
DS4432U_set_vcore(nvs_config_get_u16(NVS_CONFIG_ASIC_VOLTAGE, CONFIG_ASIC_VOLTAGE) / 1000.0);
|
2023-06-09 19:30:28 -04:00
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// Fan Tests
|
2023-05-10 18:20:37 -04:00
|
|
|
EMC2101_init();
|
2023-09-21 11:18:38 -04:00
|
|
|
if (strcmp(global_state->asic_model, "BM1366") == 0) {
|
2023-09-06 15:38:05 -04:00
|
|
|
EMC2101_set_fan_speed(0);
|
2023-09-17 00:01:54 -04:00
|
|
|
} else {
|
2023-09-06 15:38:05 -04:00
|
|
|
EMC2101_set_fan_speed(1);
|
|
|
|
}
|
2023-08-16 16:55:10 +02:00
|
|
|
vTaskDelay(500 / portTICK_PERIOD_MS);
|
2023-05-10 18:20:37 -04:00
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// oled
|
2023-09-17 00:01:54 -04:00
|
|
|
if (!OLED_init()) {
|
2023-05-10 18:20:37 -04:00
|
|
|
ESP_LOGI(TAG, "OLED init failed!");
|
2023-09-17 00:01:54 -04:00
|
|
|
} else {
|
2023-05-10 22:40:40 -04:00
|
|
|
ESP_LOGI(TAG, "OLED init success!");
|
2023-08-26 12:28:17 -04:00
|
|
|
// clear the oled screen
|
2023-05-10 22:40:40 -04:00
|
|
|
OLED_fill(0);
|
2023-05-10 18:20:37 -04:00
|
|
|
}
|
2023-09-17 00:01:54 -04:00
|
|
|
|
|
|
|
netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
2023-05-10 18:20:37 -04:00
|
|
|
}
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
static void _update_hashrate(GlobalState * GLOBAL_STATE)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-09-17 00:01:54 -04:00
|
|
|
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
|
2023-06-06 14:09:51 -04:00
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
if (module->screen_page != 0) {
|
2023-08-26 12:28:17 -04:00
|
|
|
return;
|
|
|
|
}
|
2023-06-06 08:19:46 -04:00
|
|
|
|
2023-08-28 19:56:04 -04:00
|
|
|
float efficiency = GLOBAL_STATE->POWER_MANAGEMENT_MODULE.power / (module->current_hashrate / 1000.0);
|
2023-06-06 08:19:46 -04:00
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
OLED_clearLine(0);
|
|
|
|
memset(module->oled_buf, 0, 20);
|
2023-09-17 00:01:54 -04:00
|
|
|
snprintf(module->oled_buf, 20, "Gh%s: %.1f W/Th: %.1f", module->historical_hashrate_init < HISTORY_LENGTH ? "*" : "",
|
|
|
|
module->current_hashrate, efficiency);
|
2023-08-26 12:28:17 -04:00
|
|
|
OLED_writeString(0, 0, module->oled_buf);
|
2023-06-06 08:19:46 -04:00
|
|
|
}
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
static void _update_shares(SystemModule * module)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-09-17 00:01:54 -04:00
|
|
|
if (module->screen_page != 0) {
|
2023-08-26 12:28:17 -04:00
|
|
|
return;
|
2023-06-06 08:19:46 -04:00
|
|
|
}
|
|
|
|
OLED_clearLine(1);
|
2023-06-06 14:09:51 -04:00
|
|
|
memset(module->oled_buf, 0, 20);
|
|
|
|
snprintf(module->oled_buf, 20, "A/R: %u/%u", module->shares_accepted, module->shares_rejected);
|
|
|
|
OLED_writeString(0, 1, module->oled_buf);
|
2023-06-06 08:19:46 -04:00
|
|
|
}
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
static void _update_best_diff(SystemModule * module)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-09-17 00:01:54 -04:00
|
|
|
if (module->screen_page != 0) {
|
2023-08-26 12:28:17 -04:00
|
|
|
return;
|
2023-06-08 17:48:26 -04:00
|
|
|
}
|
|
|
|
OLED_clearLine(3);
|
|
|
|
memset(module->oled_buf, 0, 20);
|
2023-08-26 12:28:17 -04:00
|
|
|
snprintf(module->oled_buf, 20, module->FOUND_BLOCK ? "!!! BLOCK FOUND !!!" : "BD: %s", module->best_diff_string);
|
2023-06-08 17:48:26 -04:00
|
|
|
OLED_writeString(0, 3, module->oled_buf);
|
|
|
|
}
|
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
static void _clear_display(void)
|
|
|
|
{
|
2023-06-05 14:08:25 -04:00
|
|
|
OLED_clearLine(0);
|
|
|
|
OLED_clearLine(1);
|
|
|
|
OLED_clearLine(2);
|
|
|
|
OLED_clearLine(3);
|
|
|
|
}
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
static void _update_system_info(GlobalState * GLOBAL_STATE)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-09-17 00:01:54 -04:00
|
|
|
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
|
|
|
|
PowerManagementModule * power_management = &GLOBAL_STATE->POWER_MANAGEMENT_MODULE;
|
2023-05-10 18:20:37 -04:00
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
if (OLED_status()) {
|
2023-06-05 14:08:25 -04:00
|
|
|
|
2023-06-06 14:09:51 -04:00
|
|
|
memset(module->oled_buf, 0, 20);
|
2023-06-13 09:26:43 -04:00
|
|
|
snprintf(module->oled_buf, 20, " Fan: %d RPM", power_management->fan_speed);
|
2023-06-06 14:09:51 -04:00
|
|
|
OLED_writeString(0, 0, module->oled_buf);
|
2023-06-03 17:02:10 -04:00
|
|
|
|
2023-06-06 14:09:51 -04:00
|
|
|
memset(module->oled_buf, 0, 20);
|
2023-06-13 09:26:43 -04:00
|
|
|
snprintf(module->oled_buf, 20, "Temp: %.1f C", power_management->chip_temp);
|
2023-06-06 14:09:51 -04:00
|
|
|
OLED_writeString(0, 1, module->oled_buf);
|
2023-05-10 18:20:37 -04:00
|
|
|
|
2023-06-06 14:09:51 -04:00
|
|
|
memset(module->oled_buf, 0, 20);
|
2023-06-13 09:26:43 -04:00
|
|
|
snprintf(module->oled_buf, 20, " Pwr: %.3f W", power_management->power);
|
2023-06-06 14:09:51 -04:00
|
|
|
OLED_writeString(0, 2, module->oled_buf);
|
2023-05-10 18:20:37 -04:00
|
|
|
|
2023-06-06 14:09:51 -04:00
|
|
|
memset(module->oled_buf, 0, 20);
|
2023-09-17 00:01:54 -04:00
|
|
|
snprintf(module->oled_buf, 20, " %i mV: %i mA", (int) power_management->voltage, (int) power_management->current);
|
2023-06-06 14:09:51 -04:00
|
|
|
OLED_writeString(0, 3, module->oled_buf);
|
2023-05-10 18:20:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
static void _update_esp32_info(SystemModule * module)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-06-04 19:34:17 -04:00
|
|
|
uint32_t free_heap_size = esp_get_free_heap_size();
|
|
|
|
|
2023-06-05 21:26:51 -04:00
|
|
|
uint16_t vcore = ADC_get_vcore();
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
if (OLED_status()) {
|
2023-06-04 19:34:17 -04:00
|
|
|
|
2023-06-06 14:09:51 -04:00
|
|
|
memset(module->oled_buf, 0, 20);
|
2023-08-16 16:55:10 +02:00
|
|
|
snprintf(module->oled_buf, 20, "FH: %lu bytes", free_heap_size);
|
2023-06-06 14:09:51 -04:00
|
|
|
OLED_writeString(0, 0, module->oled_buf);
|
2023-06-04 19:34:17 -04:00
|
|
|
|
2023-06-06 14:09:51 -04:00
|
|
|
memset(module->oled_buf, 0, 20);
|
|
|
|
snprintf(module->oled_buf, 20, "vCore: %u mV", vcore);
|
|
|
|
OLED_writeString(0, 1, module->oled_buf);
|
2023-06-04 19:34:17 -04:00
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
esp_netif_get_ip_info(netif, &ip_info);
|
|
|
|
char ip_address_str[IP4ADDR_STRLEN_MAX];
|
|
|
|
esp_ip4addr_ntoa(&ip_info.ip, ip_address_str, IP4ADDR_STRLEN_MAX);
|
|
|
|
|
|
|
|
memset(module->oled_buf, 0, 20);
|
|
|
|
snprintf(module->oled_buf, 20, "IP: %s", ip_address_str);
|
|
|
|
OLED_writeString(0, 2, module->oled_buf);
|
2023-09-18 21:57:21 -04:00
|
|
|
|
|
|
|
OLED_writeString(0, 3, esp_app_get_description()->version);
|
2023-06-04 19:34:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
static void _init_connection(SystemModule * module)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-09-17 00:01:54 -04:00
|
|
|
if (OLED_status()) {
|
2023-06-21 19:00:09 -04:00
|
|
|
memset(module->oled_buf, 0, 20);
|
2023-06-21 19:21:49 -04:00
|
|
|
snprintf(module->oled_buf, 20, "Connecting to ssid:");
|
|
|
|
OLED_writeString(0, 0, module->oled_buf);
|
2023-06-21 19:00:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
static void _update_connection(SystemModule * module)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-09-17 00:01:54 -04:00
|
|
|
if (OLED_status()) {
|
2023-06-21 19:00:09 -04:00
|
|
|
OLED_clearLine(2);
|
|
|
|
memset(module->oled_buf, 0, 20);
|
|
|
|
snprintf(module->oled_buf, 20, "%s", module->ssid);
|
2023-06-21 19:21:49 -04:00
|
|
|
OLED_writeString(0, 1, module->oled_buf);
|
2023-06-21 19:00:09 -04:00
|
|
|
|
|
|
|
OLED_clearLine(3);
|
|
|
|
memset(module->oled_buf, 0, 20);
|
|
|
|
snprintf(module->oled_buf, 20, "%s", module->wifi_status);
|
|
|
|
OLED_writeString(0, 3, module->oled_buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
static void _update_system_performance(GlobalState * GLOBAL_STATE)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-09-17 00:01:54 -04:00
|
|
|
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
|
2023-06-03 17:02:10 -04:00
|
|
|
// Calculate the uptime in seconds
|
2023-06-09 14:49:56 -04:00
|
|
|
double uptime_in_seconds = (esp_timer_get_time() - module->start_time) / 1000000;
|
2023-06-03 17:02:10 -04:00
|
|
|
int uptime_in_days = uptime_in_seconds / (3600 * 24);
|
2023-09-17 00:01:54 -04:00
|
|
|
int remaining_seconds = (int) uptime_in_seconds % (3600 * 24);
|
2023-06-04 13:16:34 -04:00
|
|
|
int uptime_in_hours = remaining_seconds / 3600;
|
|
|
|
remaining_seconds %= 3600;
|
|
|
|
int uptime_in_minutes = remaining_seconds / 60;
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
if (OLED_status()) {
|
2023-06-09 19:30:28 -04:00
|
|
|
|
2023-08-28 19:56:04 -04:00
|
|
|
_update_hashrate(GLOBAL_STATE);
|
2023-06-06 14:09:51 -04:00
|
|
|
_update_shares(module);
|
2023-06-08 17:48:26 -04:00
|
|
|
_update_best_diff(module);
|
2023-06-04 13:16:34 -04:00
|
|
|
|
2023-06-06 14:09:51 -04:00
|
|
|
memset(module->oled_buf, 0, 20);
|
|
|
|
snprintf(module->oled_buf, 20, "UT: %dd %ih %im", uptime_in_days, uptime_in_hours, uptime_in_minutes);
|
|
|
|
OLED_writeString(0, 2, module->oled_buf);
|
2023-06-03 17:02:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-26 23:52:22 -04:00
|
|
|
static void show_ap_information(const char * error)
|
2023-09-23 12:32:48 -04:00
|
|
|
{
|
|
|
|
if (OLED_status()) {
|
|
|
|
_clear_display();
|
2023-09-26 23:52:22 -04:00
|
|
|
if (error != NULL) {
|
|
|
|
OLED_writeString(0, 0, error);
|
|
|
|
}
|
|
|
|
OLED_writeString(0, 1, "connect to ssid:");
|
2023-09-23 12:32:48 -04:00
|
|
|
char ap_ssid[13];
|
|
|
|
generate_ssid(ap_ssid);
|
|
|
|
OLED_writeString(0, 2, ap_ssid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
static double _calculate_network_difficulty(uint32_t nBits)
|
|
|
|
{
|
|
|
|
uint32_t mantissa = nBits & 0x007fffff; // Extract the mantissa from nBits
|
|
|
|
uint8_t exponent = (nBits >> 24) & 0xff; // Extract the exponent from nBits
|
2023-06-21 19:00:09 -04:00
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
double target = (double) mantissa * pow(256, (exponent - 3)); // Calculate the target value
|
2023-06-21 19:00:09 -04:00
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
double difficulty = (pow(2, 208) * 65535) / target; // Calculate the difficulty
|
2023-06-09 19:30:28 -04:00
|
|
|
|
2023-06-09 08:36:50 -04:00
|
|
|
return difficulty;
|
|
|
|
}
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
static void _check_for_best_diff(SystemModule * module, double diff, uint32_t nbits)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-09-17 00:01:54 -04:00
|
|
|
if (diff < module->best_nonce_diff) {
|
2023-06-09 08:36:50 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
module->best_nonce_diff = diff;
|
2023-08-26 12:28:17 -04:00
|
|
|
// make the best_nonce_diff into a string
|
2023-09-17 00:01:54 -04:00
|
|
|
_suffix_string((uint64_t) diff, module->best_diff_string, DIFF_STRING_SIZE, 0);
|
2023-06-09 19:30:28 -04:00
|
|
|
double network_diff = _calculate_network_difficulty(nbits);
|
2023-09-17 00:01:54 -04:00
|
|
|
if (diff > network_diff) {
|
2023-06-09 19:30:28 -04:00
|
|
|
module->FOUND_BLOCK = true;
|
|
|
|
ESP_LOGI(TAG, "FOUND BLOCK!!!!!!!!!!!!!!!!!!!!!! %f > %f", diff, network_diff);
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Network diff: %f", network_diff);
|
2023-06-09 08:36:50 -04:00
|
|
|
}
|
2023-06-19 22:35:55 -04:00
|
|
|
|
|
|
|
/* 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 */
|
2023-09-17 00:01:54 -04:00
|
|
|
static void _suffix_string(uint64_t val, char * buf, size_t bufsiz, int sigdigits)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
if (val >= exa) {
|
2023-08-26 12:28:17 -04:00
|
|
|
val /= peta;
|
2023-09-17 00:01:54 -04:00
|
|
|
dval = (double) val / dkilo;
|
2023-08-26 12:28:17 -04:00
|
|
|
strcpy(suffix, "E");
|
2023-09-17 00:01:54 -04:00
|
|
|
} else if (val >= peta) {
|
2023-08-26 12:28:17 -04:00
|
|
|
val /= tera;
|
2023-09-17 00:01:54 -04:00
|
|
|
dval = (double) val / dkilo;
|
2023-08-26 12:28:17 -04:00
|
|
|
strcpy(suffix, "P");
|
2023-09-17 00:01:54 -04:00
|
|
|
} else if (val >= tera) {
|
2023-08-26 12:28:17 -04:00
|
|
|
val /= giga;
|
2023-09-17 00:01:54 -04:00
|
|
|
dval = (double) val / dkilo;
|
2023-08-26 12:28:17 -04:00
|
|
|
strcpy(suffix, "T");
|
2023-09-17 00:01:54 -04:00
|
|
|
} else if (val >= giga) {
|
2023-08-26 12:28:17 -04:00
|
|
|
val /= mega;
|
2023-09-17 00:01:54 -04:00
|
|
|
dval = (double) val / dkilo;
|
2023-08-26 12:28:17 -04:00
|
|
|
strcpy(suffix, "G");
|
2023-09-17 00:01:54 -04:00
|
|
|
} else if (val >= mega) {
|
2023-08-26 12:28:17 -04:00
|
|
|
val /= kilo;
|
2023-09-17 00:01:54 -04:00
|
|
|
dval = (double) val / dkilo;
|
2023-08-26 12:28:17 -04:00
|
|
|
strcpy(suffix, "M");
|
2023-09-17 00:01:54 -04:00
|
|
|
} else if (val >= kilo) {
|
|
|
|
dval = (double) val / dkilo;
|
2023-08-26 12:28:17 -04:00
|
|
|
strcpy(suffix, "k");
|
2023-09-17 00:01:54 -04:00
|
|
|
} else {
|
2023-08-26 12:28:17 -04:00
|
|
|
dval = val;
|
|
|
|
decimal = false;
|
|
|
|
}
|
2023-06-09 08:36:50 -04:00
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
if (!sigdigits) {
|
2023-08-26 12:28:17 -04:00
|
|
|
if (decimal)
|
|
|
|
snprintf(buf, bufsiz, "%.3g%s", dval, suffix);
|
|
|
|
else
|
2023-09-17 00:01:54 -04:00
|
|
|
snprintf(buf, bufsiz, "%d%s", (unsigned int) dval, suffix);
|
|
|
|
} else {
|
2023-08-26 12:28:17 -04:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
}
|
2023-06-04 13:16:34 -04:00
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
void SYSTEM_task(void * pvParameters)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-09-17 00:01:54 -04:00
|
|
|
GlobalState * GLOBAL_STATE = (GlobalState *) pvParameters;
|
|
|
|
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
|
2023-06-13 09:26:43 -04:00
|
|
|
|
2023-09-21 11:18:38 -04:00
|
|
|
_init_system(GLOBAL_STATE, module);
|
2023-05-10 18:20:37 -04:00
|
|
|
|
2023-06-21 19:00:09 -04:00
|
|
|
_clear_display();
|
|
|
|
_init_connection(module);
|
|
|
|
|
2023-09-23 12:32:48 -04:00
|
|
|
wifi_mode_t wifi_mode;
|
|
|
|
esp_err_t result;
|
|
|
|
|
2023-09-26 23:52:22 -04:00
|
|
|
while (GLOBAL_STATE->ASIC_functions.init_fn == NULL) {
|
|
|
|
show_ap_information("ASIC MODEL INVALID");
|
|
|
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
|
2023-08-26 12:28:17 -04:00
|
|
|
// show the connection screen
|
2023-09-17 00:01:54 -04:00
|
|
|
while (!module->startup_done) {
|
2023-09-23 12:32:48 -04:00
|
|
|
result = esp_wifi_get_mode(&wifi_mode);
|
|
|
|
if (result == ESP_OK && (wifi_mode == WIFI_MODE_APSTA || wifi_mode == WIFI_MODE_AP) &&
|
|
|
|
strcmp(module->wifi_status, "Failed to connect") == 0) {
|
2023-09-26 23:52:22 -04:00
|
|
|
show_ap_information(NULL);
|
2023-09-23 12:32:48 -04:00
|
|
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
|
|
|
} else {
|
|
|
|
_update_connection(module);
|
|
|
|
}
|
2023-08-16 16:55:10 +02:00
|
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
2023-06-21 19:00:09 -04:00
|
|
|
}
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
while (1) {
|
2023-06-06 08:19:46 -04:00
|
|
|
_clear_display();
|
2023-06-06 14:09:51 -04:00
|
|
|
module->screen_page = 0;
|
2023-08-28 19:56:04 -04:00
|
|
|
_update_system_performance(GLOBAL_STATE);
|
2023-08-16 16:55:10 +02:00
|
|
|
vTaskDelay(40000 / portTICK_PERIOD_MS);
|
2023-06-04 13:16:34 -04:00
|
|
|
|
2023-06-06 08:19:46 -04:00
|
|
|
_clear_display();
|
2023-06-06 14:09:51 -04:00
|
|
|
module->screen_page = 1;
|
2023-06-13 09:26:43 -04:00
|
|
|
_update_system_info(GLOBAL_STATE);
|
2023-08-16 16:55:10 +02:00
|
|
|
vTaskDelay(10000 / portTICK_PERIOD_MS);
|
2023-06-04 19:34:17 -04:00
|
|
|
|
2023-06-06 08:19:46 -04:00
|
|
|
_clear_display();
|
2023-06-06 14:09:51 -04:00
|
|
|
module->screen_page = 2;
|
|
|
|
_update_esp32_info(module);
|
2023-08-16 16:55:10 +02:00
|
|
|
vTaskDelay(10000 / portTICK_PERIOD_MS);
|
2023-05-10 18:20:37 -04:00
|
|
|
}
|
2023-06-06 08:19:46 -04:00
|
|
|
}
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
void SYSTEM_notify_accepted_share(SystemModule * module)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-06-06 14:09:51 -04:00
|
|
|
module->shares_accepted++;
|
|
|
|
_update_shares(module);
|
2023-06-06 08:19:46 -04:00
|
|
|
}
|
2023-09-17 00:01:54 -04:00
|
|
|
void SYSTEM_notify_rejected_share(SystemModule * module)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-06-06 14:09:51 -04:00
|
|
|
module->shares_rejected++;
|
|
|
|
_update_shares(module);
|
2023-06-06 08:19:46 -04:00
|
|
|
}
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
void SYSTEM_notify_mining_started(SystemModule * module)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-06-06 09:06:30 -04:00
|
|
|
module->duration_start = esp_timer_get_time();
|
2023-06-06 08:19:46 -04:00
|
|
|
}
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
void SYSTEM_notify_new_ntime(SystemModule * module, uint32_t ntime)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-06-09 14:49:56 -04:00
|
|
|
// Hourly clock sync
|
2023-09-17 00:01:54 -04:00
|
|
|
if (module->lastClockSync + (60 * 60) > ntime) {
|
2023-06-09 14:49:56 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Syncing clock");
|
|
|
|
module->lastClockSync = ntime;
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = ntime;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
settimeofday(&tv, NULL);
|
|
|
|
}
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
void SYSTEM_notify_found_nonce(SystemModule * module, double pool_diff, double found_diff, uint32_t nbits)
|
2023-08-26 12:28:17 -04:00
|
|
|
{
|
2023-06-06 08:19:46 -04:00
|
|
|
// Calculate the time difference in seconds with sub-second precision
|
2023-06-09 19:30:28 -04:00
|
|
|
|
2023-06-06 08:19:46 -04:00
|
|
|
// hashrate = (nonce_difficulty * 2^32) / time_to_find
|
2023-06-09 19:30:28 -04:00
|
|
|
|
2023-06-09 13:29:26 -04:00
|
|
|
module->historical_hashrate[module->historical_hashrate_rolling_index] = pool_diff;
|
2023-06-06 14:09:51 -04:00
|
|
|
module->historical_hashrate_time_stamps[module->historical_hashrate_rolling_index] = esp_timer_get_time();
|
2023-06-06 08:19:46 -04:00
|
|
|
|
2023-06-06 14:09:51 -04:00
|
|
|
module->historical_hashrate_rolling_index = (module->historical_hashrate_rolling_index + 1) % HISTORY_LENGTH;
|
2023-06-06 08:19:46 -04:00
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
// ESP_LOGI(TAG, "nonce_diff %.1f, ttf %.1f, res %.1f", nonce_diff, duration,
|
|
|
|
// historical_hashrate[historical_hashrate_rolling_index]);
|
2023-06-09 19:30:28 -04:00
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
if (module->historical_hashrate_init < HISTORY_LENGTH) {
|
2023-06-06 14:09:51 -04:00
|
|
|
module->historical_hashrate_init++;
|
2023-09-17 00:01:54 -04:00
|
|
|
} else {
|
|
|
|
module->duration_start =
|
|
|
|
module->historical_hashrate_time_stamps[(module->historical_hashrate_rolling_index + 1) % HISTORY_LENGTH];
|
2023-06-06 08:19:46 -04:00
|
|
|
}
|
|
|
|
double sum = 0;
|
2023-09-17 00:01:54 -04:00
|
|
|
for (int i = 0; i < module->historical_hashrate_init; i++) {
|
2023-06-06 14:09:51 -04:00
|
|
|
sum += module->historical_hashrate[i];
|
2023-06-06 08:19:46 -04:00
|
|
|
}
|
|
|
|
|
2023-09-17 00:01:54 -04:00
|
|
|
double duration = (double) (esp_timer_get_time() - module->duration_start) / 1000000;
|
2023-06-06 08:19:46 -04:00
|
|
|
|
|
|
|
double rolling_rate = (sum * 4294967296) / (duration * 1000000000);
|
2023-09-17 00:01:54 -04:00
|
|
|
if (module->historical_hashrate_init < HISTORY_LENGTH) {
|
2023-06-06 14:09:51 -04:00
|
|
|
module->current_hashrate = rolling_rate;
|
2023-09-17 00:01:54 -04:00
|
|
|
} else {
|
2023-06-06 08:19:46 -04:00
|
|
|
// More smoothing
|
2023-08-26 12:28:17 -04:00
|
|
|
module->current_hashrate = ((module->current_hashrate * 9) + rolling_rate) / 10;
|
2023-06-06 08:19:46 -04:00
|
|
|
}
|
|
|
|
|
2023-06-06 14:09:51 -04:00
|
|
|
_update_hashrate(module);
|
2023-06-06 08:19:46 -04:00
|
|
|
|
|
|
|
// logArrayContents(historical_hashrate, HISTORY_LENGTH);
|
|
|
|
// logArrayContents(historical_hashrate_time_stamps, HISTORY_LENGTH);
|
2023-06-09 08:36:50 -04:00
|
|
|
|
2023-06-09 19:30:28 -04:00
|
|
|
_check_for_best_diff(module, found_diff, nbits);
|
2023-06-06 08:19:46 -04:00
|
|
|
}
|