Advertisement
talofer99

Arduino-CLI python GUI

Jul 19th, 2019
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. # !/usr/bin/python3
  2. from tkinter import *
  3. from tkinter import messagebox
  4. import subprocess
  5. import os
  6.  
  7. # get example path
  8. blink_dirpath = os.getcwd() + '/' + 'blink/'
  9. print (blink_dirpath)
  10.  
  11. # global board variables
  12. FQBN = ""
  13. PORT = ""
  14. ID = ""
  15. NAME = ""
  16.  
  17. # get the first board detected info.
  18. def getConnectedBoardInfo():
  19.    global FQBN, PORT, ID, NAME
  20.    errcode, result = run_cmd('arduino-cli board list')
  21.    if errcode == None:
  22.       if len(result) > 2:
  23.          #print (result[1])
  24.          FQBN, PORT, ID, NAME =  (result[1].decode('UTF-8').replace('\n', '')).split("\t")
  25.          return True
  26.    else:
  27.       print (errcode)
  28.       return False
  29.  
  30. # save the blink file data for show
  31. def saveBlinkFile(file_data):
  32.    with open(blink_dirpath + 'blink.ino', 'w') as f:
  33.       f.write(file_data)
  34.    f.close()
  35.  
  36. # get the blink file data for show
  37. def getBlinkFile():
  38.    with open(blink_dirpath + 'blink.ino') as f:
  39.       read_data = f.read()
  40.    f.close()
  41.    return read_data
  42.  
  43. # run cmd
  44. def run_cmd(win_cmd):
  45.    result = []
  46.    process = subprocess.Popen(win_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  47.    for line in process.stdout:
  48.       result.append(line)
  49.    errcode = process.returncode
  50.    return errcode, result
  51.  
  52. # check if the runned process has error or error code
  53. def isProcessOk(errcode, result):
  54.    errorMsg = ""
  55.    if result[0].decode('UTF-8').find('Error') == -1 and  errcode == None:
  56.       return True ,errorMsg
  57.    else:
  58.       errorMsg = "error code: " + str(errcode)
  59.       errorMsg = errorMsg + "\nresult:\n"
  60.       for line in result:
  61.          errorMsg = errorMsg + line.decode('UTF-8') + "\n"
  62.       return False, errorMsg
  63.  
  64. # call back of the button press
  65. def buttonPress():
  66.    saveBlinkFile(text.get("1.0","end-1c"))
  67.    #set message
  68.    msg_output= "Process completed"
  69.    # first compile
  70.    win_cmd = 'arduino-cli compile -b ' + FQBN + '  ' + blink_dirpath
  71.    errcode, result = run_cmd(win_cmd)
  72.    isOk, errorMsg = isProcessOk(errcode, result)
  73.    if isOk:
  74.       #now we upload
  75.       win_cmd = 'arduino-cli upload -b ' + FQBN + ' -p '+ PORT +' '  + blink_dirpath
  76.       errcode, result = run_cmd(win_cmd)
  77.       # if any result it means error
  78.       if len(result):
  79.          isOk, errorMsg = isProcessOk(errcode, result)
  80.          if not isOk:
  81.                msg_output = errorMsg
  82.    else:
  83.       msg_output = errorMsg
  84.    # output as popup
  85.    msg = messagebox.showinfo( "Compile And Upload", msg_output)
  86.  
  87. # run connected board info - if it returns TRUE it means we found at least one board. if not it will exit
  88. if getConnectedBoardInfo():
  89.    root = Tk()
  90.    root.title('IDE POC')
  91.    #root.geometry("1000x650")
  92.    
  93.    frame=Frame(root, width=1000, height=650)
  94.    frame.pack()
  95.  
  96.    text = Text(frame)
  97.    text.insert(INSERT, getBlinkFile())
  98.    text.place(x=5, y=5, height=600, width=990)
  99.    #text.pack()
  100.  
  101.    B = Button(frame, text = "Compile and upload ->" + NAME + " " + PORT, command = buttonPress)
  102.    B.place(x = 5,y = 615)
  103.    
  104.    root.mainloop()
  105. else:
  106.    print("NO BOARD IS CONNECTED!!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement