title
| title |
|---|
| MaixCDK Development Guidelines and Guidance |
General Guidelines
- Simplicity and Ease of Use: Assume minimal prior experience from users. Minimize required actions and provide simple APIs with comprehensive documentation.
For example, instead of asking users to choose and download a toolchain, it is better to automatically select and download the toolchain based on the chosen platform, reducing the entry barrier.
-
Universality: APIs should be designed to be platform-agnostic and provide consistent abstraction. If this cannot be achieved, reconsider whether the feature should be included.
-
Consistency: Maintain consistent API styles and coding standards.
-
Extensibility: While ensuring core functionality, provide extension interfaces to allow users to add features easily.
-
Depth: Keep APIs simple but document the underlying principles. Open-source as much code as possible to facilitate deeper exploration by users.
Source Code and Binary Files
-
Avoid Using Git Submodules: This facilitates faster downloads for users in China. Even if the main repository is mirrored, submodules still need to be fetched from GitHub, which can be slow for Chinese users.
-
Do Not Store Large Files or Binaries in the Source Code Repository: Storing these will significantly increase the Git repository size. Git is optimized for text files; handle binary files as described below.
-
Automatically Download Third-Party Libraries and Binaries Before Compilation: Define necessary files for download in component.py. During compilation, they will be downloaded to
dl/pkgsand extracted todl/extracted, allowing direct inclusion inCMakeLists.txt, e.g.,list(APPEND ADD_INCLUDE "${DL_EXTRACTED_PATH}/sunxi_mpp/sunxi-mpp-1.0.0/include").
The list of files to be downloaded is stored in
dl/pkgs_info.jsonduring each build. Users with slow internet connections can manually download these files from official sources or third-party services and place them indl/pkgs.
- Do Not Modify Third-Party Libraries: Avoid modifying the source code of third-party libraries to facilitate upgrades. If modifications are necessary, consider applying patches automatically after extraction.
Overview of MaixCDK Architecture
For general application developers, key concepts include:
- MaixCDK consists of two main parts:
MaixCDK(library storage) andproject(application code). Official examples and apps are located inMaixCDK/examplesandMaixCDK/projects, respectively. You can also create your own projects inMaixCDK/projects.
Alternatively, you can separate the two and set an environment variable MAIXCDK_PATH pointing to the MaixCDK directory, e.g., export MAIXCDK_PATH=/home/xxx/MaixCDK. This allows projects to reside in other locations, such as /home/xxx/projects.
-
Components: Each functional module can be encapsulated as a component, making it easy to include in different applications.
- For example, the
maincomponent in examples/hello_world and various components in components. You can also add custom components, likehello_world/component1. - Use the environment variable
MAIXCDK_EXTRA_COMPONENTS_PATHto specify additional component paths. - Each component includes a
CMakeLists.txtfile describing its contents, e.g.,list(APPEND ADD_INCLUDE "include")for header files,list(APPEND ADD_SRCS "src/hello.c")for source files, andlist(APPEND ADD_REQUIREMENTS basic)for dependencies.
- For example, the
-
Kconfig: Provides terminal-based configuration options. Each component can include a
Kconfigfile for setting options, accessible viamaixcdk menuconfig. The configuration is saved inbuild/config, generatingglobal_config.cmakeandglobal_config.hfor use inCMakeLists.txtand C/C++ files. -
Third-Party Library Integration: There are two recommended methods:
- Method 1: Specify third-party libraries in the component’s
CMakeLists.txtfor automatic download during compilation. This is preferred for libraries integrated intoMaixCDK. - Method 2: Package and publish the library as a Python package on pypi.org with the naming convention
maixcdk-xxx. Users can then install it usingpip install maixcdk-xxx.
- Method 1: Specify third-party libraries in the component’s
-
Documentation: Located in the docs directory. API documentation is auto-generated from code; do not edit it manually. The application documentation serves as a user guide.
Coding Style
To ensure consistency across MaixCDK and MaixPy APIs, follow these coding style guidelines:
- Function Names: Use lowercase with underscores, e.g.,
get_name. - Variable Names: Use lowercase with underscores, e.g.,
file_path. - Class Names: Use CamelCase, with common abbreviations in uppercase, e.g.,
Person,YOLOv2,UART. - Macros: Use uppercase with underscores, e.g.,
MAIXPY_VERSION. - Class Member Variables: Use lowercase with underscores without
m_prefix, e.g.,name. - Private Class Member Variables: Prefix with
_, e.g.,_name. - Using Class Member Variables: Explicitly use
this->for clarity, e.g.,this->name. - Namespace: Use the
maixnamespace for all official APIs, with sub-namespaces for different features, e.g.,maix::thread,maix::peripheral. - Source File Naming: Use lowercase with underscores, e.g.,
maix_peripheral_uart.cpp. Use.hppfor C++ header files. - Comments: Follow Doxygen style for API documentation, as shown in components/basic/include/maix_api_example.hpp.
API Documentation Guidelines
Include the following keywords in your API comments:
- brief: A brief description of the functionality.
- param: Detailed parameter descriptions, including value requirements and direction (e.g.,
param[in] a). - return: Detailed return value descriptions.
- retval: Use this for specific return value explanations, e.g.,
@retval 0 success. - attention: Special considerations for using the API.
- maixpy: Indicates a
MaixPyAPI, including the package and variable name, e.g.,maix.example. - maixcdk: Indicates a
MaixCDKAPI.
Follow the guidelines strictly, especially when defining types and default values, to ensure accurate MaixPy API and documentation generation.
API Standardization Suggestions
- Prefer MaixCDK APIs over direct third-party APIs for better cross-platform compatibility, e.g., use
maix_fs.hppfor file operations. - Use logging APIs from
maix_log.hppfor consistent logging and to manage log levels. - Use error handling from
maix_err.hppfor consistent error codes and exception handling. - For common APIs across modules, define a base class and inherit it in specific implementations, enhancing code reuse and consistency across platforms.