Add API for MaixCDK / MaixPy
Code Guidelines
Please refer to Code Guidelines first.
How to Add an API
At the end of the Quick Start, we briefly mentioned adding a new API to MaixPy using annotations. It looks straightforward – you only need to add a comment to the API function, for example:
namespace maix::example
{
/**
* @brief Say hello to someone
* @param[in] name The name of the person, of string type
* @return A string containing "hello" + name
* @maixpy maix.example.hello
*/
std::string hello(std::string name);
}
Then you can call this in MaixPy:
from maix import example
result = example.hello("Bob")
print(result)
To ensure that the APIs we add are usable for users, we need to follow these guidelines:
- Design the API names and parameters to be reasonable, general, and highly cross-platform.
- Ensure the API is annotated (documentation will be automatically generated during compilation).
- The API should come with usage documentation, a tutorial, and example code.
Here is a more detailed process and guidelines:
- Confirm the functionality and add a usage document and example code in the MaixPy Documentation Source. This acts as a design document, helping to avoid frequent API changes due to incomplete considerations during coding. It also serves as documentation. (This is very important!)
- You can add an application document under docs/doc/application to record development details. Use lowercase filenames with underscores, e.g.,
peripheral/uart.md
orai/yolov2.md
. - Mention the sources or open-source projects referenced for API design in the development document to facilitate the review process and improve the chances of passing the review quickly.
- Refer to [components/basic/include/maix_api_example.hpp] and add the API to an appropriate
component
. If it's a new component, consider discussing its rationality first in issues.
Note: The
API
is identified through comments, which helps automatically generate documentation andMaixPy
source code. Pay close attention to the annotation guidelines and refer tomaix_api_example.hpp
for specifics.
Additionally, becauseMaixCDK
does not include definitions related toPython.h
orPybind11.h
, language-native types are automatically converted bypybind11
. For instance,void hello(std::string a, std::vector<int> b)
is equivalent todef hello(a: str, b: list)
inMaixPy
.
Common conversions includestd::vector
tolist
,std::map
todict
,std::valarray
(acceptslist
andbytes
as input, returnslist
), andmaix::Bytes
(both input and return values arebytes
).std::function
maps to a function in MaixPy. For more details, refer to the pybind11 documentation.
- Add a C++ example to the
examples
directory and ensure it compiles and runs successfully. - The documentation will be automatically generated during compilation. Check the generated files under
docs/doc/api
for any errors and correct them in the code if needed. - Test the updated
MaixPy
project with the newMaixCDK
to ensure it compiles successfully and the generated documentation is correct. Fix any errors if they occur. - Submit your code to your own GitHub repository and wait for the
action
to automatically build and test it. Correct any errors promptly. - Once all online tests pass, submit a
PR
(Pull Request) and request to merge it into thedev
branch on GitHub.
Manually Adding a MaixPy API
The above method can automatically generate a MaixPy API, but in certain scenarios, manual addition might be necessary. For example, when the parameter is a specific type, such as numpy.array
:
- Add the header file and code under
components/maix/include
in theMaixPy
project. You can use the samenamespace
as inMaixCDK
. For instance, the functionmaix.image.cv2image
converts anumpy
array to animage.Image
object. Refer to the definitions in theconvert_image.hpp
file.
More references: