1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: MIT
3
4#include "slint-esp.h"
5#include "printerdemo.h"
6#include <ctime>
7#include <memory>
8
9#include <slint-platform.h>
10
11#include <bsp/display.h>
12#include <bsp/esp-bsp.h>
13#include <bsp/touch.h>
14#include <vector>
15
16// This example renders the frame using the line by line rendering.
17// Optionally, define `USE_FRAME_BUFFER` to allocate use a buffer and use the
18// the renderer that renders the whole buffer.
19// #define USE_FRAME_BUFFER
20
21struct InkLevelModel : slint::Model<InkLevel>
22{
23 size_t row_count() const override { return m_data.size(); }
24 std::optional<InkLevel> row_data(size_t i) const override
25 {
26 if (i < row_count())
27 return { m_data[i] };
28 return {};
29 }
30
31 std::vector<InkLevel> m_data = { { slint::Color::from_rgb_uint8(255, 255, 0), 0.9 },
32 { slint::Color::from_rgb_uint8(0, 255, 255), 0.5 },
33 { slint::Color::from_rgb_uint8(255, 0, 255), 0.8 },
34 { slint::Color::from_rgb_uint8(0, 0, 0), 0.1 } };
35};
36
37extern "C" void app_main(void)
38{
39
40 /* Initialize I2C (for touch and audio) */
41 bsp_i2c_init();
42
43 /* Initialize display */
44 esp_lcd_panel_io_handle_t io_handle = NULL;
45 esp_lcd_panel_handle_t panel_handle = NULL;
46 const bsp_display_config_t bsp_disp_cfg = {
47 .max_transfer_sz = (BSP_LCD_H_RES * CONFIG_BSP_LCD_DRAW_BUF_HEIGHT) * sizeof(uint16_t),
48 };
49 bsp_display_new(&bsp_disp_cfg, &panel_handle, &io_handle);
50 esp_lcd_touch_handle_t touch_handle = NULL;
51 const bsp_touch_config_t bsp_touch_cfg = {};
52 bsp_touch_new(&bsp_touch_cfg, &touch_handle);
53
54 /* Set display brightness to 100% */
55 bsp_display_backlight_on();
56
57#ifdef USE_FRAME_BUFFER
58 std::vector<slint::platform::Rgb565Pixel> buffer(BSP_LCD_H_RES * BSP_LCD_V_RES);
59#endif
60
61 slint_esp_init(SlintPlatformConfiguration {
62 .size = slint::PhysicalSize({ BSP_LCD_H_RES, BSP_LCD_V_RES }),
63 .panel_handle = panel_handle,
64 .touch_handle = touch_handle,
65#ifdef USE_FRAME_BUFFER
66 .buffer1 = buffer,
67#endif
68 .byte_swap = true });
69
70 auto printer_demo = MainWindow::create();
71 printer_demo->set_ink_levels(std::make_shared<InkLevelModel>());
72 printer_demo->on_quit([] { std::exit(status: 0); });
73
74 auto printer_queue = std::make_shared<slint::VectorModel<PrinterQueueItem>>();
75 auto default_queue = printer_demo->global<PrinterQueue>().get_printer_queue();
76 for (int i = 0; i < default_queue->row_count(); ++i) {
77 printer_queue->push_back(*default_queue->row_data(i));
78 }
79 printer_demo->global<PrinterQueue>().set_printer_queue(printer_queue);
80
81 printer_demo->global<PrinterQueue>().on_start_job([=](slint::SharedString name) {
82 std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
83 char time_buf[100] = { 0 };
84 std::strftime(time_buf, sizeof(time_buf), "%H:%M:%S %d/%m/%Y", std::localtime(&now));
85 PrinterQueueItem item;
86 item.status = "waiting";
87 item.progress = 0;
88 item.title = std::move(name);
89 item.owner = "joe@example.com";
90 item.pages = 1;
91 item.size = "100kB";
92 item.submission_date = time_buf;
93 printer_queue->push_back(item);
94 });
95
96 printer_demo->global<PrinterQueue>().on_cancel_job(
97 [=](int index) { printer_queue->erase(int(index)); });
98
99 slint::Timer printer_queue_progress_timer(std::chrono::seconds(1), [=]() {
100 if (printer_queue->row_count() > 0) {
101 auto top_item = *printer_queue->row_data(0);
102 top_item.progress += 1;
103 if (top_item.progress > 100) {
104 printer_queue->erase(0);
105 } else {
106 top_item.status = "printing";
107 printer_queue->set_row_data(0, top_item);
108 }
109 }
110 });
111
112 printer_demo->run();
113}
114

source code of slint/demos/printerdemo_mcu/esp-idf/main/main.cpp