qp_sh1107.c 9.4 KB

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