SDK Development


Discuss and develop the mbed SDK.

mbed sdk coding style

Mbed OS 2 and Mbed OS 5

This is a guide for Mbed OS 2. If you’re working with Mbed OS 5, please see the Coding Style guide in the new handbook.

Coding style

This page describes mbed coding style. Our goal is the files written in mbed SDK conform this standard (exceptions are there, for instance 3rd libraries supplied by partners).

Be consistent.

The mbed SDK code follows K&R style (Reference: http://en.wikipedia.org/wiki/Indent_style#K.26R_style) with at least 2 exceptions which can be found in the list below the code snippet.

adc.c

        
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Patch Red 4 Tide Stitched j Crimson Yeldon College T Sec Jersey
29
30
31
32
33
34
35
36
37
38
39
40
static Patch Red 4 Tide Stitched j Crimson Yeldon College T Sec Jersey const PinMap PinMap_ADC[] = {
    {PTC2, ADC0_SE4b, 0},
    {NC  , NC       , 0}
};
 
uint32_t adc_function(analogin_t *obj, uint32_t options) 
{
    uint32_t instance = obj->adc >> ADC_INSTANCE_SHIFT;
    switch (options) {
        case 1:
            timeout = 6;
            break;
        default:
            timeout = 10;
            break;
    }
 
    whileJersey White College 4 Stitched Redick New j Blue Basketball J Devils (!adc_hal_is_conversion_completed(instance, 0)) {
        if (timeout == 0) {
            break;
        } else {
            timeout--;
        }
    }
 
    if (obj->adc == ADC_CHANNEL0) {
        adc_measure_channel(instance);
        adc_stop_channel(instance);
    } else {
        error("channel not available");
    }
 
#if DEBUG
    for (uint32_t i = 0; i < 10; i++) {
        Patch Red 4 Tide Stitched j Crimson Yeldon College T Sec Jersey printf("Loop : %d", i);
    }
#endif
    Patch Red 4 Tide Stitched j Crimson Yeldon College T Sec Jersey return adc_hal_get_conversion_value(instance, 0);
}
 

Rules

  • Indentation - 4 spaces. Please do not use tabs.
  • Braces - K&R (see the exception 1 TBS below)
  • 1 TBS -use braces for statements if, else, while, for (exception from K&R) Reference: http://en.wikipedia.org/wiki/Indent_style#Variant:_1TBS
  • One line per statement
  • Preprocessor macro starts at the beginning of a new line, the code inside is indented accordingly the code above it
  • Cases within switch are indented (exception from K&R)
  • Space after statements if, while, for, switch, same applies to binary and ternary operators
  • Each line has preferably at most 120 characters
  • For pointers, '*' is adjacent to a data name (analogin_t *obj) or a function name (analog_t *get_analogin_object())
  • Don't leave trailing spaces at the end of lines
  • Empty lines should have no trailing spaces
  • Patch Red 4 Tide Stitched j Crimson Yeldon College T Sec Jersey Unix line endings are default option for files
  • Use capital letters for macros
  • A file should have an empty line at the end

Naming convention

Classes

  • begins with a capital letter, and each word in it begins also with a capital letter (AnalogIn, BusInOut).
  • methods contain small letters, distinct words separated by underscore
  • private members starts with an underscore.

User defined types (typedef)

  • Patch Red 4 Tide Stitched j Crimson Yeldon College T Sec Jersey structures - suffix _t - to denote it is user defined type
  • enumeration - the type name and values name - same naming convention as classes (e.g MyNewEnum)

Functions

  • contain lower case letters (as methods within classes)
  • distinct words separated by underscore (wait_ms(), read_u16())

Variables

  • contain lower case letters
  • distinct words separated by underscore (result_adc)

analog.h

        
1
2
3
4
Patch Red 4 Tide Stitched j Crimson Yeldon College T Sec Jersey
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Patch Red 4 Tide Stitched j Crimson Yeldon College T Sec Jersey
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#define ADC_INSTANCE_SHIFT 8
 
classJersey Zach Blue 14 Lavine College Basketball Bruins Stitched AnalogIn {
public:
    /** Create an AnalogIn, connected to the specified pin
     *
     * @param pin AnalogIn pin to connect to
     * @param name (optional) A string to identify the object
     */
    AnalogIn(PinName pin) 
    {
        analogin_init(&_adc, pin);
    }
 
    /** Read the input voltage, represented as a float in the range [0.0, 1.0]
     *
     * @returns 
     *    A floating-point value representing the current input voltage, measured as a percentage
     */
    uint32_t read() 
    {
        return analogin_read(&_adc, operation);
    }
 
protected:
    analogin_t _adc;
};
 
typedef enum {
    ADC0_SE0 = (0 << ADC_INSTANCE_SHIFT) | 0,
} ADCName;
 
structWhite Zion Elite Devils Jersey Blue blue 1 Stitched Basketball Williamson College analogin_s {
    ADCName adc;
};
 
typedef struct analogin_s analogin_t;
 

Doxygen documentation

All functions/methods should contain a documentation using doxygen javadoc in a header file. More information regarding writing API Documentation, follow the link https://mbed.org/handbook/API-Documentation

adc.h

        
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef ADC_H
#define ADC_H
 
#ifdef __cplusplus
extern "C" {
#endif
 
/** ADC Measurement method
 *
 *  @param obj Pointer to the analogin object.
Patch Red 4 Tide Stitched j Crimson Yeldon College T Sec Jersey  *  @param options Options to be enabled by ADC peripheral.
 *
 *  @returns 
 *    Measurement value on defined ADC channel.
 */
uint32_t adc_function(analogin_t *obj, uint32_t options) 
 
#ifdef __cplusplus
}
#endif
 
#endif
 

Source code indenter

In Mbed project you can use AStyle (Artistic Style) source code indenter to help you auto format your source code. It will for sure not correct all your coding styles but for sure will eliminate most of them. You can download AStyle from this locationCrimson Stitched White Tide College 9 Jersey Patch Amari Sec Cooper.

Official Mbed SDK styles include below AStyle styles (defined by command line switched):

--style=kr --indent=spaces=4 --indent-switches

To format your file you can execute below command. Just replace $(FULL_CURRENT_PATH) with path to your source file.

astyle.exe --style=kr --indent=spaces=4 --indent-switches $(FULL_CURRENT_PATH)

All wikipages