2020-05-25 06:35:00 +02:00
# Coding style
2019-07-16 20:53:43 +02:00
## File format
2020-05-25 06:35:00 +02:00
Use [lv_misc/lv_templ.c ](https://github.com/lvgl/lvgl/blob/master/src/lv_misc/lv_templ.c ) and [lv_misc/lv_templ.h ](https://github.com/lvgl/lvgl/blob/master/src/lv_misc/lv_templ.h )
2019-07-16 20:53:43 +02:00
## Naming conventions
* Words are separated by '_'
* In variable and function names use only lower case letters (e.g. *height_tmp* )
* In enums and defines use only upper case letters (e.g. *e.g. MAX_LINE_NUM* )
* Global names (API):
* starts with *lv*
* followed by module name: *btn* , *label* , *style* etc.
* followed by the action (for functions): *set* , *get* , *refr* etc.
2021-01-11 07:28:00 -06:00
* closed with the subject: *name* , *size* , *state* etc.
2019-07-16 20:53:43 +02:00
* Typedefs
* prefer `typedef struct` and `typedef enum` instead of `struct name` and `enum name`
* always end `typedef struct` and `typedef enum` type names with `_t`
* Abbreviations:
2021-01-11 07:28:00 -06:00
* Only words longer or equal than 6 characters can be abbreviated.
2020-05-25 06:35:00 +02:00
* Abbreviate only if it makes the word at least half as long
2021-01-11 07:28:00 -06:00
* Use only very straightforward and well-known abbreviations (e.g. pos: position, def: default, btn: button)
2019-07-16 20:53:43 +02:00
## Coding guide
* Functions:
2021-01-11 07:28:00 -06:00
* Try to write function shorter than is 50 lines
* Always shorter than 200 lines (except very straightforwards)
2019-07-16 20:53:43 +02:00
* Variables:
* One line, one declaration (BAD: char x, y;)
* Use `<stdint.h>` (*uint8_t*, *int32_t* etc)
2020-05-25 06:35:00 +02:00
* Declare variables where needed (not all at function start)
2019-07-16 20:53:43 +02:00
* Use the smallest required scope
* Variables in a file (outside functions) are always *static*
* Do not use global variables (use functions to set/get static variables)
## Comments
Before every function have a comment like this:
```c
/**
* Return with the screen of an object
* @param obj pointer to an object
* @return pointer to a screen
*/
2021-01-11 07:28:00 -06:00
lv_obj_t * lv_obj_get_scr(lv_obj_t * obj);
2019-07-16 20:53:43 +02:00
```
2021-03-15 02:03:27 +08:00
Always use `/*Something*/` format and NOT `//Something`
2019-07-16 20:53:43 +02:00
2021-01-11 07:28:00 -06:00
Write readable code to avoid descriptive comments like:
2021-03-15 02:03:27 +08:00
`x++; /*Add 1 to x*/` .
2019-07-16 20:53:43 +02:00
The code should show clearly what you are doing.
2021-01-11 07:28:00 -06:00
You should write **why** have you done this:
2021-03-15 02:03:27 +08:00
`x++; /*Because of closing '\0' of the string*/`
2019-07-16 20:53:43 +02:00
Short "code summaries" of a few lines are accepted. E.g. `/*Calculate the new coordinates*/`
In comments use \` \` when referring to a variable. E.g. ``/*Update the value of ` x_act`*/` `
### Formatting
Here is example to show bracket placing and using of white spaces:
```c
/**
* Set a new text for a label. Memory will be allocated to store the text by the label.
* @param label pointer to a label object
* @param text '\0' terminated character string. NULL to refresh with the current text.
*/
void lv_label_set_text(lv_obj_t * label, const char * text)
2021-03-15 02:03:27 +08:00
{ /*Main brackets of functions in new line*/
2021-01-11 07:28:00 -06:00
2019-07-16 20:53:43 +02:00
if(label == NULL) return; /*No bracket only if the command is inline with the if statement*/
2021-01-11 07:28:00 -06:00
2019-07-16 20:53:43 +02:00
lv_obj_inv(label);
2021-01-11 07:28:00 -06:00
2019-07-16 20:53:43 +02:00
lv_label_ext_t * ext = lv_obj_get_ext(label);
2021-03-15 02:03:27 +08:00
/*Comment before a section*/
2019-07-16 20:53:43 +02:00
if(text == ext->txt || text == NULL) { /*Bracket of statements start inline*/
lv_label_refr_text(label);
2021-01-11 07:28:00 -06:00
return;
2019-07-16 20:53:43 +02:00
}
2021-01-11 07:28:00 -06:00
2019-07-16 20:53:43 +02:00
...
}
```
Use 4 spaces indentation instead of tab.
2020-05-25 06:35:00 +02:00
You can use **astyle** to format the code. Run `code-formatter.sh` from the `scrips` folder.