diff --git a/src/dpds_stdlib_1767893844.txt b/src/dpds_stdlib_1767893844.txt new file mode 100644 index 0000000..29c16b6 --- /dev/null +++ b/src/dpds_stdlib_1767893844.txt @@ -0,0 +1,53 @@ + + +REM_BLOCK + Should not have HARD CODED memory address + Must be compatible with all duckyScript and duckyPad versions + + DPDSSTDLIB + + TODO: + bitread, set, clear, toggle? + math abs, min, max? + memcpy? + + show error and quit? for unsupported hardwares +END_REM + +FUN WAITKEY(key_id) + WHILE 1 + VAR pressed = _BLOCKING_READKEY + IF pressed == key_id + RETURN 1 + END_IF + END_WHILE +END_FUN + +FUN TRY_SET_LED(target_mask) + // --- Num Lock (Bit 0) --- + VAR want_num = target_mask & 1 + IF want_num != _IS_NUMLOCK_ON + NUMLOCK + END_IF + // --- Caps Lock (Bit 1) --- + VAR want_caps = target_mask >> 1 + want_caps = want_caps & 1 + IF want_caps != _IS_CAPSLOCK_ON + CAPSLOCK + END_IF + // --- Scroll Lock (Bit 2) --- + VAR want_scroll = target_mask >> 2 + want_scroll = want_scroll & 1 + IF want_scroll != _IS_SCROLLLOCK_ON + SCROLLLOCK + END_IF +END_FUN + +FUN MEMSET(addr, value, length) + VAR i = 0 + WHILE i < length + POKE8(addr + i, value) + i = i + 1 + END_WHILE +END_FUN + diff --git a/src/ds_stdlib.py b/src/ds_stdlib.py index bafb194..c1e79a5 100644 --- a/src/ds_stdlib.py +++ b/src/ds_stdlib.py @@ -1,3 +1,6 @@ +import os +import time + default_stdlib_code = """ REM_BLOCK @@ -51,4 +54,51 @@ FUN MEMSET(addr, value, length) END_WHILE END_FUN -""" \ No newline at end of file +""" + +std_lib_filename_prefix = "dpds_stdlib" + +def ensure_dpds_stdlib(lib_dir_path): + os.makedirs(lib_dir_path, exist_ok=True) + file_exists = False + for filename in os.listdir(lib_dir_path): + if filename.startswith(std_lib_filename_prefix) and filename.endswith(".txt"): + file_exists = True + break + + if not file_exists: + timestamp = int(time.time()) + new_filename = f"{std_lib_filename_prefix}_{timestamp}.txt" + file_path = os.path.join(lib_dir_path, new_filename) + with open(file_path, 'w') as f: + f.write(default_stdlib_code) + print(f"Created new stdlib file: {file_path}") + +def get_latest_stdlib_lines(lib_dir_path): + """ + Finds the file with the newest timestamp in lib_dir_path and returns its content. + Returns default_stdlib_code (as list) on any error. + """ + try: + candidates = [] + search_prefix = f"{std_lib_filename_prefix}_" + + for filename in os.listdir(lib_dir_path): + if filename.startswith(search_prefix) and filename.endswith(".txt"): + timestamp_str = filename[len(search_prefix):-4] + try: + timestamp = int(timestamp_str) + candidates.append((timestamp, filename)) + except ValueError: + continue + + if not candidates: + raise FileNotFoundError("No matching library files found.") + _, newest_filename = max(candidates, key=lambda item: item[0]) + + full_path = os.path.join(lib_dir_path, newest_filename) + with open(full_path, 'r', encoding='utf-8') as f: + return f.read().splitlines() + except Exception as e: + print("get_latest_stdlib_lines:", e) + return default_stdlib_code.splitlines() diff --git a/src/dsvm_preprocessor.py b/src/dsvm_preprocessor.py index 2efc463..fd0c0e9 100644 --- a/src/dsvm_preprocessor.py +++ b/src/dsvm_preprocessor.py @@ -78,8 +78,8 @@ def new_define(pgm_line, dd): is_valid, comment = is_valid_var_name(define_source) if is_valid is False: return PARSE_ERROR, comment - # if define_source in dd and define_destination != dd[define_source]: - # return PARSE_ERROR, f"{define_source} is already defined" + if define_source in dd and define_destination != dd[define_source]: + return PARSE_ERROR, f"{define_source} is already defined" dd[define_source] = define_destination return PARSE_OK, '' diff --git a/src/duck_objs.py b/src/duck_objs.py index 5899999..7b750da 100644 --- a/src/duck_objs.py +++ b/src/duck_objs.py @@ -4,205 +4,206 @@ from shared import * # from dataclasses import dataclass class dp_key(object): - def __str__(self): - ret = "" - ret += str('...............') + '\n' - ret += "path:\t" + str(self.path) + '\n' - ret += "name:\t" + str(self.name) + '\n' - ret += "name2:\t" + str(self.name_line2) + '\n' - ret += "index:\t" + str(self.index) + '\n' - ret += "color:\t" + str(self.color) + '\n' - ret += "abort:\t" + str(self.allow_abort) + '\n' - ret += "norep:\t" + str(self.dont_repeat) + '\n' - ret += "scr:\t" + str(len(self.script)) + " characters\n" - ret += "scr_r:\t" + str(len(self.script_on_release)) + " characters\n" - ret += str('...............') + '\n' - return ret + def __str__(self): + ret = "" + ret += str('...............') + '\n' + ret += "path:\t" + str(self.path) + '\n' + ret += "name:\t" + str(self.name) + '\n' + ret += "name2:\t" + str(self.name_line2) + '\n' + ret += "index:\t" + str(self.index) + '\n' + ret += "color:\t" + str(self.color) + '\n' + ret += "abort:\t" + str(self.allow_abort) + '\n' + ret += "norep:\t" + str(self.dont_repeat) + '\n' + ret += "scr:\t" + str(len(self.script)) + " characters\n" + ret += "scr_r:\t" + str(len(self.script_on_release)) + " characters\n" + ret += str('...............') + '\n' + return ret - def __init__(self, path_on_press=None, path_on_release=None): - self.path = path_on_press - self.path_on_release = path_on_release - self.name = None - self.name_line2 = None - self.index = None - self.color = None - self.script = '' - self.script_on_release = '' - self.binary_array = None - self.binary_array_on_release = None - self.allow_abort = False - self.dont_repeat = False + def __init__(self, path_on_press=None, path_on_release=None): + self.path = path_on_press + self.path_on_release = path_on_release + self.name = None + self.name_line2 = None + self.index = None + self.color = None + self.script = '' + self.script_on_release = '' + self.binary_array = None + self.binary_array_on_release = None + self.allow_abort = False + self.dont_repeat = False # ----------------------------------------------------------- def get_script(path): - if path is None or os.path.exists(path) is False: - return "" - try: - with open(path, encoding='utf8') as keyfile: - return keyfile.read() - except Exception as e: - print('get_script exception:', e) - return "" + if path is None or os.path.exists(path) is False: + return "" + try: + with open(path, encoding='utf8') as keyfile: + return keyfile.read() + except Exception as e: + print('get_script exception:', e) + return "" class dp_profile(object): - def add_key_if_doesnt_exist(self, index): - if self.keylist[index] is None: - self.keylist[index] = dp_key() - self.keylist[index].index = index + def add_key_if_doesnt_exist(self, index): + if self.keylist[index] is None: + self.keylist[index] = dp_key() + self.keylist[index].index = index - def read_config(self, path): - try: - with open(os.path.join(path, "config.txt")) as configfile: - for line in configfile: - line = line.replace('\n', '').replace('\r', '') - while(' ' in line): - line = line.replace(' ', ' ') - this_split = line.split(' ', 1) - if line.startswith('BG_COLOR '): - temp_split = line.split(' ') - self.bg_color = (int(temp_split[1]), int(temp_split[2]), int(temp_split[3])) - elif line.startswith('KEYDOWN_COLOR '): - temp_split = line.split(' ') - self.kd_color = (int(temp_split[1]), int(temp_split[2]), int(temp_split[3])) - elif line.startswith("DIM_UNUSED_KEYS 0"): - self.dim_unused = False - elif line.startswith("IS_LANDSCAPE 1"): - self.is_landscape = True - elif line.startswith("UPPER_HS 1"): - self.is_upper_re_halfstep = True - elif line.startswith("LOWER_HS 1"): - self.is_lower_re_halfstep = True - elif this_split[0].startswith('z'): - this_index = int(this_split[0][1:]) - 1 - self.add_key_if_doesnt_exist(this_index) - self.keylist[this_index].name = this_split[1] - elif this_split[0].startswith('x'): - this_index = int(this_split[0][1:]) - 1 - self.add_key_if_doesnt_exist(this_index) - self.keylist[this_index].name_line2 = this_split[1] - elif this_split[0].startswith('ab'): - this_index = int(this_split[1]) - 1 - self.add_key_if_doesnt_exist(this_index) - self.keylist[this_index].allow_abort = True - elif this_split[0].startswith('dr'): - this_index = int(this_split[1]) - 1 - self.add_key_if_doesnt_exist(this_index) - self.keylist[this_index].dont_repeat = True - elif this_split[0].startswith('SWCOLOR_'): - this_index = int(this_split[0].split("_")[-1]) - 1 - self.add_key_if_doesnt_exist(this_index) - temp_split = line.split(' ') - self.keylist[this_index].color = (int(temp_split[1]), int(temp_split[2]), int(temp_split[3])) - except Exception as e: - print('>>>>> read_config:', path, e) - pass + def read_config(self, path): + try: + with open(os.path.join(path, "config.txt")) as configfile: + for line in configfile: + line = line.replace('\n', '').replace('\r', '') + while(' ' in line): + line = line.replace(' ', ' ') + this_split = line.split(' ', 1) + if line.startswith('BG_COLOR '): + temp_split = line.split(' ') + self.bg_color = (int(temp_split[1]), int(temp_split[2]), int(temp_split[3])) + elif line.startswith('KEYDOWN_COLOR '): + temp_split = line.split(' ') + self.kd_color = (int(temp_split[1]), int(temp_split[2]), int(temp_split[3])) + elif line.startswith("DIM_UNUSED_KEYS 0"): + self.dim_unused = False + elif line.startswith("IS_LANDSCAPE 1"): + self.is_landscape = True + elif line.startswith("UPPER_HS 1"): + self.is_upper_re_halfstep = True + elif line.startswith("LOWER_HS 1"): + self.is_lower_re_halfstep = True + elif this_split[0].startswith('z'): + this_index = int(this_split[0][1:]) - 1 + self.add_key_if_doesnt_exist(this_index) + self.keylist[this_index].name = this_split[1] + elif this_split[0].startswith('x'): + this_index = int(this_split[0][1:]) - 1 + self.add_key_if_doesnt_exist(this_index) + self.keylist[this_index].name_line2 = this_split[1] + elif this_split[0].startswith('ab'): + this_index = int(this_split[1]) - 1 + self.add_key_if_doesnt_exist(this_index) + self.keylist[this_index].allow_abort = True + elif this_split[0].startswith('dr'): + this_index = int(this_split[1]) - 1 + self.add_key_if_doesnt_exist(this_index) + self.keylist[this_index].dont_repeat = True + elif this_split[0].startswith('SWCOLOR_'): + this_index = int(this_split[0].split("_")[-1]) - 1 + self.add_key_if_doesnt_exist(this_index) + temp_split = line.split(' ') + self.keylist[this_index].color = (int(temp_split[1]), int(temp_split[2]), int(temp_split[3])) + except Exception as e: + print('>>>>> read_config:', path, e) + pass - def load_from_path(self, path): - folder_name = os.path.basename(os.path.normpath(path)) - if not folder_name.startswith('profile'): - print("invalid profile folder:", folder_name) - return - self.path = path - self.name = folder_name.split('_', 1)[-1] - self.read_config(path) - for this_key in self.keylist: - if this_key is not None: - on_press_path = os.path.join(path, f'key{this_key.index+1}.txt') - on_release_path = os.path.join(path, f'key{this_key.index+1}-release.txt') - this_key.script = get_script(on_press_path) - this_key.script_on_release = get_script(on_release_path) + def load_from_path(self, path): + folder_name = os.path.basename(os.path.normpath(path)) + if not folder_name.startswith('profile'): + print("invalid profile folder:", folder_name) + return + self.path = path + self.name = folder_name.split('_', 1)[-1] + self.read_config(path) + for this_key in self.keylist: + if this_key is not None: + on_press_path = os.path.join(path, f'key{this_key.index+1}.txt') + on_release_path = os.path.join(path, f'key{this_key.index+1}-release.txt') + this_key.script = get_script(on_press_path) + this_key.script_on_release = get_script(on_release_path) - def __str__(self): - ret = "" - ret += str('-----Profile info-----') + '\n' - ret += "path:\t" + str(self.path) + '\n' - ret += "name:\t" + str(self.name) + '\n' - ret += "bg_color:\t" + str(self.bg_color) + '\n' - ret += "kd_color:\t" + str(self.kd_color) + '\n' - ret += "dim_unused:\t" + str(self.dim_unused) + '\n' - ret += "key count:\t" + str(len([x for x in self.keylist if x is not None])) + '\n' - # ret += "keys:\n" - # for item in [x for x in self.keylist]: - # ret += str(item) + '\n' - # ret += str('----------------------') + '\n' - return ret + def __str__(self): + ret = "" + ret += str('-----Profile info-----') + '\n' + ret += "path:\t" + str(self.path) + '\n' + ret += "name:\t" + str(self.name) + '\n' + ret += "bg_color:\t" + str(self.bg_color) + '\n' + ret += "kd_color:\t" + str(self.kd_color) + '\n' + ret += "dim_unused:\t" + str(self.dim_unused) + '\n' + ret += "key count:\t" + str(len([x for x in self.keylist if x is not None])) + '\n' + # ret += "keys:\n" + # for item in [x for x in self.keylist]: + # ret += str(item) + '\n' + # ret += str('----------------------') + '\n' + return ret - def __init__(self): - super(dp_profile, self).__init__() - self.path = None - self.name = None - self.keylist = [None] * MAX_KEY_COUNT - self.bg_color = (84,22,180) - self.kd_color = None - self.dim_unused = True - self.is_landscape = False - self.is_upper_re_halfstep = False - self.is_lower_re_halfstep = False + def __init__(self): + super(dp_profile, self).__init__() + self.path = None + self.name = None + self.keylist = [None] * MAX_KEY_COUNT + self.bg_color = (84,22,180) + self.kd_color = None + self.dim_unused = True + self.is_landscape = False + self.is_upper_re_halfstep = False + self.is_lower_re_halfstep = False def read_profile_order_file(txt_path): - profile_num_dict = {} - with open(txt_path) as fff: - for line in fff: - line = line.strip(" \r\n") - line_split = line.split(" ", 1) - try: - pf_number = int(line_split[0]) - pf_name = line_split[1] - except Exception as e: - continue - if pf_number >= MAX_PROFILE_COUNT: - continue - profile_num_dict[pf_name] = pf_number - profile_info_list = [] - for key in profile_num_dict: - profile_info_list.append((profile_num_dict[key], key)) - profile_info_list.sort(key=lambda tup: tup[0]) - return profile_info_list + profile_num_dict = {} + with open(txt_path) as fff: + for line in fff: + line = line.strip(" \r\n") + line_split = line.split(" ", 1) + try: + pf_number = int(line_split[0]) + pf_name = line_split[1] + except Exception as e: + continue + if pf_number >= MAX_PROFILE_COUNT: + continue + profile_num_dict[pf_name] = pf_number + profile_info_list = [] + for key in profile_num_dict: + profile_info_list.append((profile_num_dict[key], key)) + profile_info_list.sort(key=lambda tup: tup[0]) + return profile_info_list def build_profile_with_pfinfo_txt(root_dir_path, profile_info_txt_path): - my_dirs = [d for d in os.listdir(root_dir_path) if os.path.isdir(os.path.join(root_dir_path, d))] - my_dirs = [x for x in my_dirs if x.startswith('profile_')] - profile_info_list = read_profile_order_file(profile_info_txt_path) + my_dirs = [d for d in os.listdir(root_dir_path) if os.path.isdir(os.path.join(root_dir_path, d))] + my_dirs = [x for x in my_dirs if x.startswith('profile_')] + profile_info_list = read_profile_order_file(profile_info_txt_path) - profile_list = [] - for item in profile_info_list: - pf_number = item[0] - pf_name = item[1] - this_profile_folder_name = f"profile_{pf_name}" - if this_profile_folder_name not in my_dirs: - continue - this_profile_folder_path = os.path.join(root_dir_path, this_profile_folder_name) - this_profile = dp_profile() - this_profile.load_from_path(this_profile_folder_path) - profile_list.append(this_profile) + profile_list = [] + for item in profile_info_list: + pf_number = item[0] + pf_name = item[1] + this_profile_folder_name = f"profile_{pf_name}" + if this_profile_folder_name not in my_dirs: + continue + this_profile_folder_path = os.path.join(root_dir_path, this_profile_folder_name) + this_profile = dp_profile() + this_profile.load_from_path(this_profile_folder_path) + profile_list.append(this_profile) - return profile_list + return profile_list def build_profile_without_pfinfo_txt(root_dir_path): - my_dirs = [d for d in os.listdir(root_dir_path) if os.path.isdir(os.path.join(root_dir_path, d))] - my_dirs = [x for x in my_dirs if x.startswith('profile') and x[7].isnumeric() and '_' in x] - my_dirs.sort(key=lambda s: int(s[7:].split("_")[0])) - my_dirs = [os.path.join(root_dir_path, d) for d in my_dirs if d.startswith("profile")] - my_dirs = my_dirs[:MAX_PROFILE_COUNT] - profile_list = [] - for item in my_dirs: - this_profile = dp_profile() - this_profile.load_from_path(item) - profile_list.append(this_profile) - return profile_list + my_dirs = [d for d in os.listdir(root_dir_path) if os.path.isdir(os.path.join(root_dir_path, d))] + my_dirs = [x for x in my_dirs if x.startswith('profile') and x[7].isnumeric() and '_' in x] + my_dirs.sort(key=lambda s: int(s[7:].split("_")[0])) + my_dirs = [os.path.join(root_dir_path, d) for d in my_dirs if d.startswith("profile")] + my_dirs = my_dirs[:MAX_PROFILE_COUNT] + profile_list = [] + for item in my_dirs: + this_profile = dp_profile() + this_profile.load_from_path(item) + profile_list.append(this_profile) + return profile_list def build_profile(root_dir_path): - profile_info_txt_path = os.path.join(root_dir_path, profile_info_dot_txt) - if os.path.exists(profile_info_txt_path) and os.path.isfile(profile_info_txt_path): - return build_profile_with_pfinfo_txt(root_dir_path, profile_info_txt_path) - return build_profile_without_pfinfo_txt(root_dir_path) + profile_info_txt_path = os.path.join(root_dir_path, profile_info_dot_txt) + if os.path.exists(profile_info_txt_path) and os.path.isfile(profile_info_txt_path): + return build_profile_with_pfinfo_txt(root_dir_path, profile_info_txt_path) + return build_profile_without_pfinfo_txt(root_dir_path) def import_profile_single(root_dir_path): - this_profile = dp_profile() - this_profile.load_from_path(root_dir_path) - return this_profile + this_profile = dp_profile() + this_profile.load_from_path(root_dir_path) + return this_profile class dp_global_settings: - def __init__(self, user_header_line_list=None): - self.user_header_line_list = user_header_line_list \ No newline at end of file + def __init__(self, user_header_line_list=None, stdlib_line_list=None): + self.user_header_line_list = user_header_line_list + self.stdlib_line_list = stdlib_line_list \ No newline at end of file diff --git a/src/duckypad_config.py b/src/duckypad_config.py index a1f63a9..b94177f 100644 --- a/src/duckypad_config.py +++ b/src/duckypad_config.py @@ -206,7 +206,10 @@ def scaled_size(size: int) -> int: ensure_dir(app_save_path) ensure_dir(backup_path) ensure_dir(hid_dump_path) -ensure_dir(ext_lib_path) + +ext_lib_path = "./" +ensure_dpds_stdlib(ext_lib_path) +this_global_setting.stdlib_line_list = get_latest_stdlib_lines(ext_lib_path) print("\n\n--------------------------") print("\n\nWelcome to duckyPad Configurator!\n") diff --git a/src/shared.py b/src/shared.py index a45869e..6c627cc 100644 --- a/src/shared.py +++ b/src/shared.py @@ -26,12 +26,6 @@ hid_dump_path = os.path.join(app_save_path, "hid_dump") temp_dir_path = os.path.join(app_save_path, "temp_dir") ext_lib_path = os.path.join(app_save_path, "dpds_libs") -std_lib_filename_prefix = "dpds_stdlib" - -# -def ensure_dpds_stdlib(): - pass - def open_discord_link(): webbrowser.open("https://discord.gg/4sJCBx5")