# !/usr/bin/python3
from tkinter import *
from tkinter import messagebox
import subprocess
import os
# get example path
blink_dirpath = os.getcwd() + '/' + 'blink/'
print (blink_dirpath)
# global board variables
FQBN = ""
PORT = ""
ID = ""
NAME = ""
# get the first board detected info.
def getConnectedBoardInfo():
global FQBN, PORT, ID, NAME
errcode, result = run_cmd('arduino-cli board list')
if errcode == None:
if len(result) > 2:
#print (result[1])
FQBN, PORT, ID, NAME = (result[1].decode('UTF-8').replace('\n', '')).split("\t")
return True
else:
print (errcode)
return False
# save the blink file data for show
def saveBlinkFile(file_data):
with open(blink_dirpath + 'blink.ino', 'w') as f:
f.write(file_data)
f.close()
# get the blink file data for show
def getBlinkFile():
with open(blink_dirpath + 'blink.ino') as f:
read_data = f.read()
f.close()
return read_data
# run cmd
def run_cmd(win_cmd):
result = []
process = subprocess.Popen(win_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for line in process.stdout:
result.append(line)
errcode = process.returncode
return errcode, result
# check if the runned process has error or error code
def isProcessOk(errcode, result):
errorMsg = ""
if result[0].decode('UTF-8').find('Error') == -1 and errcode == None:
return True ,errorMsg
else:
errorMsg = "error code: " + str(errcode)
errorMsg = errorMsg + "\nresult:\n"
for line in result:
errorMsg = errorMsg + line.decode('UTF-8') + "\n"
return False, errorMsg
# call back of the button press
def buttonPress():
saveBlinkFile(text.get("1.0","end-1c"))
#set message
msg_output= "Process completed"
# first compile
win_cmd = 'arduino-cli compile -b ' + FQBN + ' ' + blink_dirpath
errcode, result = run_cmd(win_cmd)
isOk, errorMsg = isProcessOk(errcode, result)
if isOk:
#now we upload
win_cmd = 'arduino-cli upload -b ' + FQBN + ' -p '+ PORT +' ' + blink_dirpath
errcode, result = run_cmd(win_cmd)
# if any result it means error
if len(result):
isOk, errorMsg = isProcessOk(errcode, result)
if not isOk:
msg_output = errorMsg
else:
msg_output = errorMsg
# output as popup
msg = messagebox.showinfo( "Compile And Upload", msg_output)
# run connected board info - if it returns TRUE it means we found at least one board. if not it will exit
if getConnectedBoardInfo():
root = Tk()
root.title('IDE POC')
#root.geometry("1000x650")
frame=Frame(root, width=1000, height=650)
frame.pack()
text = Text(frame)
text.insert(INSERT, getBlinkFile())
text.place(x=5, y=5, height=600, width=990)
#text.pack()
B = Button(frame, text = "Compile and upload ->" + NAME + " " + PORT, command = buttonPress)
B.place(x = 5,y = 615)
root.mainloop()
else:
print("NO BOARD IS CONNECTED!!")
Comments