halpp is a modern, object-oriented C++ (C++20/C++26) Hardware Abstraction Layer designed specifically for the ESP-IDF framework. It wraps native ESP-IDF C API components (such as the i2c_master driver, RTC modules, and display drivers) into safe, ergonomic, and highly reliable C++ classes.
The primary goal of halpp is to bring modern C++ paradigms to ESP32 embedded development without sacrificing performance or bloating firmware size.
- Safe Error Handling: Replaces verbose
esp_err_tchecking with a monadicEspResult<T>andEspErrorpattern, completely avoiding C++ exceptions. - Deterministic Memory: Strictly avoids dynamic memory allocation (
new,malloc,std::string,std::vector) in runtime execution paths to prevent heap fragmentation. - RAII Resource Management: Hardware handles (like I2C bus devices) are strictly managed by object lifecycles, ensuring proper initialization and automatic cleanup.
- Zero Overhead: Leverages
constexpr, template size deduction, andstring_viewto shift computational work to the compiler wherever possible.
📚 For a deep dive into the coding idioms, architectural rules, and contribution guidelines, please read the Architecture & Design Philosophy Guide.
halpp is designed to be lightweight but relies on a foundational utility library for its error-handling types:
- ESP-IDF (v5.x / v6.x compatible)
espbase: Provides theEspResultandEspErrorimplementations. You must includeespbasein your project alongsidehalpp.
The recommended way to integrate halpp into your ESP-IDF project is via git submodules and CMake source inclusion.
Add both halpp and espbase to a deps/ directory in your project root:
mkdir deps
git submodule add https://github.com/tapted/halpp.git deps/halpp
git submodule add https://github.com/tapted/espbase.git deps/espbase
git submodule update --init --recursive
In your main application component (e.g., main/CMakeLists.txt), include the source files and header directories from the submodules.
Here is a standard integration example:
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# 1. Glob the source files from the submodules
file(GLOB_RECURSE DEPS_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/../deps/espbase/src/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../deps/halpp/src/*.cpp
)
# 2. Glob your own application sources
file(GLOB_RECURSE MAIN_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/*.cpp
)
set(PROJECT_SOURCES ${MAIN_SRCS} ${DEPS_SRCS})
# 3. Register the component
idf_component_register(SRCS ${PROJECT_SOURCES} INCLUDE_DIRS ".")
# 4. Expose the include directories to your target
target_include_directories(${COMPONENT_LIB} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/../src
${CMAKE_CURRENT_SOURCE_DIR}/../deps/espbase/src
${CMAKE_CURRENT_SOURCE_DIR}/../deps/halpp/src
)
Make sure your top-level project CMakeLists.txt initializes the project normally:
cmake_minimum_required(VERSION 3.22)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(your_project_name)halpp uses zero-overhead compile-time configuration discovered automatically via #include "halpp/config.hpp":
halpp::detail::SharedDefaults (portable defaults)
└─ halpp::detail::Defaults (CONFIG_IDF_TARGET_* SoC defaults)
└─ halpp::boards::<Board> (optional devkit preset in halpp/boards/)
└─ halpp::board::config (your project's hal/board.hpp)
If a file named hal/board.hpp exists anywhere on your include path (for example src/hal/board.hpp or main/hal/board.hpp), halpp/config.hpp includes it and aliases halpp::config to halpp::board::config. If no hal/board.hpp is present, halpp::config falls back directly to halpp::detail::Defaults.
If your hardware matches a preset in Board Presets (src/halpp/boards/) and you don't need any project-specific overrides:
// main/hal/board.hpp
#pragma once
#include "halpp/boards/waveshare_185c_v2.hpp"
namespace halpp::board {
using config = boards::Waveshare185CV2;
} // namespace halpp::boardTo override individual fields (such as a custom boot logo or audio sample rate), inherit from the preset and shadow only the nested structs you want to customize:
// src/hal/board.hpp
#pragma once
#include "hal/display/board_display.hpp"
#include "halpp/boards/waveshare_185c_v2.hpp"
namespace halpp::board {
struct config : boards::Waveshare185CV2 {
struct Display : Waveshare185CV2::Display {
static constexpr auto BOOT_LOGO_FUNC = draw_boot_logo;
};
struct Audio : Waveshare185CV2::Audio {
static constexpr uint32_t SAMPLE_RATE = 16000;
static constexpr uint32_t MIC_SAMPLE_RATE = 16000;
};
};
} // namespace halpp::boardFor custom PCBs or unsupported devkits, inherit directly from halpp::detail::Defaults (from halpp/config_defaults.hpp) and override the peripherals you use:
// src/hal/board.hpp
#pragma once
#include "halpp/config_defaults.hpp"
namespace halpp::board {
struct config : detail::Defaults {
struct I2CConfig : detail::Defaults::I2CConfig {
static constexpr gpio_num_t PIN_SDA = GPIO_NUM_11;
static constexpr gpio_num_t PIN_SCL = GPIO_NUM_10;
};
};
} // namespace halpp::boardNote on optional components: Board presets guard managed-component headers (such as
esp_lcd_st77916,esp_lcd_touch_cst816s, andesp_io_expander_tca9554) with#if __has_include(...), so you only need to list the components you actually use inidf_component.yml/REQUIRES. If yourCMakeLists.txtlistshalppsource files explicitly instead of globbingdeps/halpp/src/*.cpp, remember to include any display vendor.cppfile referenced by your board preset (e.g.,display/vendors/st77916.cpp).
/src/halpp/— Core HAL implementation headers and source files:/boards/— Reusable header-only devkit presets (see Board Presets Index)./display/—esp_lcd+ LVGL v9 display, backlight, touch, and/display/vendors/panel init tables./audio/— I2S master, ES8311 DAC, ES7210 ADC, software mixer, AEC, and AGC./battery/— ADC oneshot + calibration LiPo battery monitor./buzzer/— Tickless LEDC passive buzzer & melody player (see Buzzer Guide)./core/—DefaultInstance<T>CRTP singleton helper./exio/—esp_io_expanderI2C port expander wrapper./gpio/— ISR + timer-debounced GPIO inputs./i2c/— Safe RAII wrappers for the I2C Master bus and devices./ledc/&/led_strip/— RAII LEDC timer/channel and RMT LED strip drivers./network/— Wi-Fi, ESP-NOW, NetStack, and NTP client wrappers./rfid/— PN532 NFC/RFID driver./rtc/— PCF85063A real-time clock driver./segmented/— Drivers for segmented displays (e.g., HT16K33 Adafruit Backpacks)./system/— Screen/radio/hibernate power management.
/emulated/— Desktop host shims (halpp_emulated) for SDL/unit-test builds without#ifdefs./src/README.md— Detailed styling and architectural guide for the library.
Licensed under the Apache License 2.0.