Skip to content

Commit

Permalink
Merge pull request #396 from WantClue/simplify_i2c_screen
Browse files Browse the repository at this point in the history
simplify screen and remove old code
  • Loading branch information
WantClue authored Oct 9, 2024
2 parents 339fbcb + 4010a0b commit fefd29d
Showing 1 changed file with 78 additions and 166 deletions.
244 changes: 78 additions & 166 deletions main/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "vcore.h"



static const char * TAG = "SystemModule";

static void _suffix_string(uint64_t, char *, size_t, int);
Expand All @@ -42,14 +41,11 @@ QueueHandle_t user_input_queue;
//local function prototypes
static esp_err_t ensure_overheat_mode_config();
static void _show_overheat_screen(GlobalState * GLOBAL_STATE);
static void _update_hashrate(GlobalState * GLOBAL_STATE);
static void _update_shares(GlobalState * GLOBAL_STATE);
static void _clear_display(GlobalState * GLOBAL_STATE);
static void _init_connection(GlobalState * GLOBAL_STATE);
static void _update_connection(GlobalState * GLOBAL_STATE);
static void _update_system_performance(GlobalState * GLOBAL_STATE);
static void _update_system_info(GlobalState * GLOBAL_STATE);
static void _update_esp32_info(GlobalState * GLOBAL_STATE);
static void _update_screen_one(GlobalState * GLOBAL_STATE);
static void _update_screen_two(GlobalState * GLOBAL_STATE);
static void show_ap_information(const char * error, GlobalState * GLOBAL_STATE);

static void _check_for_best_diff(GlobalState * GLOBAL_STATE, double diff, uint8_t job_id);
Expand Down Expand Up @@ -184,6 +180,9 @@ void SYSTEM_task(void * pvParameters)
_update_connection(GLOBAL_STATE);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}

int current_screen = 0;
TickType_t last_update_time = xTaskGetTickCount();

while (1) {
// Check for overheat mode
Expand All @@ -194,37 +193,45 @@ void SYSTEM_task(void * pvParameters)
continue; // Skip the normal screen cycle
}

// Automatically cycle through screens
for (int screen = 0; screen < 3; screen++) {
_clear_display(GLOBAL_STATE);
module->screen_page = screen;

switch (module->screen_page) {
case 0:
_update_system_performance(GLOBAL_STATE);
break;
case 1:
_update_system_info(GLOBAL_STATE);
break;
case 2:
_update_esp32_info(GLOBAL_STATE);
break;
}
// Update the current screen
_clear_display(GLOBAL_STATE);
module->screen_page = current_screen;

switch (current_screen) {
case 0:
_update_screen_one(GLOBAL_STATE);
break;
case 1:
_update_screen_two(GLOBAL_STATE);
break;
}

// Wait for 10 seconds or until a button press
for (int i = 0; i < 10; i++) {
if (xQueueReceive(user_input_queue, &input_event, pdMS_TO_TICKS(1000))) {
if (strcmp(input_event, "SHORT") == 0) {
ESP_LOGI(TAG, "Short button press detected, switching to next screen");
screen = (screen + 1) % 3; // Move to next screen
break;
} else if (strcmp(input_event, "LONG") == 0) {
ESP_LOGI(TAG, "Long button press detected, toggling WiFi SoftAP");
toggle_wifi_softap(); // Toggle AP
}
// Wait for user input or timeout
bool input_received = false;
TickType_t current_time = xTaskGetTickCount();
TickType_t wait_time = pdMS_TO_TICKS(10000) - (current_time - last_update_time);

if (wait_time > 0) {
if (xQueueReceive(user_input_queue, &input_event, wait_time) == pdTRUE) {
input_received = true;
if (strcmp(input_event, "SHORT") == 0) {
ESP_LOGI(TAG, "Short button press detected, switching to next screen");
current_screen = (current_screen + 1) % 2;
} else if (strcmp(input_event, "LONG") == 0) {
ESP_LOGI(TAG, "Long button press detected, toggling WiFi SoftAP");
toggle_wifi_softap();
}
}
}

// If no input received and 10 seconds have passed, switch to the next screen
if (!input_received && (xTaskGetTickCount() - last_update_time) >= pdMS_TO_TICKS(10000)) {
current_screen = (current_screen + 1) % 2;
}

last_update_time = xTaskGetTickCount();


}
}

Expand All @@ -246,14 +253,12 @@ void SYSTEM_notify_accepted_share(GlobalState * GLOBAL_STATE)
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;

module->shares_accepted++;
_update_shares(GLOBAL_STATE);
}
void SYSTEM_notify_rejected_share(GlobalState * GLOBAL_STATE)
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;

module->shares_rejected++;
_update_shares(GLOBAL_STATE);
}

void SYSTEM_notify_mining_started(GlobalState * GLOBAL_STATE)
Expand Down Expand Up @@ -315,7 +320,6 @@ void SYSTEM_notify_found_nonce(GlobalState * GLOBAL_STATE, double found_diff, ui
module->current_hashrate = ((module->current_hashrate * 9) + rolling_rate) / 10;
}

_update_hashrate(GLOBAL_STATE);

// logArrayContents(historical_hashrate, HISTORY_LENGTH);
// logArrayContents(historical_hashrate_time_stamps, HISTORY_LENGTH);
Expand Down Expand Up @@ -358,158 +362,96 @@ static void _show_overheat_screen(GlobalState * GLOBAL_STATE)
}
}

static void _update_hashrate(GlobalState * GLOBAL_STATE)
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;

if (module->screen_page != 0) {
return;
}

switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
case DEVICE_GAMMA:
float efficiency = GLOBAL_STATE->POWER_MANAGEMENT_MODULE.power / (module->current_hashrate / 1000.0);
OLED_clearLine(0);
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "Gh%s: %.1f J/Th: %.1f", module->historical_hashrate_init < HISTORY_LENGTH ? "*" : "",
module->current_hashrate, efficiency);
OLED_writeString(0, 0, module->oled_buf);
break;
default:
}
}

static void _update_shares(GlobalState * GLOBAL_STATE)
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;

if (module->screen_page != 0) {
return;
}
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
case DEVICE_GAMMA:
OLED_clearLine(1);
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "A/R: %llu/%llu", module->shares_accepted, module->shares_rejected);
OLED_writeString(0, 1, module->oled_buf);
break;
default:
}
}

static void _update_best_diff(GlobalState * GLOBAL_STATE)
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;

if (module->screen_page != 0) {
return;
}

switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
case DEVICE_GAMMA:
OLED_clearLine(3);
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, module->FOUND_BLOCK ? "!!! BLOCK FOUND !!!" : "BD: %s", module->best_diff_string);
OLED_writeString(0, 3, module->oled_buf);
break;
default:
}
}

static void _clear_display(GlobalState * GLOBAL_STATE)
{
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
case DEVICE_GAMMA:
OLED_clearLine(0);
OLED_clearLine(1);
OLED_clearLine(2);
OLED_clearLine(3);
break;
default:
}
}

static void _update_system_info(GlobalState * GLOBAL_STATE)
static void _update_screen_one(GlobalState * GLOBAL_STATE)
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
PowerManagementModule * power_management = &GLOBAL_STATE->POWER_MANAGEMENT_MODULE;

switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
case DEVICE_GAMMA:
if (OLED_status()) {
float efficiency = GLOBAL_STATE->POWER_MANAGEMENT_MODULE.power / (module->current_hashrate / 1000.0);

memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, " Fan: %d RPM", power_management->fan_rpm);
snprintf(module->oled_buf, 20, "Gh/s: %.2f", module->current_hashrate);
OLED_writeString(0, 0, module->oled_buf);

memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "Temp: %.1f C", power_management->chip_temp_avg);
snprintf(module->oled_buf, 20, "J/Th: %.2f", efficiency);
OLED_writeString(0, 1, module->oled_buf);

memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, " Pwr: %.3f W", power_management->power);
snprintf(module->oled_buf, 20, module->FOUND_BLOCK ? "!!! BLOCK FOUND !!!" : "BD: %s", module->best_diff_string);
OLED_writeString(0, 2, module->oled_buf);

memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, " %i mV: %i mA", (int) power_management->voltage, (int) power_management->current);
snprintf(module->oled_buf, 20, "Temp: %.1f C", power_management->chip_temp_avg);
OLED_writeString(0, 3, module->oled_buf);
}
break;
default:
break;
}
}

static void _update_esp32_info(GlobalState * GLOBAL_STATE)
static void _update_screen_two(GlobalState * GLOBAL_STATE)
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;

esp_netif_ip_info_t ip_info;
uint32_t free_heap_size = esp_get_free_heap_size();

uint16_t vcore = VCORE_get_voltage_mv(GLOBAL_STATE);

switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
case DEVICE_GAMMA:
if (OLED_status()) {

// Pool URL
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "FH: %lu bytes", free_heap_size);
if (module->is_using_fallback) {
snprintf(module->oled_buf, 20, "Pool: %.13s", module->fallback_pool_url);
} else {
snprintf(module->oled_buf, 20, "Pool: %.13s", module->pool_url);
}
OLED_writeString(0, 0, module->oled_buf);

// Second line of pool URL
memset(module->oled_buf, 0, 20);
snprintf(module->oled_buf, 20, "vCore: %u mV", vcore);
if (module->is_using_fallback) {
snprintf(module->oled_buf, 20, "%.19s", module->fallback_pool_url + 13);
} else {
snprintf(module->oled_buf, 20, "%.19s", module->pool_url + 13);
}
OLED_writeString(0, 1, module->oled_buf);

// IP Address
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);

OLED_writeString(0, 3, esp_app_get_description()->version);
}
break;
default:
break;
}
}

static void _clear_display(GlobalState * GLOBAL_STATE)
{
switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
case DEVICE_GAMMA:
OLED_clearLine(0);
OLED_clearLine(1);
OLED_clearLine(2);
OLED_clearLine(3);
break;
default:
}
}

Expand Down Expand Up @@ -562,36 +504,6 @@ static void _update_connection(GlobalState * GLOBAL_STATE)
}
}

static void _update_system_performance(GlobalState * GLOBAL_STATE)
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
// Calculate the uptime in seconds
double uptime_in_seconds = (esp_timer_get_time() - module->start_time) / 1000000;
int uptime_in_days = uptime_in_seconds / (3600 * 24);
int remaining_seconds = (int) uptime_in_seconds % (3600 * 24);
int uptime_in_hours = remaining_seconds / 3600;
remaining_seconds %= 3600;
int uptime_in_minutes = remaining_seconds / 60;

switch (GLOBAL_STATE->device_model) {
case DEVICE_MAX:
case DEVICE_ULTRA:
case DEVICE_SUPRA:
case DEVICE_GAMMA:
if (OLED_status()) {

_update_hashrate(GLOBAL_STATE);
_update_shares(GLOBAL_STATE);
_update_best_diff(GLOBAL_STATE);

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);
}
break;
default:
}
}

static void show_ap_information(const char * error, GlobalState * GLOBAL_STATE)
{
Expand Down

0 comments on commit fefd29d

Please sign in to comment.