My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

auto_build.py 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. #!/usr/bin/env python
  2. #######################################
  3. #
  4. # Marlin 3D Printer Firmware
  5. # Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  6. #
  7. # Based on Sprinter and grbl.
  8. # Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, either version 3 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. #######################################
  24. #######################################
  25. #
  26. # Revision: 2.0.1
  27. #
  28. # Description: script to automate PlatformIO builds
  29. # CLI: python auto_build.py build_option
  30. # build_option (required)
  31. # build executes -> platformio run -e target_env
  32. # clean executes -> platformio run --target clean -e target_env
  33. # upload executes -> platformio run --target upload -e target_env
  34. # traceback executes -> platformio run --target upload -e target_env
  35. # program executes -> platformio run --target program -e target_env
  36. # test executes -> platformio test upload -e target_env
  37. # remote executes -> platformio remote run --target upload -e target_env
  38. # debug executes -> platformio debug -e target_env
  39. #
  40. # 'traceback' just uses the debug variant of the target environment if one exists
  41. #
  42. #######################################
  43. #######################################
  44. #
  45. # General program flow
  46. #
  47. # 1. Scans Configuration.h for the motherboard name and Marlin version.
  48. # 2. Scans pins.h for the motherboard.
  49. # returns the CPU(s) and platformio environment(s) used by the motherboard
  50. # 3. If further info is needed then a popup gets it from the user.
  51. # 4. The OUTPUT_WINDOW class creates a window to display the output of the PlatformIO program.
  52. # 5. A thread is created by the OUTPUT_WINDOW class in order to execute the RUN_PIO function.
  53. # 6. The RUN_PIO function uses a subprocess to run the CLI version of PlatformIO.
  54. # 7. The "iter(pio_subprocess.stdout.readline, '')" function is used to stream the output of
  55. # PlatformIO back to the RUN_PIO function.
  56. # 8. Each line returned from PlatformIO is formatted to match the color coding seen in the
  57. # PlatformIO GUI.
  58. # 9. If there is a color change within a line then the line is broken at each color change
  59. # and sent separately.
  60. # 10. Each formatted segment (could be a full line or a split line) is put into the queue
  61. # IO_queue as it arrives from the platformio subprocess.
  62. # 11. The OUTPUT_WINDOW class periodically samples IO_queue. If data is available then it
  63. # is written to the window.
  64. # 12. The window stays open until the user closes it.
  65. # 13. The OUTPUT_WINDOW class continues to execute as long as the window is open. This allows
  66. # copying, saving, scrolling of the window. A right click popup is available.
  67. #
  68. #######################################
  69. from __future__ import print_function
  70. from __future__ import division
  71. import sys
  72. import os
  73. pwd = os.getcwd() # make sure we're executing from the correct directory level
  74. pwd = pwd.replace('\\', '/')
  75. if 0 <= pwd.find('buildroot/share/atom'):
  76. pwd = pwd[ : pwd.find('buildroot/share/atom')]
  77. os.chdir(pwd)
  78. print('pwd: ', pwd)
  79. num_args = len(sys.argv)
  80. if num_args > 1:
  81. build_type = str(sys.argv[1])
  82. else:
  83. print('Please specify build type')
  84. exit()
  85. print('build_type: ', build_type)
  86. print('\nWorking\n')
  87. python_ver = sys.version_info[0] # major version - 2 or 3
  88. if python_ver == 2:
  89. print("python version " + str(sys.version_info[0]) + "." + str(sys.version_info[1]) + "." + str(sys.version_info[2]))
  90. else:
  91. print("python version " + str(sys.version_info[0]))
  92. print("This script only runs under python 2")
  93. exit()
  94. import platform
  95. current_OS = platform.system()
  96. #globals
  97. target_env = ''
  98. board_name = ''
  99. from datetime import datetime, date, time
  100. #########
  101. # Python 2 error messages:
  102. # Can't find a usable init.tcl in the following directories ...
  103. # error "invalid command name "tcl_findLibrary""
  104. #
  105. # Fix for the above errors on my Win10 system:
  106. # search all init.tcl files for the line "package require -exact Tcl" that has the highest 8.5.x number
  107. # copy it into the first directory listed in the error messages
  108. # set the environmental variables TCLLIBPATH and TCL_LIBRARY to the directory where you found the init.tcl file
  109. # reboot
  110. #########
  111. ##########################################################################################
  112. #
  113. # popup to get input from user
  114. #
  115. ##########################################################################################
  116. def get_answer(board_name, cpu_label_txt, cpu_a_txt, cpu_b_txt):
  117. if python_ver == 2:
  118. import Tkinter as tk
  119. else:
  120. import tkinter as tk
  121. def CPU_exit_3(): # forward declare functions
  122. CPU_exit_3_()
  123. def CPU_exit_4():
  124. CPU_exit_4_()
  125. def kill_session():
  126. kill_session_()
  127. root_get_answer = tk.Tk()
  128. root_get_answer.chk_state_1 = 1 # declare variables used by TK and enable
  129. chk_state_1 = 0 # set initial state of check boxes
  130. global get_answer_val
  131. get_answer_val = 2 # return get_answer_val, set default to match chk_state_1 default
  132. l1 = tk.Label(text=board_name,
  133. fg = "light green",
  134. bg = "dark green",
  135. font = "Helvetica 12 bold").grid(row=1)
  136. l2 = tk.Label(text=cpu_label_txt,
  137. fg = "light green",
  138. bg = "dark green",
  139. font = "Helvetica 16 bold italic").grid(row=2)
  140. b4 = tk.Checkbutton(text=cpu_a_txt,
  141. fg = "black",
  142. font = "Times 20 bold ",
  143. variable=chk_state_1, onvalue=1, offvalue=0,
  144. command = CPU_exit_3).grid(row=3)
  145. b5 = tk.Checkbutton(text=cpu_b_txt,
  146. fg = "black",
  147. font = "Times 20 bold ",
  148. variable=chk_state_1, onvalue=0, offvalue=1,
  149. command = CPU_exit_4).grid(row=4) # use same variable but inverted so they will track
  150. b6 = tk.Button(text="CONFIRM",
  151. fg = "blue",
  152. font = "Times 20 bold ",
  153. command = root_get_answer.destroy).grid(row=5, pady=4)
  154. b7 = tk.Button(text="CANCEL",
  155. fg = "red",
  156. font = "Times 12 bold ",
  157. command = kill_session).grid(row=6, pady=4)
  158. def CPU_exit_3_():
  159. global get_answer_val
  160. get_answer_val = 1
  161. def CPU_exit_4_():
  162. global get_answer_val
  163. get_answer_val = 2
  164. def kill_session_():
  165. raise SystemExit(0) # kill everything
  166. root_get_answer.mainloop()
  167. # end - get answer
  168. #
  169. # move custom board definitions from project folder to PlatformIO
  170. #
  171. def resolve_path(path):
  172. import os
  173. # turn the selection into a partial path
  174. if 0 <= path.find('"'):
  175. path = path[ path.find('"') : ]
  176. if 0 <= path.find(', line '):
  177. path = path.replace(', line ', ':')
  178. path = path.replace('"', '')
  179. #get line and column numbers
  180. line_num = 1
  181. column_num = 1
  182. line_start = path.find(':', 2) # use 2 here so don't eat Windows full path
  183. column_start = path.find(':', line_start + 1)
  184. if column_start == -1:
  185. column_start = len(path)
  186. column_end = path.find(':', column_start + 1)
  187. if column_end == -1:
  188. column_end = len(path)
  189. if 0 <= line_start:
  190. line_num = path[ line_start + 1 : column_start]
  191. if line_num == '':
  192. line_num = 1
  193. if not(column_start == column_end):
  194. column_num = path[ column_start + 1 : column_end]
  195. if column_num == '':
  196. column_num = 0
  197. index_end = path.find(',')
  198. if 0 <= index_end:
  199. path = path[ : index_end] # delete comma and anything after
  200. index_end = path.find(':', 2)
  201. if 0 <= index_end:
  202. path = path[ : path.find(':', 2)] # delete the line number and anything after
  203. path = path.replace('\\','/')
  204. if 1 == path.find(':') and current_OS == 'Windows':
  205. return path, line_num, column_num # found a full path - no need for further processing
  206. elif 0 == path.find('/') and (current_OS == 'Linux' or current_OS == 'Darwin'):
  207. return path, line_num, column_num # found a full path - no need for further processing
  208. else:
  209. # resolve as many '../' as we can
  210. while 0 <= path.find('../'):
  211. end = path.find('../') - 1
  212. start = path.find('/')
  213. while 0 <= path.find('/',start) and end > path.find('/',start):
  214. start = path.find('/',start) + 1
  215. path = path[0:start] + path[end + 4: ]
  216. # this is an alternative to the above - it just deletes the '../' section
  217. # start_temp = path.find('../')
  218. # while 0 <= path.find('../',start_temp):
  219. # start = path.find('../',start_temp)
  220. # start_temp = start + 1
  221. # if 0 <= start:
  222. # path = path[start + 2 : ]
  223. start = path.find('/')
  224. if not(0 == start): # make sure path starts with '/'
  225. while 0 == path.find(' '): # eat any spaces at the beginning
  226. path = path[ 1 : ]
  227. path = '/' + path
  228. if current_OS == 'Windows':
  229. search_path = path.replace('/', '\\') # os.walk uses '\' in Windows
  230. else:
  231. search_path = path
  232. start_path = os.path.abspath('')
  233. # search project directory for the selection
  234. found = False
  235. full_path = ''
  236. for root, directories, filenames in os.walk(start_path):
  237. for filename in filenames:
  238. if 0 <= root.find('.git'): # don't bother looking in this directory
  239. break
  240. full_path = os.path.join(root,filename)
  241. if 0 <= full_path.find(search_path):
  242. found = True
  243. break
  244. if found:
  245. break
  246. return full_path, line_num, column_num
  247. # end - resolve_path
  248. #
  249. # Opens the file in the preferred editor at the line & column number
  250. # If the preferred editor isn't already running then it tries the next.
  251. # If none are open then the system default is used.
  252. #
  253. # Editor order:
  254. # 1. Notepad++ (Windows only)
  255. # 2. Sublime Text
  256. # 3. Atom
  257. # 4. System default (opens at line 1, column 1 only)
  258. #
  259. def open_file(path):
  260. import subprocess
  261. file_path, line_num, column_num = resolve_path(path)
  262. if file_path == '' :
  263. return
  264. if current_OS == 'Windows':
  265. editor_note = subprocess.check_output('wmic process where "name=' + "'notepad++.exe'" + '" get ExecutablePath')
  266. editor_sublime = subprocess.check_output('wmic process where "name=' + "'sublime_text.exe'" + '" get ExecutablePath')
  267. editor_atom = subprocess.check_output('wmic process where "name=' + "'atom.exe'" + '" get ExecutablePath')
  268. if 0 <= editor_note.find('notepad++.exe'):
  269. start = editor_note.find('\n') + 1
  270. end = editor_note.find('\n',start + 5) -4
  271. editor_note = editor_note[ start : end]
  272. command = file_path , ' -n' + str(line_num) , ' -c' + str(column_num)
  273. subprocess.Popen([editor_note, command])
  274. elif 0 <= editor_sublime.find('sublime_text.exe'):
  275. start = editor_sublime.find('\n') + 1
  276. end = editor_sublime.find('\n',start + 5) -4
  277. editor_sublime = editor_sublime[ start : end]
  278. command = file_path + ':' + line_num + ':' + column_num
  279. subprocess.Popen([editor_sublime, command])
  280. elif 0 <= editor_atom.find('atom.exe'):
  281. start = editor_atom.find('\n') + 1
  282. end = editor_atom.find('\n',start + 5) -4
  283. editor_atom = editor_atom[ start : end]
  284. command = file_path + ':' + str(line_num) + ':' + str(column_num)
  285. subprocess.Popen([editor_atom, command])
  286. else:
  287. os.startfile(resolve_path(path)) # open file with default app
  288. elif current_OS == 'Linux':
  289. command = file_path + ':' + str(line_num) + ':' + str(column_num)
  290. index_end = command.find(',')
  291. if 0 <= index_end:
  292. command = command[ : index_end] # sometimes a comma magically appears, don't want it
  293. running_apps = subprocess.Popen('ps ax -o cmd', stdout=subprocess.PIPE, shell=True)
  294. (output, err) = running_apps.communicate()
  295. temp = output.split('\n')
  296. def find_editor_linux(name, search_obj):
  297. for line in search_obj:
  298. if 0 <= line.find(name):
  299. path = line
  300. return True, path
  301. return False , ''
  302. (success_sublime, editor_path_sublime) = find_editor_linux('sublime_text',temp)
  303. (success_atom, editor_path_atom) = find_editor_linux('atom',temp)
  304. if success_sublime:
  305. subprocess.Popen([editor_path_sublime, command])
  306. elif success_atom:
  307. subprocess.Popen([editor_path_atom, command])
  308. else:
  309. os.system('xdg-open ' + file_path )
  310. elif current_OS == 'Darwin': # MAC
  311. command = file_path + ':' + str(line_num) + ':' + str(column_num)
  312. index_end = command.find(',')
  313. if 0 <= index_end:
  314. command = command[ : index_end] # sometimes a comma magically appears, don't want it
  315. running_apps = subprocess.Popen('ps axwww -o command', stdout=subprocess.PIPE, shell=True)
  316. (output, err) = running_apps.communicate()
  317. temp = output.split('\n')
  318. def find_editor_mac(name, search_obj):
  319. for line in search_obj:
  320. if 0 <= line.find(name):
  321. path = line
  322. if 0 <= path.find('-psn'):
  323. path = path[ : path.find('-psn') - 1 ]
  324. return True, path
  325. return False , ''
  326. (success_sublime, editor_path_sublime) = find_editor_mac('Sublime',temp)
  327. (success_atom, editor_path_atom) = find_editor_mac('Atom',temp)
  328. if success_sublime:
  329. subprocess.Popen([editor_path_sublime, command])
  330. elif success_atom:
  331. subprocess.Popen([editor_path_atom, command])
  332. else:
  333. os.system('open ' + file_path )
  334. # end - open_file
  335. # gets the last build environment
  336. def get_build_last():
  337. env_last = ''
  338. DIR_PWD = os.listdir('.')
  339. if '.pioenvs' in DIR_PWD:
  340. date_last = 0.0
  341. DIR__pioenvs = os.listdir('.pioenvs')
  342. for name in DIR__pioenvs:
  343. if 0 <= name.find('.') or 0 <= name.find('-'): # skip files in listing
  344. continue
  345. DIR_temp = os.listdir('.pioenvs/' + name)
  346. for names_temp in DIR_temp:
  347. if 0 == names_temp.find('firmware.'):
  348. date_temp = os.path.getmtime('.pioenvs/' + name + '/' + names_temp)
  349. if date_temp > date_last:
  350. date_last = date_temp
  351. env_last = name
  352. return env_last
  353. # gets the board being built from the Configuration.h file
  354. # returns: board name, major version of Marlin being used (1 or 2)
  355. def get_board_name():
  356. board_name = ''
  357. # get board name
  358. with open('Marlin/Configuration.h', 'r') as myfile:
  359. Configuration_h = myfile.read()
  360. Configuration_h = Configuration_h.split('\n')
  361. Marlin_ver = 0 # set version to invalid number
  362. for lines in Configuration_h:
  363. if 0 == lines.find('#define CONFIGURATION_H_VERSION 01'):
  364. Marlin_ver = 1
  365. if 0 == lines.find('#define CONFIGURATION_H_VERSION 02'):
  366. Marlin_ver = 2
  367. board = lines.find(' BOARD_') + 1
  368. motherboard = lines.find(' MOTHERBOARD ') + 1
  369. define = lines.find('#define ')
  370. comment = lines.find('//')
  371. if (comment == -1 or comment > board) and \
  372. board > motherboard and \
  373. motherboard > define and \
  374. define >= 0 :
  375. spaces = lines.find(' ', board) # find the end of the board substring
  376. if spaces == -1:
  377. board_name = lines[board : ]
  378. else:
  379. board_name = lines[board : spaces]
  380. break
  381. return board_name, Marlin_ver
  382. # extract first environment name it finds after the start position
  383. # returns: environment name and position to start the next search from
  384. def get_env_from_line(line, start_position):
  385. env = ''
  386. next_position = -1
  387. env_position = line.find('env:', start_position)
  388. if 0 < env_position:
  389. next_position = line.find(' ', env_position + 4)
  390. if 0 < next_position:
  391. env = line[env_position + 4 : next_position]
  392. else:
  393. env = line[env_position + 4 : ] # at the end of the line
  394. return env, next_position
  395. #scans pins.h for board name and returns the environment(s) it finds
  396. def get_starting_env(board_name_full, version):
  397. # get environment starting point
  398. if version == 1:
  399. path = 'Marlin/pins.h'
  400. if version == 2:
  401. path = 'Marlin/src/pins/pins.h'
  402. with open(path, 'r') as myfile:
  403. pins_h = myfile.read()
  404. env_A = ''
  405. env_B = ''
  406. env_C = ''
  407. board_name = board_name_full[ 6 : ] # only use the part after "BOARD_" since we're searching the pins.h file
  408. pins_h = pins_h.split('\n')
  409. environment = ''
  410. board_line = ''
  411. cpu_A = ''
  412. cpu_B = ''
  413. i = 0
  414. list_start_found = False
  415. for lines in pins_h:
  416. i = i + 1 # i is always one ahead of the index into pins_h
  417. if 0 < lines.find("Unknown MOTHERBOARD value set in Configuration.h"):
  418. break # no more
  419. if 0 < lines.find('1280'):
  420. list_start_found = True
  421. if list_start_found == False: # skip lines until find start of CPU list
  422. continue
  423. board = lines.find(board_name)
  424. comment_start = lines.find('// ')
  425. cpu_A_loc = comment_start
  426. cpu_B_loc = 0
  427. if board > 0: # need to look at the next line for environment info
  428. cpu_line = pins_h[i]
  429. comment_start = cpu_line.find('// ')
  430. env_A, next_position = get_env_from_line(cpu_line, comment_start) # get name of environment & start of search for next
  431. env_B, next_position = get_env_from_line(cpu_line, next_position) # get next environment, if it exists
  432. env_C, next_position = get_env_from_line(cpu_line, next_position) # get next environment, if it exists
  433. break
  434. return env_A, env_B, env_C
  435. # scans input string for CPUs that the users may need to select from
  436. # returns: CPU name
  437. def get_CPU_name(environment):
  438. CPU_list = ('1280', '2560','644', '1284', 'LPC1768', 'DUE')
  439. CPU_name = ''
  440. for CPU in CPU_list:
  441. if 0 < environment.find(CPU):
  442. return CPU
  443. # get environment to be used for the build
  444. # returns: environment
  445. def get_env(board_name, ver_Marlin):
  446. def no_environment():
  447. print('ERROR - no environment for this board')
  448. print(board_name)
  449. raise SystemExit(0) # no environment so quit
  450. def invalid_board():
  451. print('ERROR - invalid board')
  452. print(board_name)
  453. raise SystemExit(0) # quit if unable to find board
  454. CPU_question = ( ('1280', '2560', " 1280 or 2560 CPU? "), ('644', '1284', " 644 or 1284 CPU? ") )
  455. if 0 < board_name.find('MELZI') :
  456. get_answer(' ' + board_name + ' ', " Which flavor of Melzi? ", "Melzi (Optiboot bootloader)", "Melzi ")
  457. if 1 == get_answer_val:
  458. target_env = 'melzi_optiboot'
  459. else:
  460. target_env = 'melzi'
  461. else:
  462. env_A, env_B, env_C = get_starting_env(board_name, ver_Marlin)
  463. if env_A == '':
  464. no_environment()
  465. if env_B == '':
  466. return env_A # only one environment so finished
  467. CPU_A = get_CPU_name(env_A)
  468. CPU_B = get_CPU_name(env_B)
  469. for item in CPU_question:
  470. if CPU_A == item[0]:
  471. get_answer(' ' + board_name + ' ', item[2], item[0], item[1])
  472. if 2 == get_answer_val:
  473. target_env = env_B
  474. else:
  475. target_env = env_A
  476. return target_env
  477. if env_A == 'LPC1768':
  478. if build_type == 'traceback' or (build_type == 'clean' and get_build_last() == 'LPC1768_debug_and_upload'):
  479. target_env = 'LPC1768_debug_and_upload'
  480. else:
  481. target_env = 'LPC1768'
  482. elif env_A == 'DUE':
  483. target_env = 'DUE'
  484. if build_type == 'traceback' or (build_type == 'clean' and get_build_last() == 'DUE_debug'):
  485. target_env = 'DUE_debug'
  486. elif env_B == 'DUE_USB':
  487. get_answer(' ' + board_name + ' ', " DUE: need download port ", "USB (native USB) port", "Programming port ")
  488. if 1 == get_answer_val:
  489. target_env = 'DUE_USB'
  490. else:
  491. target_env = 'DUE'
  492. else:
  493. invalid_board()
  494. if build_type == 'traceback' and not(target_env == 'LPC1768_debug_and_upload' or target_env == 'DUE_debug') and Marlin_ver == 2:
  495. print("ERROR - this board isn't setup for traceback")
  496. print('board_name: ', board_name)
  497. print('target_env: ', target_env)
  498. raise SystemExit(0)
  499. return target_env
  500. # end - get_env
  501. # puts screen text into queue so that the parent thread can fetch the data from this thread
  502. import Queue
  503. IO_queue = Queue.Queue()
  504. PIO_queue = Queue.Queue()
  505. def write_to_screen_queue(text, format_tag = 'normal'):
  506. double_in = [text, format_tag]
  507. IO_queue.put(double_in, block = False)
  508. #
  509. # send one line to the terminal screen with syntax highlighting
  510. #
  511. # input: unformatted text, flags from previous run
  512. # returns: formatted text ready to go to the terminal, flags from this run
  513. #
  514. # This routine remembers the status from call to call because previous
  515. # lines can affect how the current line is highlighted
  516. #
  517. # 'static' variables - init here and then keep updating them from within print_line
  518. warning = False
  519. warning_FROM = False
  520. error = False
  521. standard = True
  522. prev_line_COM = False
  523. next_line_warning = False
  524. warning_continue = False
  525. line_counter = 0
  526. def line_print(line_input):
  527. global warning
  528. global warning_FROM
  529. global error
  530. global standard
  531. global prev_line_COM
  532. global next_line_warning
  533. global warning_continue
  534. global line_counter
  535. # all '0' elements must precede all '1' elements or they'll be skipped
  536. platformio_highlights = [
  537. ['Environment', 0, 'highlight_blue'],
  538. ['[SKIP]', 1, 'warning'],
  539. ['[ERROR]', 1, 'error'],
  540. ['[SUCCESS]', 1, 'highlight_green']
  541. ]
  542. def write_to_screen_with_replace(text, highlights): # search for highlights & split line accordingly
  543. did_something = False
  544. for highlight in highlights:
  545. found = text.find(highlight[0])
  546. if did_something == True:
  547. break
  548. if found >= 0 :
  549. did_something = True
  550. if 0 == highlight[1]:
  551. found_1 = text.find(' ')
  552. found_tab = text.find('\t')
  553. if found_1 < 0 or found_1 > found_tab:
  554. found_1 = found_tab
  555. write_to_screen_queue(text[ : found_1 + 1 ])
  556. for highlight_2 in highlights:
  557. if highlight[0] == highlight_2[0] :
  558. continue
  559. found = text.find(highlight_2[0])
  560. if found >= 0 :
  561. found_space = text.find(' ', found_1 + 1)
  562. found_tab = text.find('\t', found_1 + 1)
  563. if found_space < 0 or found_space > found_tab:
  564. found_space = found_tab
  565. found_right = text.find(']', found + 1)
  566. write_to_screen_queue(text[found_1 + 1 : found_space + 1 ], highlight[2])
  567. write_to_screen_queue(text[found_space + 1 : found + 1 ])
  568. write_to_screen_queue(text[found + 1 : found_right], highlight_2[2])
  569. write_to_screen_queue(text[found_right : ] + '\n')
  570. break
  571. break
  572. if 1 == highlight[1]:
  573. found_right = text.find(']', found + 1)
  574. write_to_screen_queue(text[ : found + 1 ])
  575. write_to_screen_queue(text[found + 1 : found_right ], highlight[2])
  576. write_to_screen_queue(text[found_right : ] + '\n')
  577. break
  578. if did_something == False:
  579. r_loc = text.find('\r') + 1
  580. if r_loc > 0 and r_loc < len(text): # need to split this line
  581. text = text.split('\r')
  582. for line in text:
  583. write_to_screen_queue(line + '\n')
  584. else:
  585. write_to_screen_queue(text + '\n')
  586. # end - write_to_screen_with_replace
  587. # scan the line
  588. line_counter = line_counter + 1
  589. max_search = len(line_input)
  590. if max_search > 3 :
  591. max_search = 3
  592. beginning = line_input[:max_search]
  593. # set flags
  594. if 0 < line_input.find(': warning: '): # start of warning block
  595. warning = True
  596. warning_FROM = False
  597. error = False
  598. standard = False
  599. prev_line_COM = False
  600. prev_line_COM = False
  601. warning_continue = True
  602. if 0 < line_input.find('Thank you') or 0 < line_input.find('SUMMARY') :
  603. warning = False #standard line found
  604. warning_FROM = False
  605. error = False
  606. standard = True
  607. prev_line_COM = False
  608. warning_continue = False
  609. elif beginning == 'War' or \
  610. beginning == '#er' or \
  611. beginning == 'In ' or \
  612. (beginning != 'Com' and prev_line_COM == True and not(beginning == 'Arc' or beginning == 'Lin' or beginning == 'Ind') or \
  613. next_line_warning == True):
  614. warning = True #warning found
  615. warning_FROM = False
  616. error = False
  617. standard = False
  618. prev_line_COM = False
  619. elif beginning == 'Com' or \
  620. beginning == 'Ver' or \
  621. beginning == ' [E' or \
  622. beginning == 'Rem' or \
  623. beginning == 'Bui' or \
  624. beginning == 'Ind' or \
  625. beginning == 'PLA':
  626. warning = False #standard line found
  627. warning_FROM = False
  628. error = False
  629. standard = True
  630. prev_line_COM = False
  631. warning_continue = False
  632. elif beginning == '***':
  633. warning = False # error found
  634. warning_FROM = False
  635. error = True
  636. standard = False
  637. prev_line_COM = False
  638. elif 0 < line_input.find(': error:') or \
  639. 0 < line_input.find(': fatal error:'): # start of warning /error block
  640. warning = False # error found
  641. warning_FROM = False
  642. error = True
  643. standard = False
  644. prev_line_COM = False
  645. warning_continue = True
  646. elif beginning == 'fro' and warning == True or \
  647. beginning == '.pi' : # start of warning /error block
  648. warning_FROM = True
  649. prev_line_COM = False
  650. warning_continue = True
  651. elif warning_continue == True:
  652. warning = True
  653. warning_FROM = False # keep the warning status going until find a standard line or an error
  654. error = False
  655. standard = False
  656. prev_line_COM = False
  657. warning_continue = True
  658. else:
  659. warning = False # unknown so assume standard line
  660. warning_FROM = False
  661. error = False
  662. standard = True
  663. prev_line_COM = False
  664. warning_continue = False
  665. if beginning == 'Com':
  666. prev_line_COM = True
  667. # print based on flags
  668. if standard == True:
  669. write_to_screen_with_replace(line_input, platformio_highlights) #print white on black with substitutions
  670. if warning == True:
  671. write_to_screen_queue(line_input + '\n', 'warning')
  672. if error == True:
  673. write_to_screen_queue(line_input + '\n', 'error')
  674. # end - line_print
  675. def run_PIO(dummy):
  676. ##########################################################################
  677. # #
  678. # run Platformio #
  679. # #
  680. ##########################################################################
  681. # build platformio run -e target_env
  682. # clean platformio run --target clean -e target_env
  683. # upload platformio run --target upload -e target_env
  684. # traceback platformio run --target upload -e target_env
  685. # program platformio run --target program -e target_env
  686. # test platformio test upload -e target_env
  687. # remote platformio remote run --target upload -e target_env
  688. # debug platformio debug -e target_env
  689. global build_type
  690. global target_env
  691. global board_name
  692. print('build_type: ', build_type)
  693. import subprocess
  694. import sys
  695. print('starting platformio')
  696. if build_type == 'build':
  697. # platformio run -e target_env
  698. # combine stdout & stderr so all compile messages are included
  699. pio_subprocess = subprocess.Popen(['platformio', 'run', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  700. elif build_type == 'clean':
  701. # platformio run --target clean -e target_env
  702. # combine stdout & stderr so all compile messages are included
  703. pio_subprocess = subprocess.Popen(['platformio', 'run', '--target', 'clean', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  704. elif build_type == 'upload':
  705. # platformio run --target upload -e target_env
  706. # combine stdout & stderr so all compile messages are included
  707. pio_subprocess = subprocess.Popen(['platformio', 'run', '--target', 'upload', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  708. elif build_type == 'traceback':
  709. # platformio run --target upload -e target_env - select the debug environment if there is one
  710. # combine stdout & stderr so all compile messages are included
  711. pio_subprocess = subprocess.Popen(['platformio', 'run', '--target', 'upload', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  712. elif build_type == 'program':
  713. # platformio run --target program -e target_env
  714. # combine stdout & stderr so all compile messages are included
  715. pio_subprocess = subprocess.Popen(['platformio', 'run', '--target', 'program', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  716. elif build_type == 'test':
  717. #platformio test upload -e target_env
  718. # combine stdout & stderr so all compile messages are included
  719. pio_subprocess = subprocess.Popen(['platformio', 'test', 'upload', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  720. elif build_type == 'remote':
  721. # platformio remote run --target upload -e target_env
  722. # combine stdout & stderr so all compile messages are included
  723. pio_subprocess = subprocess.Popen(['platformio', 'remote', 'run', '--target', 'program', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  724. elif build_type == 'debug':
  725. # platformio debug -e target_env
  726. # combine stdout & stderr so all compile messages are included
  727. pio_subprocess = subprocess.Popen(['platformio', 'debug', '-e', target_env], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  728. else:
  729. print('ERROR - unknown build type: ', build_type)
  730. raise SystemExit(0) # kill everything
  731. # stream output from subprocess and split it into lines
  732. for line in iter(pio_subprocess.stdout.readline, ''):
  733. line_print(line.replace('\n', ''))
  734. # append info used to run PlatformIO
  735. write_to_screen_queue('\nBoard name: ' + board_name + '\n') # put build info at the bottom of the screen
  736. write_to_screen_queue('Build type: ' + build_type + '\n')
  737. write_to_screen_queue('Environment used: ' + target_env + '\n')
  738. write_to_screen_queue(str(datetime.now()) + '\n')
  739. # end - run_PIO
  740. ########################################################################
  741. import time
  742. import threading
  743. import Tkinter as tk
  744. import ttk
  745. import Queue
  746. import subprocess
  747. import sys
  748. que = Queue.Queue()
  749. #IO_queue = Queue.Queue()
  750. from Tkinter import Tk, Frame, Text, Scrollbar, Menu
  751. from tkMessageBox import askokcancel
  752. import tkFileDialog
  753. from tkMessageBox import askokcancel
  754. import tkFileDialog
  755. class output_window(Text):
  756. # based on Super Text
  757. global continue_updates
  758. continue_updates = True
  759. global search_position
  760. search_position = '' # start with invalid search position
  761. global error_found
  762. error_found = False # are there any errors?
  763. def __init__(self):
  764. self.root = tk.Tk()
  765. self.frame = tk.Frame(self.root)
  766. self.frame.pack(fill='both', expand=True)
  767. # text widget
  768. #self.text = tk.Text(self.frame, borderwidth=3, relief="sunken")
  769. Text.__init__(self, self.frame, borderwidth=3, relief="sunken")
  770. self.config(tabs=(400,)) # configure Text widget tab stops
  771. self.config(background = 'black', foreground = 'white', font= ("consolas", 12), wrap = 'word', undo = 'True')
  772. # self.config(background = 'black', foreground = 'white', font= ("consolas", 12), wrap = 'none', undo = 'True')
  773. self.config(height = 24, width = 100)
  774. self.config(insertbackground = 'pale green') # keyboard insertion point
  775. self.pack(side='left', fill='both', expand=True)
  776. self.tag_config('normal', foreground = 'white')
  777. self.tag_config('warning', foreground = 'yellow' )
  778. self.tag_config('error', foreground = 'red')
  779. self.tag_config('highlight_green', foreground = 'green')
  780. self.tag_config('highlight_blue', foreground = 'cyan')
  781. self.tag_config('error_highlight_inactive', background = 'dim gray')
  782. self.tag_config('error_highlight_active', background = 'light grey')
  783. self.bind_class("Text","<Control-a>", self.select_all) # required in windows, works in others
  784. self.bind_all("<Control-Shift-E>", self.scroll_errors)
  785. self.bind_class("<Control-Shift-R>", self.rebuild)
  786. # scrollbar
  787. scrb = tk.Scrollbar(self.frame, orient='vertical', command=self.yview)
  788. self.config(yscrollcommand=scrb.set)
  789. scrb.pack(side='right', fill='y')
  790. # self.scrb_Y = tk.Scrollbar(self.frame, orient='vertical', command=self.yview)
  791. # self.scrb_Y.config(yscrollcommand=self.scrb_Y.set)
  792. # self.scrb_Y.pack(side='right', fill='y')
  793. #
  794. # self.scrb_X = tk.Scrollbar(self.frame, orient='horizontal', command=self.xview)
  795. # self.scrb_X.config(xscrollcommand=self.scrb_X.set)
  796. # self.scrb_X.pack(side='bottom', fill='x')
  797. # scrb_X = tk.Scrollbar(self, orient=tk.HORIZONTAL, command=self.xview) # tk.HORIZONTAL now have a horizsontal scroll bar BUT... shrinks it to a postage stamp and hides far right behind the vertical scroll bar
  798. # self.config(xscrollcommand=scrb_X.set)
  799. # scrb_X.pack(side='bottom', fill='x')
  800. #
  801. # scrb= tk.Scrollbar(self, orient='vertical', command=self.yview)
  802. # self.config(yscrollcommand=scrb.set)
  803. # scrb.pack(side='right', fill='y')
  804. # self.config(height = 240, width = 1000) # didn't get the size baCK TO NORMAL
  805. # self.pack(side='left', fill='both', expand=True) # didn't get the size baCK TO NORMAL
  806. # pop-up menu
  807. self.popup = tk.Menu(self, tearoff=0)
  808. self.popup.add_command(label='Copy', command=self._copy)
  809. self.popup.add_command(label='Paste', command=self._paste)
  810. self.popup.add_separator()
  811. self.popup.add_command(label='Cut', command=self._cut)
  812. self.popup.add_separator()
  813. self.popup.add_command(label='Select All', command=self._select_all)
  814. self.popup.add_command(label='Clear All', command=self._clear_all)
  815. self.popup.add_separator()
  816. self.popup.add_command(label='Save As', command=self._file_save_as)
  817. self.popup.add_separator()
  818. # self.popup.add_command(label='Repeat Build(CTL-shift-r)', command=self._rebuild)
  819. self.popup.add_command(label='Repeat Build', command=self._rebuild)
  820. self.popup.add_separator()
  821. self.popup.add_command(label='Scroll Errors (CTL-shift-e)', command=self._scroll_errors)
  822. self.popup.add_separator()
  823. self.popup.add_command(label='Open File at Cursor', command=self._open_selected_file)
  824. if current_OS == 'Darwin': # MAC
  825. self.bind('<Button-2>', self._show_popup) # macOS only
  826. else:
  827. self.bind('<Button-3>', self._show_popup) # Windows & Linux
  828. # threading & subprocess section
  829. def start_thread(self, ):
  830. global continue_updates
  831. # create then start a secondary thread to run an arbitrary function
  832. # must have at least one argument
  833. self.secondary_thread = threading.Thread(target = lambda q, arg1: q.put(run_PIO(arg1)), args=(que, ''))
  834. self.secondary_thread.start()
  835. continue_updates = True
  836. # check the Queue in 50ms
  837. self.root.after(50, self.check_thread)
  838. self.root.after(50, self.update)
  839. def check_thread(self): # wait for user to kill the window
  840. global continue_updates
  841. if continue_updates == True:
  842. self.root.after(10, self.check_thread)
  843. def update(self):
  844. global continue_updates
  845. if continue_updates == True:
  846. self.root.after(10, self.update)#method is called every 50ms
  847. temp_text = ['0','0']
  848. if IO_queue.empty():
  849. if not(self.secondary_thread.is_alive()):
  850. continue_updates = False # queue is exhausted and thread is dead so no need for further updates
  851. else:
  852. try:
  853. temp_text = IO_queue.get(block = False)
  854. except Queue.Empty:
  855. continue_updates = False # queue is exhausted so no need for further updates
  856. else:
  857. self.insert('end', temp_text[0], temp_text[1])
  858. self.see("end") # make the last line visible (scroll text off the top)
  859. # text editing section
  860. def _scroll_errors(self):
  861. global search_position
  862. global error_found
  863. if search_position == '': # first time so highlight all errors
  864. countVar = tk.IntVar()
  865. search_position = '1.0'
  866. search_count = 0
  867. while not(search_position == '') and search_count < 100:
  868. search_position = self.search("error", search_position, stopindex="end", count=countVar, nocase=1)
  869. search_count = search_count + 1
  870. if not(search_position == ''):
  871. error_found = True
  872. end_pos = '{}+{}c'.format(search_position, 5)
  873. self.tag_add("error_highlight_inactive", search_position, end_pos)
  874. search_position = '{}+{}c'.format(search_position, 1) # point to the next character for new search
  875. else:
  876. break
  877. if error_found:
  878. if search_position == '':
  879. search_position = self.search("error", '1.0', stopindex="end", nocase=1) # new search
  880. else: # remove active highlight
  881. end_pos = '{}+{}c'.format(search_position, 5)
  882. start_pos = '{}+{}c'.format(search_position, -1)
  883. self.tag_remove("error_highlight_active", start_pos, end_pos)
  884. search_position = self.search("error", search_position, stopindex="end", nocase=1) # finds first occurrence AGAIN on the first time through
  885. if search_position == "": # wrap around
  886. search_position = self.search("error", '1.0', stopindex="end", nocase=1)
  887. end_pos = '{}+{}c'.format(search_position, 5)
  888. self.tag_add("error_highlight_active", search_position, end_pos) # add active highlight
  889. self.see(search_position)
  890. search_position = '{}+{}c'.format(search_position, 1) # point to the next character for new search
  891. def scroll_errors(self, event):
  892. self._scroll_errors()
  893. def _rebuild(self):
  894. #global board_name
  895. #global Marlin_ver
  896. #global target_env
  897. #board_name, Marlin_ver = get_board_name()
  898. #target_env = get_env(board_name, Marlin_ver)
  899. self.start_thread()
  900. def rebuild(self, event):
  901. print("event happened")
  902. self._rebuild()
  903. def _open_selected_file(self):
  904. current_line = self.index('insert')
  905. line_start = current_line[ : current_line.find('.')] + '.0'
  906. line_end = current_line[ : current_line.find('.')] + '.200'
  907. self.mark_set("path_start", line_start)
  908. self.mark_set("path_end", line_end)
  909. path = self.get("path_start", "path_end")
  910. from_loc = path.find('from ')
  911. colon_loc = path.find(': ')
  912. if 0 <= from_loc and ((colon_loc == -1) or (from_loc < colon_loc)) :
  913. path = path [ from_loc + 5 : ]
  914. if 0 <= colon_loc:
  915. path = path [ : colon_loc ]
  916. if 0 <= path.find('\\') or 0 <= path.find('/'): # make sure it really contains a path
  917. open_file(path)
  918. def _file_save_as(self):
  919. self.filename = tkFileDialog.asksaveasfilename(defaultextension = '.txt')
  920. f = open(self.filename, 'w')
  921. f.write(self.get('1.0', 'end'))
  922. f.close()
  923. def copy(self, event):
  924. try:
  925. selection = self.get(*self.tag_ranges('sel'))
  926. self.clipboard_clear()
  927. self.clipboard_append(selection)
  928. except TypeError:
  929. pass
  930. def cut(self, event):
  931. try:
  932. selection = self.get(*self.tag_ranges('sel'))
  933. self.clipboard_clear()
  934. self.clipboard_append(selection)
  935. self.delete(*self.tag_ranges('sel'))
  936. except TypeError:
  937. pass
  938. def _show_popup(self, event):
  939. '''right-click popup menu'''
  940. if self.root.focus_get() != self:
  941. self.root.focus_set()
  942. try:
  943. self.popup.tk_popup(event.x_root, event.y_root, 0)
  944. finally:
  945. self.popup.grab_release()
  946. def _cut(self):
  947. try:
  948. selection = self.get(*self.tag_ranges('sel'))
  949. self.clipboard_clear()
  950. self.clipboard_append(selection)
  951. self.delete(*self.tag_ranges('sel'))
  952. except TypeError:
  953. pass
  954. def cut(self, event):
  955. self._cut()
  956. def _copy(self):
  957. try:
  958. selection = self.get(*self.tag_ranges('sel'))
  959. self.clipboard_clear()
  960. self.clipboard_append(selection)
  961. except TypeError:
  962. pass
  963. def copy(self, event):
  964. self._copy()
  965. def _paste(self):
  966. self.insert('insert', self.selection_get(selection='CLIPBOARD'))
  967. def _select_all(self):
  968. self.tag_add('sel', '1.0', 'end')
  969. def select_all(self, event):
  970. self.tag_add('sel', '1.0', 'end')
  971. def _clear_all(self):
  972. '''erases all text'''
  973. isok = askokcancel('Clear All', 'Erase all text?', frame=self,
  974. default='ok')
  975. if isok:
  976. self.delete('1.0', 'end')
  977. # end - output_window
  978. def main():
  979. ##########################################################################
  980. # #
  981. # main program #
  982. # #
  983. ##########################################################################
  984. global build_type
  985. global target_env
  986. global board_name
  987. board_name, Marlin_ver = get_board_name()
  988. target_env = get_env(board_name, Marlin_ver)
  989. os.environ["BUILD_TYPE"] = build_type # let sub processes know what is happening
  990. os.environ["TARGET_ENV"] = target_env
  991. os.environ["BOARD_NAME"] = board_name
  992. auto_build = output_window()
  993. auto_build.start_thread() # executes the "run_PIO" function
  994. auto_build.root.mainloop()
  995. if __name__ == '__main__':
  996. main()