refactor(printer): replace direct_printer with printer naming

This commit is contained in:
admin
2026-03-01 14:49:50 +08:00
parent 57adabaa3f
commit 742cff63bb
15 changed files with 237 additions and 511 deletions

View File

@@ -25,10 +25,6 @@ bool domain_policy_is_retryable_error(esp_err_t err);
uint32_t domain_policy_printer_stop_timeout_ms(void);
uint32_t domain_policy_printer_connect_timeout_ms(void);
uint32_t domain_policy_printer_connect_timeout_min_ms(void);
uint32_t domain_policy_printer_connect_timeout_max_ms(void);
uint32_t domain_policy_image_generation_timeout_default_ms(void);
uint32_t domain_policy_image_generation_timeout_min_ms(void);
uint32_t domain_policy_image_generation_timeout_max_ms(void);
@@ -192,7 +188,7 @@ typedef struct {
typedef struct {
// Printer-only option: bypass paper/temp/battery precheck for this print job.
bool direct_ignore_precheck;
bool ignore_precheck;
} printer_print_options_t;
typedef struct {
@@ -205,10 +201,9 @@ typedef struct {
uint16_t override_strobe_interval_us;
uint16_t override_motor_step_us;
uint8_t override_steps_per_line;
} printer_direct_debug_config_t;
} printer_debug_config_t;
typedef struct {
bool connected;
bool busy;
bool has_paper;
int8_t paper_gpio_level;
@@ -224,13 +219,13 @@ typedef uint32_t printer_status_poll_pause_token_t;
esp_err_t printer_protocol_init(void);
esp_err_t printer_protocol_stop(uint32_t timeout_ms);
esp_err_t printer_protocol_get_direct_debug_config(printer_direct_debug_config_t *out_config,
char *err,
size_t err_len);
esp_err_t printer_protocol_set_direct_debug_config(const printer_direct_debug_config_t *config,
bool reset_defaults,
char *err,
size_t err_len);
esp_err_t printer_protocol_get_debug_config(printer_debug_config_t *out_config,
char *err,
size_t err_len);
esp_err_t printer_protocol_set_debug_config(const printer_debug_config_t *config,
bool reset_defaults,
char *err,
size_t err_len);
void printer_protocol_get_runtime_status(printer_runtime_status_t *out_status);
printer_status_poll_pause_token_t printer_protocol_status_poll_pause_acquire(void);

View File

@@ -26,7 +26,7 @@
typedef struct {
bool used;
bool cancel_requested;
bool direct_ignore_precheck;
bool ignore_precheck;
uint32_t id;
print_job_state_t state;
uint8_t progress;
@@ -64,9 +64,6 @@ extern TaskHandle_t s_status_poll_task_handle;
extern bool s_protocol_initialized;
extern bool s_protocol_stopping;
bool printer_protocol_acquire_control_lane(uint32_t timeout_ms, char *err, size_t err_len);
void printer_protocol_release_control_lane(void);
int printer_protocol_find_job_idx_locked(uint32_t id);
int printer_protocol_alloc_job_slot_locked(void);
bool printer_protocol_is_terminal_state(print_job_state_t state);

View File

@@ -76,18 +76,6 @@ uint32_t domain_policy_printer_stop_timeout_ms(void) {
return runtime_policy_printer_stop_timeout_ms();
}
uint32_t domain_policy_printer_connect_timeout_ms(void) {
return runtime_policy_printer_connect_timeout_ms();
}
uint32_t domain_policy_printer_connect_timeout_min_ms(void) {
return 1000;
}
uint32_t domain_policy_printer_connect_timeout_max_ms(void) {
return 60000;
}
uint32_t domain_policy_image_generation_timeout_default_ms(void) {
return runtime_policy_image_generation_timeout_default_ms();
}

View File

@@ -1,7 +1,6 @@
#include "domain.h"
#include "printer_protocol_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -44,15 +43,12 @@ static const char *TAG = "printer_protocol";
#endif
static esp_err_t ensure_direct_backend_ready(void) {
if (!runtime_policy_direct_printer_enabled()) {
return ESP_ERR_NOT_SUPPORTED;
}
return platform_direct_printer_init();
return platform_printer_init();
}
static void refresh_direct_status_locked(void) {
platform_printer_sensors_t sensors = {0};
if (platform_direct_printer_get_sensors(&sensors) == ESP_OK) {
if (platform_printer_get_sensors(&sensors) == ESP_OK) {
s_status.has_paper = sensors.has_paper;
s_status.paper_gpio_level = sensors.paper_gpio_level;
s_status.paper_present_level = sensors.paper_present_level;
@@ -210,57 +206,6 @@ void printer_protocol_status_poll_pause_release(printer_status_poll_pause_token_
*token = 0;
}
bool printer_protocol_acquire_control_lane(uint32_t timeout_ms, char *err, size_t err_len) {
if (!s_protocol_initialized || s_mutex == NULL) {
if (err != NULL && err_len > 0) {
snprintf(err, err_len, "printer protocol not initialized");
}
return false;
}
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(timeout_ms)) != pdTRUE) {
if (err != NULL && err_len > 0) {
snprintf(err, err_len, "lock timeout");
}
return false;
}
if (s_protocol_stopping) {
xSemaphoreGive(s_mutex);
if (err != NULL && err_len > 0) {
snprintf(err, err_len, "printer protocol stopping");
}
return false;
}
if (s_busy_refcnt != 0) {
xSemaphoreGive(s_mutex);
if (err != NULL && err_len > 0) {
snprintf(err, err_len, "printer busy");
}
return false;
}
s_busy_refcnt = 1;
xSemaphoreGive(s_mutex);
return true;
}
void printer_protocol_release_control_lane(void) {
if (s_mutex == NULL) {
return;
}
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(1000)) != pdTRUE) {
return;
}
if (s_busy_refcnt > 0) {
--s_busy_refcnt;
}
xSemaphoreGive(s_mutex);
}
static void status_poll_task(void *arg) {
(void)arg;
@@ -278,8 +223,7 @@ static void status_poll_task(void *arg) {
break;
}
if (can_poll && platform_direct_printer_is_connected() &&
xSemaphoreTake(s_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
if (can_poll && xSemaphoreTake(s_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
refresh_direct_status_locked();
xSemaphoreGive(s_mutex);
}
@@ -350,21 +294,11 @@ esp_err_t printer_protocol_init(void) {
return err;
}
uint32_t connect_timeout_ms = domain_policy_printer_connect_timeout_ms();
err = platform_direct_printer_connect(connect_timeout_ms);
if (err != ESP_OK) {
s_protocol_initialized = false;
platform_direct_printer_disconnect();
platform_direct_printer_deinit();
destroy_runtime_objects();
return err;
}
if (xSemaphoreTake(s_mutex, pdMS_TO_TICKS(200)) == pdTRUE) {
refresh_direct_status_locked();
xSemaphoreGive(s_mutex);
}
ESP_LOGI(TAG, "printer auto connected at startup");
ESP_LOGI(TAG, "printer driver ready at startup");
BaseType_t ok = printer_create_task_prefer_psram(printer_protocol_worker_task,
"print_worker",
@@ -423,8 +357,6 @@ esp_err_t printer_protocol_stop(uint32_t timeout_ms) {
(void)xQueueSend(s_job_queue, &sentinel, 0);
}
platform_direct_printer_disconnect();
bool worker_done = (s_worker_task == NULL);
bool poll_done = (s_status_poll_task_handle == NULL);
@@ -468,7 +400,7 @@ esp_err_t printer_protocol_stop(uint32_t timeout_ms) {
runtime_diag_set_gauge(RUNTIME_DIAG_GAUGE_STATUS_POLL_PAUSE_DEPTH, 0);
runtime_diag_set_gauge(RUNTIME_DIAG_GAUGE_PRINTER_QUEUE_DEPTH, 0);
platform_direct_printer_deinit();
platform_printer_deinit();
ESP_LOGI(TAG, "printer protocol stopped");
return (worker_done && poll_done) ? ESP_OK : ESP_ERR_TIMEOUT;
@@ -482,10 +414,8 @@ void printer_protocol_get_runtime_status(printer_runtime_status_t *out_status) {
memset(out_status, 0, sizeof(*out_status));
out_status->paper_gpio_level = -1;
out_status->connected = platform_direct_printer_is_connected();
platform_printer_sensors_t sensors = {0};
if (platform_direct_printer_get_sensors(&sensors) == ESP_OK &&
if (platform_printer_get_sensors(&sensors) == ESP_OK &&
s_mutex != NULL &&
xSemaphoreTake(s_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
s_status.has_paper = sensors.has_paper;

View File

@@ -10,34 +10,26 @@ static void write_err(char *err, size_t err_len, const char *msg) {
}
esp_err_t printer_protocol_gap_move(uint32_t timeout_ms, char *err, size_t err_len) {
if (!runtime_policy_direct_printer_enabled()) {
write_err(err, err_len, "printer driver disabled");
return ESP_ERR_NOT_SUPPORTED;
}
return platform_direct_printer_gap_move(timeout_ms, err, err_len);
return platform_printer_gap_move(timeout_ms, err, err_len);
}
esp_err_t printer_protocol_get_direct_debug_config(printer_direct_debug_config_t *out_config,
char *err,
size_t err_len) {
esp_err_t printer_protocol_get_debug_config(printer_debug_config_t *out_config,
char *err,
size_t err_len) {
if (out_config == NULL) {
write_err(err, err_len, "invalid args");
return ESP_ERR_INVALID_ARG;
}
if (!runtime_policy_direct_printer_enabled()) {
write_err(err, err_len, "printer driver disabled");
return ESP_ERR_NOT_SUPPORTED;
}
esp_err_t rc = platform_direct_printer_get_debug_config(&out_config->shift_clock_high_us,
&out_config->shift_clock_low_us,
&out_config->latch_pulse_us,
&out_config->strobe_active_high,
&out_config->boost_active_high,
&out_config->override_strobe_on_us,
&out_config->override_strobe_interval_us,
&out_config->override_motor_step_us,
&out_config->override_steps_per_line);
esp_err_t rc = platform_printer_get_debug_config(&out_config->shift_clock_high_us,
&out_config->shift_clock_low_us,
&out_config->latch_pulse_us,
&out_config->strobe_active_high,
&out_config->boost_active_high,
&out_config->override_strobe_on_us,
&out_config->override_strobe_interval_us,
&out_config->override_motor_step_us,
&out_config->override_steps_per_line);
if (rc != ESP_OK) {
write_err(err, err_len, "get printer debug config failed");
return rc;
@@ -45,32 +37,28 @@ esp_err_t printer_protocol_get_direct_debug_config(printer_direct_debug_config_t
return ESP_OK;
}
esp_err_t printer_protocol_set_direct_debug_config(const printer_direct_debug_config_t *config,
bool reset_defaults,
char *err,
size_t err_len) {
esp_err_t printer_protocol_set_debug_config(const printer_debug_config_t *config,
bool reset_defaults,
char *err,
size_t err_len) {
if (config == NULL && !reset_defaults) {
write_err(err, err_len, "invalid args");
return ESP_ERR_INVALID_ARG;
}
if (!runtime_policy_direct_printer_enabled()) {
write_err(err, err_len, "printer driver disabled");
return ESP_ERR_NOT_SUPPORTED;
}
return platform_direct_printer_set_debug_config(config != NULL,
config != NULL ? config->shift_clock_high_us : 0,
config != NULL ? config->shift_clock_low_us : 0,
config != NULL ? config->latch_pulse_us : 0,
config != NULL ? config->strobe_active_high : false,
config != NULL ? config->boost_active_high : false,
config != NULL ? config->override_strobe_on_us : 0,
config != NULL ? config->override_strobe_interval_us : 0,
config != NULL ? config->override_motor_step_us : 0,
config != NULL ? config->override_steps_per_line : 0,
reset_defaults,
err,
err_len);
return platform_printer_set_debug_config(config != NULL,
config != NULL ? config->shift_clock_high_us : 0,
config != NULL ? config->shift_clock_low_us : 0,
config != NULL ? config->latch_pulse_us : 0,
config != NULL ? config->strobe_active_high : false,
config != NULL ? config->boost_active_high : false,
config != NULL ? config->override_strobe_on_us : 0,
config != NULL ? config->override_strobe_interval_us : 0,
config != NULL ? config->override_motor_step_us : 0,
config != NULL ? config->override_steps_per_line : 0,
reset_defaults,
err,
err_len);
}
const char *printer_protocol_job_state_str(print_job_state_t state) {

View File

@@ -135,11 +135,11 @@ esp_err_t printer_protocol_submit_raster_job_ex(const uint8_t *raster,
s_jobs[idx].width = width;
s_jobs[idx].height = height;
s_jobs[idx].data_len = raster_len;
s_jobs[idx].direct_ignore_precheck = (options != NULL && options->direct_ignore_precheck);
s_jobs[idx].ignore_precheck = (options != NULL && options->ignore_precheck);
s_jobs[idx].created_ms = esp_timer_get_time() / 1000;
s_jobs[idx].data = copy;
strlcpy(s_jobs[idx].density, density != NULL ? density : "中等", sizeof(s_jobs[idx].density));
bool ignore_precheck = s_jobs[idx].direct_ignore_precheck;
bool ignore_precheck = s_jobs[idx].ignore_precheck;
xSemaphoreGive(s_mutex);

View File

@@ -31,13 +31,13 @@ static uint16_t density_to_hot_time(const char *density) {
return 2000;
}
static bool precheck_printer_ready(bool direct_ignore_precheck, char *err, size_t err_len) {
if (direct_ignore_precheck) {
static bool precheck_printer_ready(bool ignore_precheck, char *err, size_t err_len) {
if (ignore_precheck) {
return true;
}
platform_printer_sensors_t sensors = {0};
if (platform_direct_printer_get_sensors(&sensors) != ESP_OK) {
if (platform_printer_get_sensors(&sensors) != ESP_OK) {
snprintf(err, err_len, "sensor read failed");
return false;
}
@@ -46,8 +46,8 @@ static bool precheck_printer_ready(bool direct_ignore_precheck, char *err, size_
return false;
}
float temp_min = runtime_policy_direct_printer_temp_min_c();
float temp_max = runtime_policy_direct_printer_temp_max_c();
float temp_min = runtime_policy_printer_temp_min_c();
float temp_max = runtime_policy_printer_temp_max_c();
if (temp_max <= temp_min) {
temp_max = temp_min + 1.0f;
}
@@ -56,7 +56,7 @@ static bool precheck_printer_ready(bool direct_ignore_precheck, char *err, size_
return false;
}
if (sensors.battery_percent < runtime_policy_direct_printer_battery_min_percent()) {
if (sensors.battery_percent < runtime_policy_printer_battery_min_percent()) {
snprintf(err, err_len, "battery too low");
return false;
}
@@ -64,28 +64,23 @@ static bool precheck_printer_ready(bool direct_ignore_precheck, char *err, size_
}
static bool run_print_job_direct(job_slot_t *job) {
if (!platform_direct_printer_is_connected()) {
snprintf(job->error, sizeof(job->error), "printer not connected");
return false;
}
char check_err[96];
if (!precheck_printer_ready(job->direct_ignore_precheck, check_err, sizeof(check_err))) {
if (!precheck_printer_ready(job->ignore_precheck, check_err, sizeof(check_err))) {
snprintf(job->error, sizeof(job->error), "%s", check_err);
return false;
}
platform_direct_print_request_t req = {
platform_printer_print_request_t req = {
.width = job->width,
.height = job->height,
.raster = job->data,
.raster_len = job->data_len,
.strobe_on_time_us = density_to_hot_time(job->density),
.strobe_interval_us = runtime_policy_direct_printer_strobe_interval_us(),
.motor_step_delay_us = runtime_policy_direct_printer_motor_step_us(),
.motor_steps_per_line = runtime_policy_direct_printer_steps_per_line(),
.timeout_ms = runtime_policy_direct_printer_operation_timeout_ms(),
.ignore_precheck = job->direct_ignore_precheck,
.strobe_interval_us = runtime_policy_printer_strobe_interval_us(),
.motor_step_delay_us = runtime_policy_printer_motor_step_us(),
.motor_steps_per_line = runtime_policy_printer_steps_per_line(),
.timeout_ms = runtime_policy_printer_operation_timeout_ms(),
.ignore_precheck = job->ignore_precheck,
.cancel_flag = &job->cancel_requested,
};
@@ -95,7 +90,7 @@ static bool run_print_job_direct(job_slot_t *job) {
}
char direct_err[96] = {0};
esp_err_t rc = platform_direct_printer_print(&req, direct_err, sizeof(direct_err));
esp_err_t rc = platform_printer_print(&req, direct_err, sizeof(direct_err));
if (rc != ESP_OK) {
if (direct_err[0] != 0) {
snprintf(job->error, sizeof(job->error), "%s", direct_err);
@@ -124,7 +119,7 @@ static bool run_print_job(job_slot_t *job) {
(unsigned)job->width,
(unsigned)job->height,
job->density,
job->direct_ignore_precheck);
job->ignore_precheck);
if (printer_protocol_is_stopping()) {
snprintf(job->error, sizeof(job->error), "protocol stopping");

View File

@@ -316,7 +316,7 @@ static esp_err_t voice_submit_marker_generated_image(const voice_image_generatio
uint32_t job_id = 0;
char submit_err[128] = {0};
printer_print_options_t print_opt = {
.direct_ignore_precheck = true,
.ignore_precheck = true,
};
esp_err_t submit_rc = printer_protocol_submit_raster_job_ex(raster,
raster_len,