Skip to content

Commit ecc986a

Browse files
committed
updates the driver use example.
1 parent 123bb35 commit ecc986a

File tree

2 files changed

+34
-205
lines changed

2 files changed

+34
-205
lines changed
Lines changed: 20 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,7 @@
1-
import heap_caps
21
from micropython import const
32

43
_WIDTH = const(480)
54
_HEIGHT = const(320)
6-
_BYTES_PER_PIXEL = const(2) # RGB565
7-
_BUFFER_SIZE = const(int(_WIDTH * _HEIGHT * _BYTES_PER_PIXEL / 10))
8-
9-
10-
def memory_allocation_failure(user_data, function_name, caps, size):
11-
12-
print('Frme buffer allocation failed:', user_data, function_name, caps, size)
13-
14-
15-
# we want to allocate the frame buffers as soon as the application starts.
16-
# this is really important to do if wanting to secure space
17-
# in the internal memory. It is even more important to do if you are
18-
# using an ESP32 and want to use DMA memory and also internal memory.
19-
# There is only a really small amount of DMA memory available.
20-
21-
# allocate the buffer in DMA section of internal memory
22-
# DMA memory will allow for the best performance using 2 buffers
23-
# and the internal SRAM is a lot faster than the external SPIRAM
24-
25-
# you can also set an optional callback if the allocation fails.
26-
heap_caps.register_failed_alloc_callback(memory_allocation_failure, 'frame buffer 1')
27-
frame_buf1 = heap_caps.malloc(_BUFFER_SIZE, heap_caps.CAP_DMA | heap_caps.CAP_INTERNAL)
28-
29-
heap_caps.register_failed_alloc_callback(memory_allocation_failure, 'frame buffer 2')
30-
frame_buf2 = heap_caps.malloc(_BUFFER_SIZE, heap_caps.CAP_DMA | heap_caps.CAP_INTERNAL)
31-
325

336
import lcd_bus # NOQA
347
# this next section is the setup for the SPIBus.
@@ -39,18 +12,6 @@ def memory_allocation_failure(user_data, function_name, caps, size):
3912
# this is a required setting. You MUST supply a value here.
4013
_DC_PIN = const(11)
4114

42-
# This setting sets the size of the transfer that can be done.
43-
# If you are NOT using DMA memory for the buffers then you do not set this
44-
# at all or set this to `lcd_bus.SPIBus.MAXIMUM_BUFFER_SIZE`
45-
_MAX_TRANSFER_SZ = const(_BUFFER_SIZE)
46-
47-
# the default for the host is -1. if this is set to -1 the host will be picked
48-
# based on the pin numbers supplied. If the spi pin numbers are all set to -1
49-
# then the pin numbers that are assigned to the host at the hardware level will
50-
# be used. Setting the host and making the connections to the hardware pins
51-
# is the easiest thing to do.
52-
_SPI_HOST = const(lcd_bus.SPIBus.HOST2)
53-
5415
# if you leave the `host` at its default or set it to -1 then this must be
5516
# supplied. This is the pin that sends data to the display.
5617
_MOSI_PIN = const(13)
@@ -80,68 +41,32 @@ def memory_allocation_failure(user_data, function_name, caps, size):
8041

8142
_FREQ = const(80000000)
8243

83-
8444
# these pins get set Automatically if you are using hardware SPI pins and you
8545
# have qud_spi set to True. You can set them manually if you are using
8646
# something other than the hardware pins.
8747
_WP_PIN = const(-1)
8848
_HD_PIN = const(-1)
8949

90-
# set this to True if you are using quad_spi
91-
_QUAD_SPI = False
92-
93-
# Set this to True if you do not have the miso line connected.
94-
_TX_ONLY = False
95-
96-
# this is the default value and only needs to be changed if the datasheet
97-
# for the display says to
98-
_CMD_BITS = const(8)
99-
100-
# this is the default value and only needs to be changed if the datasheet
101-
# for the display says to
102-
_PARAM_BITS = const(8)
103-
104-
# this is the default value and only needs to be changed if the datasheet
105-
# for the display says to
106-
_DC_LOW_ON_DATA = False
107-
108-
# this is the default value and only needs to be changed if the datasheet
109-
# for the display says to
110-
_SIO_MODE = const()
111-
112-
# this is the default value and only needs to be changed if the datasheet
113-
# for the display says to
114-
_LSB_FIRST = False
115-
116-
# this is the default value and only needs to be changed if the datasheet
117-
# for the display says to
118-
_CS_HIGH_ACTIVE = False
119-
120-
# this is the default value and only needs to be changed if the datasheet
121-
# for the display says to
122-
_SPI_MODE = const(0)
123-
12450

12551
display_bus = lcd_bus.SPIBus(
12652
dc=_DC_PIN,
127-
max_transfer_sz=_MAX_TRANSFER_SZ,
128-
host=_SPI_HOST,
53+
host=1,
54+
sck=_SCLK_PIN,
55+
freq=_FREQ,
12956
mosi=_MOSI_PIN,
13057
miso=_MISO_PIN,
131-
sclk=_SCLK_PIN,
13258
cs=_CS_PIN,
133-
freq=_FREQ,
13459
wp=_WP_PIN,
13560
hd=_HD_PIN,
136-
quad_spi=_QUAD_SPI,
137-
tx_only=_TX_ONLY,
138-
cmd_bits=_CMD_BITS,
139-
param_bits=_PARAM_BITS,
140-
dc_low_on_data=_DC_LOW_ON_DATA,
141-
sio_mode=_SIO_MODE,
142-
lsb_first=_LSB_FIRST,
143-
cs_high_active=_CS_HIGH_ACTIVE,
144-
spi_mode=_SPI_MODE
61+
quad_spi=False,
62+
tx_only=False,
63+
cmd_bits=8,
64+
param_bits=8,
65+
dc_low_on_data=False,
66+
sio_mode=False,
67+
lsb_first=False,
68+
cs_high_active=False,
69+
spi_mode=0
14570
)
14671

14772

@@ -151,34 +76,15 @@ def memory_allocation_failure(user_data, function_name, caps, size):
15176
# If there is no reset pin for your display set this to -1.
15277
_RESET_PIN = const(43)
15378

154-
# this is the direction the pin needs to be set to in order for the display
155-
# to perform the reset. STATE_HIGH is the default setting
156-
# valid choices are STATE_HIGH and STATE_LOW
157-
_RESET_STATE = const(st7796.STATE_HIGH)
158-
159-
16079
# power pin, this pin is used to power on the display.
16180
# If there is no power pin for your display set this to -1.
162-
16381
_POWER_PIN = const(44)
16482

165-
# this is the direction the pin needs to be set to in order for the display
166-
# to power on. STATE_HIGH is the default setting
167-
# valid choices are STATE_HIGH and STATE_LOW
168-
_POWER_ON_STATE = const(st7796.STATE_HIGH)
169-
17083
# backlight pin, this pin is used to turn on and off the
17184
# backlight to the display.
17285
# If there is no backlight pin for your display set this to -1.
17386
_BACKLIGHT_PIN = const(45)
17487

175-
# this is the direction the pin needs to be set to in order for the
176-
# backlight to turn on and off. STATE_HIGH is the default setting
177-
# valid choices are STATE_HIGH, STATE_LOW and STATE_PWM.
178-
# STATE_PWM can only be used if there is a transistor in the circuit that is
179-
# used to supply backlight power. Check your datacheet for the display prior
180-
# to using STATE_PWM.
181-
_BACKLIGHT_ON_STATE = const(st7796.STATE_PWM)
18288

18389
# some displays have a bezel that covers a small portion of the viewable area
18490
# of the display. This is to offset the display data so it is not covered by
@@ -191,14 +97,15 @@ def memory_allocation_failure(user_data, function_name, caps, size):
19197
data_bus=display_bus,
19298
display_width=_WIDTH,
19399
display_height=_HEIGHT,
194-
frame_buffer1=frame_buf1,
195-
frame_buffer2=frame_buf2,
100+
# we are going to let the driver handle the allocation of the frame buffers
101+
frame_buffer1=None,
102+
frame_buffer2=None,
196103
reset_pin=_RESET_PIN,
197-
reset_state=_RESET_STATE,
104+
reset_state=st7796.STATE_HIGH,
198105
power_pin=_POWER_PIN,
199-
power_on_state=_POWER_ON_STATE,
106+
power_on_state=st7796.STATE_HIGH,
200107
backlight_pin=_BACKLIGHT_PIN,
201-
backlight_on_state=_BACKLIGHT_ON_STATE,
108+
backlight_on_state=st7796.STATE_HIGH,
202109
offset_x=_OFFSET_X,
203110
offset_y=_OFFSET_Y
204111
)
@@ -214,102 +121,10 @@ def memory_allocation_failure(user_data, function_name, caps, size):
214121
# The old drivers used to inot the display automatically. I have decided against
215122
# doing that to save memory. So the init must be done at some point before
216123
# you create any LVGL objects.
217-
218124
display.init()
219125

220-
# you can set the orientation any time after the display can be constructed.
221-
# It doesn't matter if it is before calling init or after.
222-
display.orientation = st7796.LANDSCAPE
223-
224126
import lvgl as lv # NOQA
225127

226-
# place your code here that is going to create your UI
227-
228-
229-
# Now turn the backlight on. If you do not have PWM you can use 0 for off or
230-
# False for off and any number that is not 0 for on or True for on.
231-
# if you do have PWM set then 100 is full brightness and 0 if off.
232-
# I recommend using the 0-100 scale no matter what state is set to.
233-
# you will see why I have the setting of the backlight in a function call.
234-
# NOTE: Fractional numbers are allowed when setting the backlight.
235-
def set_backlight(value):
236-
237-
import time
238-
239-
curr_value = display.backlight
240-
# backlight pin not supplied
241-
242-
if curr_value == -1:
243-
return
244-
245-
if value > curr_value:
246-
increment = 1
247-
else:
248-
increment = -1
249-
250-
while curr_value != value:
251-
curr_value += increment
252-
display.backlight = value
253-
time.sleep_ms(1)
254-
255-
# this is here in case there is anything that needs to be
256-
# updated in the UI. Maybe an opening animation.
257-
update_lvgl()
258-
259-
260-
# I removed the use of having tick_inc and task_handler called.
261-
# I found this to be something that causes a lot of headache and it also
262-
# degrades the performance due to it using an ISR and scheduling a callback
263-
# to have task_handler called. It caused the display update to be slow.
264-
# because that has been removed you will now need to provide your own loop
265-
# I recommend using the loop code below and adding any thing that needs
266-
# to be done to it.
267-
268-
# this is how I go about doing a nice ramp up with the backlight (if supported)
269-
270-
import time # NOQA
271-
272-
# time keeping variables
273-
start_time = time.ticks_us()
274-
left_over = 0
275-
276-
277-
# I set the updating into a function so I would not have duplicated code for
278-
# when the backlight is ramping up and when the loop is running
279-
def update_lvgl():
280-
global start_time
281-
global left_over
282-
283-
# I am using microsecond resolution when updating but due to LVGL only
284-
# supporting millisecond timing I have to check and see if enough
285-
# microseconds have passed before calling the task handler.
286-
# the update happens every 1024 microseconds which is 24 microseconds
287-
# past 1 millisecond. I am doing this so I m able to use simple bit shifting
288-
# and avoid having to do any floating point math.
289-
290-
curr_time = time.ticks_us()
291-
new_amount = time.ticks_diff(start_time, curr_time) + left_over
292-
if new_amount < 0:
293-
return
294-
295-
new_amount = abs(new_amount)
296-
left_over = new_amount & 0x400
297-
# 1024 in binary is 0100 0000 0000
298-
new_amount >>= 10
299-
new_amount = abs(new_amount)
300-
if new_amount:
301-
# adjustment to the left over for the 24 microseconds.
302-
# we want to keep the time keeping in sync so 24 extra microseconds for
303-
# each millisecond that has passed needs to be adjusted for
304-
left_over -= 24 * new_amount
305-
lv.tick_inc(new_amount)
306-
lv.task_handler()
307-
308-
start_time = curr_time
309-
310-
311-
set_backlight(100)
128+
import task_handler # NOQA
312129

313-
while True:
314-
update_lvgl()
315-
# any code that needs to be run inside the loop gets done here
130+
task_handler.TaskHandler()

ext_mod/sdl/micropython.mk

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
################################################################################
3+
# lcd_bus build rules
4+
5+
MOD_DIR := $(USERMOD_DIR)
6+
7+
CFLAGS_USERMOD += -I$(MOD_DIR)
8+
CFLAGS_USERMOD += -I$(MOD_DIR)/include
9+
10+
SRC_USERMOD_C += $(MOD_DIR)/modsdl.c
11+
SRC_USERMOD_C += $(MOD_DIR)/src/keyboard.c
12+
SRC_USERMOD_C += $(MOD_DIR)/src/mouse.c
13+
SRC_USERMOD_C += $(MOD_DIR)/src/mousewheel.c
14+
SRC_USERMOD_C += $(MOD_DIR)/src/display.c

0 commit comments

Comments
 (0)