Skip to content

Macros Utility Header

Path: src/modbus/macros.h

Revision History: Initial documentation

A comprehensive collection of utility macros for ESP-32 development. This header provides macro definitions for commonly used operations in embedded systems programming, avoiding the use of the standard library. It includes macros for type manipulation, bit operations, mathematical functions, array handling, compile-time optimizations, and more.

  • ESP-32 platform
  • C17 compliant compiler
  • No standard library dependencies
  • Incrementation/Decrementation: INC_X, DEC_X, INCREMENT, DECREMENT
  • Compiler Optimizations: FORCE_INLINE, _UNUSED, NOOP
  • Argument Counting/Testing: NUM_ARGS, TWO_ARGS
  • String Helpers: CAT, _CAT
  • Conditional Compilation: ENABLED, DISABLED, ANY, ALL, NONE
  • Bit Manipulation: __BV, TEST, SBI, CBI, SET_BIT_TO
  • Math Operations: WITHIN, RADIANS, DEGREES, NEAR_ZERO
  • Time Helpers: PENDING, ELAPSED, HOUR_MS, MIN_MS
  • Array Initialization: ARRAY_N, ARRAY_1 through ARRAY_6
  • C++11 Template Utilities: Min/Max templates, SFINAE helpers
  • Preprocessor macros for common calculations and operations
  • Bit manipulation utilities
  • Compile-time optimizations
  • Safe type conversion helpers
  • Template-based compile-time checks
  • Time management utilities
  • Array initialization helpers
  • Conditional execution based on feature flags

Macros

XTypes

The macros in this header provide utility functions that are used throughout the codebase. They do not have state or flow by themselves but enable cleaner, more efficient code.

Uses macros

Expand at compile time

Application Code

Macros

Optimized Binary

  • Consider separating into multiple headers for faster compilation
  • Add pre-processor optimization for frequently used macro combinations
  • No direct security concerns as these are compile-time macros
  • Ensure macros don’t introduce unintended behaviors when used with user input
  • Compatible with C17 standard
  • Designed for ESP-32 and industrial Modbus applications
  • No use of standard library functions
  • Group related macros when using in implementation files
  • Use the ENABLED/DISABLED macros for feature toggling
  • Prefer the template-based MIN/MAX over macro versions when in C++ code
  • Consider the resource constraints of ESP-32 when using complex macro expansions