Skip to content
taptedPublic

About

ESP32 C++ Modular Hardware Abstraction Layers

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

167 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HAL++ (halpp)

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.

Project Goals

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_t checking with a monadic EspResult<T> and EspError pattern, 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, and string_view to 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.

Dependencies

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 the EspResult and EspError implementations. You must include espbase in your project alongside halpp.

Integration Guide

The recommended way to integrate halpp into your ESP-IDF project is via git submodules and CMake source inclusion.

1. Add Submodules

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

2. Update your component CMakeLists.txt

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)

3. Configure Your Board (hal/board.hpp)

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.

Option A: Use a Built-In Board Preset Directly

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::board

Option B: Extend a Board Preset with Project Overrides

To 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::board

Option C: Define a Custom Board from Scratch

For 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::board

Note on optional components: Board presets guard managed-component headers (such as esp_lcd_st77916, esp_lcd_touch_cst816s, and esp_io_expander_tca9554) with #if __has_include(...), so you only need to list the components you actually use in idf_component.yml / REQUIRES. If your CMakeLists.txt lists halpp source files explicitly instead of globbing deps/halpp/src/*.cpp, remember to include any display vendor .cpp file referenced by your board preset (e.g., display/vendors/st77916.cpp).

Directory Structure

  • /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_expander I2C 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.

License

Licensed under the Apache License 2.0.

About

ESP32 C++ Modular Hardware Abstraction Layers

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages