logo

qmk_firmware

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

qp_sh1107.c (9670B)


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