Skip to content

Commit

Permalink
Adapt self_test to lvgl code
Browse files Browse the repository at this point in the history
Other changes:
 * self test results, is_screen_active and ip_addr_str in GLOBAL_STATE
 * Fix long label scrolling
 * Add callback for long press function
  • Loading branch information
mutatrum committed Dec 1, 2024
1 parent aa85011 commit 6238a3b
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 210 deletions.
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);
14 changes: 5 additions & 9 deletions main/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#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"
Expand All @@ -23,8 +24,6 @@
#define LCD_CMD_BITS 8
#define LCD_PARAM_BITS 8

static bool is_display_active = false;

static const char * TAG = "display";

static lv_theme_t theme;
Expand All @@ -38,8 +37,10 @@ static void theme_apply(lv_theme_t *theme, lv_obj_t *obj) {
}
}

esp_err_t display_init(void)
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);

Expand Down Expand Up @@ -116,12 +117,7 @@ esp_err_t display_init(void)
// 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");

is_display_active = true;
GLOBAL_STATE->SYSTEM_MODULE.is_screen_active = true;

return ESP_OK;
}

bool display_active(void)
{
return is_display_active;
}
3 changes: 1 addition & 2 deletions main/display.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef DISPLAY_H_
#define DISPLAY_H_

esp_err_t display_init(void);
bool display_active(void);
esp_err_t display_init(void * pvParameters);

#endif /* DISPLAY_H_ */
5 changes: 4 additions & 1 deletion main/global_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ 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;
Expand All @@ -76,13 +77,15 @@ typedef struct
bool is_using_fallback;
uint16_t overheat_mode;
uint32_t lastClockSync;
bool is_screen_active;
} SystemModule;

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

typedef struct
Expand Down
20 changes: 13 additions & 7 deletions main/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ static const char * TAG = "input";
static lv_indev_state_t button_state = LV_INDEV_STATE_RELEASED;
static lv_point_t points[] = { {0, 0} }; // must be static

static void (*button_long_pressed)(void) = NULL;

static void button_read(lv_indev_t *indev, lv_indev_data_t *data)
{
data->key = LV_KEY_ENTER;
Expand All @@ -28,20 +30,24 @@ static void IRAM_ATTR button_isr_handler(void *arg)
button_state = pressed ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
}

static void button_short_clicked(lv_event_t *e)
static void button_short_clicked_event_cb(lv_event_t *e)
{
ESP_LOGI(TAG, "Short button press detected, switching to next screen");
screen_next();
}

static void button_long_pressed(lv_event_t *e)
static void button_long_pressed_event_cb(lv_event_t *e)
{
ESP_LOGI(TAG, "Long button press detected, toggling WiFi SoftAP");
toggle_wifi_softap();
if (button_long_pressed != NULL) {
ESP_LOGI(TAG, "Long button press detected");
button_long_pressed();
}
}

esp_err_t input_init(void)
esp_err_t input_init(void (*button_long_pressed_cb)(void))
{
button_long_pressed = button_long_pressed_cb;

// Button handling
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << BUTTON_BOOT_GPIO),
Expand All @@ -61,8 +67,8 @@ esp_err_t input_init(void)
lv_indev_set_long_press_time(indev, LONG_PRESS_DURATION_MS);
lv_indev_set_read_cb(indev, button_read);
lv_indev_set_button_points(indev, points);
lv_indev_add_event_cb(indev, button_short_clicked, LV_EVENT_SHORT_CLICKED, NULL);
lv_indev_add_event_cb(indev, button_long_pressed, LV_EVENT_LONG_PRESSED, NULL);
lv_indev_add_event_cb(indev, button_short_clicked_event_cb, LV_EVENT_SHORT_CLICKED, NULL);
lv_indev_add_event_cb(indev, button_long_pressed_event_cb, LV_EVENT_LONG_PRESSED, NULL);

return ESP_OK;
}
2 changes: 1 addition & 1 deletion main/input.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef INPUT_H_
#define INPUT_H_

esp_err_t input_init(void);
esp_err_t input_init(void (*button_long_pressed_cb)(void));

#endif /* INPUT_H_ */
4 changes: 2 additions & 2 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void app_main(void)
//should we run the self test?
if (should_test(&GLOBAL_STATE)) {
self_test((void *) &GLOBAL_STATE);
vTaskDelay(60 * 60 * 1000 / portTICK_PERIOD_MS);
return;
}

SYSTEM_init_system(&GLOBAL_STATE);
Expand All @@ -74,7 +74,7 @@ void app_main(void)
GLOBAL_STATE.SYSTEM_MODULE.ssid[sizeof(GLOBAL_STATE.SYSTEM_MODULE.ssid)-1] = 0;

// init and connect to wifi
wifi_init(wifi_ssid, wifi_pass, hostname);
wifi_init(wifi_ssid, wifi_pass, hostname, GLOBAL_STATE.SYSTEM_MODULE.ip_addr_str);

generate_ssid(GLOBAL_STATE.SYSTEM_MODULE.ap_ssid);

Expand Down
Loading

0 comments on commit 6238a3b

Please sign in to comment.