Skip to content

A header-only, platform-independent C++ utility library for embedded and systems programming.

License

Notifications You must be signed in to change notification settings

KSchattenfeld/uTBits

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

µTBits

PlatformIO Registry License PlatformIO Stars

Overview

µTBits is a header‑only C++ utility library focused on embedded and systems programming. It provides lightweight, typed memory/register views, simple color and math helpers, and compact PlatformIO examples. The library aims for maximum portability, minimal dependencies, and direct, safe control of hardware registers.


Key Features

  • Header-only: drop-in headers, no build dependencies.
  • fast_addr_t / fast_register_view: typed, safe bit views for memory and peripheral registers.
  • Color utilities: utb::graphic::color for float-based color handling and conversions.
  • Algorithm helpers: optimized copy/move, fill, search, bit utilities, and more
  • Low-level examples: native, AVR (Arduino Mega), ESP32, and WS2812 low-level drivers that use only uTBits APIs.
  • License: EUPL‑1.2.

Quick Install (PlatformIO)

Add the library from the PlatformIO registry or clone the repository and include the include/ directory in your project. The library is framework- and platform-agnostic.

Minimal platformio.ini for native tests

[env:native]
platform = native
lib_deps = 
  pba3h11aso/µTBits

[env:mega]
platform = atmelavr
board = megaatmega2560
framework = arduino
lib_deps = 
  pba3h11aso/µTBits

Include in code

#include <utfast_addr.h>
#include <utcolor.h>

Example: WS2812 (low-level, API-first)

This example demonstrates a WS2812 driver that uses only uTBits APIs: create_fast_view<T>(address), set(pos, bool), flip(pos), and utb::graphic::color. It uses timer polling for deterministic timing and avoids digitalWrite() or direct register macros.

Snippet

// create fast views for DDR and PORT
ddrd  = utb::create_fast_view<uint8_t>(0x2A);
portd = utb::create_fast_view<uint8_t>(0x2B);

// set pin as output via DDR
ddrd->set(LED_BIT, true);

// send a color (utb::graphic::color uses floats 0.0..1.0)
utb::graphic::color c(1.0f, 0.0f, 0.0f);
sendColor(c);

Full examples are available in the examples/ files: arduino_mega_fast_addr.cpp, esp32_fast_addr.cpp, arduino_mega_WS2812.cpp, and native_fast_addr.cpp.


API Summary

  • fast_addr_t<T> / fast_register_view<T, Bits>

    • create_fast_view<T>(address) — create a typed view to a memory/register address.
    • set(pos, bool) — set a single bit (bounds-checked).
    • flip() / flip(pos) — toggle all bits or a single bit.
    • num_ones() / num_zeros() — bit statistics.
    • value union member exposes the underlying numeric value when needed.
  • Color utilities

    • utb::graphic::color — float-based color types (0.0f..1.0f).
    • from_name<T>(color_name) — construct a color from predefined names.
  • Algorithms & Utilities (from utalgorithm.h)

    • copy_n, move_n, fill, fill_n — optimized memory operations.
    • construct_n, destruct_n — object lifecycle helpers.
    • lower_bound, upper_bound, binary_search, find, find_if — search algorithms.
    • foreach, accumulate — range utilities.
    • swap_ranges, random_shuffle — sequence manipulation.
    • nlz, popcount — bit-level helpers.
    • hex2char, isalpha, isxdigit — character utilities.

Examples & Tests

  • Examples: examples/ contains ready-to-run PlatformIO examples for native, AVR, and ESP32.
  • Tests: test/ contains Unity-based native tests for core functionality.

Contribution

Contributions are welcome. Please open issues for bugs or feature requests. Pull requests should:

  • Keep the library header-only and portable.
  • Include small, focused changes with tests or example updates when applicable.
  • Document API changes in the README or add example snippets.

License

This project is licensed under EUPL‑1.2. See LICENCE.md for details.