Skip to content

Commit a40f821

Browse files
committed
Created SH1107 driver for quantum painter based closely on the existing SH1106 driver.
1 parent 767dfbb commit a40f821

File tree

6 files changed

+335
-0
lines changed

6 files changed

+335
-0
lines changed

drivers/painter/sh1107/qp_sh1107.c

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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

drivers/painter/sh1107/qp_sh1107.h

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#pragma once
2+
3+
#include "gpio.h"
4+
#include "qp_internal.h"
5+
6+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7+
// Quantum Painter SH1107 configurables (add to your keyboard's config.h)
8+
9+
#if defined(QUANTUM_PAINTER_SH1107_SPI_ENABLE) && !defined(SH1107_NUM_SPI_DEVICES)
10+
/**
11+
* @def This controls the maximum number of SPI SH1107 devices that Quantum Painter can communicate with at any one time.
12+
* Increasing this number allows for multiple displays to be used.
13+
*/
14+
# define SH1107_NUM_SPI_DEVICES 1
15+
#else
16+
# define SH1107_NUM_SPI_DEVICES 0
17+
#endif
18+
19+
#if defined(QUANTUM_PAINTER_SH1107_I2C_ENABLE) && !defined(SH1107_NUM_I2C_DEVICES)
20+
/**
21+
* @def This controls the maximum number of I2C SH1107 devices that Quantum Painter can communicate with at any one time.
22+
* Increasing this number allows for multiple displays to be used.
23+
*/
24+
# define SH1107_NUM_I2C_DEVICES 1
25+
#else
26+
# define SH1107_NUM_I2C_DEVICES 0
27+
#endif
28+
29+
#define SH1107_NUM_DEVICES ((SH1107_NUM_SPI_DEVICES) + (SH1107_NUM_I2C_DEVICES))
30+
31+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
32+
// Quantum Painter SH1107 device factories
33+
34+
#ifdef QUANTUM_PAINTER_SH1107_SPI_ENABLE
35+
36+
/**
37+
* Factory method for an SH1107 SPI LCD device.
38+
*
39+
* @param panel_width[in] the width of the display in pixels (usually 64)
40+
* @param panel_height[in] the height of the display in pixels (usually 128)
41+
* @param chip_select_pin[in] the GPIO pin used for SPI chip select
42+
* @param dc_pin[in] the GPIO pin used for D/C control
43+
* @param reset_pin[in] the GPIO pin used for RST
44+
* @param spi_divisor[in] the SPI divisor to use when communicating with the display
45+
* @param spi_mode[in] the SPI mode to use when communicating with the display
46+
* @return the device handle used with all drawing routines in Quantum Painter
47+
*/
48+
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);
49+
50+
#endif // QUANTUM_PAINTER_SH1107_SPI_ENABLE
51+
52+
#ifdef QUANTUM_PAINTER_SH1107_I2C_ENABLE
53+
54+
/**
55+
* Factory method for an SH1107 I2C LCD device.
56+
*
57+
* @param panel_width[in] the width of the display in pixels (usually 64)
58+
* @param panel_height[in] the height of the display in pixels (usually 128)
59+
* @param i2c_address[in] the I2C address to use
60+
* @return the device handle used with all drawing routines in Quantum Painter
61+
*/
62+
painter_device_t qp_sh1107_make_i2c_device(uint16_t panel_width, uint16_t panel_height, uint8_t i2c_address);
63+
64+
#endif // QUANTUM_PAINTER_SH1107_I2C_ENABLE
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2024 Steve Branam (@smbranam)
2+
// SPDX-License-Identifier: GPL-2.0-or-later
3+
#pragma once
4+
5+
#define SH1107_DISPLAY_ON 0xAF
6+
#define SH1107_DISPLAY_OFF 0xAE
7+
#define SH1107_SET_OSC_DIVFREQ 0xD5
8+
#define SH1107_SET_MUX_RATIO 0xA8
9+
#define SH1107_DISPLAY_OFFSET 0xD3
10+
#define SH1107_SET_START_LINE 0xDC // Key/sole difference from SH1106 (which uses 0x40)
11+
#define SH1107_SET_CHARGE_PUMP 0x8D
12+
#define SH1107_SET_SEGMENT_REMAP_NORMAL 0xA0
13+
#define SH1107_SET_SEGMENT_REMAP_INV 0xA1
14+
#define SH1107_COM_SCAN_DIR_INC 0xC0
15+
#define SH1107_COM_SCAN_DIR_DEC 0xC8
16+
#define SH1107_COM_PADS_HW_CFG 0xDA
17+
#define SH1107_SET_CONTRAST 0x81
18+
#define SH1107_SET_PRECHARGE_PERIOD 0xD9
19+
#define SH1107_VCOM_DESELECT_LEVEL 0xDB
20+
#define SH1107_ALL_ON_RESUME 0xA4
21+
#define SH1107_NON_INVERTING_DISPLAY 0xA6
22+
#define SH1107_DEACTIVATE_SCROLL 0x2E
23+
24+
#define SH1107_SETCOLUMN_LSB 0x00
25+
#define SH1107_SETCOLUMN_MSB 0x10
26+
#define SH1107_PAGE_ADDR 0xB0

quantum/painter/qp.h

+6
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,12 @@ int16_t qp_drawtext_recolor(painter_device_t device, uint16_t x, uint16_t y, pai
557557
# define SH1106_NUM_DEVICES 0
558558
#endif // QUANTUM_PAINTER_SH1106_ENABLE
559559

560+
#ifdef QUANTUM_PAINTER_SH1107_ENABLE
561+
# include "qp_sh1107.h"
562+
#else // QUANTUM_PAINTER_SH1107_ENABLE
563+
# define SH1107_NUM_DEVICES 0
564+
#endif // QUANTUM_PAINTER_SH1107_ENABLE
565+
560566
#ifdef QUANTUM_PAINTER_LD7032_ENABLE
561567
# include "qp_ld7032.h"
562568
#else // QUANTUM_PAINTER_LD7032_ENABLE

quantum/painter/qp_internal.c

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ enum {
1919
+ (GC9107_NUM_DEVICES) // GC9107
2020
+ (SSD1351_NUM_DEVICES) // SSD1351
2121
+ (SH1106_NUM_DEVICES) // SH1106
22+
+ (SH1107_NUM_DEVICES) // SH1107
2223
+ (LD7032_NUM_DEVICES) // LD7032
2324
};
2425

quantum/painter/rules.mk

+25
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ VALID_QUANTUM_PAINTER_DRIVERS := \
1818
ssd1351_spi \
1919
sh1106_i2c \
2020
sh1106_spi \
21+
sh1107_i2c \
22+
sh1107_spi \
2123
ld7032_i2c \
2224
ld7032_spi
2325

@@ -184,6 +186,29 @@ define handle_quantum_painter_driver
184186
$(DRIVER_PATH)/painter/oled_panel/qp_oled_panel.c \
185187
$(DRIVER_PATH)/painter/sh1106/qp_sh1106.c
186188

189+
else ifeq ($$(strip $$(CURRENT_PAINTER_DRIVER)),sh1107_spi)
190+
QUANTUM_PAINTER_NEEDS_SURFACE := yes
191+
QUANTUM_PAINTER_NEEDS_COMMS_SPI := yes
192+
QUANTUM_PAINTER_NEEDS_COMMS_SPI_DC_RESET := yes
193+
OPT_DEFS += -DQUANTUM_PAINTER_SH1107_ENABLE -DQUANTUM_PAINTER_SH1107_SPI_ENABLE
194+
COMMON_VPATH += \
195+
$(DRIVER_PATH)/painter/oled_panel \
196+
$(DRIVER_PATH)/painter/sh1107
197+
SRC += \
198+
$(DRIVER_PATH)/painter/oled_panel/qp_oled_panel.c \
199+
$(DRIVER_PATH)/painter/sh1107/qp_sh1107.c
200+
201+
else ifeq ($$(strip $$(CURRENT_PAINTER_DRIVER)),sh1107_i2c)
202+
QUANTUM_PAINTER_NEEDS_SURFACE := yes
203+
QUANTUM_PAINTER_NEEDS_COMMS_I2C := yes
204+
OPT_DEFS += -DQUANTUM_PAINTER_SH1107_ENABLE -DQUANTUM_PAINTER_SH1107_I2C_ENABLE
205+
COMMON_VPATH += \
206+
$(DRIVER_PATH)/painter/oled_panel \
207+
$(DRIVER_PATH)/painter/sh1107
208+
SRC += \
209+
$(DRIVER_PATH)/painter/oled_panel/qp_oled_panel.c \
210+
$(DRIVER_PATH)/painter/sh1107/qp_sh1107.c
211+
187212
else ifeq ($$(strip $$(CURRENT_PAINTER_DRIVER)),ld7032_spi)
188213
QUANTUM_PAINTER_NEEDS_SURFACE := yes
189214
QUANTUM_PAINTER_NEEDS_COMMS_SPI := yes

0 commit comments

Comments
 (0)