Skip to content

Commit

Permalink
Merge branch 'lvgl-all-the-things' of https://github.com/mutatrum/ESP…
Browse files Browse the repository at this point in the history
…-Miner into lvgl_display_timeout
  • Loading branch information
terratec committed Dec 9, 2024
2 parents 13791f1 + a176ded commit 19d610c
Show file tree
Hide file tree
Showing 29 changed files with 1,416 additions and 969 deletions.
40 changes: 38 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,48 @@
"array": "c",
"string": "c",
"string_view": "c",
"*.tcc": "c"
"*.tcc": "c",
"esp_lvgl_port.h": "c",
"cstdint": "c",
"regex": "c",
"i2c_bitaxe.h": "c",
"i2c_master.h": "c",
"nvs_config.h": "c",
"display.h": "c",
"esp_lcd_panel_vendor.h": "c",
"esp_lcd_panel_st7789.h": "c",
"esp_lcd_panel_ssd1306.h": "c",
"esp_lcd_panel_io.h": "c",
"esp_lcd_panel_ops.h": "c",
"esp_lcd_io_i2c.h": "c",
"esp_lcd_types.h": "c",
"i2c.h": "c",
"cstdlib": "c",
"i2c_types.h": "c",
"esp_lcd_panel_dev.h": "c",
"bitset": "c",
"memory": "c",
"random": "c",
"future": "c",
"optional": "c",
"esp_lcd_panel_interface.h": "c",
"span": "c",
"oled.h": "c",
"charconv": "c",
"chrono": "c",
"format": "c",
"ratio": "c",
"system_error": "c",
"functional": "c",
"tuple": "c",
"type_traits": "c",
"utility": "c",
"compare": "c"
},
"editor.formatOnSave": false,
"cSpell.words": [
"ssid"
],
"idf.port": "/dev/cu.usbmodem1434301",
"idf.port": "/dev/ttyACM0",
"C_Cpp.intelliSenseEngine": "Tag Parser"
}
11 changes: 9 additions & 2 deletions components/connect/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ static const char * TAG = "wifi_station";

static int s_retry_num = 0;

static char * _ip_addr_str;

static void event_handler(void * arg, esp_event_base_t event_base, int32_t event_id, void * event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
Expand All @@ -75,8 +77,11 @@ static void event_handler(void * arg, esp_event_base_t event_base, int32_t event
MINER_set_wifi_status(WIFI_RETRYING, s_retry_num);

} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {

ip_event_got_ip_t * event = (ip_event_got_ip_t *) event_data;
ESP_LOGI(TAG, "Bitaxe ip:" IPSTR, IP2STR(&event->ip_info.ip));
snprintf(_ip_addr_str, IP4ADDR_STRLEN_MAX, IPSTR, IP2STR(&event->ip_info.ip));

ESP_LOGI(TAG, "Bitaxe ip: %s", _ip_addr_str);
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
MINER_set_wifi_status(WIFI_CONNECTED, 0);
Expand Down Expand Up @@ -194,8 +199,10 @@ esp_netif_t * wifi_init_sta(const char * wifi_ssid, const char * wifi_pass)
return esp_netif_sta;
}

void wifi_init(const char * wifi_ssid, const char * wifi_pass, const char * hostname)
void wifi_init(const char * wifi_ssid, const char * wifi_pass, const char * hostname, char * ip_addr_str)
{
_ip_addr_str = ip_addr_str;

s_wifi_event_group = xEventGroupCreate();

ESP_ERROR_CHECK(esp_netif_init());
Expand Down
2 changes: 1 addition & 1 deletion components/connect/include/connect.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ typedef enum
void toggle_wifi_softap(void);
void wifi_softap_on(void);
void wifi_softap_off(void);
void wifi_init(const char * wifi_ssid, const char * wifi_pass, const char * hostname);
void wifi_init(const char * wifi_ssid, const char * wifi_pass, const char * hostname, char * ip_addr_str);
EventBits_t wifi_connect(void);
void generate_ssid(char * ssid);
11 changes: 8 additions & 3 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@ SRCS
"adc.c"
"DS4432U.c"
"EMC2101.c"
"fonts.c"
"i2c_bitaxe.c"
"INA260.c"
"led_controller.c"
"main.c"
"nvs_config.c"
"oled.c"
"display.c"
"screen.c"
"input.c"
"system.c"
"TPS546.c"
"vcore.c"
"work_queue.c"
"nvs_device.c"
"lv_font_portfolio-6x8.c"
"logo.c"
"./http_server/http_server.c"
"./self_test/self_test.c"
"./tasks/stratum_task.c"
"./tasks/create_jobs_task.c"
"./tasks/asic_task.c"
"./tasks/asic_result_task.c"
"./tasks/user_input_task.c"
"./tasks/power_management_task.c"

INCLUDE_DIRS
Expand Down Expand Up @@ -51,6 +53,9 @@ PRIV_REQUIRES
"esp_driver_i2c"
)

idf_build_set_property(COMPILE_OPTIONS "-DLV_CONF_INCLUDE_SIMPLE=1" APPEND)
idf_build_set_property(COMPILE_OPTIONS "-DLV_CONF_PATH= ${CMAKE_SOURCE_DIR}/main/lv_conf.h" APPEND)

set(WEB_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/http_server/axe-os")

if("$ENV{GITHUB_ACTIONS}" STREQUAL "true")
Expand Down
133 changes: 133 additions & 0 deletions main/display.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_timer.h"
#include "esp_lcd_panel_io.h"
#include "esp_lcd_panel_ops.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_check.h"
#include "lvgl.h"
#include "lvgl__lvgl/src/themes/lv_theme_private.h"
#include "esp_lvgl_port.h"
#include "global_state.h"
#include "nvs_config.h"
#include "i2c_bitaxe.h"
#include "driver/i2c_master.h"
#include "driver/i2c_types.h"
#include "esp_lcd_panel_ssd1306.h"

#define SSD1306_I2C_ADDRESS 0x3C

#define LCD_H_RES 128
#define LCD_V_RES 32
#define LCD_CMD_BITS 8
#define LCD_PARAM_BITS 8

static const char * TAG = "display";

static lv_theme_t theme;
static lv_style_t scr_style;

extern const lv_font_t lv_font_portfolio_6x8;

static void theme_apply(lv_theme_t *theme, lv_obj_t *obj) {
if (lv_obj_get_parent(obj) == NULL) {
lv_obj_add_style(obj, &scr_style, LV_PART_MAIN);
}
}

esp_err_t display_init(void * pvParameters)
{
GlobalState * GLOBAL_STATE = (GlobalState *) pvParameters;

uint8_t flip_screen = nvs_config_get_u16(NVS_CONFIG_FLIP_SCREEN, 1);
uint8_t invert_screen = nvs_config_get_u16(NVS_CONFIG_INVERT_SCREEN, 0);

i2c_master_bus_handle_t i2c_master_bus_handle;
ESP_RETURN_ON_ERROR(i2c_bitaxe_get_master_bus_handle(&i2c_master_bus_handle), TAG, "Failed to get i2c master bus handle");

ESP_LOGI(TAG, "Install panel IO");
esp_lcd_panel_io_handle_t io_handle = NULL;
esp_lcd_panel_io_i2c_config_t io_config = {
.scl_speed_hz = I2C_BUS_SPEED_HZ,
.dev_addr = SSD1306_I2C_ADDRESS,
.control_phase_bytes = 1,
.lcd_cmd_bits = LCD_CMD_BITS,
.lcd_param_bits = LCD_PARAM_BITS,
.dc_bit_offset = 6
};

ESP_RETURN_ON_ERROR(esp_lcd_new_panel_io_i2c(i2c_master_bus_handle, &io_config, &io_handle), TAG, "Failed to initialise i2c panel bus");

ESP_LOGI(TAG, "Install SSD1306 panel driver");
esp_lcd_panel_handle_t panel_handle = NULL;
esp_lcd_panel_dev_config_t panel_config = {
.bits_per_pixel = 1,
.reset_gpio_num = -1,
};

esp_lcd_panel_ssd1306_config_t ssd1306_config = {
.height = LCD_V_RES,
};
panel_config.vendor_config = &ssd1306_config;

ESP_RETURN_ON_ERROR(esp_lcd_new_panel_ssd1306(io_handle, &panel_config, &panel_handle), TAG, "No display found");
ESP_RETURN_ON_ERROR(esp_lcd_panel_reset(panel_handle), TAG, "Panel reset failed");
esp_err_t esp_lcd_panel_init_err = esp_lcd_panel_init(panel_handle);
if (esp_lcd_panel_init_err != ESP_OK) {
ESP_LOGE(TAG, "Panel init failed, no display connected?");
} else {
ESP_RETURN_ON_ERROR(esp_lcd_panel_invert_color(panel_handle, invert_screen), TAG, "Panel invert failed");
// ESP_RETURN_ON_ERROR(esp_lcd_panel_mirror(panel_handle, false, false), TAG, "Panel mirror failed");
}

ESP_LOGI(TAG, "Initialize LVGL");

const lvgl_port_cfg_t lvgl_cfg = ESP_LVGL_PORT_INIT_CONFIG();
ESP_RETURN_ON_ERROR(lvgl_port_init(&lvgl_cfg), TAG, "LVGL init failed");

const lvgl_port_display_cfg_t disp_cfg = {
.io_handle = io_handle,
.panel_handle = panel_handle,
.buffer_size = LCD_H_RES * LCD_V_RES,
.double_buffer = true,
.hres = LCD_H_RES,
.vres = LCD_V_RES,
.monochrome = true,
.color_format = LV_COLOR_FORMAT_RGB565,
.rotation = {
.swap_xy = false,
.mirror_x = !flip_screen, // The screen is not flipped, this is for backwards compatibility
.mirror_y = !flip_screen,
},
.flags = {
.swap_bytes = false,
.sw_rotate = false,
}
};

lv_disp_t * disp = lvgl_port_add_disp(&disp_cfg);

if (lvgl_port_lock(0)) {
lv_style_init(&scr_style);
lv_style_set_text_font(&scr_style, &lv_font_portfolio_6x8);
lv_style_set_bg_opa(&scr_style, LV_OPA_COVER);

lv_theme_set_apply_cb(&theme, theme_apply);

lv_display_set_theme(disp, &theme);
lvgl_port_unlock();
}

if (esp_lcd_panel_init_err == ESP_OK) {
// Only turn on the screen when it has been cleared
ESP_RETURN_ON_ERROR(esp_lcd_panel_disp_on_off(panel_handle, true), TAG, "Panel display on failed");

GLOBAL_STATE->SYSTEM_MODULE.is_screen_active = true;
} else {
ESP_LOGW(TAG, "No display found.");
}

return ESP_OK;
}
6 changes: 6 additions & 0 deletions main/display.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef DISPLAY_H_
#define DISPLAY_H_

esp_err_t display_init(void * pvParameters);

#endif /* DISPLAY_H_ */
50 changes: 0 additions & 50 deletions main/fonts.c

This file was deleted.

13 changes: 12 additions & 1 deletion main/global_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ typedef struct
uint64_t shares_accepted;
uint64_t shares_rejected;
int screen_page;
char oled_buf[20];
uint64_t best_nonce_diff;
char best_diff_string[DIFF_STRING_SIZE];
uint64_t best_session_nonce_diff;
Expand All @@ -69,15 +68,26 @@ typedef struct
bool startup_done;
char ssid[32];
char wifi_status[20];
char ip_addr_str[16]; // IP4ADDR_STRLEN_MAX
char ap_ssid[32];
char * pool_url;
char * fallback_pool_url;
uint16_t pool_port;
uint16_t fallback_pool_port;
bool is_using_fallback;
uint16_t overheat_mode;
uint32_t lastClockSync;
bool is_screen_active;
} SystemModule;

typedef struct
{
bool active;
char *message;
bool result;
bool finished;
} SelfTestModule;

typedef struct
{
DeviceModel device_model;
Expand All @@ -98,6 +108,7 @@ typedef struct
SystemModule SYSTEM_MODULE;
AsicTaskModule ASIC_TASK_MODULE;
PowerManagementModule POWER_MANAGEMENT_MODULE;
SelfTestModule SELF_TEST_MODULE;

char * extranonce_str;
int extranonce_2_len;
Expand Down
9 changes: 7 additions & 2 deletions main/i2c_bitaxe.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#define I2C_MASTER_SCL_IO 48 /*!< GPIO number used for I2C master clock */
#define I2C_MASTER_SDA_IO 47 /*!< GPIO number used for I2C master data */
#define I2C_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */

#define I2C_MASTER_NUM 0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */
#define I2C_MASTER_TIMEOUT_MS 1000
Expand Down Expand Up @@ -41,12 +40,18 @@ esp_err_t i2c_bitaxe_add_device(uint8_t device_address, i2c_master_dev_handle_t
i2c_device_config_t dev_cfg = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = device_address,
.scl_speed_hz = I2C_MASTER_FREQ_HZ,
.scl_speed_hz = I2C_BUS_SPEED_HZ,
};

return i2c_master_bus_add_device(i2c_bus_handle, &dev_cfg, dev_handle);
}

esp_err_t i2c_bitaxe_get_master_bus_handle(i2c_master_bus_handle_t * dev_handle)
{
*dev_handle = i2c_bus_handle;
return ESP_OK;
}

/**
* @brief Read a sequence of I2C bytes
* @param dev_handle The I2C device handle
Expand Down
Loading

0 comments on commit 19d610c

Please sign in to comment.