Skip to content

Commit

Permalink
Serial console (#117)
Browse files Browse the repository at this point in the history
* Update readme

* Update README.md (#95)

* v1.6.4 (#113)

* Fix gcode previews with special chars not loading

* Add .gitignore file (#108)

* Bulletproof ci.py (#107)

* Implement file sorting (implement #89)

* Set chip family to ESP32-S3 for specific models (fix #67)

* Add files menu to params panel while printing (implement #80)

* Update ci.py (#110)

Typo fix for ESP32-S3 boards array name

---------

Co-authored-by: Sebastian Göls <[email protected]>
Co-authored-by: Miroslav Zuzelka <[email protected]>

* Added serial console

* backspace, minor tweaks

* - added temporary_config alongside global_config, to hold non-persistent configuration
- added macros LOG, LOG_F, LOG_LN conditional on temporary_config.debug for Serial.print/printf/println
- put all debug to console behind these macros
- added 'debug' serial command to toggle temporary_config.debug, defaults to REPO_DEVELOPMENT
- added 'echo' serial command to toggle remote echo, temporary_config.remote_echo
- added #define DISPLAY_SECRETS to global_config.h, to censor wifi password and api key on serial console
- added entries about serial console to README.md and to _site/index.html

* restored -DREPO_DEVELOPMENT=1 (m)

* Build failed on esp32-3248S035C, reduced console input buffer size (static char cmdline) as it was failing to fit.

* typo

* A lot of what should be LOG_F was LOG_LN instead
Handling undefined REPO_DEVELOPMENT when initializing temporary_config.debug

---------

Co-authored-by: Sims <[email protected]>
Co-authored-by: Beebles <[email protected]>
Co-authored-by: Sebastian Göls <[email protected]>
Co-authored-by: Miroslav Zuzelka <[email protected]>
Co-authored-by: Bartosz Wucke <[email protected]>
  • Loading branch information
6 people authored Aug 2, 2024
1 parent 19cfaef commit 41aa073
Show file tree
Hide file tree
Showing 21 changed files with 664 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/compile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
if: (github.event_name == 'release' && github.event.action == 'created') || (github.event_name != 'pull_request' && github.ref == 'refs/heads/master')
if: github.event_name == 'release' && github.event.action == 'created'
steps:
- name: Print GitHub event name
run: |
Expand Down
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
8 changes: 4 additions & 4 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 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;
}
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
4 changes: 3 additions & 1 deletion CYD-Klipper/src/ui/ip_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "switch_printer.h"
#include "macros.h"
#include "../core/lv_setup.h"
#include "serial/serial_console.h"

lv_obj_t * hostEntry;
lv_obj_t * portEntry;
Expand Down Expand Up @@ -66,7 +67,7 @@ connection_status_t verify_ip(){
return httpCode == 200 ? CONNECT_OK : CONNECT_FAIL;
}
catch (...) {
Serial.println("Failed to connect");
LOG_LN("Failed to connect");
return CONNECT_FAIL;
}
}
Expand Down Expand Up @@ -253,5 +254,6 @@ void ip_init(){
while (!get_current_printer_config()->ip_configured)
{
lv_handler();
serial_console::run();
}
}
4 changes: 2 additions & 2 deletions CYD-Klipper/src/ui/macros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PRINTER_CONFIG * curernt_config = NULL;
static void btn_press(lv_event_t * e){
lv_obj_t * btn = lv_event_get_target(e);
const char* macro = (const char*)lv_event_get_user_data(e);
Serial.printf("Macro: %s\n", macro);
LOG_F(("Macro: %s\n", macro))
send_gcode(false, macro);
}

Expand All @@ -25,7 +25,7 @@ static void power_device_toggle(lv_event_t * e)
auto state = lv_obj_get_state(lv_event_get_target(e));
bool checked = (state & LV_STATE_CHECKED == LV_STATE_CHECKED);
const char* power_device_name = (const char*)lv_event_get_user_data(e);
Serial.printf("Power Device: %s, State: %d -> %d\n", power_device_name, !checked, checked);
LOG_F(("Power Device: %s, State: %d -> %d\n", power_device_name, !checked, checked))

if (curernt_config != NULL)
set_power_state(power_device_name, checked, curernt_config);
Expand Down
Loading

0 comments on commit 41aa073

Please sign in to comment.