logo

qmk_firmware

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

qp_sh1106.c (9504B)


  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. qp_comms_bulk_command_sequence(device, sh1106_init_sequence, sizeof(sh1106_init_sequence));
  62. return true;
  63. }
  64. // Screen flush
  65. bool qp_sh1106_flush(painter_device_t device) {
  66. sh1106_device_t *driver = (sh1106_device_t *)device;
  67. if (!driver->oled.surface.dirty.is_dirty) {
  68. return true;
  69. }
  70. switch (driver->oled.base.rotation) {
  71. default:
  72. case QP_ROTATION_0:
  73. qp_oled_panel_page_column_flush_rot0(device, &driver->oled.surface.dirty, driver->framebuffer);
  74. break;
  75. case QP_ROTATION_90:
  76. qp_oled_panel_page_column_flush_rot90(device, &driver->oled.surface.dirty, driver->framebuffer);
  77. break;
  78. case QP_ROTATION_180:
  79. qp_oled_panel_page_column_flush_rot180(device, &driver->oled.surface.dirty, driver->framebuffer);
  80. break;
  81. case QP_ROTATION_270:
  82. qp_oled_panel_page_column_flush_rot270(device, &driver->oled.surface.dirty, driver->framebuffer);
  83. break;
  84. }
  85. // Clear the dirty area
  86. qp_flush(&driver->oled.surface);
  87. return true;
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  90. // Driver vtable
  91. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  92. const oled_panel_painter_driver_vtable_t sh1106_driver_vtable = {
  93. .base =
  94. {
  95. .init = qp_sh1106_init,
  96. .power = qp_oled_panel_power,
  97. .clear = qp_oled_panel_clear,
  98. .flush = qp_sh1106_flush,
  99. .pixdata = qp_oled_panel_passthru_pixdata,
  100. .viewport = qp_oled_panel_passthru_viewport,
  101. .palette_convert = qp_oled_panel_passthru_palette_convert,
  102. .append_pixels = qp_oled_panel_passthru_append_pixels,
  103. .append_pixdata = qp_oled_panel_passthru_append_pixdata,
  104. },
  105. .opcodes =
  106. {
  107. .display_on = SH1106_DISPLAY_ON,
  108. .display_off = SH1106_DISPLAY_OFF,
  109. .set_page = SH1106_PAGE_ADDR,
  110. .set_column_lsb = SH1106_SETCOLUMN_LSB,
  111. .set_column_msb = SH1106_SETCOLUMN_MSB,
  112. },
  113. };
  114. #ifdef QUANTUM_PAINTER_SH1106_SPI_ENABLE
  115. // Factory function for creating a handle to the SH1106 device
  116. 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) {
  117. for (uint32_t i = 0; i < SH1106_NUM_DEVICES; ++i) {
  118. sh1106_device_t *driver = &sh1106_drivers[i];
  119. if (!driver->oled.base.driver_vtable) {
  120. painter_device_t surface = qp_make_mono1bpp_surface_advanced(&driver->oled.surface, 1, panel_width, panel_height, driver->framebuffer);
  121. if (!surface) {
  122. return NULL;
  123. }
  124. // Setup the OLED device
  125. driver->oled.base.driver_vtable = (const painter_driver_vtable_t *)&sh1106_driver_vtable;
  126. driver->oled.base.comms_vtable = (const painter_comms_vtable_t *)&spi_comms_with_dc_vtable;
  127. driver->oled.base.native_bits_per_pixel = 1; // 1bpp mono
  128. driver->oled.base.panel_width = panel_width;
  129. driver->oled.base.panel_height = panel_height;
  130. driver->oled.base.rotation = QP_ROTATION_0;
  131. driver->oled.base.offset_x = 0;
  132. driver->oled.base.offset_y = 0;
  133. // SPI and other pin configuration
  134. driver->oled.base.comms_config = &driver->oled.spi_dc_reset_config;
  135. driver->oled.spi_dc_reset_config.spi_config.chip_select_pin = chip_select_pin;
  136. driver->oled.spi_dc_reset_config.spi_config.divisor = spi_divisor;
  137. driver->oled.spi_dc_reset_config.spi_config.lsb_first = false;
  138. driver->oled.spi_dc_reset_config.spi_config.mode = spi_mode;
  139. driver->oled.spi_dc_reset_config.dc_pin = dc_pin;
  140. driver->oled.spi_dc_reset_config.reset_pin = reset_pin;
  141. driver->oled.spi_dc_reset_config.command_params_uses_command_pin = true;
  142. if (!qp_internal_register_device((painter_device_t)driver)) {
  143. memset(driver, 0, sizeof(sh1106_device_t));
  144. return NULL;
  145. }
  146. return (painter_device_t)driver;
  147. }
  148. }
  149. return NULL;
  150. }
  151. #endif // QUANTUM_PAINTER_SH1106_SPI_ENABLE
  152. #ifdef QUANTUM_PAINTER_SH1106_I2C_ENABLE
  153. // Factory function for creating a handle to the SH1106 device
  154. painter_device_t qp_sh1106_make_i2c_device(uint16_t panel_width, uint16_t panel_height, uint8_t i2c_address) {
  155. for (uint32_t i = 0; i < SH1106_NUM_DEVICES; ++i) {
  156. sh1106_device_t *driver = &sh1106_drivers[i];
  157. if (!driver->oled.base.driver_vtable) {
  158. // Instantiate the surface, intentional swap of width/high due to transpose
  159. painter_device_t surface = qp_make_mono1bpp_surface_advanced(&driver->oled.surface, 1, panel_width, panel_height, driver->framebuffer);
  160. if (!surface) {
  161. return NULL;
  162. }
  163. // Setup the OLED device
  164. driver->oled.base.driver_vtable = (const painter_driver_vtable_t *)&sh1106_driver_vtable;
  165. driver->oled.base.comms_vtable = (const painter_comms_vtable_t *)&i2c_comms_cmddata_vtable;
  166. driver->oled.base.native_bits_per_pixel = 1; // 1bpp mono
  167. driver->oled.base.panel_width = panel_width;
  168. driver->oled.base.panel_height = panel_height;
  169. driver->oled.base.rotation = QP_ROTATION_0;
  170. driver->oled.base.offset_x = 0;
  171. driver->oled.base.offset_y = 0;
  172. // I2C configuration
  173. driver->oled.base.comms_config = &driver->oled.i2c_config;
  174. driver->oled.i2c_config.chip_address = i2c_address;
  175. if (!qp_internal_register_device((painter_device_t)driver)) {
  176. memset(driver, 0, sizeof(sh1106_device_t));
  177. return NULL;
  178. }
  179. return (painter_device_t)driver;
  180. }
  181. }
  182. return NULL;
  183. }
  184. #endif // QUANTUM_PAINTER_SH1106_I2C_ENABLE