qp_ld7032.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // Copyright 2023 Nick Brassel (@tzarc)
  2. // Copyright 2023 Dasky (@daskygit)
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. #include "qp_internal.h"
  5. #include "qp_comms.h"
  6. #include "qp_oled_panel.h"
  7. #include "qp_ld7032.h"
  8. #include "qp_ld7032_opcodes.h"
  9. #include "qp_surface.h"
  10. #include "qp_surface_internal.h"
  11. typedef bool (*ld7032_driver_comms_send_command_and_data_func)(painter_device_t device, uint8_t cmd, uint8_t data);
  12. typedef uint32_t (*ld7032_driver_comms_send_command_and_databuf_func)(painter_device_t device, uint8_t cmd, const void *data, uint32_t byte_count);
  13. typedef struct ld7032_comms_with_command_vtable_t {
  14. painter_comms_vtable_t base; // must be first, so this object can be cast from the painter_comms_vtable_t* type
  15. painter_driver_comms_send_command_func send_command;
  16. painter_driver_comms_bulk_command_sequence bulk_command_sequence;
  17. ld7032_driver_comms_send_command_and_data_func send_command_data;
  18. ld7032_driver_comms_send_command_and_databuf_func send_command_databuf;
  19. } ld7032_comms_with_command_vtable_t;
  20. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21. // LD7032 Internal API
  22. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  23. #ifdef QUANTUM_PAINTER_LD7032_I2C_ENABLE
  24. bool ld7032_comms_i2c_send_command_and_data(painter_device_t device, uint8_t cmd, uint8_t data) {
  25. uint8_t buf[2] = {cmd, data};
  26. return qp_comms_i2c_send_data(device, buf, 2);
  27. }
  28. bool ld7032_comms_i2c_bulk_command_sequence(painter_device_t device, const uint8_t *sequence, size_t sequence_len) {
  29. uint8_t buf[32];
  30. for (size_t i = 0; i < sequence_len;) {
  31. uint8_t command = sequence[i];
  32. uint8_t delay = sequence[i + 1];
  33. uint8_t num_bytes = sequence[i + 2];
  34. buf[0] = command;
  35. memcpy(&buf[1], &sequence[i + 3], num_bytes);
  36. if (!qp_comms_i2c_send_data(device, buf, num_bytes + 1)) {
  37. return false;
  38. }
  39. if (delay > 0) {
  40. wait_ms(delay);
  41. }
  42. i += (3 + num_bytes);
  43. }
  44. return true;
  45. }
  46. uint32_t ld7032_comms_i2c_send_command_and_databuf(painter_device_t device, uint8_t cmd, const void *data, uint32_t byte_count) {
  47. uint8_t buf[byte_count + 1];
  48. memset(buf, 0, sizeof(buf));
  49. buf[0] = cmd;
  50. memcpy(&buf[1], data, byte_count);
  51. return qp_comms_send(device, buf, byte_count + 1);
  52. }
  53. #endif // QUANTUM_PAINTER_LD7032_I2C_ENABLE
  54. // Power control
  55. bool qp_ld7032_power(painter_device_t device, bool power_on) {
  56. painter_driver_t *driver = (painter_driver_t *)device;
  57. ld7032_comms_with_command_vtable_t *comms_vtable = (ld7032_comms_with_command_vtable_t *)driver->comms_vtable;
  58. comms_vtable->send_command_data(device, LD7032_DISP_ON_OFF, power_on ? 0x01 : 0x00);
  59. return true;
  60. }
  61. // Screen clear
  62. bool qp_ld7032_clear(painter_device_t device) {
  63. qp_rect(device, 0, 0, 127, 127, 0, 0, 0, true); // clear memory
  64. painter_driver_t *driver = (painter_driver_t *)device;
  65. driver->driver_vtable->init(device, driver->rotation); // Re-init the display
  66. return true;
  67. }
  68. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  69. // Flush helpers
  70. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  71. void ld7032_flush_0(painter_device_t device, surface_dirty_data_t *dirty, const uint8_t *framebuffer, bool inverted) {
  72. painter_driver_t *driver = (painter_driver_t *)device;
  73. ld7032_comms_with_command_vtable_t *comms_vtable = (ld7032_comms_with_command_vtable_t *)driver->comms_vtable;
  74. int x_start = dirty->l >> 3;
  75. int x_end = dirty->r >> 3;
  76. int y_start = dirty->t;
  77. int y_end = dirty->b;
  78. int x_length = (x_end - x_start) + 1;
  79. uint8_t x_view_offset = driver->offset_x >> 3;
  80. uint8_t y_view_offset = driver->offset_y;
  81. for (int y_pos = y_start; y_pos <= y_end; y_pos++) {
  82. int y_new_pos = y_pos;
  83. if (inverted) {
  84. y_new_pos = y_end - y_pos;
  85. }
  86. uint8_t packet[x_length];
  87. memcpy(packet, &framebuffer[(y_pos * (driver->panel_width >> 3)) + x_start], x_length);
  88. uint8_t x_write_start = MIN(x_start + x_view_offset, (128 >> 3));
  89. uint8_t x_write_end = MIN(x_end + x_view_offset, (128 >> 3));
  90. uint8_t y_write_start = MIN(y_new_pos + y_view_offset, 39);
  91. uint8_t y_write_end = MIN(y_new_pos + y_view_offset, 39);
  92. comms_vtable->send_command_data(device, LD7032_X_BOX_ADR_START, x_write_start);
  93. comms_vtable->send_command_data(device, LD7032_X_BOX_ADR_END, x_write_end);
  94. comms_vtable->send_command_data(device, LD7032_Y_BOX_ADR_START, y_write_start);
  95. comms_vtable->send_command_data(device, LD7032_Y_BOX_ADR_END, y_write_end);
  96. comms_vtable->send_command_databuf(device, LD7032_DATA_RW, packet, x_length);
  97. }
  98. }
  99. void ld7032_flush_90(painter_device_t device, surface_dirty_data_t *dirty, const uint8_t *framebuffer, bool inverted) {
  100. painter_driver_t *driver = (painter_driver_t *)device;
  101. ld7032_comms_with_command_vtable_t *comms_vtable = (ld7032_comms_with_command_vtable_t *)driver->comms_vtable;
  102. int x_start = dirty->t >> 3;
  103. int x_end = dirty->b >> 3;
  104. int y_start = dirty->l;
  105. int y_end = dirty->r;
  106. int x_length = (x_end - x_start) + 1;
  107. uint8_t x_view_offset = driver->offset_x >> 3;
  108. uint8_t y_view_offset = driver->offset_y;
  109. for (int y_pos = y_start; y_pos <= y_end; y_pos++) {
  110. int y_new_pos = y_pos;
  111. if (inverted) {
  112. y_new_pos = y_end - y_pos;
  113. }
  114. uint8_t packet[x_length];
  115. memset(packet, 0, sizeof(packet));
  116. int count = 0;
  117. for (int x_pos = x_start; x_pos <= x_end; x_pos++) {
  118. for (int x = 0; x < 8; ++x) {
  119. uint32_t pixel_num = (((x_pos << 3) + x) * driver->panel_height) + y_pos;
  120. uint32_t byte_offset = pixel_num / 8;
  121. uint8_t bit_offset = pixel_num % 8;
  122. packet[count] |= ((framebuffer[byte_offset] & (1 << bit_offset)) >> bit_offset) << x;
  123. }
  124. count++;
  125. }
  126. uint8_t x_width = (driver->panel_width >> 3) - 1;
  127. uint8_t x_write_start = MAX((int)x_width - x_end - x_view_offset, 0);
  128. uint8_t x_write_end = MAX((int)x_width - x_start - x_view_offset, 0);
  129. uint8_t y_write_start = MIN(y_new_pos + y_view_offset, 39);
  130. uint8_t y_write_end = MIN(y_new_pos + y_view_offset, 39);
  131. comms_vtable->send_command_data(device, LD7032_X_BOX_ADR_START, x_write_start);
  132. comms_vtable->send_command_data(device, LD7032_X_BOX_ADR_END, x_write_end);
  133. comms_vtable->send_command_data(device, LD7032_Y_BOX_ADR_START, y_write_start);
  134. comms_vtable->send_command_data(device, LD7032_Y_BOX_ADR_END, y_write_end);
  135. comms_vtable->send_command_databuf(device, LD7032_DATA_RW, packet, x_length);
  136. }
  137. }
  138. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  139. // Driver storage
  140. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  141. typedef struct ld7032_device_t {
  142. oled_panel_painter_device_t oled;
  143. uint8_t framebuffer[SURFACE_REQUIRED_BUFFER_BYTE_SIZE(128, 40, 1)];
  144. } ld7032_device_t;
  145. static ld7032_device_t ld7032_drivers[LD7032_NUM_DEVICES] = {0};
  146. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  147. // Quantum Painter API implementations
  148. // Initialisation
  149. __attribute__((weak)) bool qp_ld7032_init(painter_device_t device, painter_rotation_t rotation) {
  150. ld7032_device_t *driver = (ld7032_device_t *)device;
  151. // Change the surface geometry based on the panel rotation
  152. if (rotation == QP_ROTATION_90 || rotation == QP_ROTATION_270) {
  153. driver->oled.surface.base.panel_width = driver->oled.base.panel_height;
  154. driver->oled.surface.base.panel_height = driver->oled.base.panel_width;
  155. } else {
  156. driver->oled.surface.base.panel_width = driver->oled.base.panel_width;
  157. driver->oled.surface.base.panel_height = driver->oled.base.panel_height;
  158. }
  159. // Init the internal surface
  160. if (!qp_init(&driver->oled.surface.base, QP_ROTATION_0)) {
  161. qp_dprintf("Failed to init internal surface in qp_ld7032_init\n");
  162. return false;
  163. }
  164. // clang-format off
  165. const uint8_t ld7032_init_sequence[] = {
  166. // Command, Delay, N, Data[N]
  167. LD7032_DISP_STBY_ON_OFF, 0, 1, 0x00,
  168. LD7032_DISP_ON_OFF, 0, 1, 0x00,
  169. LD7032_DFRAME, 0, 1, 0x05,
  170. //LD7032_WRITE_DIRECTION, 0, 1, 0b00001000, // 0 Right, 1 Up, 2 Vertical, 3 Bit Order, 4-7 Unused
  171. LD7032_DISP_DIRECTION, 0, 1, 0x00,
  172. LD7032_PEAK_WIDTH, 0, 1, 0x1F,
  173. LD7032_PEAK_DELAY, 0, 1, 0x05,
  174. LD7032_SCAN_MODE, 0, 1, 0x01,
  175. LD7032_DOT_CURRENT, 0, 1, 0x1f,
  176. LD7032_VDD_SEL, 0, 1, 0x01,
  177. };
  178. // clang-format on
  179. if (!qp_comms_bulk_command_sequence(device, ld7032_init_sequence, sizeof(ld7032_init_sequence))) {
  180. return false;
  181. }
  182. uint8_t display_y_start = 40 - driver->oled.base.panel_height;
  183. uint8_t display_x_start = (128 - driver->oled.base.panel_width) / 2;
  184. // clang-format off
  185. uint8_t ld7032_memory_setup[] = {
  186. // Command, Delay, N, Data[N]
  187. LD7032_DISP_SIZE_X, 0, 2, 0x00, 0x7F,
  188. LD7032_DISP_SIZE_Y, 0, 2, 0x00, 0x27,
  189. LD7032_X_DISP_START, 0, 1, 0x0,
  190. LD7032_Y_DISP_START, 0, 1, 0x0,
  191. };
  192. // clang-format on
  193. ld7032_memory_setup[3] = display_x_start;
  194. ld7032_memory_setup[4] = display_x_start + driver->oled.base.panel_width - 1;
  195. ld7032_memory_setup[8] = display_y_start;
  196. ld7032_memory_setup[9] = display_y_start + driver->oled.base.panel_height - 1;
  197. ld7032_memory_setup[13] = ld7032_memory_setup[4] + 1;
  198. ld7032_memory_setup[17] = driver->oled.base.panel_height;
  199. if (!qp_comms_bulk_command_sequence(device, ld7032_memory_setup, sizeof(ld7032_memory_setup))) {
  200. return false;
  201. }
  202. uint8_t write_direction = 0;
  203. switch (rotation) {
  204. default:
  205. case QP_ROTATION_0:
  206. write_direction = 0b00001000;
  207. break;
  208. case QP_ROTATION_90:
  209. write_direction = 0b00000001;
  210. break;
  211. case QP_ROTATION_180:
  212. write_direction = 0b00000001;
  213. break;
  214. case QP_ROTATION_270:
  215. write_direction = 0b00001000;
  216. break;
  217. }
  218. painter_driver_t *pdriver = (painter_driver_t *)device;
  219. ld7032_comms_with_command_vtable_t *comms_vtable = (ld7032_comms_with_command_vtable_t *)pdriver->comms_vtable;
  220. if (!comms_vtable->send_command_data(device, LD7032_WRITE_DIRECTION, write_direction)) {
  221. return false;
  222. }
  223. qp_ld7032_power(device, true);
  224. return true;
  225. }
  226. // Screen flush
  227. bool qp_ld7032_flush(painter_device_t device) {
  228. ld7032_device_t *driver = (ld7032_device_t *)device;
  229. if (!driver->oled.surface.dirty.is_dirty) {
  230. return true;
  231. }
  232. switch (driver->oled.base.rotation) {
  233. default:
  234. case QP_ROTATION_0:
  235. ld7032_flush_0(device, &driver->oled.surface.dirty, driver->framebuffer, false);
  236. break;
  237. case QP_ROTATION_180:
  238. ld7032_flush_0(device, &driver->oled.surface.dirty, driver->framebuffer, true);
  239. break;
  240. case QP_ROTATION_90:
  241. ld7032_flush_90(device, &driver->oled.surface.dirty, driver->framebuffer, false);
  242. break;
  243. case QP_ROTATION_270:
  244. ld7032_flush_90(device, &driver->oled.surface.dirty, driver->framebuffer, true);
  245. break;
  246. }
  247. // Clear the dirty area
  248. qp_flush(&driver->oled.surface);
  249. return true;
  250. }
  251. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  252. // Driver vtable
  253. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  254. const painter_driver_vtable_t ld7032_driver_vtable = {
  255. .init = qp_ld7032_init,
  256. .power = qp_ld7032_power,
  257. .clear = qp_ld7032_clear,
  258. .flush = qp_ld7032_flush,
  259. .pixdata = qp_oled_panel_passthru_pixdata,
  260. .viewport = qp_oled_panel_passthru_viewport,
  261. .palette_convert = qp_oled_panel_passthru_palette_convert,
  262. .append_pixels = qp_oled_panel_passthru_append_pixels,
  263. .append_pixdata = qp_oled_panel_passthru_append_pixdata,
  264. };
  265. #ifdef QUANTUM_PAINTER_LD7032_SPI_ENABLE
  266. const ld7032_comms_with_command_vtable_t ld7032_spi_comms_vtable = {
  267. .base =
  268. {
  269. .comms_init = qp_comms_spi_dc_reset_init,
  270. .comms_start = qp_comms_spi_start,
  271. .comms_send = qp_comms_spi_dc_reset_send_data,
  272. .comms_stop = qp_comms_spi_stop,
  273. },
  274. .send_command = qp_comms_spi_dc_reset_send_command,
  275. .send_command_data = qp_comms_command_databyte,
  276. .send_command_databuf = qp_comms_command_databuf,
  277. .bulk_command_sequence = qp_comms_spi_dc_reset_bulk_command_sequence,
  278. };
  279. // Factory function for creating a handle to the LD7032 device
  280. painter_device_t qp_ld7032_make_spi_device(uint16_t panel_width, uint16_t panel_height, pin_t chip_select_pin, pin_t dc_pin, pin_t reset_pin, uint16_t spi_divisor, int spi_mode) {
  281. for (uint32_t i = 0; i < LD7032_NUM_DEVICES; ++i) {
  282. ld7032_device_t *driver = &ld7032_drivers[i];
  283. if (!driver->oled.base.driver_vtable) {
  284. painter_device_t surface = qp_make_mono1bpp_surface_advanced(&driver->oled.surface, 1, panel_width, panel_height, driver->framebuffer);
  285. if (!surface) {
  286. return NULL;
  287. }
  288. // Setup the OLED device
  289. driver->oled.base.driver_vtable = (const painter_driver_vtable_t *)&ld7032_driver_vtable;
  290. driver->oled.base.comms_vtable = (const painter_comms_vtable_t *)&ld7032_spi_comms_vtable;
  291. driver->oled.base.native_bits_per_pixel = 1; // 1bpp mono
  292. driver->oled.base.panel_width = panel_width;
  293. driver->oled.base.panel_height = panel_height;
  294. driver->oled.base.rotation = QP_ROTATION_0;
  295. driver->oled.base.offset_x = 0;
  296. driver->oled.base.offset_y = 0;
  297. // SPI and other pin configuration
  298. driver->oled.base.comms_config = &driver->oled.spi_dc_reset_config;
  299. driver->oled.spi_dc_reset_config.spi_config.chip_select_pin = chip_select_pin;
  300. driver->oled.spi_dc_reset_config.spi_config.divisor = spi_divisor;
  301. driver->oled.spi_dc_reset_config.spi_config.lsb_first = false;
  302. driver->oled.spi_dc_reset_config.spi_config.mode = spi_mode;
  303. driver->oled.spi_dc_reset_config.dc_pin = dc_pin;
  304. driver->oled.spi_dc_reset_config.reset_pin = reset_pin;
  305. driver->oled.spi_dc_reset_config.command_params_uses_command_pin = true;
  306. if (!qp_internal_register_device((painter_device_t)driver)) {
  307. memset(driver, 0, sizeof(ld7032_device_t));
  308. return NULL;
  309. }
  310. return (painter_device_t)driver;
  311. }
  312. }
  313. return NULL;
  314. }
  315. #endif // QUANTUM_PAINTER_LD7032_SPI_ENABLE
  316. #ifdef QUANTUM_PAINTER_LD7032_I2C_ENABLE
  317. const ld7032_comms_with_command_vtable_t ld7032_i2c_comms_vtable = {
  318. .base =
  319. {
  320. .comms_init = qp_comms_i2c_init,
  321. .comms_start = qp_comms_i2c_start,
  322. .comms_send = qp_comms_i2c_send_data,
  323. .comms_stop = qp_comms_i2c_stop,
  324. },
  325. .send_command = NULL,
  326. .send_command_data = ld7032_comms_i2c_send_command_and_data,
  327. .send_command_databuf = ld7032_comms_i2c_send_command_and_databuf,
  328. .bulk_command_sequence = ld7032_comms_i2c_bulk_command_sequence,
  329. };
  330. // Factory function for creating a handle to the LD7032 device
  331. painter_device_t qp_ld7032_make_i2c_device(uint16_t panel_width, uint16_t panel_height, uint8_t i2c_address) {
  332. for (uint32_t i = 0; i < LD7032_NUM_DEVICES; ++i) {
  333. ld7032_device_t *driver = &ld7032_drivers[i];
  334. if (!driver->oled.base.driver_vtable) {
  335. painter_device_t surface = qp_make_mono1bpp_surface_advanced(&driver->oled.surface, 1, panel_width, panel_height, driver->framebuffer);
  336. if (!surface) {
  337. return NULL;
  338. }
  339. // Setup the OLED device
  340. driver->oled.base.driver_vtable = (const painter_driver_vtable_t *)&ld7032_driver_vtable;
  341. driver->oled.base.comms_vtable = (const painter_comms_vtable_t *)&ld7032_i2c_comms_vtable;
  342. driver->oled.base.native_bits_per_pixel = 1; // 1bpp mono
  343. driver->oled.base.panel_width = panel_width;
  344. driver->oled.base.panel_height = panel_height;
  345. driver->oled.base.rotation = QP_ROTATION_0;
  346. driver->oled.base.offset_x = 0;
  347. driver->oled.base.offset_y = 0;
  348. // I2C configuration
  349. driver->oled.base.comms_config = &driver->oled.i2c_config;
  350. driver->oled.i2c_config.chip_address = i2c_address;
  351. if (!qp_internal_register_device((painter_device_t)driver)) {
  352. memset(driver, 0, sizeof(ld7032_device_t));
  353. return NULL;
  354. }
  355. return (painter_device_t)driver;
  356. }
  357. }
  358. return NULL;
  359. }
  360. #endif // QUANTUM_PAINTER_LD7032_SPI_ENABLE