Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

同步 #1

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 40 additions & 80 deletions functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import dataclasses
import random
import struct
from typing import Union, List

from serial import Serial

from const_vars import FIRMWARE_VERSION_LIST, EEPROM_SIZE
import serial_utils
Expand Down Expand Up @@ -97,6 +100,31 @@ def serial_port_combo_callback(_, serial_port: str, status_label: tk.Label, eepr
status_label['text'] = '当前操作: 无'


def write_data(serial_port: Serial, start_addr: int, data: Union[bytes, List[int]],
progress: ttk.Progressbar, window: tk.Tk, step: int = 128):
data_len = len(data)
total_page = data_len // 128
addr = start_addr
current_step = 0
offset = 0
while addr < start_addr + data_len:
percent_float = (current_step / total_page) * 100
percent = int(percent_float)
progress['value'] = percent
log(f'进度: {percent_float:.1f}%, addr={hex(addr)}', '')
window.update()

writing_data = bytes(data[:step])
data = data[step:]
if addr - offset * 0x10000 >= 0x10000:
offset += 1
serial_utils.write_extra_eeprom(serial_port, offset, addr - offset * 0x10000, writing_data)
addr += step
current_step += 1
progress['value'] = 0
window.update()


def clean_eeprom(serial_port: str, window: tk.Tk, progress: ttk.Progressbar, status_label: tk.Label, eeprom_size: int,
firmware_version: int):
log('开始清空EEPROM流程')
Expand Down Expand Up @@ -136,22 +164,7 @@ def clean_eeprom(serial_port: str, window: tk.Tk, progress: ttk.Progressbar, sta
target_eeprom_offset = 0x2000
if eeprom_size > 0:
target_eeprom_offset = 0x20000 * eeprom_size
total_steps = target_eeprom_offset // 128
current_step = 0
addr = 0x0
offset = 0x0
while addr < target_eeprom_offset:
percent_float = (current_step / total_steps) * 100
percent = int(percent_float)
progress['value'] = percent
log(f'进度: {percent_float:.1f}%, addr={hex(addr)}', '')
window.update()

if addr - offset * 0x10000 >= 0x10000:
offset += 1
serial_utils.write_extra_eeprom(serial_port, offset, addr - offset * 0x10000, b'\xff' * 128)
addr += 128
current_step += 1
write_data(serial_port, 0, b'\xff' * target_eeprom_offset, progress, window)
progress['value'] = 0
window.update()
serial_utils.reset_radio(serial_port)
Expand All @@ -161,9 +174,9 @@ def clean_eeprom(serial_port: str, window: tk.Tk, progress: ttk.Progressbar, sta


def write_font(serial_port_text: str, window: tk.Tk, progress: ttk.Progressbar, status_label: tk.Label,
eeprom_size: int, firmware_version: int, compress: bool = False):
eeprom_size: int, firmware_version: int, compress: bool = False, old_font: bool = False):
log('开始写入字库流程')
font_version = 'H' if not compress else 'K'
font_version = ('H' if not compress else 'K') if not old_font else '旧'
log(f'字库版本: {font_version}')
log('选择的串口: ' + serial_port_text)
status_label['text'] = f'当前操作: 写入字库 ({font_version})'
Expand Down Expand Up @@ -203,30 +216,14 @@ def write_font(serial_port_text: str, window: tk.Tk, progress: ttk.Progressbar,
status_label['text'] = '当前操作: 无'
return

if compress:
font_data = font.GB2312_COMPRESSED
if old_font:
font_data = font.OLD_FONT
else:
font_data = font.GB2312_UNCOMPRESSED
font_len = len(font_data)
total_page = font_len // 128
addr = 0x2E00
current_step = 0
offset = 0
print(font_len)
while addr < 0x2E00 + font_len:
percent_float = (current_step / total_page) * 100
percent = int(percent_float)
progress['value'] = percent
log(f'进度: {percent_float:.1f}%, addr={hex(addr)}', '')
window.update()

write_data = bytes(font_data[:128])
font_data = font_data[128:]
if addr - offset * 0x10000 >= 0x10000:
offset += 1
serial_utils.write_extra_eeprom(serial_port, offset, addr - offset * 0x10000, write_data)
addr += 128
current_step += 1
if compress:
font_data = font.GB2312_COMPRESSED
else:
font_data = font.GB2312_UNCOMPRESSED
write_data(serial_port, 0x2E00, font_data, progress, window)
progress['value'] = 0
window.update()
serial_utils.reset_radio(serial_port)
Expand Down Expand Up @@ -270,26 +267,7 @@ def write_font_conf(serial_port_text: str, window: tk.Tk, progress: ttk.Progress
messagebox.showinfo('EEPROM大小不足', msg)
status_label['text'] = '当前操作: 无'
return
font_data = font.FONT_CONF
font_len = len(font_data)
total_page = font_len // 128
addr = 0x2480
current_step = 0
offset = 0
while addr < 0x2480 + font_len:
percent_float = (current_step / total_page) * 100
percent = int(percent_float)
progress['value'] = percent
log(f'进度: {percent_float:.1f}%, addr={hex(addr)}', '')
window.update()

write_data = bytes(font_data[:128])
font_data = font_data[128:]
if addr - offset * 0x10000 >= 0x10000:
offset += 1
serial_utils.write_extra_eeprom(serial_port, offset, addr - offset * 0x10000, write_data)
addr += 128
current_step += 1
write_data(serial_port, 0x2480, font.FONT_CONF, progress, window)
progress['value'] = 0
window.update()
serial_utils.reset_radio(serial_port)
Expand Down Expand Up @@ -335,25 +313,7 @@ def write_tone_options(serial_port_text: str, window: tk.Tk, progress: ttk.Progr
data = b''
for tone_data in tone.CTCSS_OPTIONS + tone.DCS_OPTIONS:
data += struct.pack('<H', tone_data)
data_len = len(data)
total_page = data_len // 128
addr = 0x2C00
current_step = 0
offset = 0
while addr < 0x2C00 + data_len:
percent_float = (current_step / total_page) * 100
percent = int(percent_float)
progress['value'] = percent
log(f'进度: {percent_float:.1f}%, addr={hex(addr)}', '')
window.update()

write_data = data[:128]
data = data[128:]
if addr - offset * 0x10000 >= 0x10000:
offset += 1
serial_utils.write_extra_eeprom(serial_port, offset, addr - offset * 0x10000, write_data)
addr += 128
current_step += 1
write_data(serial_port, 0x2C00, data, progress, window)
progress['value'] = 0
window.update()
serial_utils.reset_radio(serial_port)
Expand Down
25 changes: 18 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
write_font_conf, write_tone_options

window = tk.Tk()
version = '0.1'
version = '0.2'


class TextRedirector(tk.Text):
Expand Down Expand Up @@ -65,25 +65,36 @@ def main():
)
clean_eeprom_button.place(x=10, y=100)

write_font_old_button = tk.Button(
write_font_k_button = tk.Button(
window,
text='写入字库 (K)',
command=lambda: write_font(
serial_port_combo.get(), window, progress, label2,
EEPROM_SIZE.index(eeprom_size_combo.get()), FIRMWARE_VERSION_LIST.index(firmware_combo.get()), True
)
)
write_font_old_button.place(x=105, y=100)
write_font_k_button.place(x=105, y=100)

write_font_new_button = tk.Button(
write_font_h_button = tk.Button(
window,
text='写入字库 (H)',
command=lambda: write_font(
serial_port_combo.get(), window, progress, label2,
EEPROM_SIZE.index(eeprom_size_combo.get()), FIRMWARE_VERSION_LIST.index(firmware_combo.get()), False
)
)
write_font_new_button.place(x=193, y=100)
write_font_h_button.place(x=193, y=100)

write_font_old_button = tk.Button(
window,
text='写入字库 (旧)',
command=lambda: write_font(
serial_port_combo.get(), window, progress, label2,
EEPROM_SIZE.index(eeprom_size_combo.get()), FIRMWARE_VERSION_LIST.index(firmware_combo.get()),
True, True
)
)
write_font_old_button.place(x=282, y=100)

write_font_conf_button = tk.Button(
window,
Expand All @@ -93,7 +104,7 @@ def main():
EEPROM_SIZE.index(eeprom_size_combo.get()), FIRMWARE_VERSION_LIST.index(firmware_combo.get())
)
)
write_font_conf_button.place(x=282, y=100)
write_font_conf_button.place(x=10, y=140)

write_tone_options_button = tk.Button(
window,
Expand All @@ -103,7 +114,7 @@ def main():
EEPROM_SIZE.index(eeprom_size_combo.get()), FIRMWARE_VERSION_LIST.index(firmware_combo.get())
)
)
write_tone_options_button.place(x=10, y=140)
write_tone_options_button.place(x=105, y=140)

textbox = tk.Text(window, width=56, height=10)
textbox.place(x=10, y=185)
Expand Down
2 changes: 1 addition & 1 deletion nuitka_build.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nuitka --standalone --onefile --windows-disable-console --include-data-dir="resources/"="resources/" --enable-plugin=tk-inter main.py
nuitka --standalone --windows-disable-console --include-data-dir="resources/"="resources/" --enable-plugin=tk-inter main.py
1 change: 1 addition & 0 deletions nuitka_build_onefile.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nuitka --standalone --onefile --windows-disable-console --include-data-dir="resources/"="resources/" --enable-plugin=tk-inter main.py
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "k5-tools"
version = "0.1.0"
version = "0.2.0"
description = ""
authors = ["hank9999 <[email protected]>"]
readme = "README.md"
Expand Down
Loading