Skip to content

Example

To clarify the project coding guidelines it is sometimes useful to generate Example c Files.

Example for c source file

/**
 * @file Example.c
 * @brief Example implementation
 *
 * @copyright Copyright (c) 2021 Tillmann Kaier
 *
 */

/* Includes ------------------------------------------------------------------*/
// Module Interface
#include "Example.h"
// Application
// Common Lib
// BSP
// Std Lib
#include <string.h>

/* Private define ------------------------------------------------------------*/

#define SOME_UNSIGNED_DEFINE UINT32_C(10) ///< std cast to unsigned 32bit
#define SOME_SIGNED_DEFINE   INT32_C(10)  ///< std cast to signed 32bit

/* Private macro -------------------------------------------------------------*/

/* Private typedef -----------------------------------------------------------*/

/**
 * @brief   enum with local scope
 * @details
 * The enum name is used to identify the enum members.
 */
typedef enum Constant_e {
    CONSTANT_1 = 0, ///< Constant 1
    CONSTANT_2,     ///< Constant 2
    CONSTANT_3,     ///< Constant 3
    CONSTANT_NUM    ///< Number of Constants
} Constant_t;

/**
 * @brief struct with local scope
 */
typedef struct InternalData_s {
    uint32_t      Data;  ///< member 1
    Example_Ret_t State; ///< member 2
} InternalData_t;

/* Private constants ---------------------------------------------------------*/

/**
 * @brief constant with local scope
 * @details
 * The constant members initialiced directly.
 */
static const InternalData_t InternalConstant = {.Data = SOME_UNSIGNED_DEFINE,
                                                .State = EXAMPLE_RET_OK};

/* Private variables ---------------------------------------------------------*/

/**
 * @brief local data
 */
static InternalData_t InternalData;

/* Private function prototypes -----------------------------------------------*/

/**
 * @brief local function
 *
 * @param Para local function parameter
 */
static void doSomething(InternalData_t *const Para);

/**
 * @brief Get the State object
 *
 * @return true everything is ok
 */
static bool getState(void);

/* Interface functions -------------------------------------------------------*/

bool Example_init(Example_Data_t Para1, Example_Ret_t State)
{
    InternalData = InternalConstant;

    InternalData.Data = Para1.Var;
    InternalData.State = State;

    doSomething(&InternalData);
}

Example_Ret_t Example_getState(void) { return InternalData.State; }

/* Private functions ---------------------------------------------------------*/

static void doSomething(InternalData_t *const Para) { Para->Data = 0U; }

/* END OF FILE ---------------------------------------------------------------*/

Example for c header file

/**
 * @file Example.h
 * @brief Example interface
 * @details
 * This is an example intertface to show how a coding guidline could be used.
 *
 * @copyright Copyright (c) 2021 Tillmann Kaier
 *
 */
#pragma once // prevent recursive inclusion

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* Includes ------------------------------------------------------------------*/

#include <stdbool.h>
#include <stdint.h>

/* Public defines ------------------------------------------------------------*/

/* Public constants ----------------------------------------------------------*/

/* Public types --------------------------------------------------------------*/

/**
 * @brief public enum example
 * @details
 * Here all the enum members are written in capital case, the first part of
 * the name contains the namespace and the last enum member is used to flag
 * the amount of enum members.
 */
typedef enum Example_Ret_e {
    EXAMPLE_RET_OK = 0,          ///< Success
    EXAMPLE_RET_ERR_INTERNAL,    ///< Module internal error
    EXAMPLE_RET_ERR_NULLPOINTER, ///< NULL pointer error
    EXAMPLE_RET_NUM              ///< Number of enum elements
} Example_Ret_t;

/**
 * @brief public struct example
 * @details
 * The namespace is used for the first part of the struct type name.
 */
typedef struct Example_Data_s {
    uint32_t Var; ///< Struct Member Derscription
} Example_Data_t;

/* Public macro --------------------------------------------------------------*/

/* Public functions ----------------------------------------------------------*/

/**
 * @brief public function example
 * @details
 * The namespace is used for the first part of the function name.
 *
 * @param Para1 first parameter
 * @param State second parameter
 * @return true if initialisation was successfull.
 */
bool Example_init(Example_Data_t Para1, Example_Ret_t State);

/**
 * @brief   read the Example State
 * @return  State of Example
 */
Example_Ret_t Example_getState(void);

/* Public Variables ----------------------------------------------------------*/

#ifdef __cplusplus
}
#endif /* __cplusplus */

/* END OF FILE ---------------------------------------------------------------*/