From 54857cff84c23fc060c98a7983f5aad674e23d5f Mon Sep 17 00:00:00 2001 From: elral <3263285+elral@users.noreply.github.com> Date: Wed, 12 Feb 2025 12:42:51 +0100 Subject: [PATCH] Add script for Renaming Template names according to user requirement (#13) --- README.md | 60 ++------ .../mobiflight_template_micro.board.json | 6 +- .../mobiflight_template_nano.board.json | 6 +- ...iflight_template_raspberry_pico.board.json | 2 +- .../boards/mobiflight_template_uno.board.json | 6 +- ...e.json => mobiflight_template.device.json} | 0 ....json => mobiflight_template2.device.json} | 0 copy_fw_files.py | 17 +++ renaming.py | 136 ++++++++++++++++++ 9 files changed, 171 insertions(+), 62 deletions(-) rename Template/Community/devices/{mobiflight.template.device.json => mobiflight_template.device.json} (100%) rename Template/Community/devices/{mobiflight.template2.device.json => mobiflight_template2.device.json} (100%) create mode 100644 renaming.py diff --git a/README.md b/README.md index 8045f15..02d7537 100644 --- a/README.md +++ b/README.md @@ -5,63 +5,19 @@ * MobiFlight firmware development is done with [VSCode](https://code.visualstudio.com/) and the [PlatformIO](https://platformio.org) extension. Make sure to install both. * **Create** a new reposority by using the [Community Template repository](https://github.com/MobiFlight/CommunityTemplate)* +* Clone this reposority and open it in PlatformIO. Cloning could be directly done within PlatformIO, Follow the next steps carefully! ## Preparing the firmware -1. Rename the folder `/Template`, the name should reflect your device -2. Do **not** rename `MFCustomDevice.cpp/.h` as these filenames are required from the existing code. -4. Rename `MyCustomClass.cpp` and `MyCustomClass.h` to a name which fits to your community device. -5. Rename `MyCustomDevice_platformio.ini` to a name which fits to your community device (see step 1) but keep `_platformio.ini`, e.g. `YourChangedName_platformio.ini` -6. Rename the include path's from `MyCustomClass.cpp` and `MFCustomDevice.h` from `#include MyCustomClass.h` to `#include YourChangedName.h` -7. Adapt the `YourChangedName_platformio.ini`: - 1. Decide which board should be supported. The template is set up for the Mega and Pico. Is one of them is not needed, delete this part which is called in the following _environment_. - 2. Rename `[env_template]` to your community device and change `-I./Template` to the path where your sources are located (see 1.). - 3. Define which core firmware version should be used, e.g. `custom_core_firmware_version = 2.5.1` for the latest release as for now. - 4. Rename `Template` from `custom_device_folder` to the folder of your sources (see 1.) It's required again for copying the firmware files and creating the ZIP file (see below). - 5. Rename `Your_Project` from `custom_community_project`, this name will be the name of the generated ZIP file. - 6. rename each required environment from `[env:mobiflight_template_xyz]` to your community device. It should be like `[env:YourCommunityDevice_board]` - 7. Rename `-I./Template` under `build_flags` to your choosed folder name. - 8. Rename `+<../Template>` under `build_src_filter` to your choosed folder name. - 9. Rename `'-DMOBIFLIGHT_TYPE="Mobiflight Template Mega"'` to your needs. It should be like `'-DMOBIFLIGHT_TYPE="YourDevice_board Mega"'`. This is important and this must match with "MobiFlightType" within the .json files (see below). - 10. Rename `'-DMOBIFLIGHT_NAME="Mobiflight Template Mega"'` to your needs. It should be like `'-DMOBIFLIGHT_TYPE="YourDevice_board Mega"'`. This name will show up as name for this board and can be changed within the connector. - 11. If the Raspberry Pico should be supported, follow the next steps. Otherwise delete the section `[env:mobiflight_template_raspberrypico]` - 12. Rename `'-DMOBIFLIGHT_TYPE="Mobiflight Template RaspiPico"'` to your needs. It should be like `'-DMOBIFLIGHT_TYPE="YourDevice_board RaspiPico"'`. This is important and this must match with "MobiFlightType" within the .json files (see below) - 13. Rename `'-DMOBIFLIGHT_NAME="Mobiflight Template RaspiPico"'` to your needs. It should be like `'-DMOBIFLIGHT_TYPE="YourDevice_board Mega"'`. This name will show up as name for this board and can be changed within the connector. - 14. Rename all `env_template` in each environment to the name you have choosen under topic 7.II -8. Nothing has to be changed within the `platformio.ini`: - -If you are done, it should look like this: - -![image](https://github.com/MobiFlight/MobiFlight-Connector/assets/3263285/58bf29c7-11df-47e9-a045-99ca731d917c) - -Now it's time to test all these settings. Open a teminal window in PlatformIO and type `pio run`. This will compile all environments defined in `platformio.ini` including your newly defined environments. If everything is setup correct, no failure should be reported. -The FW files gets generated and will be copied to the `firmware` folder within `/Community`. Addiotionaly a folder `/zip` and a ZIP file gets generated which contains the complete `Community` folder -If you want only to compile your new environment, type `pio run -e YourName_YourCustomDevice_board` (see 7.VI). - -## Setting up the connector files - -### community.board.json files - -To get your new defined board recognized from the connector, for each supported board a board.json file has to be set up. Within the copied folder is a sub folder named `connector_files`. Rename both board.json files to your needs. It should be like `YourName_YourDevice_board.board.json`. Both files (or just one if only one board should be supported) must be modified: -* Rename `"FirmwareBaseName": "mobiflight_template_raspberrypico"` to your defined environment, see point 6. -* Rename `"FriendlyName": "Mobiflight Template Mega"` to your needs. This will show up as name of the board in the Mobiflight Modules. This can be get overwritten within this dialog. -* Rename `"MobiFlightType": "Mobiflight Template Mega"` to the type you have defined in the `YourChangedName_platformio.ini` under 7.IX. and 7.XII. This **must** match the Type you have defined under 7.IX/7.XII. Otherwise the connector will not find your community board! -* Rename `"CustomDeviceType": ["MOBIFLIGHT_TEMPLATE","MOBIFLIGHT_TEMPLATE"]` to your community device type(s). If only one type is supported, delete the second entry. These definitions are used to filter the list of community devices which are under `/devices` are available, so only community devices can be added which fit to the community firmware. This/these entry(ies) must match `"Type": "MOBIFLIGHT_TEMPLATE"` within the `.device.json` file. -(* delete/adapt the list of pins if required. E.g. if you have setup an OLED with hardware SPI connection, the pins are predefined by the processor. So these pins would be deleted in this board.json file and would not be in the list of required pins in your device.json file (see below).) -> TBD - -### custom.device.json files -The json files are in different folders under `/Community`. The boad.json files are under `boards` and the device.json files are under `devices`. -For each community device which will be supported a device.json file has to be setup. For the first one rename `mobiflight.template.device.json` to e.g. `YourName.YourDevice.device.json` and rename the entries to your needs: -1. `"Type": "MOBIFLIGHT_TEMPLATE"` must match one of the definitions from `"CustomDeviceType": ["MOBIFLIGHT_TEMPLATE","MOBIFLIGHT_TEMPLATE"]` (only two ore more entires if you support two or more community devices) within the board.json file you have set up. This information is part of of the complete definition of the community device which is strored in the EEPROM. Additionally check this in your code within `MFCustomDevice.cpp` to prevent that a wrong community device gets loaded which is not supported from your software. See the example in `MFCustomDevice.cpp` which got copied in the steps before. -Within `MFCustomDevice.cpp` is an example, how to read this information from the EEPROM. -2. Define all pins you require for your device. These pins with the hints will show up in the dialog from the UI if you add a community device to your configuration. Within `MFCustomDevice.cpp` the pins will be read from the EEPROM for your further use. There is an example in this file how to do this. -3. Define all messages you need for your community device. These messages can be choosen within the output ConfigWizard under the display tab to choose which value will be sent to which message type. The description entry will also show up to give additional hints what the user should choose. -4. Rename "Label": "Mobiflight's template", to your needs. This label will show up if you add a community device within the connector - -### All .json files -Copy the complete `/Community` folder into the `/Community` folder which is in your Mobiflight installation folder. +* Open a terminal window within PlatformIO if it's not already opened +![image](https://github.com/user-attachments/assets/871652b5-eab5-4709-af93-2338cd526e75) +* type `python renaming.py` into the terminal window and answer the 2 questions + +![image](https://github.com/user-attachments/assets/bc9e553f-f55e-48cd-8374-fb27e9f1ba43) + +All required files and folders get renamed according your naming. ### Testing Now it's a good point to test everything you have set up. diff --git a/Template/Community/boards/mobiflight_template_micro.board.json b/Template/Community/boards/mobiflight_template_micro.board.json index 420edb2..5d08f06 100644 --- a/Template/Community/boards/mobiflight_template_micro.board.json +++ b/Template/Community/boards/mobiflight_template_micro.board.json @@ -26,9 +26,9 @@ "CanResetBoard": true, "FirmwareBaseName": "mobiflight_template_micro", "FirmwareExtension": "hex", - "FriendlyName": "Arduino Pro Micro", - "LatestFirmwareVersion": "2.5.1", - "MobiFlightType": "MobiFlight Template Micro", + "FriendlyName": "Mobiflight Template Micro", + "LatestFirmwareVersion": "0.0.1", + "MobiFlightType": "Mobiflight Template Micro", "ResetFirmwareFile": "reset.arduino_promicro_1_0_2.hex", "CustomDeviceTypes": [ "MOBIFLIGHT_TEMPLATE", diff --git a/Template/Community/boards/mobiflight_template_nano.board.json b/Template/Community/boards/mobiflight_template_nano.board.json index 0e5ae50..7e3b003 100644 --- a/Template/Community/boards/mobiflight_template_nano.board.json +++ b/Template/Community/boards/mobiflight_template_nano.board.json @@ -32,9 +32,9 @@ "CanResetBoard": true, "FirmwareBaseName": "mobiflight_template_nano", "FirmwareExtension": "hex", - "FriendlyName": "Arduino Nano", - "LatestFirmwareVersion": "2.5.1", - "MobiFlightType": "MobiFlight Template Nano", + "FriendlyName": "Mobiflight Template Nano", + "LatestFirmwareVersion": "0.0.1", + "MobiFlightType": "Mobiflight Template Nano", "ResetFirmwareFile": "reset.arduino_uno_1_0_2.hex", "CustomDeviceTypes": [ "MOBIFLIGHT_TEMPLATE", diff --git a/Template/Community/boards/mobiflight_template_raspberry_pico.board.json b/Template/Community/boards/mobiflight_template_raspberry_pico.board.json index 20c8877..7bdadf9 100644 --- a/Template/Community/boards/mobiflight_template_raspberry_pico.board.json +++ b/Template/Community/boards/mobiflight_template_raspberry_pico.board.json @@ -19,7 +19,7 @@ "CanResetBoard": true, "FirmwareBaseName": "mobiflight_template_raspberrypico", "FirmwareExtension": "uf2", - "FriendlyName": "MobiFlight Template RaspiPico", + "FriendlyName": "Mobiflight Template RaspiPico", "LatestFirmwareVersion": "0.0.1", "MobiFlightType": "Mobiflight Template RaspiPico", "ResetFirmwareFile": "reset.raspberry_pico_flash_nuke.uf2", diff --git a/Template/Community/boards/mobiflight_template_uno.board.json b/Template/Community/boards/mobiflight_template_uno.board.json index 70beabc..3405f30 100644 --- a/Template/Community/boards/mobiflight_template_uno.board.json +++ b/Template/Community/boards/mobiflight_template_uno.board.json @@ -29,11 +29,11 @@ "Info": { "CanInstallFirmware": true, "CanResetBoard": true, - "FriendlyName": "Arduino Uno", "FirmwareBaseName": "mobiflight_template_uno", "FirmwareExtension": "hex", - "LatestFirmwareVersion": "2.5.1", - "MobiFlightType": "MobiFlight Template Uno", + "FriendlyName": "Mobiflight Template Uno", + "LatestFirmwareVersion": "0.0.1", + "MobiFlightType": "Mobiflight Template Uno", "ResetFirmwareFile": "reset.arduino_uno_1_0_2.hex", "CustomDeviceTypes": [ "MOBIFLIGHT_TEMPLATE", diff --git a/Template/Community/devices/mobiflight.template.device.json b/Template/Community/devices/mobiflight_template.device.json similarity index 100% rename from Template/Community/devices/mobiflight.template.device.json rename to Template/Community/devices/mobiflight_template.device.json diff --git a/Template/Community/devices/mobiflight.template2.device.json b/Template/Community/devices/mobiflight_template2.device.json similarity index 100% rename from Template/Community/devices/mobiflight.template2.device.json rename to Template/Community/devices/mobiflight_template2.device.json diff --git a/copy_fw_files.py b/copy_fw_files.py index 07ebb09..0a4a07e 100644 --- a/copy_fw_files.py +++ b/copy_fw_files.py @@ -26,6 +26,13 @@ def copy_fw_files (source, target, env): if os.path.exists("./_build/" + custom_source_folder) == False: os.makedirs("./_build/" + custom_source_folder + "/Community/firmware") shutil.copytree(custom_source_folder + "/Community", "./_build/" + custom_source_folder + "/Community", dirs_exist_ok=True) + # set FW version within boad.json files + replacements = { + "0.0.1": firmware_version + } + build_path_json = Path("./_build/" + custom_source_folder + "/Community/boards") + for file_path in build_path_json.rglob("*.json"): + replace_in_file(file_path, replacements) print("Creating /_build folder") if platform == "raspberrypi": @@ -49,6 +56,16 @@ def createZIP(original_folder_path, zip_file_path, new_folder_name): # Add the file to the ZIP file zipf.write(os.path.join(root, file), new_path) +def replace_in_file(file_path, replacements): + """Replace all keys in `replacements` with their values in the given file.""" + with open(file_path, "r", encoding="utf-8") as file: + content = file.read() + + for old, new in replacements.items(): + content = content.replace(old, new) + + with open(file_path, "w", encoding="utf-8") as file: + file.write(content) env.AddPostAction("$BUILD_DIR/${PROGNAME}.hex", copy_fw_files) env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", copy_fw_files) diff --git a/renaming.py b/renaming.py new file mode 100644 index 0000000..7b9f8c8 --- /dev/null +++ b/renaming.py @@ -0,0 +1,136 @@ +# +# This renaming script is based on the excellent work from ccrawford +# All credits are going to him!! +# +# His original repo can be found under https://github.com/ccrawford/MobiFlight-Custom-Device-Setup/ +# And on Discord see: https://discord.com/channels/608690978081210392/1331350567754403840 +# +# Some slight adaptions has been made to reflect the latest status of the repo. +# New Device Name and author are asked from the python script +# Just start it by 'python renaming.py' into the terminal window +# + + +import os +import shutil +import sys +import fileinput +from pathlib import Path + +def replace_in_file(file_path, replacements): + """Replace all keys in `replacements` with their values in the given file.""" + with open(file_path, "r", encoding="utf-8") as file: + content = file.read() + + for old, new in replacements.items(): + content = content.replace(old, new) + + with open(file_path, "w", encoding="utf-8") as file: + file.write(content) + + +def rename_files_and_directories(base_path, replacements): + """Recursively rename files and directories based on replacements.""" + for root, dirs, files in os.walk(base_path, topdown=False): + # Rename files + for name in files: + old_path = Path(root) / name + new_name = name + for old, new in replacements.items(): + new_name = new_name.replace(old, new) + new_path = Path(root) / new_name + old_path.rename(new_path) + + # Rename directories + for name in dirs: + old_path = Path(root) / name + new_name = name + for old, new in replacements.items(): + new_name = new_name.replace(old, new) + new_path = Path(root) / new_name + old_path.rename(new_path) + + +def main(): + # get the new device name + device_name = input("Enter the name of your device: ") + # get the new prefix + prefix = input("Enter your author name: ") + # get statement from user to rename everything + print(F"Device will be renamed to: " + device_name) + print(F"Author will be renamed to: " + prefix) + response = input("Do you want to rename it? (y/N): ").strip().lower() + if response != "y": + print("Operation canceled.") + sys.exit(1) + + # Locate the 'Template' folder + template_folder = Path("Template") + + # Rename the Template folder to DEVICE_NAME + new_device_path = Path(device_name) + if new_device_path.exists(): + print(f"Device folder '{new_device_path}' already exists.") + response = input("Do you want to overwrite it? (y/N): ").strip().lower() + if response != "y": + print("Operation canceled.") + sys.exit(1) + shutil.rmtree(new_device_path) # Remove the existing directory + + # Perform the rename + template_folder.rename(new_device_path) + + # Continue with renaming files and updating contents within the renamed folder + rename_files_and_directories(new_device_path, { + "MyCustomClass.cpp": f"{device_name}.cpp", + "MyCustomClass.h": f"{device_name}.h", + "MyCustomDevice_platformio.ini": f"{device_name}_platformio.ini", + }) + + replacements = { + "MyCustomClass": device_name, + "Mobiflight Template": f"{prefix} {device_name}", + "mobiflight_template": f"{prefix.lower()}_{device_name.lower()}", + "Template": device_name, + "YourProject": device_name, + "MOBIFLIGHT_TEMPLATE": device_name, + "env_template": f"env_{device_name}", + "YourProject": f"{prefix}_{device_name}" + } + + # Update content in relevant files + for file_path in new_device_path.rglob("*.cpp"): + replace_in_file(file_path, replacements) + + for file_path in new_device_path.rglob("*.h"): + replace_in_file(file_path, replacements) + + for file_path in new_device_path.rglob("*_platformio.ini"): + replace_in_file(file_path, replacements) + + # Update Community boards and devices + community_path = new_device_path / "Community" + for file_path in community_path.glob("boards/*.json"): + replace_in_file(file_path, { + "mobiflight_template": f"{prefix.lower()}_{device_name.lower()}", + "Mobiflight Template": f"{prefix} {device_name}", + "MOBIFLIGHT_TEMPLATE": device_name, + }) + + for file_path in community_path.glob("devices/*.json"): + replace_in_file(file_path, { + "MOBIFLIGHT_TEMPLATE": device_name, + "Mobiflight": prefix, + "template": device_name, + }) + + # Rename JSON files in boards and devices + rename_files_and_directories(community_path / "boards", { + "mobiflight_template": f"{prefix}_{device_name}", + }) + rename_files_and_directories(community_path / "devices", { + "mobiflight_template": f"{prefix}_{device_name}", + }) + +if __name__ == "__main__": + main()