-
Notifications
You must be signed in to change notification settings - Fork 6
Add embedded #12
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
base: master
Are you sure you want to change the base?
Add embedded #12
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,94 @@ | ||||||||||||
| # Embedded Development Guidelines | ||||||||||||
|
|
||||||||||||
| These guidelines apply to embedded and safety-critical firmware projects. Follow project- | ||||||||||||
| specific rules first (e.g., ArduPilot and JSF style guide), then apply the rules below for any gaps. | ||||||||||||
| When these rules conflict with a project standard, the project standard wins. | ||||||||||||
|
|
||||||||||||
| ## General | ||||||||||||
|
|
||||||||||||
| - Follow the project code style and keep it consistent within each file. | ||||||||||||
| - Prefer small, focused functions over long procedural blocks. | ||||||||||||
| - Prefer compile-time checksm add run-time checks for safety-critical paths. | ||||||||||||
| - Encapsulate low-level or hardware-specific code behind clear interfaces. | ||||||||||||
| - Do not add unused or commented-out code unless it is justified and documented. | ||||||||||||
|
|
||||||||||||
| ## Core constraints (JSF AV-inspired) | ||||||||||||
|
|
||||||||||||
| - Keep functions under 200 logical source lines. | ||||||||||||
| - Keep cyclomatic complexity at or below 20 (exceptions only for large switch statements). | ||||||||||||
| - Do not use self-modifying code. (Up to discussion) | ||||||||||||
| - Use ISO/IEC 14882:2017-compliant C++ (C++17) or newer, do not rely on compiler extensions. | ||||||||||||
| - When possible, pass the compiler flag to disable extensions (e.g., `-fno-gnu-extensions` for GCC/Clang). | ||||||||||||
| - Use only the basic C++ source character set, do not use trigraphs, digraphs, multibyte | ||||||||||||
| characters, or wide string literals. | ||||||||||||
|
|
||||||||||||
| ## Preprocessor rules | ||||||||||||
|
|
||||||||||||
| - Only use `#pragma once` and `#include` | ||||||||||||
| - Do not use `#define` for inline macros or constants, use `inline`, `const`, `constexpr`, or `consteval` instead. | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assumes that C++ only code is being used, do we want to broad this for maybe Rust/C? |
||||||||||||
| - `#include` only header files (template implementations may be included by headers). | ||||||||||||
|
|
||||||||||||
| ## Header and implementation structure | ||||||||||||
|
|
||||||||||||
| - `.h` for headers, `.cpp` for implementations. | ||||||||||||
| - Headers contain declarations only, no non-const data or function definitions. | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| - Exceptions: inline functions and templates. | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Join this with suggestion above |
||||||||||||
| - Each `.cpp` includes the headers that define all inline functions, types, and templates it uses. | ||||||||||||
| - Header files should include only what they need, keep implementation-only includes in `.cpp`. | ||||||||||||
|
|
||||||||||||
| ## Style and naming | ||||||||||||
|
|
||||||||||||
| - Avoid tabs, indent consistently (use 2 or four spaces, be consistent with the project). | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| - Use braces for all control statements. | ||||||||||||
| - Braces for blocks on their own lines and aligned. | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this include starting braces, and braces that are part of block continuations? I'm personally more inclined to if (condition) {
...
} else {
...
}than if (condition)
{
...
}
else
{
...
} |
||||||||||||
| - No spaces around `.` or `->`, and no spaces around unary operators. | ||||||||||||
| - Naming rules follow the project standard. If none is defined, use: | ||||||||||||
| - `lower_snake_case` for functions and variables | ||||||||||||
| - `UpperCamel_Case` for classes, structs, enums, and typedefs (e.g., `AP_Compass`) | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a valid UpperCamelCase (PascalCase); it is a mixture of naming conventions.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| - lowercase constants and enumerators | ||||||||||||
| - uppercase acronyms inside identifiers (e.g., `GCS_MAVLink`) | ||||||||||||
| - use underscore for private properties | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we expect to use lots of libraries in our embedded code?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Not sure if this was the intent (or double preceding underscore, or one before + one after, etc), but it should be clarified. |
||||||||||||
|
|
||||||||||||
| ## Class design | ||||||||||||
|
|
||||||||||||
| - Use `struct` for plain data, `class` for invariants and encapsulation. | ||||||||||||
| - Keep data members private, avoid public/protected data in classes. | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of C++ references tends to use member variables/functions to address properties and methods.
Suggested change
|
||||||||||||
| - Declare member functions as `const` by default. | ||||||||||||
| - Use `constexpr` or `consteval` for functions and variables that can be evaluated at compile time. | ||||||||||||
| - Do not call virtual functions from constructors or destructors. | ||||||||||||
| - Use member initializer lists for non-static members. | ||||||||||||
| - Declare copy/assignment when owning pointers or nontrivial destructors. | ||||||||||||
| - Base classes with virtual functions must have virtual destructors. | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
|
||||||||||||
| ## Documentation | ||||||||||||
|
|
||||||||||||
| - Document units in names or comments for physical quantities. | ||||||||||||
| - Prefer doxygen-style comments for C++ projects. | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we also have preferences for other languages? |
||||||||||||
|
|
||||||||||||
| ## Embedded-specific constraints | ||||||||||||
|
|
||||||||||||
| - Avoid the C++ standard library (`std::vector`, `std::string`, `std::unordered_map`) | ||||||||||||
| unless the platform and toolchain explicitly support it and memory costs are measured. | ||||||||||||
| - Prefer fixed-size arrays, if dynamic containers are needed, use project-approved | ||||||||||||
| alternatives. | ||||||||||||
| - Avoid bit fields, use plain booleans. | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accordingly to JSF AV Rules [154, 155, 156]
Suggested change
|
||||||||||||
| - Remove dead code, do not comment out unused code. | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it acceptable/recommended to include "TODO" comments in the place of code that is not yet in use elsewhere? |
||||||||||||
| - Prefer multiplication over division for unit conversions. | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How strong is this preference / how spelled out should conversions be?
Suggested change
|
||||||||||||
| - Use `constexpr` for compile-time constants and lookup tables, use `consteval` (C++20+) | ||||||||||||
| when a value must be evaluated at compile time. | ||||||||||||
|
|
||||||||||||
| ## Testing and verification | ||||||||||||
|
|
||||||||||||
| - Tests must exercise safety-critical paths and any new hardware interactions. | ||||||||||||
| - Add logging or assertions that can be compiled out for production builds. | ||||||||||||
| - Ensure CI covers at least one supported target and simulator when available. | ||||||||||||
|
|
||||||||||||
| ## Portability and build | ||||||||||||
|
|
||||||||||||
| - Target multiple embedded platforms when feasible, avoid vendor-locked toolchains. | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| - Make dependencies and build steps explicit and reproducible. | ||||||||||||
|
|
||||||||||||
| ## References | ||||||||||||
|
|
||||||||||||
| - JSF Air Vehicle C++ Coding Standards (Stroustrup): https://www.stroustrup.com/JSF-AV-rules.pdf | ||||||||||||
| - ArduPilot Style Guide: https://ardupilot.org/dev/docs/style-guide.html | ||||||||||||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.