logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

qp_ld7032.c (18051B)


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