Skip to content

Commit

Permalink
修复and增加
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharkbia committed Feb 23, 2024
1 parent adff955 commit 18ea8e0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
54 changes: 24 additions & 30 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,11 @@ def auto_write_font(serial_port_text: str, window: tk.Tk, progress: ttk.Progress
m += 1
log(f'正在进行 {m}/{n}: 写入亚音参数')
write_tone_options(serial_port_text, window, progress, status_label, eeprom_size, firmware_version, True)
reset_radio(serial_port_text, status_label)
if n == 4:
messagebox.showinfo('提示', f'{version_number}{version_code}版本字库\n字库配置\n亚音参数\n拼音检索表\n写入成功!')
else:
messagebox.showinfo('提示', f'{version_number}{version_code}版本字库\n字库配置\n亚音参数\n写入成功!')
reset_radio(serial_port_text, status_label)
else:
messagebox.showinfo('提示', f'非LOSEHU扩容固件,无法写入')

Expand Down Expand Up @@ -644,7 +644,7 @@ def write_pinyin_index(serial_port_text: str, window: tk.Tk, progress: ttk.Progr
messagebox.showinfo('提示', '写入拼音检索表成功!')
status_label['text'] = '当前操作: 无'

def backups_eeprom(serial_port_text: str, window: tk.Tk, progress: ttk.Progressbar,
def backup_eeprom(serial_port_text: str, window: tk.Tk, progress: ttk.Progressbar,
status_label: tk.Label, eeprom_size: int):
log('开始备份')
log('选择的串口: ' + serial_port_text)
Expand All @@ -655,18 +655,12 @@ def backups_eeprom(serial_port_text: str, window: tk.Tk, progress: ttk.Progressb
status_label['text'] = '当前操作: 无'
return

if eeprom_size == 1:
end_addr = 0x20000
elif eeprom_size == 2:
end_addr = 0x40000
elif eeprom_size == 3:
end_addr = 0x60000
elif eeprom_size == 4:
end_addr = 0x80000
target_eeprom_offset = 0x2000
if eeprom_size > 0:
target_eeprom_offset = 0x20000 * eeprom_size
else:
end_addr = 0x2000
target_eeprom_offset = 0x2000


with serial.Serial(serial_port_text, 38400, timeout=2) as serial_port:
serial_check = check_serial_port(serial_port, False)
if not serial_check.status:
Expand All @@ -675,21 +669,21 @@ def backups_eeprom(serial_port_text: str, window: tk.Tk, progress: ttk.Progressb
return

start_addr = 0x0 # 起始地址为0x0
total_steps = (end_addr - start_addr) // 128 # 计算总步数
total_steps = (target_eeprom_offset - start_addr) // 128 # 计算总步数
current_step = 0
addr = start_addr
offset = 0

backups_data = b''
backup_data = b''

while addr < end_addr: # 限制地址范围为start_addr到end_addr
if end_addr < 0x10000:
while addr < target_eeprom_offset: # 限制地址范围为start_addr到target_eeprom_offset
if target_eeprom_offset < 0x10000:
read_data = serial_utils.read_eeprom(serial_port, addr, 128)
else:
if addr - offset * 0x10000 >= 0x10000:
offset += 1
read_data = serial_utils.read_extra_eeprom(serial_port, offset, addr - offset * 0x10000, 128)
backups_data += read_data
backup_data += read_data
addr += 128
current_step += 1
percent_float = (current_step / total_steps) * 100
Expand All @@ -707,9 +701,9 @@ def backups_eeprom(serial_port_text: str, window: tk.Tk, progress: ttk.Progressb
return # 用户取消保存,直接返回

with open(file_path, 'wb') as fp:
fp.write(backups_data)
fp.write(backup_data)

log('备份完成')
log('EEPROM 备份完成')
status_label['text'] = '当前操作: 无'
messagebox.showinfo('提示', '保存成功!')

Expand All @@ -725,17 +719,11 @@ def restore_eeprom(serial_port_text: str, window: tk.Tk, progress: ttk.Progressb
return

start_addr = 0x0

if eeprom_size == 1:
end_addr = 0x20000
elif eeprom_size == 2:
end_addr = 0x40000
elif eeprom_size == 3:
end_addr = 0x60000
elif eeprom_size == 4:
end_addr = 0x80000
target_eeprom_offset = 0x2000
if eeprom_size > 0:
target_eeprom_offset = 0x20000 * eeprom_size
else:
end_addr = 0x2000
target_eeprom_offset = 0x2000

file_path = filedialog.askopenfilename(filetypes=[("Binary files", "*.bin"), ("All files", "*.*")])

Expand All @@ -747,6 +735,12 @@ def restore_eeprom(serial_port_text: str, window: tk.Tk, progress: ttk.Progressb
with open(file_path, 'rb') as fp:
restore_data = fp.read()

if len(restore_data) != target_eeprom_offset:
if messagebox.askquestion('提示', f'选择的文件大小为{len(restore_data)},但目标eeprom大小为{target_eeprom_offset},是否继续?') == 'no':
log('用户取消恢复')
messagebox.showinfo('提示', '用户取消恢复')
return # 用户取消恢复,直接返回

with serial.Serial(serial_port_text, 38400, timeout=2) as serial_port:
serial_check = check_serial_port(serial_port, False)
if not serial_check.status:
Expand All @@ -756,7 +750,7 @@ def restore_eeprom(serial_port_text: str, window: tk.Tk, progress: ttk.Progressb

write_data(serial_port, start_addr, restore_data, progress, window)

log('写入校准参数完成')
log('EEPROM恢复完成')
status_label['text'] = '当前操作: 无'
serial_utils.reset_radio(serial_port)
messagebox.showinfo('提示', '写入成功!')
13 changes: 8 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
write_config,
write_pinyin_index,
todo_function,
backups_eeprom,
backup_eeprom,
restore_eeprom
)

Expand Down Expand Up @@ -340,15 +340,15 @@ def main():
)
write_pinyin_new_index_button.pack(side='left', padx=3, pady=(2, 15))

backups_eeprom_button = tk.Button(
backup_eeprom_button = tk.Button(
frame7,
text='备份EEPROM',
width=14,
command=lambda:backups_eeprom(
command=lambda:backup_eeprom(
serial_port_combo.get(), window, progress, label2,EEPROM_SIZE.index(eeprom_size_combo.get())
)
)
backups_eeprom_button.pack(side='left', padx=3, pady=(2, 15))
backup_eeprom_button.pack(side='left', padx=3, pady=(2, 15))

restore_eeprom_button = tk.Button(
frame7,
Expand Down Expand Up @@ -387,7 +387,7 @@ def main():

# 在此统一设置tooltip
Tooltip(serial_port_combo, "点击选择K5/K6所在串口")
Tooltip(eeprom_size_combo, "EEPROM芯片容量,若自动检测正确则无需修改")
Tooltip(eeprom_size_combo, "EEPROM芯片容量,若自动检测正确则无需修改,可以使用该下拉框修改所需备份或恢复的容量")
Tooltip(firmware_combo, "固件版本,若自动检测正确则无需修改")
Tooltip(clean_eeprom_button, "清除EEPROM中的所有数据")
Tooltip(auto_write_font_button, "自动写入机器固件所需字库等文件,如不清楚点那个按钮的情况下,点这个总没错")
Expand All @@ -402,6 +402,9 @@ def main():
Tooltip(write_font_old_button, "萝狮虎117版本及之前版本使用,旧字库")
Tooltip(write_pinyin_old_index_button, "123版本拼音索引,如果不使用自动写入,请在执行完字库写入后点击")
Tooltip(write_pinyin_new_index_button, "124及以上版本拼音索引,如果不使用自动写入,请在执行完字库写入后点击")
Tooltip(backup_eeprom_button, "备份EEPROM中的数据,使用EEPROM下拉框可以选择所要备份的大小")
Tooltip(restore_eeprom_button, "恢复EEPROM中的数据,使用EEPROM下拉框可以选择所要恢复的大小")
Tooltip(todo_button, "敬请期待")
window.mainloop()


Expand Down

0 comments on commit 18ea8e0

Please sign in to comment.