Skip to content

Commit 199796d

Browse files
committed
Clean up display drivers
Clean up ST7789 PIO driver Remove debug print from ST7789 base class Fix display base class to have correct default for Pin mode and alt
1 parent fc54ace commit 199796d

File tree

3 files changed

+39
-35
lines changed

3 files changed

+39
-35
lines changed

cv2_drivers/displays/cv2_display.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ def savePinModeAlt(self, pin):
113113
# Look up the mode in Pin class dictionary
114114
mode = Pin.__dict__[modeStr]
115115
else:
116-
# No mode specified, just set to None
117-
mode = None
116+
# No mode specified, just set to -1 (default)
117+
mode = -1
118118

119119
# Extrct the "alt" parameter from the pin string
120120
if "alt=" in pinStr:
@@ -130,8 +130,8 @@ def savePinModeAlt(self, pin):
130130
# Convert the altStr to an integer
131131
alt = int(altStr)
132132
else:
133-
# No alt specified, just set to None
134-
alt = None
133+
# No alt specified, just set to -1 (default)
134+
alt = -1
135135

136136
# Return the mode and alt as a tuple
137137
return (mode, alt)

cv2_drivers/displays/st7789.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ def send_init(self, commands):
169169
Send initialisation commands to display.
170170
"""
171171
for command, data, delay in commands:
172-
print(command, data, delay)
173172
self._write(command, data)
174173
sleep_ms(delay)
175174

cv2_drivers/displays/st7789_pio.py

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ST7789_PIO(ST7789):
1616
pin_tx (Pin): Transmit pin number **Required**
1717
pin_dc (Pin): Data/Command pin number **Required**
1818
pin_cs (Pin): Chip Select pin number
19+
freq (int): State machine frequency in Hz, default -1 (system clock)
1920
rotation (int): Orientation of display
2021
- 0-Portrait, default
2122
- 1-Landscape
@@ -36,47 +37,56 @@ def __init__(
3637
pin_tx,
3738
pin_dc,
3839
pin_cs=None,
40+
freq=-1,
3941
rotation=0,
4042
color_order=ST7789.BGR,
4143
reverse_bytes_in_word=True,
4244
):
4345
# Store PIO arguments
4446
self.sm_id = sm_id
45-
self.pin_clk = pin_clk
46-
self.pin_tx = pin_tx
47-
# self.pin_dc = pin_dc
48-
# self.pin_cs = pin_cs
49-
50-
self.clk = Pin(pin_clk, Pin.OUT) # Don't change mode/alt
51-
self.tx = Pin(pin_tx, Pin.OUT) # Don't change mode/alt
52-
self.clk = Pin(pin_clk, Pin.ALT, alt=Pin.ALT_PIO0) # Don't change mode/alt
53-
self.tx = Pin(pin_tx, Pin.ALT, alt=Pin.ALT_PIO0) # Don't change mode/alt
54-
self.dc = Pin(pin_dc, Pin.OUT) # Don't change mode/alt
47+
self.clk = Pin(pin_clk) # Don't change mode/alt
48+
self.tx = Pin(pin_tx) # Don't change mode/alt
49+
self.dc = Pin(pin_dc) # Don't change mode/alt
5550
self.cs = Pin(pin_cs, Pin.OUT, value=1) if pin_cs else None
51+
self.freq = freq
5652

57-
program = self._pio_write_spi
58-
# program[0][0]=0x6001
59-
# program[0][4]=0xb042
60-
print(program)
53+
# Get the current mode and alt of the pins so they can be restored
54+
txMode, txAlt = self.savePinModeAlt(self.tx)
55+
clkMode, clkAlt = self.savePinModeAlt(self.clk)
6156

57+
# Initialize the PIO state machine
6258
self.sm = rp2.StateMachine(
6359
self.sm_id,
64-
program,
65-
out_base = self.pin_tx,
66-
sideset_base = self.pin_clk,
67-
# out_shiftdir = rp2.PIO.SHIFT_LEFT,
60+
self._pio_write_spi,
61+
freq = self.freq,
62+
out_base = self.tx,
63+
sideset_base = self.clk,
6864
)
65+
66+
# The tx and clk pins just got their mode and alt set for PIO0 or PIO1,
67+
# so we need to save them again to restore later when _write() is called
68+
self.txMode, self.txAlt = self.savePinModeAlt(self.tx)
69+
self.clkMode, self.clkAlt = self.savePinModeAlt(self.clk)
70+
71+
# Now restore the original mode and alt of the pins
72+
self.tx.init(mode=txMode, alt=txAlt)
73+
self.clk.init(mode=clkMode, alt=clkAlt)
6974

75+
# Call the parent class constructor
7076
super().__init__(width, height, rotation, color_order, reverse_bytes_in_word)
7177

7278
def _write(self, command=None, data=None):
7379
"""SPI write to the device: commands and data."""
74-
# Save the current mode and alt of the DC pin in case it's used by
80+
# Save the current mode and alt of the spi pins in case they're used by
7581
# another device on the same SPI bus
76-
# dcMode, dcAlt = self.savePinModeAlt(self.dc)
82+
dcMode, dcAlt = self.savePinModeAlt(self.dc)
83+
txMode, txAlt = self.savePinModeAlt(self.tx)
84+
clkMode, clkAlt = self.savePinModeAlt(self.clk)
7785

78-
# Temporarily set the DC pin to output mode
86+
# Temporarily set the SPI pins to the correct mode and alt for PIO
7987
self.dc.init(mode=Pin.OUT)
88+
self.tx.init(mode=self.txMode, alt=self.txAlt)
89+
self.clk.init(mode=self.clkMode, alt=self.clkAlt)
8090

8191
# Write to the display
8292
if self.cs:
@@ -90,8 +100,10 @@ def _write(self, command=None, data=None):
90100
if self.cs:
91101
self.cs.on()
92102

93-
# Restore the DC pin to its original mode and alt
94-
# self.dc.init(mode=dcMode, alt=dcAlt)
103+
# Restore the SPI pins to their original mode and alt
104+
self.dc.init(mode=dcMode, alt=dcAlt)
105+
self.tx.init(mode=txMode, alt=txAlt)
106+
self.clk.init(mode=clkMode, alt=clkAlt)
95107

96108
def _pio_write(self, data):
97109
"""Write data to the display using PIO."""
@@ -105,7 +117,6 @@ def _pio_write(self, data):
105117
self.sm.active(0)
106118

107119
@rp2.asm_pio(
108-
# fifo_join = rp2.PIO.JOIN_TX,
109120
out_init = rp2.PIO.OUT_LOW,
110121
sideset_init = rp2.PIO.OUT_LOW,
111122
out_shiftdir = rp2.PIO.SHIFT_LEFT,
@@ -114,10 +125,4 @@ def _pio_write(self, data):
114125
)
115126
def _pio_write_spi():
116127
out(pins, 1).side(0)
117-
nop()
118-
nop()
119-
nop()
120128
nop().side(1)
121-
nop()
122-
nop()
123-
nop()

0 commit comments

Comments
 (0)