qp_sh1106.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2023 Nick Brassel (@tzarc)
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "qp_internal.h"
  4. #include "qp_comms.h"
  5. #include "qp_oled_panel.h"
  6. #include "qp_sh1106.h"
  7. #include "qp_sh1106_opcodes.h"
  8. #include "qp_surface.h"
  9. #include "qp_surface_internal.h"
  10. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  11. // Driver storage
  12. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  13. typedef struct sh1106_device_t {
  14. oled_panel_painter_device_t oled;
  15. uint8_t framebuffer[SURFACE_REQUIRED_BUFFER_BYTE_SIZE(128, 64, 1)];
  16. } sh1106_device_t;
  17. static sh1106_device_t sh1106_drivers[SH1106_NUM_DEVICES] = {0};
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  19. // Quantum Painter API implementations
  20. // Initialisation
  21. __attribute__((weak)) bool qp_sh1106_init(painter_device_t device, painter_rotation_t rotation) {
  22. sh1106_device_t *driver = (sh1106_device_t *)device;
  23. // Change the surface geometry based on the panel rotation
  24. if (rotation == QP_ROTATION_90 || rotation == QP_ROTATION_270) {
  25. driver->oled.surface.base.panel_width = driver->oled.base.panel_height;
  26. driver->oled.surface.base.panel_height = driver->oled.base.panel_width;
  27. } else {
  28. driver->oled.surface.base.panel_width = driver->oled.base.panel_width;
  29. driver->oled.surface.base.panel_height = driver->oled.base.panel_height;
  30. }
  31. // Init the internal surface
  32. if (!qp_init(&driver->oled.surface.base, QP_ROTATION_0)) {
  33. qp_dprintf("Failed to init internal surface in qp_sh1106_init\n");
  34. return false;
  35. }
  36. // clang-format off
  37. uint8_t sh1106_init_sequence[] = {
  38. // Command, Delay, N, Data[N]
  39. SH1106_SET_MUX_RATIO, 0, 1, 0x3F,
  40. SH1106_DISPLAY_OFFSET, 0, 1, 0x00,
  41. SH1106_DISPLAY_START_LINE, 0, 0,
  42. SH1106_SET_SEGMENT_REMAP_INV, 0, 0,
  43. SH1106_COM_SCAN_DIR_DEC, 0, 0,
  44. SH1106_COM_PADS_HW_CFG, 0, 1, 0x12,
  45. SH1106_SET_CONTRAST, 0, 1, 0x7F,
  46. SH1106_ALL_ON_RESUME, 0, 0,
  47. SH1106_NON_INVERTING_DISPLAY, 0, 0,
  48. SH1106_SET_OSC_DIVFREQ, 0, 1, 0x80,
  49. SH1106_SET_CHARGE_PUMP, 0, 1, 0x14,
  50. SH1106_DISPLAY_ON, 0, 0,
  51. };
  52. // clang-format on
  53. // If the display height is anything other than the default 64 pixels, change SH1106_SET_MUX_RATIO data byte to the correct value
  54. if (driver->oled.base.panel_height != 64) {
  55. sh1106_init_sequence[3] = driver->oled.base.panel_height - 1;
  56. }
  57. // For 128x32 or 96x16 displays, change SH1106_COM_PADS_HW_CFG data byte from alternative (0x12) to sequential (0x02) configuration
  58. if (driver->oled.base.panel_height <= 32) {
  59. sh1106_init_sequence[20] = 0x02;
  60. }
  61. return qp_comms_bulk_command_sequence(device, sh1106_init_sequence, sizeof(sh1106_init_sequence));
  62. }
  63. // Screen flush
  64. bool qp_sh1106_flush(painter_device_t device) {
  65. sh1106_device_t *driver = (sh1106_device_t *)device;
  66. if (!driver->oled.surface.dirty.is_dirty) {
  67. return true;
  68. }
  69. switch (driver->oled.base.rotation) {
  70. default:
  71. case QP_ROTATION_0:
  72. qp_oled_panel_page_column_flush_rot0(device, &driver->oled.surface.dirty, driver->framebuffer);
  73. break;
  74. case QP_ROTATION_90:
  75. qp_oled_panel_page_column_flush_rot90(device, &driver->oled.surface.dirty, driver->framebuffer);
  76. break;
  77. case QP_ROTATION_180:
  78. qp_oled_panel_page_column_flush_rot180(device, &driver->oled.surface.dirty, driver->framebuffer);
  79. break;
  80. case QP_ROTATION_270:
  81. qp_oled_panel_page_column_flush_rot270(device, &driver->oled.surface.dirty, driver->framebuffer);
  82. break;
  83. }
  84. // Clear the dirty area
  85. qp_flush(&driver->oled.surface);
  86. return true;
  87. }
  88. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  89. // Driver vtable
  90. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  91. const oled_panel_painter_driver_vtable_t sh1106_driver_vtable = {
  92. .base =
  93. {
  94. .init = qp_sh1106_init,
  95. .power = qp_oled_panel_power,
  96. .clear = qp_oled_panel_clear,
  97. .flush = qp_sh1106_flush,
  98. .pixdata = qp_oled_panel_passthru_pixdata,
  99. .viewport = qp_oled_panel_passthru_viewport,
  100. .palette_convert = qp_oled_panel_passthru_palette_convert,
  101. .append_pixels = qp_oled_panel_passthru_append_pixels,
  102. .append_pixdata = qp_oled_panel_passthru_append_pixdata,
  103. },
  104. .opcodes =
  105. {
  106. .display_on = SH1106_DISPLAY_ON,
  107. .display_off = SH1106_DISPLAY_OFF,
  108. .set_page = SH1106_PAGE_ADDR,
  109. .set_column_lsb = SH1106_SETCOLUMN_LSB,
  110. .set_column_msb = SH1106_SETCOLUMN_MSB,
  111. },
  112. };
  113. #ifdef QUANTUM_PAINTER_SH1106_SPI_ENABLE
  114. // Factory function for creating a handle to the SH1106 device
  115. painter_device_t qp_sh1106_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) {
  116. for (uint32_t i = 0; i < SH1106_NUM_DEVICES; ++i) {
  117. sh1106_device_t *driver = &sh1106_drivers[i];
  118. if (!driver->oled.base.driver_vtable) {
  119. painter_device_t surface = qp_make_mono1bpp_surface_advanced(&driver->oled.surface, 1, panel_width, panel_height, driver->framebuffer);
  120. if (!surface) {
  121. return NULL;
  122. }
  123. // Setup the OLED device
  124. driver->oled.base.driver_vtable = (const painter_driver_vtable_t *)&sh1106_driver_vtable;
  125. driver->oled.base.comms_vtable = (const painter_comms_vtable_t *)&spi_comms_with_dc_vtable;
  126. driver->oled.base.native_bits_per_pixel = 1; // 1bpp mono
  127. driver->oled.base.panel_width = panel_width;
  128. driver->oled.base.panel_height = panel_height;
  129. driver->oled.base.rotation = QP_ROTATION_0;
  130. driver->oled.base.offset_x = 0;
  131. driver->oled.base.offset_y = 0;
  132. // SPI and other pin configuration
  133. driver->oled.base.comms_config = &driver->oled.spi_dc_reset_config;
  134. driver->oled.spi_dc_reset_config.spi_config.chip_select_pin = chip_select_pin;
  135. driver->oled.spi_dc_reset_config.spi_config.divisor = spi_divisor;
  136. driver->oled.spi_dc_reset_config.spi_config.lsb_first = false;
  137. driver->oled.spi_dc_reset_config.spi_config.mode = spi_mode;
  138. driver->oled.spi_dc_reset_config.dc_pin = dc_pin;
  139. driver->oled.spi_dc_reset_config.reset_pin = reset_pin;
  140. driver->oled.spi_dc_reset_config.command_params_uses_command_pin = true;
  141. if (!qp_internal_register_device((painter_device_t)driver)) {
  142. memset(driver, 0, sizeof(sh1106_device_t));
  143. return NULL;
  144. }
  145. return (painter_device_t)driver;
  146. }
  147. }
  148. return NULL;
  149. }
  150. #endif // QUANTUM_PAINTER_SH1106_SPI_ENABLE
  151. #ifdef QUANTUM_PAINTER_SH1106_I2C_ENABLE
  152. // Factory function for creating a handle to the SH1106 device
  153. painter_device_t qp_sh1106_make_i2c_device(uint16_t panel_width, uint16_t panel_height, uint8_t i2c_address) {
  154. for (uint32_t i = 0; i < SH1106_NUM_DEVICES; ++i) {
  155. sh1106_device_t *driver = &sh1106_drivers[i];
  156. if (!driver->oled.base.driver_vtable) {
  157. // Instantiate the surface, intentional swap of width/high due to transpose
  158. painter_device_t surface = qp_make_mono1bpp_surface_advanced(&driver->oled.surface, 1, panel_width, panel_height, driver->framebuffer);
  159. if (!surface) {
  160. return NULL;
  161. }
  162. // Setup the OLED device
  163. driver->oled.base.driver_vtable = (const painter_driver_vtable_t *)&sh1106_driver_vtable;
  164. driver->oled.base.comms_vtable = (const painter_comms_vtable_t *)&i2c_comms_cmddata_vtable;
  165. driver->oled.base.native_bits_per_pixel = 1; // 1bpp mono
  166. driver->oled.base.panel_width = panel_width;
  167. driver->oled.base.panel_height = panel_height;
  168. driver->oled.base.rotation = QP_ROTATION_0;
  169. driver->oled.base.offset_x = 0;
  170. driver->oled.base.offset_y = 0;
  171. // I2C configuration
  172. driver->oled.base.comms_config = &driver->oled.i2c_config;
  173. driver->oled.i2c_config.chip_address = i2c_address;
  174. if (!qp_internal_register_device((painter_device_t)driver)) {
  175. memset(driver, 0, sizeof(sh1106_device_t));
  176. return NULL;
  177. }
  178. return (painter_device_t)driver;
  179. }
  180. }
  181. return NULL;
  182. }
  183. #endif // QUANTUM_PAINTER_SH1106_I2C_ENABLE