Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback question #20

Closed
vahithosan opened this issue Oct 5, 2022 · 9 comments
Closed

Callback question #20

vahithosan opened this issue Oct 5, 2022 · 9 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@vahithosan
Copy link

Hi. Thanks for sharing this library. I have a question. How can call function on esp32 with nextion button.

@brainelectronics brainelectronics added the enhancement New feature or request label Oct 23, 2022
@brainelectronics
Copy link
Owner

Hi @vahithosan
What you need is the attachPop or attachPush callback function implemented in NexTouch.cpp

This requires calls of the nexLoop function of NexHardware.cpp.

Both functions are currently not implemented in this micropython lib. It would make sense to change to an async UART communication with the display and run this nexLoop call in its own async loop.

@vahithosan
Copy link
Author

Thanks for your answer. I will also use uart with async. When the buttons are pressed, I send strings like print "page1" print "page2".

page1 = NexPage(nh, 1 ,0, "page1")
page2 = NexPage(nh, 2 ,0, "page2")
page3 = NexPage(nh, 3 ,0, "page3")

async def receiver():
    sreader = asyncio.StreamReader(uart)
    while True:
        res = await sreader.read(8)
        print('Recieved', res)
        if res==b'PAGE01':
            page1.show()
            p1n0=NexNumber(nh, 1, 8, "n0")
            p1n0.setValue(55)
        elif res==b'PAGE02':
            page2.show()
            p2n0=NexNumber(nh, 2, 8, "n0")
            p2n0.setValue(25)
        elif res==b'PAGE03':
            page3.show()
            p3n0=NexNumber(nh, 3, 8, "n0")
            p3n0.setValue(35)

I have one more question. Can we change the variable in a non-open page? For example, can we change the value on page 2 when page 1 is open? I couldn't change.(The variables are set global)

@brainelectronics brainelectronics added the documentation Improvements or additions to documentation label Oct 23, 2022
@brainelectronics
Copy link
Owner

I assume it would be more efficient to create the NexNumber objects outside of the while True: loop. With the code you posted, p1n0=NexNumber(nh, 1, 8, "n0") will be recreated everytime res==b'PAGE01'.

The actual root cause is the same name of your three NexNumber objects. All are named n0, which will update the NexNumber named n0 in your layout. This happens accross all your pages. May you have a look at the setValue function of common.py. This will require a change in the .HMI file and thereby the .tft file.

I don't have a setup here at the moment, but maybe you can try it like this.

page1 = NexPage(nh, 1, 0, "page1")  # NexHardware, page ID, component ID, name
page2 = NexPage(nh, 2, 0, "page2")
page3 = NexPage(nh, 3, 0, "page3")

# use a different unique name per NexNumber object
p1n0 = NexNumber(nh, 1, 8, "n1")    # NexHardware, page ID, component ID, unique name
p2n0 = NexNumber(nh, 2, 8, "n2")
p3n0 = NexNumber(nh, 3, 8, "n3")

async def receiver():
    sreader = asyncio.StreamReader(uart)
    while True:
        res = await sreader.read(8)
        print('Recieved', res)
        if res==b'PAGE01':
            page1.show()
            p1n0.setValue(55)
        elif res==b'PAGE02':
            page2.show()
            p2n0.setValue(25)
        elif res==b'PAGE03':
            page3.show()
            p3n0.setValue(35)

@vahithosan
Copy link
Author

Thanks. I will try

@brainelectronics
Copy link
Owner

brainelectronics commented Oct 23, 2022

@vahithosan may you can provide the full code to run this simple example, like the complete main.py file? I would update the docs with it later

@vahithosan
Copy link
Author

I have just started. That's all for now, but I'll post again when I'm done.
There are 12 pages in total. I will go back and forth between pages.

import uasyncio as asyncio
from machine import UART
import ujson
import time
from lib.nextion import NexNumber, NexHardware, NexText, NexPage

uart = UART(2, baudrate=9600, tx=21, rx=22)
uart.init(baudrate=9600, bits=8, parity=None, stop=1)

tx_pin = 21
rx_pin = 22
k=0
# # create Nextion hardware interface
nh = NexHardware(rx_pin=rx_pin, tx_pin=tx_pin)
nh.nexInit()

page1 = NexPage(nh, 1 ,0, "page1")
page2 = NexPage(nh, 2 ,0, "page2")
page3 = NexPage(nh, 3 ,0, "page3")
page4 = NexPage(nh, 4 ,0, "page4")
page5 = NexPage(nh, 5 ,0, "page5")
page6 = NexPage(nh, 6 ,0, "page6")
page7 = NexPage(nh, 7 ,0, "page7")
page8 = NexPage(nh, 8 ,0, "page8")
page9 = NexPage(nh, 9 ,0, "page9")
page10 = NexPage(nh, 10 ,0, "page10")
page11 = NexPage(nh, 11 ,0, "page11")
page12 = NexPage(nh, 12 ,0, "page12")

#Page1
p1n0=NexNumber(nh, 1, 8, "n0")
p1n1=NexNumber(nh, 1, 10, "n1")
p1n2=NexNumber(nh, 1, 12, "n2")
p1n3=NexNumber(nh, 1, 13, "n3")
p1n4=NexNumber(nh, 1, 13, "n4")
p1n5=NexNumber(nh, 1, 13, "n5")
#Page2
p2n6=NexNumber(nh, 2, 6, "n6")
#Page3
p3n7=NexNumber(nh, 3, 7, "n7")
#Page4
p4n8=NexNumber(nh, 4, 8, "n8")
#Page5
p5n9=NexNumber(nh, 5, 9, "n9")
#Page8
p8n10=NexNumber(nh, 8, 10, "n10")
#Page9
p9n11=NexNumber(nh, 9, 11, "n11")
#Page10
p10n12=NexNumber(nh, 10, 12, "n12")
#Page10
p11n13=NexNumber(nh, 11, 13, "n13")

async def receiver():
    sreader = asyncio.StreamReader(uart)
    while True:
        res = await sreader.read(8)
        print('Recieved', res)
        if res==b'PAGE01':
            page1.show()
            p1n0.setValue(55)

        elif res==b'PAGE02':
            page2.show()
            p2n6.setValue(25)

        elif res==b'PAGE03':
            page3.show()
            p3n7.setValue(35)

        elif res==b'PAGE04':
            page4.show()
            p4n8.setValue(45)

        elif res==b'PAGE05':
            page5.show()
            p5n9.setValue(1225)

loop = asyncio.get_event_loop()
loop.create_task(receiver())
loop.run_forever()

I don't use component id on buttons because password is one page. If the password matches, there will be a page transition.

Touch press event in Nextion

112233

@vahithosan
Copy link
Author

I simply tried.

import uasyncio as asyncio
from machine import UART
import ujson
import time
from lib.nextion import NexNumber, NexHardware, NexText, NexPage

uart = UART(2, baudrate=9600, tx=21, rx=22)
uart.init(baudrate=9600, bits=8, parity=None, stop=1)

tx_pin = 21
rx_pin = 22
k=0
# # create Nextion hardware interface
nh = NexHardware(rx_pin=rx_pin, tx_pin=tx_pin)
nh.nexInit()

page1 = NexPage(nh, 1 ,0, "page1")
page2 = NexPage(nh, 2 ,0, "page2")
page3 = NexPage(nh, 3 ,0, "page3")


#Page1
p1n0=NexNumber(nh, 1, 10, "n0")
p1n1=NexNumber(nh, 1, 11, "n1")
p1n2=NexNumber(nh, 1, 12, "n2")
p1n3=NexNumber(nh, 1, 13, "n3")
p1n4=NexNumber(nh, 1, 25, "n4")
p1n5=NexNumber(nh, 1, 26, "n5")
#Page2
p2n6=NexNumber(nh, 2, 8, "n6")
#Page3
p3n7=NexNumber(nh, 3, 8, "n7")

While on page 2, I tried to change the value on page 3, it did not change.

>>> page2.show()
True
>>> p3n7.setValue(35)
False

I was able to open and change page 3. Is this how Nextion displays work?

>>> page3.show()
True
>>> p3n7.setValue(35)
True

@vahithosan
Copy link
Author

I was able to open and change page 3. Is this how Nextion displays work?

>>> page3.show()
True
>>> p3n7.setValue(35)
True

I was try like this yesterday

#Page3
p3n7=NexNumber(nh, 3, 8, "n7")

I wrote page name before object

#Page3
p3n7=NexNumber(nh, 3, 8, "page3.n7")

>>> page2.show()
True
>>> p3n7.setValue(35)
True
>>> page3.show()
True

Its working (variable change other page)

@brainelectronics
Copy link
Owner

@vahithosan thanks for the update! Sound great. I'll update the docs with your finding and code for callbacks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants