|
| 1 | +#ifdef CYD_BOARD_JC3248W535C |
| 2 | + |
| 3 | +#include "../screen_driver.h" |
| 4 | +#include <databus/Arduino_ESP32QSPI.h> |
| 5 | +#include <display/Arduino_AXS15231B.h> |
| 6 | +#include <canvas/Arduino_Canvas.h> |
| 7 | +#include "lvgl.h" |
| 8 | +#include "../lv_setup.h" |
| 9 | +#include "../../conf/global_config.h" |
| 10 | +#include <Wire.h> |
| 11 | +#define CPU_FREQ_HIGH 240 |
| 12 | +#define CPU_FREQ_LOW 80 |
| 13 | + |
| 14 | + |
| 15 | +struct TouchPoint { |
| 16 | + uint8_t gesture; |
| 17 | + uint8_t num; |
| 18 | + uint8_t x_h : 4; |
| 19 | + uint8_t _pad1 : 2; |
| 20 | + uint8_t event : 2; |
| 21 | + uint8_t x_l; |
| 22 | + uint8_t y_h : 4; |
| 23 | + uint8_t _pad2 : 4; |
| 24 | + uint8_t y_l; |
| 25 | +} __attribute__((packed)); |
| 26 | + |
| 27 | +static Arduino_ESP32QSPI qspiBus( |
| 28 | + LCD_CS, LCD_CLK, |
| 29 | + LCD_D0, LCD_D1, |
| 30 | + LCD_D2, LCD_D3, |
| 31 | + false); |
| 32 | + |
| 33 | + |
| 34 | +Arduino_GFX *gfx = new Arduino_AXS15231B( |
| 35 | + &qspiBus, -1, 2, true, |
| 36 | + LCD_WIDTH, LCD_HEIGHT, |
| 37 | + 0, 0, 0, 0); |
| 38 | + |
| 39 | +#ifdef CYD_SCREEN_VERTICAL |
| 40 | + static bool horizontal = false; |
| 41 | + static Arduino_Canvas canvas(CYD_SCREEN_WIDTH_PX, CYD_SCREEN_HEIGHT_PX, gfx, 0, 0); |
| 42 | +#else |
| 43 | + static bool horizontal = true; |
| 44 | + static Arduino_Canvas canvas(CYD_SCREEN_HEIGHT_PX, CYD_SCREEN_WIDTH_PX, gfx, 0, 0); |
| 45 | + |
| 46 | +#endif |
| 47 | + |
| 48 | +static lv_disp_draw_buf_t draw_buf; |
| 49 | +static lv_color_t *buf = nullptr; |
| 50 | +static lv_disp_t *main_disp = nullptr; |
| 51 | + |
| 52 | + |
| 53 | +void screen_setBrightness(uint8_t brightness) |
| 54 | +{ |
| 55 | + uint32_t duty = (4095UL * brightness) / 255UL; |
| 56 | + ledcWrite(0, duty); |
| 57 | +} |
| 58 | + |
| 59 | +void screen_lv_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) |
| 60 | +{ |
| 61 | + int x = area->x1; |
| 62 | + int y = area->y1; |
| 63 | + int w = area->x2 - area->x1 + 1; |
| 64 | + int h = area->y2 - area->y1 + 1; |
| 65 | + canvas.draw16bitRGBBitmap(x, y, (uint16_t *)color_p, w, h); |
| 66 | + canvas.flush(); |
| 67 | + |
| 68 | + lv_disp_flush_ready(disp); |
| 69 | +} |
| 70 | + |
| 71 | +// Reads 1 touch point from AXS15231B controller at I2C addr 0x3B |
| 72 | +bool read_touch(uint16_t &x, uint16_t &y) { |
| 73 | + const uint8_t addr = 0x3B; |
| 74 | + |
| 75 | + const uint8_t read_cmd[11] = { |
| 76 | + 0xB5, 0xAB, 0xA5, 0x5A, 0x00, 0x00, |
| 77 | + 0x00, 8, // length of expected reply (MSB, LSB) |
| 78 | + 0x00, 0x00, 0x00 |
| 79 | + }; |
| 80 | + |
| 81 | + Wire.beginTransmission(addr); |
| 82 | + Wire.write(read_cmd, sizeof(read_cmd)); |
| 83 | + if (Wire.endTransmission(false) != 0) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + uint8_t data[8] = {0}; |
| 88 | + if (Wire.requestFrom(addr, (uint8_t)sizeof(data)) != sizeof(data)) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + for (uint8_t i = 0; i < sizeof(data); i++) { |
| 93 | + data[i] = Wire.read(); |
| 94 | + } |
| 95 | + |
| 96 | + TouchPoint *p = (TouchPoint *)data; |
| 97 | + |
| 98 | + if (p->num > 0 && p->num <= 2) { |
| 99 | + x = ((p->x_h & 0x0F) << 8) | p->x_l; |
| 100 | + y = ((p->y_h & 0x0F) << 8) | p->y_l; |
| 101 | + |
| 102 | + // Clamp to screen bounds |
| 103 | + if (x >= CYD_SCREEN_WIDTH_PX) x = CYD_SCREEN_WIDTH_PX - 1; |
| 104 | + if (y >= CYD_SCREEN_HEIGHT_PX) y = CYD_SCREEN_HEIGHT_PX - 1; |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + return false; |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +void screen_lv_touchRead(lv_indev_drv_t * /*indev_driver*/, lv_indev_data_t *data) |
| 113 | +{ |
| 114 | + uint16_t x, y; |
| 115 | + if (read_touch(x, y)) { |
| 116 | + x = min(x, uint16_t(CYD_SCREEN_WIDTH_PX - 1)); |
| 117 | + y = min(y, uint16_t(CYD_SCREEN_HEIGHT_PX - 1)); |
| 118 | + // Adjust coordinates based on screen rotation |
| 119 | + if (global_config.rotate_screen) { |
| 120 | + // Assuming rotation = 2 (180 degrees) |
| 121 | + x = CYD_SCREEN_WIDTH_PX - x; |
| 122 | + y = CYD_SCREEN_HEIGHT_PX - y; |
| 123 | + } |
| 124 | + |
| 125 | + data->state = LV_INDEV_STATE_PR; |
| 126 | + data->point.x = x; |
| 127 | + data->point.y = y; |
| 128 | + } else { |
| 129 | + data->state = LV_INDEV_STATE_REL; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +void set_invert_display() |
| 134 | +{ |
| 135 | + gfx->invertDisplay(global_config.printer_config[global_config.printer_index].invert_colors); |
| 136 | +} |
| 137 | + |
| 138 | +void screen_setup() |
| 139 | +{ |
| 140 | + pinMode(LCD_BL_PIN, OUTPUT); |
| 141 | + ledcSetup(0, 5000, 12); |
| 142 | + ledcAttachPin(LCD_BL_PIN, 0); |
| 143 | + screen_setBrightness(255); |
| 144 | + |
| 145 | + // gfx->begin(); |
| 146 | + canvas.begin(); |
| 147 | + gfx->invertDisplay(true); // OK after begin() |
| 148 | + gfx->setRotation(global_config.rotate_screen ? 2 : 0); |
| 149 | + canvas.fillScreen(0x0000); |
| 150 | + canvas.flush(); |
| 151 | + |
| 152 | + Wire.begin(TOUCH_SDA, TOUCH_SCL); |
| 153 | + |
| 154 | + lv_init(); |
| 155 | + |
| 156 | + // Allocate full canvas buffer for LVGL |
| 157 | + buf = (lv_color_t *)heap_caps_malloc( |
| 158 | + CYD_SCREEN_WIDTH_PX * CYD_SCREEN_HEIGHT_PX * sizeof(lv_color_t), |
| 159 | + MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); |
| 160 | + |
| 161 | + assert(buf); |
| 162 | + |
| 163 | + lv_disp_draw_buf_init(&draw_buf, buf, NULL, CYD_SCREEN_WIDTH_PX * CYD_SCREEN_HEIGHT_PX); |
| 164 | + |
| 165 | + static lv_disp_drv_t disp_drv; |
| 166 | + lv_disp_drv_init(&disp_drv); |
| 167 | + disp_drv.flush_cb = screen_lv_flush; |
| 168 | + disp_drv.draw_buf = &draw_buf; |
| 169 | + |
| 170 | + disp_drv.hor_res = CYD_SCREEN_WIDTH_PX; |
| 171 | + disp_drv.ver_res = CYD_SCREEN_HEIGHT_PX; |
| 172 | + if(horizontal){ |
| 173 | + main_disp = lv_disp_drv_register(&disp_drv); |
| 174 | + lv_disp_set_rotation(main_disp, LV_DISP_ROT_90); |
| 175 | + } else { |
| 176 | + lv_disp_drv_register(&disp_drv); |
| 177 | + } |
| 178 | + static lv_indev_drv_t indev_drv; |
| 179 | + lv_indev_drv_init(&indev_drv); |
| 180 | + indev_drv.type = LV_INDEV_TYPE_POINTER; |
| 181 | + indev_drv.read_cb = screen_lv_touchRead; |
| 182 | + lv_indev_drv_register(&indev_drv); |
| 183 | +} |
| 184 | + |
| 185 | +#endif // CYD_BOARD_JC3248W535C |
0 commit comments