RPI-PICO-I2C-LCD lib slow work(Solved) #16219
-
Hello! Are there any solutions to add more function without slowing down the code? Raspberry Pi Pico with RP2040 from machine import Pin, I2C, Timer
import time
from i2c_lcd import I2cLcd
I2C_ADDR_LCD = 0x27 # lcd I2C 2004
i2c = I2C(0, scl=Pin(21), sda=Pin(20), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR_LCD, 4, 20)
#########################################################################
def test1():
i=0
return i
# ..... for test1 to test60
def test60():
i=0
return i
############################################################
rows, cols = 4, 20
lcd_buffer = [" " * cols for _ in range(rows)]
def update_lcd(new_data):
global lcd_buffer
for i in range(rows):
if not isinstance(new_data[i], str):
new_data[i] = str(new_data[i])
if len(new_data[i]) < cols:
new_data[i] += " " * (cols - len(new_data[i]))
elif len(new_data[i]) > cols:
new_data[i] = new_data[i][:cols]
for row in range(rows):
for col in range(cols):
if lcd_buffer[row][col] != new_data[row][col]:
lcd.move_to(col, row)
lcd.putstr(new_data[row][col])
lcd_buffer[row] = new_data[row]
new_lcd_data = [
'some text1', #
'some text2 ', #
'some text3 ', #
'some text4 '] #
new_lcd_data1 = [
'text test1',
'text test2',
'text test3',
'text test4']
while True:
update_lcd(new_lcd_data)
time.sleep(0.1)
update_lcd(new_lcd_data1)
time.sleep(0.1) |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 14 replies
-
Please format your code as described here. |
Beta Was this translation helpful? Give feedback.
-
For test I put global variables inside a function and nothing change with slowly work,only wrong work :) But when i tried send result not to Lcd,as here lcd.move_to(col, row)
lcd.putstr(new_data[row][col]) I delete this two function and add 'print' for send results to Thommy, all working fast. These two function doesn't like a lot of empty functions, but why and how it fix ? |
Beta Was this translation helpful? Give feedback.
-
This is strange. Having many and unused functions does not affect the execution of a function. Maybe you can try using different I2C? on my board:
|
Beta Was this translation helpful? Give feedback.
-
And if put Lcd function first and empty function after calling lcd -all work good too |
Beta Was this translation helpful? Give feedback.
-
Is it possible that you are causing hash table collisions for function name lookups with very similar function names? I haven't looked at how MP does hashing. Try 60 randomly generated function names (use a python program to generate the functions, then paste them into this code). See if it runs fast then. |
Beta Was this translation helpful? Give feedback.
-
do you pre-compile your code, or is all the code in a single large file that
And thus fragmenting memory . In my experience i've been able to run large and complex applications in surprisingly little memory , once I learned how to avoid fragmenting it. |
Beta Was this translation helpful? Give feedback.
-
SOLVED! |
Beta Was this translation helpful? Give feedback.
SOLVED!
For fast work in i2c_lcd lib change "utime" to "time" and delete a lot of gc.collect().
https://github.com/T-622/RPI-PICO-I2C-LCD/issues/17#issue-2092503824
Thanks all for helping!