Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8609

MicroPython • Problem with NEXION HMIs with Micropython on an RP2040

$
0
0
I created a function library (nex.py) and placed it in the root of my module.
Everything works, but I can't place the nex_return function in the library. As soon as I transfer it from the main program to the library, I get a return:
Traceback (most recent call last):
File "<stdin>", line 27, in <module>
NameError: name 'ok' isn't defined


The main program :
but ok is a global var.

Code:

def nex_return():    while True:    #Retrieve commands from Touchpanel    global ok, rxdata,tabreturn    if nex_comm.any():     rxdata=nex_comm.read()     tabreturn=list(rxdata)     #print ('tab:',tabreturn)     ok=True     break    else :     ok=False     break   #------main loop-------------------------------------- while True: # waiting for the display touch      nex_return()     if ok:     ok=False        if tabreturn[0]==101:      page_id = tabreturn[1]      device_id = tabreturn[2]      event_id = tabreturn[3]#----check of the device_id-------------------------------------------      # check the number tool test-----------------------      if device_id==1:#number will try to recover the number       #print ('je suis id=',device_id)       tabreturn=""       nex_getval('n0') #send order to receive value       while True:        nex_return()        if ok:         ok=False         #print ('tab2:',tabreturn)         if tabreturn[0]==113:          #print ('tabreturn', tabreturn[0])          xvalue=tabreturn[1]+(tabreturn[2]*256)+(tabreturn[3]*65536)+(tabreturn[4]*16777216)          print('value from number:', xvalue)          mystring=str(xvalue)          nex_text('t0', mystring) #we write the value in text tool          sleep (2)          nex_text('t0'," ")          break #----gauge test-------------------------------------------                 elif device_id==2: #button          #print ('tabreturn', tabreturn[0])          nex_backcolor('b0', Red) # we change the color of button          sleep(1)          nex_backcolor('b0', Grey)          #ok=False          #break#----gauge test-------------------------------------------                 elif device_id==7: # hotspot to play with gauge (d_id=3)          #print ('tabreturn', tabreturn)          for mynum in range(0, 370, 10):            nex_val('z0',mynum)            sleep(0.2)          #break #----progress bar  test-------------------------------------------                  elif device_id==4: #progress bar          for mynum in range(0, 101,2):            nex_val('j0',mynum)            sleep(0.1)          for mynum in range(100, -1, -2):            nex_val('j0',mynum)              sleep(0.1)          nex_val('j0',50)        elif device_id==5: #scolling text          mystring='The quick brown fox jumps over the lazy dog' #'Voix ambiguë d'un cœur qui, au zéphyr, préfère les jattes de kiwis'          nex_text('g0', mystring)          #break#----slicer test-------------------------------------------                elif device_id==6: #read the value of a slicer                    tabreturn=""          nex_getval('h0')          while True:           nex_return()           if ok:            ok=False         #print ('tab2:',tabreturn)            if tabreturn[0]==113:          #print ('tabreturn', tabreturn[0])                         xvalue=tabreturn[1]+(tabreturn[2]*256)+(tabreturn[3]*65536)+(tabreturn[4]*16777216)             print('Slicer value:', xvalue)             mystring=str(xvalue)             nex_text('t0', mystring)             break#----check box test-------------------------------------------                    elif device_id==9: #read the value of a check box               tabreturn=""          nex_getval('c0')          while True:           nex_return()           if ok:            ok=False         #print ('tab2:',tabreturn)            if tabreturn[0]==113:          #print ('tabreturn', tabreturn[0])                         xvalue=tabreturn[1]+(tabreturn[2]*256)+(tabreturn[3]*65536)+(tabreturn[4]*16777216)             print('check box value:', xvalue)             mystring=str(xvalue)             nex_text('t0', mystring)             break

Code:

from machine import UART, Pinnex_comm = UART(0, 9600, parity=None, stop = 1, bits = 8, tx=Pin(0), rx=Pin(1),timeout=10)#pin0=jaune pin1=bleu du nextion# Variable pour stocker les données reçuesdata_buffer = bytearray()fff = b'\xff\xff\xff'quote='"'Green = 2016White = 65535Black = 0Red = 63488Yellow = 65504Blue = 31Grey = 50712mytext= ""my_id=""font0=0font1=1font2=2'''def nex_return():    while True:    #Retrieve commands from Touchpanel    global ok, rxdata,tabreturn    if nex_comm.any():     rxdata=nex_comm.read()     tabreturn=list(rxdata)     #print ('tab:',tabreturn)     ok=True     break    else :     ok=False     break'''    #----to send a text to a text zone----------------------------def nex_text(text_id, sentence):    my_id=text_id+'.txt='+quote    mytext=my_id+sentence+quote    nex_comm.write(mytext)    nex_comm.write(fff)#----to change the back color of a tool nextion--------------def nex_backcolor(text_id, color):    gtext = text_id + ".bco=" + str(color)  #change the Back color     nex_comm.write(gtext)    nex_comm.write(fff)    #print (gtext)#----to change the color of the font of a tool nextion--------   def nex_txtcolor(text_id, color):    gtext = text_id + ".pco=" + str(color)  # change the text color    nex_comm.write(gtext)    nex_comm.write(fff)  #  print (gtext)#----to change the font of a tool nextion--------def nex_font(text_id, font):    gtext = text_id + ".font=" + str(font)  # change the text color    nex_comm.write(gtext)    nex_comm.write(fff)   # print (gtext)#----to change the page in a project nextion--------    def nex_page(npage):    my_page='page '+ str(npage)    nex_comm.write(my_page)    nex_comm.write(fff)   # print (my_page)#---to get à numeric value from a tool nextion----def nex_getval(text_id):    gtext='get '+text_id+'.val'    nex_comm.write(gtext)    nex_comm.write(fff) #----to get a sentence-----------------------def nex_gettext(text_id):    gtext='get '+text_id+'.val'    nex_comm.write(gtext)    nex_comm.write(fff)    print (gtext) #-------to change a value of a tool waiting a number (Gauge, slide bar)---def nex_val(text_id,value):     gtext = text_id + ".val=" + str(value)  #change the value     nex_comm.write(gtext)     nex_comm.write(fff)
I would like also add a return to the nex_return fucntion

thanks for your help
Gus

Statistics: Posted by gustave — Wed Jul 02, 2025 1:58 pm



Viewing all articles
Browse latest Browse all 8609

Trending Articles