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