Browse Source

🧑‍💻 Use spaces indent for Python

Scott Lahteine 1 year ago
parent
commit
93139e757c

+ 8
- 8
Marlin/src/HAL/DUE/upload_extra_script.py View File

@@ -6,14 +6,14 @@
6 6
 #
7 7
 import pioutil
8 8
 if pioutil.is_pio_build():
9
-	import platform
10
-	current_OS = platform.system()
9
+    import platform
10
+    current_OS = platform.system()
11 11
 
12
-	if current_OS == 'Windows':
12
+    if current_OS == 'Windows':
13 13
 
14
-		Import("env")
14
+        Import("env")
15 15
 
16
-		# Use bossac.exe on Windows
17
-		env.Replace(
18
-			UPLOADCMD="bossac --info --unlock --write --verify --reset --erase -U false --boot $SOURCE"
19
-		)
16
+        # Use bossac.exe on Windows
17
+        env.Replace(
18
+            UPLOADCMD="bossac --info --unlock --write --verify --reset --erase -U false --boot $SOURCE"
19
+        )

+ 10
- 10
buildroot/share/PlatformIO/scripts/STM32F103RC_MEEB_3DP.py View File

@@ -4,16 +4,16 @@
4 4
 import pioutil
5 5
 if pioutil.is_pio_build():
6 6
 
7
-	Import("env", "projenv")
7
+    Import("env", "projenv")
8 8
 
9
-	flash_size = 0
10
-	vect_tab_addr = 0
9
+    flash_size = 0
10
+    vect_tab_addr = 0
11 11
 
12
-	for define in env['CPPDEFINES']:
13
-		if define[0] == "VECT_TAB_ADDR":
14
-			vect_tab_addr = define[1]
15
-		if define[0] == "STM32_FLASH_SIZE":
16
-			flash_size = define[1]
12
+    for define in env['CPPDEFINES']:
13
+        if define[0] == "VECT_TAB_ADDR":
14
+            vect_tab_addr = define[1]
15
+        if define[0] == "STM32_FLASH_SIZE":
16
+            flash_size = define[1]
17 17
 
18
-	print('Use the {0:s} address as the marlin app entry point.'.format(vect_tab_addr))
19
-	print('Use the {0:d}KB flash version of stm32f103rct6 chip.'.format(flash_size))
18
+    print('Use the {0:s} address as the marlin app entry point.'.format(vect_tab_addr))
19
+    print('Use the {0:d}KB flash version of stm32f103rct6 chip.'.format(flash_size))

+ 19
- 19
buildroot/share/PlatformIO/scripts/STM32F103RC_fysetc.py View File

@@ -3,25 +3,25 @@
3 3
 #
4 4
 import pioutil
5 5
 if pioutil.is_pio_build():
6
-	from os.path import join
7
-	from os.path import expandvars
8
-	Import("env")
6
+    from os.path import join
7
+    from os.path import expandvars
8
+    Import("env")
9 9
 
10
-	# Custom HEX from ELF
11
-	env.AddPostAction(
12
-		join("$BUILD_DIR", "${PROGNAME}.elf"),
13
-		env.VerboseAction(" ".join([
14
-			"$OBJCOPY", "-O ihex", "$TARGET",
15
-			"\"" + join("$BUILD_DIR", "${PROGNAME}.hex") + "\"", # Note: $BUILD_DIR is a full path
16
-		]), "Building $TARGET"))
10
+    # Custom HEX from ELF
11
+    env.AddPostAction(
12
+        join("$BUILD_DIR", "${PROGNAME}.elf"),
13
+        env.VerboseAction(" ".join([
14
+            "$OBJCOPY", "-O ihex", "$TARGET",
15
+            "\"" + join("$BUILD_DIR", "${PROGNAME}.hex") + "\"", # Note: $BUILD_DIR is a full path
16
+        ]), "Building $TARGET"))
17 17
 
18
-	# In-line command with arguments
19
-	UPLOAD_TOOL="stm32flash"
20
-	platform = env.PioPlatform()
21
-	if platform.get_package_dir("tool-stm32duino") != None:
22
-		UPLOAD_TOOL=expandvars("\"" + join(platform.get_package_dir("tool-stm32duino"),"stm32flash","stm32flash") + "\"")
18
+    # In-line command with arguments
19
+    UPLOAD_TOOL="stm32flash"
20
+    platform = env.PioPlatform()
21
+    if platform.get_package_dir("tool-stm32duino") != None:
22
+        UPLOAD_TOOL=expandvars("\"" + join(platform.get_package_dir("tool-stm32duino"),"stm32flash","stm32flash") + "\"")
23 23
 
24
-	env.Replace(
25
-		UPLOADER=UPLOAD_TOOL,
26
-		UPLOADCMD=expandvars(UPLOAD_TOOL + " -v -i rts,-dtr,dtr -R -b 115200 -g 0x8000000 -w \"" + join("$BUILD_DIR","${PROGNAME}.hex")+"\"" + " $UPLOAD_PORT")
27
-	)
24
+    env.Replace(
25
+        UPLOADER=UPLOAD_TOOL,
26
+        UPLOADCMD=expandvars(UPLOAD_TOOL + " -v -i rts,-dtr,dtr -R -b 115200 -g 0x8000000 -w \"" + join("$BUILD_DIR","${PROGNAME}.hex")+"\"" + " $UPLOAD_PORT")
27
+    )

+ 18
- 18
buildroot/share/PlatformIO/scripts/STM32F1_create_variant.py View File

@@ -3,29 +3,29 @@
3 3
 #
4 4
 import pioutil
5 5
 if pioutil.is_pio_build():
6
-	import shutil,marlin
7
-	from pathlib import Path
6
+    import shutil,marlin
7
+    from pathlib import Path
8 8
 
9
-	Import("env")
10
-	platform = env.PioPlatform()
11
-	board = env.BoardConfig()
9
+    Import("env")
10
+    platform = env.PioPlatform()
11
+    board = env.BoardConfig()
12 12
 
13
-	FRAMEWORK_DIR = Path(platform.get_package_dir("framework-arduinoststm32-maple"))
14
-	assert FRAMEWORK_DIR.is_dir()
13
+    FRAMEWORK_DIR = Path(platform.get_package_dir("framework-arduinoststm32-maple"))
14
+    assert FRAMEWORK_DIR.is_dir()
15 15
 
16
-	source_root = Path("buildroot/share/PlatformIO/variants")
17
-	assert source_root.is_dir()
16
+    source_root = Path("buildroot/share/PlatformIO/variants")
17
+    assert source_root.is_dir()
18 18
 
19
-	variant = board.get("build.variant")
20
-	variant_dir = FRAMEWORK_DIR / "STM32F1/variants" / variant
19
+    variant = board.get("build.variant")
20
+    variant_dir = FRAMEWORK_DIR / "STM32F1/variants" / variant
21 21
 
22
-	source_dir = source_root / variant
23
-	assert source_dir.is_dir()
22
+    source_dir = source_root / variant
23
+    assert source_dir.is_dir()
24 24
 
25
-	if variant_dir.is_dir():
26
-		shutil.rmtree(variant_dir)
25
+    if variant_dir.is_dir():
26
+        shutil.rmtree(variant_dir)
27 27
 
28
-	if not variant_dir.is_dir():
29
-		variant_dir.mkdir()
28
+    if not variant_dir.is_dir():
29
+        variant_dir.mkdir()
30 30
 
31
-	marlin.copytree(source_dir, variant_dir)
31
+    marlin.copytree(source_dir, variant_dir)

+ 140
- 140
buildroot/share/scripts/config-labels.py View File

@@ -47,150 +47,150 @@ different_out_dir = not (output_examples_dir == input_examples_dir)
47 47
 #----------------------------------------------
48 48
 def process_file(subdir: str, filename: str):
49 49
 #----------------------------------------------
50
-	global filenum
51
-	filenum += 1
52
-
53
-	print(str(filenum) + '  ' + filename + ':  ' + subdir)
54
-
55
-	def_line = (def_macro_name + ' "'  + subdir.replace('\\', '/')  + '"')
56
-
57
-	#------------------------
58
-	# Read file
59
-	#------------------------
60
-	lines = []
61
-	infilepath = Path(input_examples_dir, subdir, filename)
62
-	try:
63
-		# UTF-8 because some files contain unicode chars
64
-		with infilepath.open('rt', encoding="utf-8") as infile:
65
-			lines = infile.readlines()
66
-
67
-	except Exception as e:
68
-		print('Failed to read file: ' + str(e) )
69
-		raise Exception
70
-
71
-	lines = [line.rstrip('\r\n') for line in lines]
72
-
73
-	#------------------------
74
-	# Process lines
75
-	#------------------------
76
-	file_modified    = False
77
-
78
-	# region state machine
79
-	# -1 = before pragma once;
80
-	# 0 = region to place define;
81
-	# 1 = past region to place define
82
-	region      = -1
83
-
84
-	outlines = []
85
-	for line in lines:
86
-		outline = line
87
-
88
-		if (region == -1) and (def_macro_name in line):
89
-			outline       = None
90
-			file_modified = True
91
-
92
-		elif (region == -1) and ('pragma once' in line):
93
-			region = 0
94
-
95
-		elif (region == 0):
96
-			if (line.strip() == ''):
97
-				pass
98
-			elif (def_macro_name in line):
99
-				region = 1
100
-				if line == def_line:   # leave it as is
101
-					pass
102
-				else:
103
-					outline       = def_line
104
-					file_modified = True
105
-			else: # some other string
106
-				outlines.append(def_line)
107
-				outlines.append('')
108
-				region = 1
109
-				file_modified = True
110
-
111
-		elif (region == 1):
112
-			if (def_macro_name in line):
113
-				outline       = None
114
-				file_modified = True
115
-			else:
116
-				pass
117
-
118
-		# end if
119
-		if outline is not None:
120
-			outlines.append(outline)
121
-	# end for
122
-
123
-	#-------------------------
124
-	#     Output file
125
-	#-------------------------
126
-	outdir      = Path(output_examples_dir, subdir)
127
-	outfilepath = outdir / filename
128
-
129
-	if file_modified:
130
-		# Note: no need to create output dirs, as the initial copy_tree
131
-		# will do that.
132
-
133
-		print('  writing ' + str(outfilepath))
134
-		try:
135
-			# Preserve unicode chars; Avoid CR-LF on Windows.
136
-			with outfilepath.open("w", encoding="utf-8", newline='\n') as outfile:
137
-				outfile.write("\n".join(outlines) + "\n")
138
-
139
-		except Exception as e:
140
-			print('Failed to write file: ' + str(e) )
141
-			raise Exception
142
-	else:
143
-		print('  no change for ' + str(outfilepath))
50
+    global filenum
51
+    filenum += 1
52
+
53
+    print(str(filenum) + '  ' + filename + ':  ' + subdir)
54
+
55
+    def_line = (def_macro_name + ' "'  + subdir.replace('\\', '/')  + '"')
56
+
57
+    #------------------------
58
+    # Read file
59
+    #------------------------
60
+    lines = []
61
+    infilepath = Path(input_examples_dir, subdir, filename)
62
+    try:
63
+        # UTF-8 because some files contain unicode chars
64
+        with infilepath.open('rt', encoding="utf-8") as infile:
65
+            lines = infile.readlines()
66
+
67
+    except Exception as e:
68
+        print('Failed to read file: ' + str(e) )
69
+        raise Exception
70
+
71
+    lines = [line.rstrip('\r\n') for line in lines]
72
+
73
+    #------------------------
74
+    # Process lines
75
+    #------------------------
76
+    file_modified    = False
77
+
78
+    # region state machine
79
+    # -1 = before pragma once;
80
+    # 0 = region to place define;
81
+    # 1 = past region to place define
82
+    region      = -1
83
+
84
+    outlines = []
85
+    for line in lines:
86
+        outline = line
87
+
88
+        if (region == -1) and (def_macro_name in line):
89
+            outline       = None
90
+            file_modified = True
91
+
92
+        elif (region == -1) and ('pragma once' in line):
93
+            region = 0
94
+
95
+        elif (region == 0):
96
+            if (line.strip() == ''):
97
+                pass
98
+            elif (def_macro_name in line):
99
+                region = 1
100
+                if line == def_line:   # leave it as is
101
+                    pass
102
+                else:
103
+                    outline       = def_line
104
+                    file_modified = True
105
+            else: # some other string
106
+                outlines.append(def_line)
107
+                outlines.append('')
108
+                region = 1
109
+                file_modified = True
110
+
111
+        elif (region == 1):
112
+            if (def_macro_name in line):
113
+                outline       = None
114
+                file_modified = True
115
+            else:
116
+                pass
117
+
118
+        # end if
119
+        if outline is not None:
120
+            outlines.append(outline)
121
+    # end for
122
+
123
+    #-------------------------
124
+    #     Output file
125
+    #-------------------------
126
+    outdir      = Path(output_examples_dir, subdir)
127
+    outfilepath = outdir / filename
128
+
129
+    if file_modified:
130
+        # Note: no need to create output dirs, as the initial copy_tree
131
+        # will do that.
132
+
133
+        print('  writing ' + str(outfilepath))
134
+        try:
135
+            # Preserve unicode chars; Avoid CR-LF on Windows.
136
+            with outfilepath.open("w", encoding="utf-8", newline='\n') as outfile:
137
+                outfile.write("\n".join(outlines) + "\n")
138
+
139
+        except Exception as e:
140
+            print('Failed to write file: ' + str(e) )
141
+            raise Exception
142
+    else:
143
+        print('  no change for ' + str(outfilepath))
144 144
 
145 145
 #----------
146 146
 def main():
147 147
 #----------
148
-	global filenum
149
-	global input_examples_dir
150
-	global output_examples_dir
151
-	filenum = 0
152
-
153
-	#--------------------------------
154
-	# Check for requirements
155
-	#--------------------------------
156
-	input_examples_dir  = input_examples_dir.strip()
157
-	input_examples_dir  = input_examples_dir.rstrip('\\/')
158
-	output_examples_dir = output_examples_dir.strip()
159
-	output_examples_dir = output_examples_dir.rstrip('\\/')
160
-
161
-	for dir in (input_examples_dir, output_examples_dir):
162
-		if not Path(dir).exists():
163
-			print('Directory not found: ' + dir)
164
-			sys.exit(1)
165
-
166
-	#--------------------------------
167
-	# Copy tree if necessary.
168
-	#--------------------------------
169
-	# This includes files that are not otherwise included in the
170
-	# insertion of the define statement.
171
-	#
172
-	if different_out_dir:
173
-		print('Copying files to new directory: ' + output_examples_dir)
174
-		try:
175
-			copy_tree(input_examples_dir, output_examples_dir)
176
-		except Exception as e:
177
-			print('Failed to copy directory: ' + str(e) )
178
-			raise Exception
179
-
180
-	#-----------------------------
181
-	# Find and process files
182
-	#-----------------------------
183
-	len_input_examples_dir = 1 + len(input_examples_dir)
184
-
185
-	for filename in files_to_mod:
186
-		input_path = Path(input_examples_dir)
187
-		filepathlist = input_path.rglob(filename)
188
-
189
-		for filepath in filepathlist:
190
-			fulldirpath = str(filepath.parent)
191
-			subdir      = fulldirpath[len_input_examples_dir:]
192
-
193
-			process_file(subdir, filename)
148
+    global filenum
149
+    global input_examples_dir
150
+    global output_examples_dir
151
+    filenum = 0
152
+
153
+    #--------------------------------
154
+    # Check for requirements
155
+    #--------------------------------
156
+    input_examples_dir  = input_examples_dir.strip()
157
+    input_examples_dir  = input_examples_dir.rstrip('\\/')
158
+    output_examples_dir = output_examples_dir.strip()
159
+    output_examples_dir = output_examples_dir.rstrip('\\/')
160
+
161
+    for dir in (input_examples_dir, output_examples_dir):
162
+        if not Path(dir).exists():
163
+            print('Directory not found: ' + dir)
164
+            sys.exit(1)
165
+
166
+    #--------------------------------
167
+    # Copy tree if necessary.
168
+    #--------------------------------
169
+    # This includes files that are not otherwise included in the
170
+    # insertion of the define statement.
171
+    #
172
+    if different_out_dir:
173
+        print('Copying files to new directory: ' + output_examples_dir)
174
+        try:
175
+            copy_tree(input_examples_dir, output_examples_dir)
176
+        except Exception as e:
177
+            print('Failed to copy directory: ' + str(e) )
178
+            raise Exception
179
+
180
+    #-----------------------------
181
+    # Find and process files
182
+    #-----------------------------
183
+    len_input_examples_dir = 1 + len(input_examples_dir)
184
+
185
+    for filename in files_to_mod:
186
+        input_path = Path(input_examples_dir)
187
+        filepathlist = input_path.rglob(filename)
188
+
189
+        for filepath in filepathlist:
190
+            fulldirpath = str(filepath.parent)
191
+            subdir      = fulldirpath[len_input_examples_dir:]
192
+
193
+            process_file(subdir, filename)
194 194
 
195 195
 #==============
196 196
 print('--- Starting config-labels ---')

+ 30
- 30
buildroot/share/scripts/gen-tft-image.py View File

@@ -26,38 +26,38 @@ import sys,struct
26 26
 from PIL import Image
27 27
 
28 28
 def image2bin(image, output_file):
29
-	if output_file.endswith(('.c', '.cpp')):
30
-		f = open(output_file, 'wt')
31
-		is_cpp = True
32
-		f.write("const uint16_t image[%d] = {\n" % (image.size[1] * image.size[0]))
33
-	else:
34
-		f = open(output_file, 'wb')
35
-		is_cpp = False
36
-	pixs = image.load()
37
-	for y in range(image.size[1]):
38
-		for x in range(image.size[0]):
39
-			R = pixs[x, y][0] >> 3
40
-			G = pixs[x, y][1] >> 2
41
-			B = pixs[x, y][2] >> 3
42
-			rgb = (R << 11) | (G << 5) | B
43
-			if is_cpp:
44
-				strHex = '0x{0:04X}, '.format(rgb)
45
-				f.write(strHex)
46
-			else:
47
-				f.write(struct.pack("B", (rgb & 0xFF)))
48
-				f.write(struct.pack("B", (rgb >> 8) & 0xFF))
49
-		if is_cpp:
50
-			f.write("\n")
51
-	if is_cpp:
52
-		f.write("};\n")
53
-	f.close()
29
+    if output_file.endswith(('.c', '.cpp')):
30
+        f = open(output_file, 'wt')
31
+        is_cpp = True
32
+        f.write("const uint16_t image[%d] = {\n" % (image.size[1] * image.size[0]))
33
+    else:
34
+        f = open(output_file, 'wb')
35
+        is_cpp = False
36
+    pixs = image.load()
37
+    for y in range(image.size[1]):
38
+        for x in range(image.size[0]):
39
+            R = pixs[x, y][0] >> 3
40
+            G = pixs[x, y][1] >> 2
41
+            B = pixs[x, y][2] >> 3
42
+            rgb = (R << 11) | (G << 5) | B
43
+            if is_cpp:
44
+                strHex = '0x{0:04X}, '.format(rgb)
45
+                f.write(strHex)
46
+            else:
47
+                f.write(struct.pack("B", (rgb & 0xFF)))
48
+                f.write(struct.pack("B", (rgb >> 8) & 0xFF))
49
+        if is_cpp:
50
+            f.write("\n")
51
+    if is_cpp:
52
+        f.write("};\n")
53
+    f.close()
54 54
 
55 55
 if len(sys.argv) <= 2:
56
-	print("Utility to export a image in Marlin TFT friendly format.")
57
-	print("It will dump a raw bin RGB565 image or create a CPP file with an array of 16 bit image pixels.")
58
-	print("Usage: gen-tft-image.py INPUT_IMAGE.(png|bmp|jpg) OUTPUT_FILE.(cpp|bin)")
59
-	print("Author: rhapsodyv")
60
-	exit(1)
56
+    print("Utility to export a image in Marlin TFT friendly format.")
57
+    print("It will dump a raw bin RGB565 image or create a CPP file with an array of 16 bit image pixels.")
58
+    print("Usage: gen-tft-image.py INPUT_IMAGE.(png|bmp|jpg) OUTPUT_FILE.(cpp|bin)")
59
+    print("Author: rhapsodyv")
60
+    exit(1)
61 61
 
62 62
 output_img = sys.argv[2]
63 63
 img = Image.open(sys.argv[1])

+ 314
- 314
buildroot/share/scripts/upload.py View File

@@ -25,320 +25,320 @@ import MarlinBinaryProtocol
25 25
 #-----------------#
26 26
 def Upload(source, target, env):
27 27
 
28
-	#-------#
29
-	# Debug #
30
-	#-------#
31
-	Debug = False                # Set to True to enable script debug
32
-	def debugPrint(data):
33
-		if Debug: print(f"[Debug]: {data}")
34
-
35
-	#------------------#
36
-	# Marlin functions #
37
-	#------------------#
38
-	def _GetMarlinEnv(marlinEnv, feature):
39
-		if not marlinEnv: return None
40
-		return marlinEnv[feature] if feature in marlinEnv else None
41
-
42
-	#----------------#
43
-	# Port functions #
44
-	#----------------#
45
-	def _GetUploadPort(env):
46
-		debugPrint('Autodetecting upload port...')
47
-		env.AutodetectUploadPort(env)
48
-		portName = env.subst('$UPLOAD_PORT')
49
-		if not portName:
50
-			raise Exception('Error detecting the upload port.')
51
-		debugPrint('OK')
52
-		return portName
53
-
54
-	#-------------------------#
55
-	# Simple serial functions #
56
-	#-------------------------#
57
-	def _OpenPort():
58
-		# Open serial port
59
-		if port.is_open: return
60
-		debugPrint('Opening upload port...')
61
-		port.open()
62
-		port.reset_input_buffer()
63
-		debugPrint('OK')
64
-
65
-	def _ClosePort():
66
-		# Open serial port
67
-		if port is None: return
68
-		if not port.is_open: return
69
-		debugPrint('Closing upload port...')
70
-		port.close()
71
-		debugPrint('OK')
72
-
73
-	def _Send(data):
74
-		debugPrint(f'>> {data}')
75
-		strdata = bytearray(data, 'utf8') + b'\n'
76
-		port.write(strdata)
77
-		time.sleep(0.010)
78
-
79
-	def _Recv():
80
-		clean_responses = []
81
-		responses = port.readlines()
82
-		for Resp in responses:
83
-			# Suppress invalid chars (coming from debug info)
84
-			try:
85
-				clean_response = Resp.decode('utf8').rstrip().lstrip()
86
-				clean_responses.append(clean_response)
87
-				debugPrint(f'<< {clean_response}')
88
-			except:
89
-				pass
90
-		return clean_responses
91
-
92
-	#------------------#
93
-	# SDCard functions #
94
-	#------------------#
95
-	def _CheckSDCard():
96
-		debugPrint('Checking SD card...')
97
-		_Send('M21')
98
-		Responses = _Recv()
99
-		if len(Responses) < 1 or not any('SD card ok' in r for r in Responses):
100
-			raise Exception('Error accessing SD card')
101
-		debugPrint('SD Card OK')
102
-		return True
103
-
104
-	#----------------#
105
-	# File functions #
106
-	#----------------#
107
-	def _GetFirmwareFiles(UseLongFilenames):
108
-		debugPrint('Get firmware files...')
109
-		_Send(f"M20 F{'L' if UseLongFilenames else ''}")
110
-		Responses = _Recv()
111
-		if len(Responses) < 3 or not any('file list' in r for r in Responses):
112
-			raise Exception('Error getting firmware files')
113
-		debugPrint('OK')
114
-		return Responses
115
-
116
-	def _FilterFirmwareFiles(FirmwareList, UseLongFilenames):
117
-		Firmwares = []
118
-		for FWFile in FirmwareList:
119
-			# For long filenames take the 3rd column of the firmwares list
120
-			if UseLongFilenames:
121
-				Space = 0
122
-				Space = FWFile.find(' ')
123
-				if Space >= 0: Space = FWFile.find(' ', Space + 1)
124
-				if Space >= 0: FWFile = FWFile[Space + 1:]
125
-			if not '/' in FWFile and '.BIN' in FWFile.upper():
126
-				Firmwares.append(FWFile[:FWFile.upper().index('.BIN') + 4])
127
-		return Firmwares
128
-
129
-	def _RemoveFirmwareFile(FirmwareFile):
130
-		_Send(f'M30 /{FirmwareFile}')
131
-		Responses = _Recv()
132
-		Removed = len(Responses) >= 1 and any('File deleted' in r for r in Responses)
133
-		if not Removed:
134
-			raise Exception(f"Firmware file '{FirmwareFile}' not removed")
135
-		return Removed
136
-
137
-	def _RollbackUpload(FirmwareFile):
138
-		if not rollback: return
139
-		print(f"Rollback: trying to delete firmware '{FirmwareFile}'...")
140
-		_OpenPort()
141
-		# Wait for SD card release
142
-		time.sleep(1)
143
-		# Remount SD card
144
-		_CheckSDCard()
145
-		print(' OK' if _RemoveFirmwareFile(FirmwareFile) else ' Error!')
146
-		_ClosePort()
147
-
148
-
149
-	#---------------------#
150
-	# Callback Entrypoint #
151
-	#---------------------#
152
-	port = None
153
-	protocol = None
154
-	filetransfer = None
155
-	rollback = False
156
-
157
-	# Get Marlin evironment vars
158
-	MarlinEnv = env['MARLIN_FEATURES']
159
-	marlin_pioenv = _GetMarlinEnv(MarlinEnv, 'PIOENV')
160
-	marlin_motherboard = _GetMarlinEnv(MarlinEnv, 'MOTHERBOARD')
161
-	marlin_board_info_name = _GetMarlinEnv(MarlinEnv, 'BOARD_INFO_NAME')
162
-	marlin_board_custom_build_flags = _GetMarlinEnv(MarlinEnv, 'BOARD_CUSTOM_BUILD_FLAGS')
163
-	marlin_firmware_bin = _GetMarlinEnv(MarlinEnv, 'FIRMWARE_BIN')
164
-	marlin_long_filename_host_support = _GetMarlinEnv(MarlinEnv, 'LONG_FILENAME_HOST_SUPPORT') is not None
165
-	marlin_longname_write = _GetMarlinEnv(MarlinEnv, 'LONG_FILENAME_WRITE_SUPPORT') is not None
166
-	marlin_custom_firmware_upload = _GetMarlinEnv(MarlinEnv, 'CUSTOM_FIRMWARE_UPLOAD') is not None
167
-	marlin_short_build_version = _GetMarlinEnv(MarlinEnv, 'SHORT_BUILD_VERSION')
168
-	marlin_string_config_h_author = _GetMarlinEnv(MarlinEnv, 'STRING_CONFIG_H_AUTHOR')
169
-
170
-	# Get firmware upload params
171
-	upload_firmware_source_name = str(source[0])    # Source firmware filename
172
-	upload_speed = env['UPLOAD_SPEED'] if 'UPLOAD_SPEED' in env else 115200
173
-													# baud rate of serial connection
174
-	upload_port = _GetUploadPort(env)               # Serial port to use
175
-
176
-	# Set local upload params
177
-	upload_firmware_target_name = os.path.basename(upload_firmware_source_name)
178
-													# Target firmware filename
179
-	upload_timeout = 1000                           # Communication timout, lossy/slow connections need higher values
180
-	upload_blocksize = 512                          # Transfer block size. 512 = Autodetect
181
-	upload_compression = True                       # Enable compression
182
-	upload_error_ratio = 0                          # Simulated corruption ratio
183
-	upload_test = False                             # Benchmark the serial link without storing the file
184
-	upload_reset = True                             # Trigger a soft reset for firmware update after the upload
185
-
186
-	# Set local upload params based on board type to change script behavior
187
-	# "upload_delete_old_bins": delete all *.bin files in the root of SD Card
188
-	upload_delete_old_bins = marlin_motherboard in ['BOARD_CREALITY_V4',   'BOARD_CREALITY_V4210', 'BOARD_CREALITY_V422', 'BOARD_CREALITY_V423',
189
-													'BOARD_CREALITY_V427', 'BOARD_CREALITY_V431',  'BOARD_CREALITY_V452', 'BOARD_CREALITY_V453',
190
-													'BOARD_CREALITY_V24S1']
191
-	# "upload_random_name": generate a random 8.3 firmware filename to upload
192
-	upload_random_filename = marlin_motherboard in ['BOARD_CREALITY_V4',   'BOARD_CREALITY_V4210', 'BOARD_CREALITY_V422', 'BOARD_CREALITY_V423',
193
-													'BOARD_CREALITY_V427', 'BOARD_CREALITY_V431',  'BOARD_CREALITY_V452', 'BOARD_CREALITY_V453',
194
-													'BOARD_CREALITY_V24S1'] and not marlin_long_filename_host_support
195
-
196
-	try:
197
-
198
-		# Start upload job
199
-		print(f"Uploading firmware '{os.path.basename(upload_firmware_target_name)}' to '{marlin_motherboard}' via '{upload_port}'")
200
-
201
-		# Dump some debug info
202
-		if Debug:
203
-			print('Upload using:')
204
-			print('---- Marlin -----------------------------------')
205
-			print(f' PIOENV                      : {marlin_pioenv}')
206
-			print(f' SHORT_BUILD_VERSION         : {marlin_short_build_version}')
207
-			print(f' STRING_CONFIG_H_AUTHOR      : {marlin_string_config_h_author}')
208
-			print(f' MOTHERBOARD                 : {marlin_motherboard}')
209
-			print(f' BOARD_INFO_NAME             : {marlin_board_info_name}')
210
-			print(f' CUSTOM_BUILD_FLAGS          : {marlin_board_custom_build_flags}')
211
-			print(f' FIRMWARE_BIN                : {marlin_firmware_bin}')
212
-			print(f' LONG_FILENAME_HOST_SUPPORT  : {marlin_long_filename_host_support}')
213
-			print(f' LONG_FILENAME_WRITE_SUPPORT : {marlin_longname_write}')
214
-			print(f' CUSTOM_FIRMWARE_UPLOAD      : {marlin_custom_firmware_upload}')
215
-			print('---- Upload parameters ------------------------')
216
-			print(f' Source                      : {upload_firmware_source_name}')
217
-			print(f' Target                      : {upload_firmware_target_name}')
218
-			print(f' Port                        : {upload_port} @ {upload_speed} baudrate')
219
-			print(f' Timeout                     : {upload_timeout}')
220
-			print(f' Block size                  : {upload_blocksize}')
221
-			print(f' Compression                 : {upload_compression}')
222
-			print(f' Error ratio                 : {upload_error_ratio}')
223
-			print(f' Test                        : {upload_test}')
224
-			print(f' Reset                       : {upload_reset}')
225
-			print('-----------------------------------------------')
226
-
227
-		# Custom implementations based on board parameters
228
-		# Generate a new 8.3 random filename
229
-		if upload_random_filename:
230
-			upload_firmware_target_name = f"fw-{''.join(random.choices('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', k=5))}.BIN"
231
-			print(f"Board {marlin_motherboard}: Overriding firmware filename to '{upload_firmware_target_name}'")
232
-
233
-		# Delete all *.bin files on the root of SD Card (if flagged)
234
-		if upload_delete_old_bins:
235
-			# CUSTOM_FIRMWARE_UPLOAD is needed for this feature
236
-			if not marlin_custom_firmware_upload:
237
-				raise Exception(f"CUSTOM_FIRMWARE_UPLOAD must be enabled in 'Configuration_adv.h' for '{marlin_motherboard}'")
238
-
239
-			# Init & Open serial port
240
-			port = serial.Serial(upload_port, baudrate = upload_speed, write_timeout = 0, timeout = 0.1)
241
-			_OpenPort()
242
-
243
-			# Check SD card status
244
-			_CheckSDCard()
245
-
246
-			# Get firmware files
247
-			FirmwareFiles = _GetFirmwareFiles(marlin_long_filename_host_support)
248
-			if Debug:
249
-				for FirmwareFile in FirmwareFiles:
250
-					print(f'Found: {FirmwareFile}')
251
-
252
-			# Get all 1st level firmware files (to remove)
253
-			OldFirmwareFiles = _FilterFirmwareFiles(FirmwareFiles[1:len(FirmwareFiles)-2], marlin_long_filename_host_support)   # Skip header and footers of list
254
-			if len(OldFirmwareFiles) == 0:
255
-				print('No old firmware files to delete')
256
-			else:
257
-				print(f"Remove {len(OldFirmwareFiles)} old firmware file{'s' if len(OldFirmwareFiles) != 1 else ''}:")
258
-				for OldFirmwareFile in OldFirmwareFiles:
259
-					print(f" -Removing- '{OldFirmwareFile}'...")
260
-					print(' OK' if _RemoveFirmwareFile(OldFirmwareFile) else ' Error!')
261
-
262
-			# Close serial
263
-			_ClosePort()
264
-
265
-			# Cleanup completed
266
-			debugPrint('Cleanup completed')
267
-
268
-		# WARNING! The serial port must be closed here because the serial transfer that follow needs it!
269
-
270
-		# Upload firmware file
271
-		debugPrint(f"Copy '{upload_firmware_source_name}' --> '{upload_firmware_target_name}'")
272
-		protocol = MarlinBinaryProtocol.Protocol(upload_port, upload_speed, upload_blocksize, float(upload_error_ratio), int(upload_timeout))
273
-		#echologger = MarlinBinaryProtocol.EchoProtocol(protocol)
274
-		protocol.connect()
275
-		# Mark the rollback (delete broken transfer) from this point on
276
-		rollback = True
277
-		filetransfer = MarlinBinaryProtocol.FileTransferProtocol(protocol)
278
-		transferOK = filetransfer.copy(upload_firmware_source_name, upload_firmware_target_name, upload_compression, upload_test)
279
-		protocol.disconnect()
280
-
281
-		# Notify upload completed
282
-		protocol.send_ascii('M117 Firmware uploaded' if transferOK else 'M117 Firmware upload failed')
283
-
284
-		# Remount SD card
285
-		print('Wait for SD card release...')
286
-		time.sleep(1)
287
-		print('Remount SD card')
288
-		protocol.send_ascii('M21')
289
-
290
-		# Transfer failed?
291
-		if not transferOK:
292
-			protocol.shutdown()
293
-			_RollbackUpload(upload_firmware_target_name)
294
-		else:
295
-			# Trigger firmware update
296
-			if upload_reset:
297
-				print('Trigger firmware update...')
298
-				protocol.send_ascii('M997', True)
299
-			protocol.shutdown()
300
-
301
-		print('Firmware update completed' if transferOK else 'Firmware update failed')
302
-		return 0 if transferOK else -1
303
-
304
-	except KeyboardInterrupt:
305
-		print('Aborted by user')
306
-		if filetransfer: filetransfer.abort()
307
-		if protocol:
308
-			protocol.disconnect()
309
-			protocol.shutdown()
310
-		_RollbackUpload(upload_firmware_target_name)
311
-		_ClosePort()
312
-		raise
313
-
314
-	except serial.SerialException as se:
315
-		# This exception is raised only for send_ascii data (not for binary transfer)
316
-		print(f'Serial excepion: {se}, transfer aborted')
317
-		if protocol:
318
-			protocol.disconnect()
319
-			protocol.shutdown()
320
-		_RollbackUpload(upload_firmware_target_name)
321
-		_ClosePort()
322
-		raise Exception(se)
323
-
324
-	except MarlinBinaryProtocol.FatalError:
325
-		print('Too many retries, transfer aborted')
326
-		if protocol:
327
-			protocol.disconnect()
328
-			protocol.shutdown()
329
-		_RollbackUpload(upload_firmware_target_name)
330
-		_ClosePort()
331
-		raise
332
-
333
-	except Exception as ex:
334
-		print(f"\nException: {ex}, transfer aborted")
335
-		if protocol:
336
-			protocol.disconnect()
337
-			protocol.shutdown()
338
-		_RollbackUpload(upload_firmware_target_name)
339
-		_ClosePort()
340
-		print('Firmware not updated')
341
-		raise
28
+    #-------#
29
+    # Debug #
30
+    #-------#
31
+    Debug = False                # Set to True to enable script debug
32
+    def debugPrint(data):
33
+        if Debug: print(f"[Debug]: {data}")
34
+
35
+    #------------------#
36
+    # Marlin functions #
37
+    #------------------#
38
+    def _GetMarlinEnv(marlinEnv, feature):
39
+        if not marlinEnv: return None
40
+        return marlinEnv[feature] if feature in marlinEnv else None
41
+
42
+    #----------------#
43
+    # Port functions #
44
+    #----------------#
45
+    def _GetUploadPort(env):
46
+        debugPrint('Autodetecting upload port...')
47
+        env.AutodetectUploadPort(env)
48
+        portName = env.subst('$UPLOAD_PORT')
49
+        if not portName:
50
+            raise Exception('Error detecting the upload port.')
51
+        debugPrint('OK')
52
+        return portName
53
+
54
+    #-------------------------#
55
+    # Simple serial functions #
56
+    #-------------------------#
57
+    def _OpenPort():
58
+        # Open serial port
59
+        if port.is_open: return
60
+        debugPrint('Opening upload port...')
61
+        port.open()
62
+        port.reset_input_buffer()
63
+        debugPrint('OK')
64
+
65
+    def _ClosePort():
66
+        # Open serial port
67
+        if port is None: return
68
+        if not port.is_open: return
69
+        debugPrint('Closing upload port...')
70
+        port.close()
71
+        debugPrint('OK')
72
+
73
+    def _Send(data):
74
+        debugPrint(f'>> {data}')
75
+        strdata = bytearray(data, 'utf8') + b'\n'
76
+        port.write(strdata)
77
+        time.sleep(0.010)
78
+
79
+    def _Recv():
80
+        clean_responses = []
81
+        responses = port.readlines()
82
+        for Resp in responses:
83
+            # Suppress invalid chars (coming from debug info)
84
+            try:
85
+                clean_response = Resp.decode('utf8').rstrip().lstrip()
86
+                clean_responses.append(clean_response)
87
+                debugPrint(f'<< {clean_response}')
88
+            except:
89
+                pass
90
+        return clean_responses
91
+
92
+    #------------------#
93
+    # SDCard functions #
94
+    #------------------#
95
+    def _CheckSDCard():
96
+        debugPrint('Checking SD card...')
97
+        _Send('M21')
98
+        Responses = _Recv()
99
+        if len(Responses) < 1 or not any('SD card ok' in r for r in Responses):
100
+            raise Exception('Error accessing SD card')
101
+        debugPrint('SD Card OK')
102
+        return True
103
+
104
+    #----------------#
105
+    # File functions #
106
+    #----------------#
107
+    def _GetFirmwareFiles(UseLongFilenames):
108
+        debugPrint('Get firmware files...')
109
+        _Send(f"M20 F{'L' if UseLongFilenames else ''}")
110
+        Responses = _Recv()
111
+        if len(Responses) < 3 or not any('file list' in r for r in Responses):
112
+            raise Exception('Error getting firmware files')
113
+        debugPrint('OK')
114
+        return Responses
115
+
116
+    def _FilterFirmwareFiles(FirmwareList, UseLongFilenames):
117
+        Firmwares = []
118
+        for FWFile in FirmwareList:
119
+            # For long filenames take the 3rd column of the firmwares list
120
+            if UseLongFilenames:
121
+                Space = 0
122
+                Space = FWFile.find(' ')
123
+                if Space >= 0: Space = FWFile.find(' ', Space + 1)
124
+                if Space >= 0: FWFile = FWFile[Space + 1:]
125
+            if not '/' in FWFile and '.BIN' in FWFile.upper():
126
+                Firmwares.append(FWFile[:FWFile.upper().index('.BIN') + 4])
127
+        return Firmwares
128
+
129
+    def _RemoveFirmwareFile(FirmwareFile):
130
+        _Send(f'M30 /{FirmwareFile}')
131
+        Responses = _Recv()
132
+        Removed = len(Responses) >= 1 and any('File deleted' in r for r in Responses)
133
+        if not Removed:
134
+            raise Exception(f"Firmware file '{FirmwareFile}' not removed")
135
+        return Removed
136
+
137
+    def _RollbackUpload(FirmwareFile):
138
+        if not rollback: return
139
+        print(f"Rollback: trying to delete firmware '{FirmwareFile}'...")
140
+        _OpenPort()
141
+        # Wait for SD card release
142
+        time.sleep(1)
143
+        # Remount SD card
144
+        _CheckSDCard()
145
+        print(' OK' if _RemoveFirmwareFile(FirmwareFile) else ' Error!')
146
+        _ClosePort()
147
+
148
+
149
+    #---------------------#
150
+    # Callback Entrypoint #
151
+    #---------------------#
152
+    port = None
153
+    protocol = None
154
+    filetransfer = None
155
+    rollback = False
156
+
157
+    # Get Marlin evironment vars
158
+    MarlinEnv = env['MARLIN_FEATURES']
159
+    marlin_pioenv = _GetMarlinEnv(MarlinEnv, 'PIOENV')
160
+    marlin_motherboard = _GetMarlinEnv(MarlinEnv, 'MOTHERBOARD')
161
+    marlin_board_info_name = _GetMarlinEnv(MarlinEnv, 'BOARD_INFO_NAME')
162
+    marlin_board_custom_build_flags = _GetMarlinEnv(MarlinEnv, 'BOARD_CUSTOM_BUILD_FLAGS')
163
+    marlin_firmware_bin = _GetMarlinEnv(MarlinEnv, 'FIRMWARE_BIN')
164
+    marlin_long_filename_host_support = _GetMarlinEnv(MarlinEnv, 'LONG_FILENAME_HOST_SUPPORT') is not None
165
+    marlin_longname_write = _GetMarlinEnv(MarlinEnv, 'LONG_FILENAME_WRITE_SUPPORT') is not None
166
+    marlin_custom_firmware_upload = _GetMarlinEnv(MarlinEnv, 'CUSTOM_FIRMWARE_UPLOAD') is not None
167
+    marlin_short_build_version = _GetMarlinEnv(MarlinEnv, 'SHORT_BUILD_VERSION')
168
+    marlin_string_config_h_author = _GetMarlinEnv(MarlinEnv, 'STRING_CONFIG_H_AUTHOR')
169
+
170
+    # Get firmware upload params
171
+    upload_firmware_source_name = str(source[0])    # Source firmware filename
172
+    upload_speed = env['UPLOAD_SPEED'] if 'UPLOAD_SPEED' in env else 115200
173
+                                                    # baud rate of serial connection
174
+    upload_port = _GetUploadPort(env)               # Serial port to use
175
+
176
+    # Set local upload params
177
+    upload_firmware_target_name = os.path.basename(upload_firmware_source_name)
178
+                                                    # Target firmware filename
179
+    upload_timeout = 1000                           # Communication timout, lossy/slow connections need higher values
180
+    upload_blocksize = 512                          # Transfer block size. 512 = Autodetect
181
+    upload_compression = True                       # Enable compression
182
+    upload_error_ratio = 0                          # Simulated corruption ratio
183
+    upload_test = False                             # Benchmark the serial link without storing the file
184
+    upload_reset = True                             # Trigger a soft reset for firmware update after the upload
185
+
186
+    # Set local upload params based on board type to change script behavior
187
+    # "upload_delete_old_bins": delete all *.bin files in the root of SD Card
188
+    upload_delete_old_bins = marlin_motherboard in ['BOARD_CREALITY_V4',   'BOARD_CREALITY_V4210', 'BOARD_CREALITY_V422', 'BOARD_CREALITY_V423',
189
+                                                    'BOARD_CREALITY_V427', 'BOARD_CREALITY_V431',  'BOARD_CREALITY_V452', 'BOARD_CREALITY_V453',
190
+                                                    'BOARD_CREALITY_V24S1']
191
+    # "upload_random_name": generate a random 8.3 firmware filename to upload
192
+    upload_random_filename = marlin_motherboard in ['BOARD_CREALITY_V4',   'BOARD_CREALITY_V4210', 'BOARD_CREALITY_V422', 'BOARD_CREALITY_V423',
193
+                                                    'BOARD_CREALITY_V427', 'BOARD_CREALITY_V431',  'BOARD_CREALITY_V452', 'BOARD_CREALITY_V453',
194
+                                                    'BOARD_CREALITY_V24S1'] and not marlin_long_filename_host_support
195
+
196
+    try:
197
+
198
+        # Start upload job
199
+        print(f"Uploading firmware '{os.path.basename(upload_firmware_target_name)}' to '{marlin_motherboard}' via '{upload_port}'")
200
+
201
+        # Dump some debug info
202
+        if Debug:
203
+            print('Upload using:')
204
+            print('---- Marlin -----------------------------------')
205
+            print(f' PIOENV                      : {marlin_pioenv}')
206
+            print(f' SHORT_BUILD_VERSION         : {marlin_short_build_version}')
207
+            print(f' STRING_CONFIG_H_AUTHOR      : {marlin_string_config_h_author}')
208
+            print(f' MOTHERBOARD                 : {marlin_motherboard}')
209
+            print(f' BOARD_INFO_NAME             : {marlin_board_info_name}')
210
+            print(f' CUSTOM_BUILD_FLAGS          : {marlin_board_custom_build_flags}')
211
+            print(f' FIRMWARE_BIN                : {marlin_firmware_bin}')
212
+            print(f' LONG_FILENAME_HOST_SUPPORT  : {marlin_long_filename_host_support}')
213
+            print(f' LONG_FILENAME_WRITE_SUPPORT : {marlin_longname_write}')
214
+            print(f' CUSTOM_FIRMWARE_UPLOAD      : {marlin_custom_firmware_upload}')
215
+            print('---- Upload parameters ------------------------')
216
+            print(f' Source                      : {upload_firmware_source_name}')
217
+            print(f' Target                      : {upload_firmware_target_name}')
218
+            print(f' Port                        : {upload_port} @ {upload_speed} baudrate')
219
+            print(f' Timeout                     : {upload_timeout}')
220
+            print(f' Block size                  : {upload_blocksize}')
221
+            print(f' Compression                 : {upload_compression}')
222
+            print(f' Error ratio                 : {upload_error_ratio}')
223
+            print(f' Test                        : {upload_test}')
224
+            print(f' Reset                       : {upload_reset}')
225
+            print('-----------------------------------------------')
226
+
227
+        # Custom implementations based on board parameters
228
+        # Generate a new 8.3 random filename
229
+        if upload_random_filename:
230
+            upload_firmware_target_name = f"fw-{''.join(random.choices('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', k=5))}.BIN"
231
+            print(f"Board {marlin_motherboard}: Overriding firmware filename to '{upload_firmware_target_name}'")
232
+
233
+        # Delete all *.bin files on the root of SD Card (if flagged)
234
+        if upload_delete_old_bins:
235
+            # CUSTOM_FIRMWARE_UPLOAD is needed for this feature
236
+            if not marlin_custom_firmware_upload:
237
+                raise Exception(f"CUSTOM_FIRMWARE_UPLOAD must be enabled in 'Configuration_adv.h' for '{marlin_motherboard}'")
238
+
239
+            # Init & Open serial port
240
+            port = serial.Serial(upload_port, baudrate = upload_speed, write_timeout = 0, timeout = 0.1)
241
+            _OpenPort()
242
+
243
+            # Check SD card status
244
+            _CheckSDCard()
245
+
246
+            # Get firmware files
247
+            FirmwareFiles = _GetFirmwareFiles(marlin_long_filename_host_support)
248
+            if Debug:
249
+                for FirmwareFile in FirmwareFiles:
250
+                    print(f'Found: {FirmwareFile}')
251
+
252
+            # Get all 1st level firmware files (to remove)
253
+            OldFirmwareFiles = _FilterFirmwareFiles(FirmwareFiles[1:len(FirmwareFiles)-2], marlin_long_filename_host_support)   # Skip header and footers of list
254
+            if len(OldFirmwareFiles) == 0:
255
+                print('No old firmware files to delete')
256
+            else:
257
+                print(f"Remove {len(OldFirmwareFiles)} old firmware file{'s' if len(OldFirmwareFiles) != 1 else ''}:")
258
+                for OldFirmwareFile in OldFirmwareFiles:
259
+                    print(f" -Removing- '{OldFirmwareFile}'...")
260
+                    print(' OK' if _RemoveFirmwareFile(OldFirmwareFile) else ' Error!')
261
+
262
+            # Close serial
263
+            _ClosePort()
264
+
265
+            # Cleanup completed
266
+            debugPrint('Cleanup completed')
267
+
268
+        # WARNING! The serial port must be closed here because the serial transfer that follow needs it!
269
+
270
+        # Upload firmware file
271
+        debugPrint(f"Copy '{upload_firmware_source_name}' --> '{upload_firmware_target_name}'")
272
+        protocol = MarlinBinaryProtocol.Protocol(upload_port, upload_speed, upload_blocksize, float(upload_error_ratio), int(upload_timeout))
273
+        #echologger = MarlinBinaryProtocol.EchoProtocol(protocol)
274
+        protocol.connect()
275
+        # Mark the rollback (delete broken transfer) from this point on
276
+        rollback = True
277
+        filetransfer = MarlinBinaryProtocol.FileTransferProtocol(protocol)
278
+        transferOK = filetransfer.copy(upload_firmware_source_name, upload_firmware_target_name, upload_compression, upload_test)
279
+        protocol.disconnect()
280
+
281
+        # Notify upload completed
282
+        protocol.send_ascii('M117 Firmware uploaded' if transferOK else 'M117 Firmware upload failed')
283
+
284
+        # Remount SD card
285
+        print('Wait for SD card release...')
286
+        time.sleep(1)
287
+        print('Remount SD card')
288
+        protocol.send_ascii('M21')
289
+
290
+        # Transfer failed?
291
+        if not transferOK:
292
+            protocol.shutdown()
293
+            _RollbackUpload(upload_firmware_target_name)
294
+        else:
295
+            # Trigger firmware update
296
+            if upload_reset:
297
+                print('Trigger firmware update...')
298
+                protocol.send_ascii('M997', True)
299
+            protocol.shutdown()
300
+
301
+        print('Firmware update completed' if transferOK else 'Firmware update failed')
302
+        return 0 if transferOK else -1
303
+
304
+    except KeyboardInterrupt:
305
+        print('Aborted by user')
306
+        if filetransfer: filetransfer.abort()
307
+        if protocol:
308
+            protocol.disconnect()
309
+            protocol.shutdown()
310
+        _RollbackUpload(upload_firmware_target_name)
311
+        _ClosePort()
312
+        raise
313
+
314
+    except serial.SerialException as se:
315
+        # This exception is raised only for send_ascii data (not for binary transfer)
316
+        print(f'Serial excepion: {se}, transfer aborted')
317
+        if protocol:
318
+            protocol.disconnect()
319
+            protocol.shutdown()
320
+        _RollbackUpload(upload_firmware_target_name)
321
+        _ClosePort()
322
+        raise Exception(se)
323
+
324
+    except MarlinBinaryProtocol.FatalError:
325
+        print('Too many retries, transfer aborted')
326
+        if protocol:
327
+            protocol.disconnect()
328
+            protocol.shutdown()
329
+        _RollbackUpload(upload_firmware_target_name)
330
+        _ClosePort()
331
+        raise
332
+
333
+    except Exception as ex:
334
+        print(f"\nException: {ex}, transfer aborted")
335
+        if protocol:
336
+            protocol.disconnect()
337
+            protocol.shutdown()
338
+        _RollbackUpload(upload_firmware_target_name)
339
+        _ClosePort()
340
+        print('Firmware not updated')
341
+        raise
342 342
 
343 343
 # Attach custom upload callback
344 344
 env.Replace(UPLOADCMD=Upload)

Loading…
Cancel
Save