Skip to content

Commit

Permalink
Last stable version
Browse files Browse the repository at this point in the history
- UI tasks deleted
- UI only refreshes every 5 seconds
- UI animations activated only at startup and when button is pressed
- Improved wifi reconnections
- Improved socket reconnection
- Miner doesn't reboot if wifi is lost, waits until it reaches the network again
-
  • Loading branch information
BitMaker-hub committed May 14, 2024
1 parent bcddf1b commit 59257c0
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 58 deletions.
43 changes: 39 additions & 4 deletions components/stratum/stratum_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ static int send_uid = 1;

static void debug_stratum_tx(const char *);

int is_socket_connected(int socket) {
struct timeval tv;
fd_set writefds;

tv.tv_sec = 0;
tv.tv_usec = 100000; // 100 ms

FD_ZERO(&writefds);
FD_SET(socket, &writefds);

int ret = select(socket + 1, NULL, &writefds, NULL, &tv);
if (ret > 0 && FD_ISSET(socket, &writefds)) {
return 1; // Socket is ready
} else {
return 0; // Socket not ready
}
}

void STRATUM_V1_initialize_buffer()
{
json_rpc_buffer = malloc(BUFFER_SIZE);
Expand Down Expand Up @@ -78,11 +96,22 @@ char * STRATUM_V1_receive_jsonrpc_line(int sockfd)
do {
memset(recv_buffer, 0, BUFFER_SIZE);
nbytes = recv(sockfd, recv_buffer, BUFFER_SIZE - 1, 0);
if (nbytes == -1) {
perror("recv");
esp_restart();
if (nbytes < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// Try again if the operation would block
vTaskDelay(100 / portTICK_PERIOD_MS);
continue;
}
ESP_LOGE("STRATUM", "Socket recv failed: errno %d", errno);
//free(recv_buffer);
return NULL;
}
else if (nbytes == 0) {
// Connection closed
ESP_LOGW("STRATUM", "Socket connection closed");
//free(recv_buffer);
return NULL;
}

realloc_json_buffer(nbytes);
strncat(json_rpc_buffer, recv_buffer, nbytes);
} while (!strstr(json_rpc_buffer, "\n"));
Expand Down Expand Up @@ -292,6 +321,12 @@ void STRATUM_V1_submit_share(int socket, const char * username, const char * job
const uint32_t nonce, const uint32_t version)
{
char submit_msg[BUFFER_SIZE];

if (!is_socket_connected(socket)) {
ESP_LOGI(TAG,"Socket not connected. Cannot send message.\n");
return;
}

sprintf(submit_msg,
//"{\"id\": %d, \"method\": \"mining.submit\", \"params\": [\"%s\", \"%s\", \"%s\", \"%08lx\", \"%08lx\", \"%08lx\"]}\n",
"{\"id\": %d, \"method\": \"mining.submit\", \"params\": [\"%s\", \"%s\", \"%s\", \"%08x\", \"%08x\", \"%08x\"]}\n",
Expand Down
65 changes: 49 additions & 16 deletions main/displays/displayDriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

#include "displayDriver.h"
#include "ui.h"
#include "ui_helpers.h"

static const char *TAG = "TDisplayS3";
static bool lvgl_update_enabled = true; // Set animations on at beginning
static bool doCangeScreenFlag = false;



Expand Down Expand Up @@ -47,21 +50,47 @@ static void example_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_
esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
}

static void example_increase_lvgl_tick(void *arg)


static void increase_lvgl_tick()
{
/* Tell LVGL how many milliseconds has elapsed */
lv_tick_inc(TDISPLAYS3_LVGL_TICK_PERIOD_MS);
}

// Refresh screen values (for manual operations)
void display_RefreshScreen() {
lv_timer_handler(); // Maneja las tareas pendientes de LVGL
increase_lvgl_tick(); // Incrementa el tick según el periodo que definías antes
}

static void lvglTimerTask(void* param)
{
while(1)
{
// The task running lv_timer_handler should have lower priority than that running `lv_tick_inc`
lv_timer_handler();
int64_t myLastTime = esp_timer_get_time();
while(1) {
if(lvgl_update_enabled) {
//int32_t elapsedTimeInMilliseconds = (esp_timer_get_time() - myLastTime) / 1000;
//if(elapsedTimeInMilliseconds>20) lv_tick_inc(TDISPLAYS3_LVGL_TICK_PERIOD_MS);
//else lv_tick_inc(elapsedTimeInMilliseconds);
increase_lvgl_tick();
lv_timer_handler(); // Process pending LVGL tasks
vTaskDelay(15 / portTICK_PERIOD_MS); // Delay during animations

}
else{
if(doCangeScreenFlag) {
doCangeScreenFlag = false;
changeScreen();
}
vTaskDelay(200 / portTICK_PERIOD_MS); // Delay waiting animation trigger
}

}
}

vTaskDelay(50/portTICK_PERIOD_MS);
}
// Función para activar las actualizaciones
void enable_lvgl_animations(bool enable) {
lvgl_update_enabled = enable;
}

static void main_creatSysteTasks(void)
Expand Down Expand Up @@ -188,13 +217,13 @@ lv_obj_t * initTDisplayS3(void){

ESP_LOGI(TAG, "Install LVGL tick timer");
// Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
const esp_timer_create_args_t lvgl_tick_timer_args = {
/*const esp_timer_create_args_t lvgl_tick_timer_args = {
.callback = &example_increase_lvgl_tick,
.name = "lvgl_tick"
};
};*/
esp_timer_handle_t lvgl_tick_timer = NULL;
ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, TDISPLAYS3_LVGL_TICK_PERIOD_MS * 1000));
//ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
//ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, TDISPLAYS3_LVGL_TICK_PERIOD_MS * 1000));

ESP_LOGI(TAG, "Display LVGL animation");
lv_obj_t *scr = lv_disp_get_scr_act(disp);
Expand Down Expand Up @@ -233,7 +262,8 @@ void display_updateShares(SystemModule * module){
void display_updateTime(SystemModule * module){
char strData[20];

// Calculate the uptime in seconds
// Calculate the uptime in seconds
//int64_t currentTimeTest = esp_timer_get_time() + (8 * 3600 * 1000000LL) + (1800 * 1000000LL);//(8 * 60 * 60 * 10000);
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);
Expand All @@ -244,12 +274,12 @@ void display_updateTime(SystemModule * module){

snprintf(strData, sizeof(strData), "%dd %ih %im %is", uptime_in_days, uptime_in_hours, uptime_in_minutes, current_seconds);
lv_label_set_text(ui_lbTime, strData); // Update label

}

void display_updateGlobalState(GlobalState * GLOBAL_STATE){
char strData[20];


SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;
PowerManagementModule * power_management = &GLOBAL_STATE->POWER_MANAGEMENT_MODULE;

Expand All @@ -275,7 +305,6 @@ void display_updateGlobalState(GlobalState * GLOBAL_STATE){
uint16_t vcore = ADC_get_vcore();
snprintf(strData, sizeof(strData), "%umV", vcore);
lv_label_set_text(ui_lbVcore, strData); // Update label

}

void display_updateIpAddress(char * ip_address_str){
Expand All @@ -284,6 +313,7 @@ void display_updateIpAddress(char * ip_address_str){
snprintf(strData, sizeof(strData), "%s", ip_address_str);
lv_label_set_text(ui_lbIP, ip_address_str); // Update label
lv_label_set_text(ui_lbIPSet, ip_address_str); // Update label

}

/*
Expand All @@ -307,7 +337,9 @@ void startUpdateScreenTask() {
// ISR Handler para el botón
static void boton_isr_handler(void* arg) {
//ESP_LOGI("UI", "Button pressed changing screen");
lv_async_call(changeScreen, NULL);
doCangeScreenFlag = true;
lvgl_update_enabled = false;
//lv_async_call(changeScreen, NULL);
}

void buttons_init(void) {
Expand Down Expand Up @@ -335,7 +367,8 @@ void display_init(void)
lv_obj_t * scr = initTDisplayS3();

ui_init();

//manual_lvgl_update();

//startUpdateScreenTask(); //Start screen update task
main_creatSysteTasks();

Expand Down
4 changes: 3 additions & 1 deletion main/displays/displayDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
#define TDISPLAYS3_LCD_CMD_BITS 8
#define TDISPLAYS3_LCD_PARAM_BITS 8

#define TDISPLAYS3_LVGL_TICK_PERIOD_MS 8


/* FUNCTIONS DECLARATION -----------------------------------------------------*/
void display_init(void);
Expand All @@ -58,6 +58,8 @@ void display_updateShares(SystemModule * module);
void display_updateTime(SystemModule * module);
void display_updateGlobalState(GlobalState * GLOBAL_STATE);
void display_updateIpAddress(char * ip_address_str);
void enable_lvgl_animations(bool enable);
void display_RefreshScreen(void);



Expand Down
55 changes: 37 additions & 18 deletions main/displays/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "esp_log.h"
#include "esp_timer.h"
#include "displayDriver.h"

///////////////////// VARIABLES ////////////////////
lv_obj_t *current_screen = NULL;
Expand Down Expand Up @@ -47,21 +48,23 @@ lv_obj_t * ui_lbPortSet;

int64_t last_screen_change_time = 0;

void changeScreen(void * arg) {
void changeScreen(void){ // * arg) {
int64_t current_time = esp_timer_get_time();

//Check if screen is changing to avoid problems during change
if (current_time - last_screen_change_time < 500000) return; // 500000 microsegundos = 500 ms - No cambies pantalla
if ((current_time - last_screen_change_time) < 1500000) return; // 1500000 microsegundos = 1500 ms = 1.5s - No cambies pantalla
last_screen_change_time = current_time;

if(current_screen == ui_MiningScreen) {
_ui_screen_change(ui_SettingsScreen, LV_SCR_LOAD_ANIM_MOVE_LEFT, 400, 0);
if(current_screen == ui_MiningScreen) {
enable_lvgl_animations(true); //AutoStops after loading the screen
_ui_screen_change(ui_SettingsScreen, LV_SCR_LOAD_ANIM_MOVE_LEFT, 350, 0);
current_screen = ui_SettingsScreen; // Actualiza la pantalla actual
ESP_LOGI("UI", "NewScreen Settings displayed");
} else {
_ui_screen_change(ui_MiningScreen, LV_SCR_LOAD_ANIM_MOVE_RIGHT, 400, 0);
enable_lvgl_animations(true); //AutoStops after loading the screen
_ui_screen_change(ui_MiningScreen, LV_SCR_LOAD_ANIM_MOVE_RIGHT, 350, 0);
current_screen = ui_MiningScreen; // Actualiza la pantalla actual
ESP_LOGI("UI", "NewScreen Mining displayed");
ESP_LOGI("UI", "NewScreen Mining displayed");
}

}
Expand All @@ -81,6 +84,30 @@ void ui_event_Splash2(lv_event_t * e)
lv_obj_t * target = lv_event_get_target(e);
if(event_code == LV_EVENT_SCREEN_LOADED) {
_ui_screen_change(ui_MiningScreen, LV_SCR_LOAD_ANIM_FADE_ON, 500, 1500);
//Delete previous screen and free memory
lv_obj_clean(ui_Splash1);
}
}

void ui_event_MiningScreen(lv_event_t * e)
{
lv_event_code_t event_code = lv_event_get_code(e);
lv_obj_t * target = lv_event_get_target(e);
if(event_code == LV_EVENT_SCREEN_LOADED) {
//Delete previous screen and free memory
lv_obj_clean(ui_Splash2);
enable_lvgl_animations(false);
}
}

void ui_event_SettingsScreen(lv_event_t * e)
{
lv_event_code_t event_code = lv_event_get_code(e);
lv_obj_t * target = lv_event_get_target(e);
if(event_code == LV_EVENT_SCREEN_LOADED) {
//Delete previous screen and free memory
//lv_obj_clean(ui_Splash2);
enable_lvgl_animations(false);
}
}

Expand Down Expand Up @@ -128,6 +155,7 @@ void ui_Splash2_screen_init(void)

lv_obj_add_event_cb(ui_Splash2, ui_event_Splash2, LV_EVENT_ALL, NULL);


}

/*void ui_Portal_screen_init(void)
Expand Down Expand Up @@ -315,6 +343,8 @@ void ui_MiningScreen_screen_init(void)
lv_obj_set_style_text_align(ui_lbASIC, LV_TEXT_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_text_font(ui_lbASIC, &ui_font_OpenSansBold14, LV_PART_MAIN | LV_STATE_DEFAULT);

lv_obj_add_event_cb(ui_MiningScreen, ui_event_MiningScreen, LV_EVENT_ALL, NULL);

}
void ui_SettingsScreen_screen_init(void)
{
Expand Down Expand Up @@ -437,18 +467,7 @@ void ui_SettingsScreen_screen_init(void)
lv_obj_set_style_text_align(ui_lbPortSet, LV_TEXT_ALIGN_LEFT, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_text_font(ui_lbPortSet, &ui_font_OpenSansBold13, LV_PART_MAIN | LV_STATE_DEFAULT);

/*ui_lbBestDifficulty = lv_label_create(ui_SettingsScreen);
lv_obj_set_width(ui_lbBestDifficulty, LV_SIZE_CONTENT); /// 1
lv_obj_set_height(ui_lbBestDifficulty, LV_SIZE_CONTENT); /// 1
lv_obj_set_x(ui_lbBestDifficulty, 34);
lv_obj_set_y(ui_lbBestDifficulty, 21);
lv_obj_set_align(ui_lbBestDifficulty, LV_ALIGN_LEFT_MID);
lv_label_set_text(ui_lbBestDifficulty, "22M");
lv_obj_set_style_text_color(ui_lbBestDifficulty, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_text_opa(ui_lbBestDifficulty, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_text_align(ui_lbBestDifficulty, LV_TEXT_ALIGN_LEFT, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_text_font(ui_lbBestDifficulty, &ui_font_OpenSansBold14, LV_PART_MAIN | LV_STATE_DEFAULT);
*/

}

void ui_init(void)
Expand Down
5 changes: 4 additions & 1 deletion main/displays/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ LV_FONT_DECLARE(ui_font_OpenSansBold13);
LV_FONT_DECLARE(ui_font_OpenSansBold14);


#define TDISPLAYS3_LVGL_TICK_PERIOD_MS 30

void ui_init(void);
void changeScreen(void * arg);
void changeScreen(void); //* arg);
void manual_lvgl_update();

#ifdef __cplusplus
} /*extern "C"*/
Expand Down
1 change: 1 addition & 0 deletions main/displays/ui_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ void _ui_slider_set_text_value(lv_obj_t * trg, lv_obj_t * src, char * prefix, ch
void _ui_checked_set_text_value(lv_obj_t * trg, lv_obj_t * src, char * txt_on, char * txt_off);



#endif
Loading

0 comments on commit 59257c0

Please sign in to comment.