Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.7.0 #122

Merged
merged 11 commits into from
Aug 2, 2024
12 changes: 10 additions & 2 deletions CYD-Klipper/src/conf/global_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "lvgl.h"

GLOBAL_CONFIG global_config = {0};
TEMPORARY_CONFIG temporary_config = {0};

COLOR_DEF color_defs[] = {
{LV_PALETTE_BLUE, 0, LV_PALETTE_RED},
Expand Down Expand Up @@ -31,9 +32,9 @@ void verify_version()

GLOBAL_CONFIG config = {0};
preferences.getBytes("global_config", &config, sizeof(config));
Serial.printf("Config version: %d\n", config.version);
LOG_F(("Config version: %d\n", config.version))
if (config.version != CONFIG_VERSION) {
Serial.println("Clearing Global Config");
LOG_LN("Clearing Global Config");
preferences.clear();
}

Expand Down Expand Up @@ -131,4 +132,11 @@ void load_global_config()
preferences.begin("global_config", true);
preferences.getBytes("global_config", &global_config, sizeof(global_config));
preferences.end();

#if defined REPO_DEVELOPMENT && REPO_DEVELOPMENT == 1
temporary_config.debug = true;
#else
temporary_config.debug = false;
#endif
temporary_config.remote_echo = true;
}
13 changes: 13 additions & 0 deletions CYD-Klipper/src/conf/global_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#define CONFIG_VERSION 6
#define PRINTER_CONFIG_COUNT 8
#define DISPLAY_SECRETS 0

enum {
REMAINING_TIME_CALC_PERCENTAGE = 0,
Expand Down Expand Up @@ -86,16 +87,28 @@ typedef struct _GLOBAL_CONFIG {
unsigned char screen_timeout;
unsigned char printer_index;
} GLOBAL_CONFIG;

// Volatile/temporary config that doesn't survive a reset
typedef struct _TEMPORARY_CONFIG {
bool debug : 1;
bool remote_echo : 1;
} TEMPORARY_CONFIG;


typedef struct _COLOR_DEF {
lv_palette_t primary_color;
short primary_color_light;
lv_palette_t secondary_color;
} COLOR_DEF;

extern GLOBAL_CONFIG global_config;
extern TEMPORARY_CONFIG temporary_config;
extern COLOR_DEF color_defs[];

#define LOG(x) if(temporary_config.debug){ Serial.print(x);}
#define LOG_LN(x) if(temporary_config.debug){ Serial.println(x);}
#define LOG_F(x) if(temporary_config.debug){ Serial.printf x ;} // use with double braces, LOF_F(("x=%d\n",x));

void write_global_config();
void verify_version();
void load_global_config();
Expand Down
10 changes: 5 additions & 5 deletions CYD-Klipper/src/core/data_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void unfreeze_render_thread(){

void send_gcode(bool wait, const char *gcode)
{
Serial.printf("Sending gcode: %s\n", gcode);
LOG_F(("Sending gcode: %s\n", gcode))

SETUP_HTTP_CLIENT_FULL("/printer/gcode/script?script=" + urlEncode(gcode), false, wait ? 5000 : 750);
try
Expand All @@ -52,7 +52,7 @@ void send_gcode(bool wait, const char *gcode)
}
catch (...)
{
Serial.println("Failed to send gcode");
LOG_LN("Failed to send gcode");
}
}

Expand All @@ -73,7 +73,7 @@ int get_slicer_time_estimate_s()
JsonDocument doc;
deserializeJson(doc, client.getStream());
int time_estimate_s = doc["result"]["estimated_time"];
Serial.printf("Got slicer time estimate: %ds\n", time_estimate_s);
LOG_F(("Got slicer time estimate: %ds\n", time_estimate_s))
return time_estimate_s;
}

Expand Down Expand Up @@ -330,7 +330,7 @@ void fetch_printer_data()
unfreeze_render_thread();
}

Serial.printf("Failed to fetch printer data: %d\n", httpCode);
LOG_F(("Failed to fetch printer data: %d\n", httpCode))
}
}

Expand All @@ -349,7 +349,7 @@ void fetch_printer_data_minimal()

delay(10);
HTTPClient client;
configure_http_client(client, get_full_url("/printer/objects/query?webhooks&print_stats&virtual_sdcard", config), true, 1000);
configure_http_client(client, get_full_url("/printer/objects/query?webhooks&print_stats&virtual_sdcard", config), true, 1000, config);
freeze_request_thread();

int httpCode = client.GET();
Expand Down
12 changes: 6 additions & 6 deletions CYD-Klipper/src/core/files_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ FILESYSTEM_FILE* get_files(int limit)
freeze_request_thread();
clear_files();

Serial.printf("Heap space pre-file-parse: %d bytes\n", esp_get_free_heap_size());
LOG_F(("Heap space pre-file-parse: %d bytes\n", esp_get_free_heap_size()))
std::list<FILESYSTEM_FILE> files;

auto timer_request = millis();
Expand All @@ -41,7 +41,7 @@ FILESYSTEM_FILE* get_files(int limit)
if (httpCode == 200){
JsonDocument doc;
auto parseResult = deserializeJson(doc, client.getStream());
Serial.printf("Json parse: %s\n", parseResult.c_str());
LOG_F(("Json parse: %s\n", parseResult.c_str()))
auto result = doc["result"].as<JsonArray>();

for (auto file : result){
Expand All @@ -62,7 +62,7 @@ FILESYSTEM_FILE* get_files(int limit)

f.name = (char*)malloc(strlen(path) + 1);
if (f.name == NULL){
Serial.println("Failed to allocate memory");
LOG_LN("Failed to allocate memory");
continue;
}
strcpy(f.name, path);
Expand All @@ -88,7 +88,7 @@ FILESYSTEM_FILE* get_files(int limit)
FILESYSTEM_FILE* result = (FILESYSTEM_FILE*)malloc(size);

if (result == NULL){
Serial.println("Failed to allocate memory");
LOG_LN("Failed to allocate memory");

for (auto file : files){
free(file.name);
Expand All @@ -106,8 +106,8 @@ FILESYSTEM_FILE* get_files(int limit)
result += 1;
}

Serial.printf("Heap space post-file-parse: %d bytes\n", esp_get_free_heap_size());
Serial.printf("Got %d files. Request took %dms, parsing took %dms\n", files.size(), timer_parse - timer_request, millis() - timer_parse);
LOG_F(("Heap space post-file-parse: %d bytes\n", esp_get_free_heap_size()))
LOG_F(("Got %d files. Request took %dms, parsing took %dms\n", files.size(), timer_parse - timer_request, millis() - timer_parse))
unfreeze_request_thread();
return last_query;
}
17 changes: 10 additions & 7 deletions CYD-Klipper/src/core/http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

String get_full_url(String url_part, PRINTER_CONFIG * config)
{
if (config == NULL){
config = get_current_printer_config();
}

return "http://" + String(config->klipper_host) + ":" + String(config->klipper_port) + url_part;
}

String get_full_url(String url_part)
void configure_http_client(HTTPClient &client, String url, bool stream, int timeout, PRINTER_CONFIG * config)
{
return "http://" + String(get_current_printer_config()->klipper_host) + ":" + String(get_current_printer_config()->klipper_port) + url_part;
}
if (config == NULL){
config = get_current_printer_config();
}

void configure_http_client(HTTPClient &client, String url, bool stream, int timeout)
{
if (stream){
client.useHTTP10(true);
}
Expand All @@ -23,7 +26,7 @@ void configure_http_client(HTTPClient &client, String url, bool stream, int time

client.begin(url);

if (get_current_printer_config()->auth_configured) {
client.addHeader("X-Api-Key", get_current_printer_config()->klipper_auth);
if (config->auth_configured) {
client.addHeader("X-Api-Key", config->klipper_auth);
}
}
7 changes: 2 additions & 5 deletions CYD-Klipper/src/core/http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
#include <HTTPClient.h>
#include "../conf/global_config.h"

String get_full_url(String url_part);
String get_full_url(String url_part, PRINTER_CONFIG * config);

void configure_http_client(HTTPClient &client, String url, bool stream = true, int timeout = 1000);
String get_full_url(String url_part, PRINTER_CONFIG * config = NULL);
void configure_http_client(HTTPClient &client, String url, bool stream = true, int timeout = 1000, PRINTER_CONFIG * config = NULL);

#define SETUP_HTTP_CLIENT(url_part) HTTPClient client; configure_http_client(client, get_full_url(url_part));

#define SETUP_HTTP_CLIENT_FULL(url_part, stream, timeout) HTTPClient client; configure_http_client(client, get_full_url(url_part), stream, timeout);
10 changes: 6 additions & 4 deletions CYD-Klipper/src/core/lv_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "lvgl.h"
#include "../ui/ui_utils.h"
#include <Esp.h>
#include "../ui/serial/serial_console.h"

#ifndef CPU_FREQ_HIGH
#define CPU_FREQ_HIGH 240
Expand Down Expand Up @@ -117,6 +118,7 @@ void lv_do_calibration(){

while (true){
lv_handler();
serial_console::run();

if (point[0] != 0 && point[1] != 0){
break;
Expand Down Expand Up @@ -175,15 +177,15 @@ void lv_do_calibration(){
global_config.screen_cal_y_offset = 10.0 - ((float)y1 * global_config.screen_cal_y_mult);

if (global_config.screen_cal_x_mult == std::numeric_limits<float>::infinity() || global_config.screen_cal_y_mult == std::numeric_limits<float>::infinity()){
Serial.println("Calibration failed, please try again");
LOG_LN("Calibration failed, please try again");
ESP.restart();
}

global_config.screen_calibrated = true;
write_global_config();

lv_obj_clean(lv_scr_act());
Serial.printf("Calibration done: X*%.2f + %.2f, Y*%.2f + %.2f\n", global_config.screen_cal_x_mult, global_config.screen_cal_x_offset, global_config.screen_cal_y_mult, global_config.screen_cal_y_offset);
LOG_F(("Calibration done: X*%.2f + %.2f, Y*%.2f + %.2f\n", global_config.screen_cal_x_mult, global_config.screen_cal_x_offset, global_config.screen_cal_y_mult, global_config.screen_cal_y_offset))
}

void set_screen_brightness()
Expand All @@ -208,7 +210,7 @@ void screen_timer_wake()

// Reset cpu freq
setCpuFrequencyMhz(CPU_FREQ_HIGH);
Serial.printf("CPU Speed: %d MHz\n", ESP.getCpuFreqMHz());
LOG_F(("CPU Speed: %d MHz\n", ESP.getCpuFreqMHz()))
#endif
}

Expand All @@ -220,7 +222,7 @@ void screen_timer_sleep(lv_timer_t *timer)

// Screen is off, no need to make the cpu run fast, the user won't notice ;)
setCpuFrequencyMhz(CPU_FREQ_LOW);
Serial.printf("CPU Speed: %d MHz\n", ESP.getCpuFreqMHz());
LOG_F(("CPU Speed: %d MHz\n", ESP.getCpuFreqMHz()))
#endif
}

Expand Down
6 changes: 3 additions & 3 deletions CYD-Klipper/src/lib/ESP32OTAPull.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class ESP32OTAPull
size_t bytes_written = Update.write(buff, bytes_read);
if (bytes_read != bytes_written)
{
// Serial.printf("Unexpected error in OTA: %d %d %d\n", bytes_to_read, bytes_read, bytes_written);
// LOG_F(("Unexpected error in OTA: %d %d %d\n", bytes_to_read, bytes_read, bytes_written))
break;
}
offset += bytes_written;
Expand Down Expand Up @@ -212,8 +212,8 @@ class ESP32OTAPull
String CDevice = config["Device"].isNull() ? "" : (const char *)config["Device"];
CVersion = config["Version"].isNull() ? "" : (const char *)config["Version"];
String CConfig = config["Config"].isNull() ? "" : (const char *)config["Config"];
//Serial.printf("Checking %s %s %s %s\n", CBoard.c_str(), CDevice.c_str(), CVersion.c_str(), CConfig.c_str());
//Serial.printf("Against %s %s %s %s\n", BoardName.c_str(), DeviceName.c_str(), CurrentVersion, ConfigName.c_str());
//LOG_F(("Checking %s %s %s %s\n", CBoard.c_str(), CDevice.c_str(), CVersion.c_str(), CConfig.c_str()))
//LOG_F(("Against %s %s %s %s\n", BoardName.c_str(), DeviceName.c_str(), CurrentVersion, ConfigName.c_str()))
if ((CBoard.isEmpty() || CBoard == BoardName) &&
(CDevice.isEmpty() || CDevice == DeviceName) &&
(CConfig.isEmpty() || CConfig == ConfigName))
Expand Down
6 changes: 4 additions & 2 deletions CYD-Klipper/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "core/screen_driver.h"
#include "ui/wifi_setup.h"
#include "ui/ip_setup.h"
#include "ui/serial/serial_console.h"
#include "lvgl.h"
#include "core/data_setup.h"
#include "ui/main_ui.h"
Expand All @@ -12,11 +13,11 @@

void setup() {
Serial.begin(115200);
Serial.println("Hello World");
serial_console::greet();
load_global_config();
screen_setup();
lv_setup();
Serial.println("Screen init done");
LOG_LN("Screen init done");

wifi_init();
ota_init();
Expand All @@ -31,6 +32,7 @@ void loop(){
wifi_ok();
data_loop();
lv_handler();
serial_console::run();

if (is_ready_for_ota_update())
{
Expand Down
20 changes: 10 additions & 10 deletions CYD-Klipper/src/ui/gcode_img.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static lv_img_dsc_t img_header = {0};
bool has_32_32_gcode_img(const char* filename)
{
if (filename == NULL){
Serial.println("No gcode filename");
LOG_LN("No gcode filename");
return false;
}

Expand All @@ -24,7 +24,7 @@ bool has_32_32_gcode_img(const char* filename)
httpCode = client.GET();
}
catch (...){
Serial.println("Exception while fetching gcode img location");
LOG_LN("Exception while fetching gcode img location");
return false;
}

Expand Down Expand Up @@ -52,14 +52,14 @@ bool has_32_32_gcode_img(const char* filename)
}

if (chosen_thumb != NULL){
Serial.printf("Found 32x32 PNG gcode img at %s\n", filename);
LOG_F(("Found 32x32 PNG gcode img at %s\n", filename))
strcpy(img_filename_path, chosen_thumb);
return true;
}
}
else
{
Serial.printf("Failed to fetch gcode image data: %d\n", httpCode);
LOG_F(("Failed to fetch gcode image data: %d\n", httpCode))
}

return false;
Expand All @@ -70,7 +70,7 @@ lv_obj_t* draw_gcode_img()
clear_img_mem();

if (img_filename_path[0] == 0){
Serial.println("No gcode img path");
LOG_LN("No gcode img path");
return NULL;
}

Expand All @@ -81,7 +81,7 @@ lv_obj_t* draw_gcode_img()
httpCode = client.GET();
}
catch (...){
Serial.println("Exception while fetching gcode img");
LOG_LN("Exception while fetching gcode img");
return NULL;
}

Expand All @@ -90,13 +90,13 @@ lv_obj_t* draw_gcode_img()
size_t len = client.getSize();
if (len <= 0)
{
Serial.println("No gcode img data");
LOG_LN("No gcode img data");
return NULL;
}

data_png = (unsigned char*)malloc(len + 1);
if (len != client.getStream().readBytes(data_png, len)){
Serial.println("Failed to read gcode img data");
LOG_LN("Failed to read gcode img data");
clear_img_mem();
return NULL;
}
Expand All @@ -120,12 +120,12 @@ lv_obj_t* draw_gcode_img()
lv_obj_t* show_gcode_img(const char* filename)
{
if (filename == NULL){
Serial.println("No gcode filename");
LOG_LN("No gcode filename");
return NULL;
}

if (!has_32_32_gcode_img(filename)){
Serial.println("No 32x32 gcode img found");
LOG_LN("No 32x32 gcode img found");
return NULL;
}

Expand Down
Loading
Loading