# robotdynofficial/rbddimmer architecture

> Architecture diagram of the GitHub repository robotdynofficial/rbddimmer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-11-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/robotdynofficial/rbddimmer
- Repository: https://github.com/robotdynofficial/rbddimmer

## Overview

##### Project type and purpose
- This is an Arduino “RBDdimmer” library for AC‐triac dimming on multiple MCUs (AVR, SAM, SAMD21, ESP8266, ESP32, STM32, etc.).
- Purpose: provide a simple API (dimmerLamp, begin, setPower, getPower, setMode, getState, etc.) that abstracts zero‐cross detection, timer setup, and triac firing.

##### File structure
- Top level:
- README files and metadata (library.json, library.properties, index.json, keywords.txt) – used by Arduino IDE.
- examples/ – user sketches demonstrating API usage.
- src/
– RBDdimmer.h – core API and shared logic
– Platform subdirectories (avr, esp8266, esp32, sam, samd, stm32duino) each with RBDmcuXXX.cpp/.h – hardware‐abstraction implementations.
- No backend/frontend; it’s a zero‐footprint embedded library.
- Build: Arduino IDE picks the matching src/<platform> based on defined macros.

##### README for additional insights
- Describes supported boards and their zero‐cross and output pins.
- Details 10 API functions and two operating modes (NORMAL_MODE, TOGGLE_MODE).
- Illustrates how user code calls begin(), setPower(), etc., triggering timers and interrupts under the hood.

##### Main components
a. Components
1. User Application (Arduino Sketch – user code in examples/)
2. RBDdimmer Core (RBDdimmer.h + core logic)
3. Hardware Abstraction Layer (HAL) – dispatch to platform modules
4. Platform Modules (RBDmcuAVR, RBDmcuESP8266, RBDmcuESP32, RBDmcuSAM, RBDmcuSAMD21, RBDmcuSTM32F1…)
5. Hardware Resources
- Zero-cross detector (external interrupt pin)
- Timer peripheral (internal MCU timer)
- Output pin controlling triac/gate
b. Relationships and interactions
- User calls API → RBDdimmer Core
- Core calls HAL init functions → selects specific RBDmcuXXX based on MCU macro
- Platform module configures timer and external interrupt, implements low‐level trigger()
- Hardware module issues triac gate pulses on output pin at computed times
- Zero-cross interrupt fires, control returns to core for phase angle calculation
c. Architectural patterns / design principles
- Layered architecture: API layer → core logic → hardware abstraction → platform implementations → hardware
- Strategy / polymorphism: each RBDmcuXXX implements the same interface for timers and interrupts
- Conditional compilation: Arduino build system picks platform code via #ifdefs
d. Key technologies / libraries
- Standard Arduino API (pinMode, digitalWrite, attachInterrupt)
- MCU‐specific peripheral registers (timers, EXTI) abstracted in RBDmcu*.cpp
- build metadata (library.json) for Arduino CLI/IDE

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User Layer
    User["User Sketch"]:::user
    click User "https://github.com/robotdynofficial/rbddimmer/tree/master/examples/"

    %% Core Layer
    Core["RBDdimmer Core\n(RBDdimmer.h)"]:::core
    click Core "https://github.com/robotdynofficial/rbddimmer/blob/master/src/RBDdimmer.h"

    %% HAL Layer
    HAL["Hardware Abstraction Layer"]:::platform

    %% Platform Modules
    subgraph "Platform Modules"
        AVR["RBDmcuAVR"]:::platform
        click AVR "https://github.com/robotdynofficial/rbddimmer/blob/master/src/avr/RBDmcuAVR.cpp"
        ESP8266["RBDmcuESP8266"]:::platform
        click ESP8266 "https://github.com/robotdynofficial/rbddimmer/blob/master/src/esp8266/RBDmcuESP8266.cpp"
        ESP32["RBDmcuESP32"]:::platform
        click ESP32 "https://github.com/robotdynofficial/rbddimmer/blob/master/src/esp32/RBDmcuESP32.cpp"
        SAM["RBDmcuSAM"]:::platform
        click SAM "https://github.com/robotdynofficial/rbddimmer/blob/master/src/sam/RBDmcuSAM.cpp"
        SAMD21["RBDmcuSAMD21"]:::platform
        click SAMD21 "https://github.com/robotdynofficial/rbddimmer/blob/master/src/samd/RBDmcuSAMD21.cpp"
        STM32["RBDmcuSTM32F1"]:::platform
        click STM32 "https://github.com/robotdynofficial/rbddimmer/blob/master/src/stm32duino/STM32F1/RBDmcuSTM32F1.cpp"
    end

    %% Hardware Resources
    subgraph "Hardware Resources"
        ZeroCross["Zero-Cross Detector"]:::hw
        Timer["Timer Peripheral"]:::hw
        Triac["Triac Output Pin"]:::hw
    end

    %% Connections
    User -->|"API call"| Core
    Core -->|"init()/trigger()"| HAL
    HAL -->|"selects and calls"| AVR
    HAL -->|"selects and calls"| ESP8266
    HAL -->|"selects and calls"| ESP32
    HAL -->|"selects and calls"| SAM
    HAL -->|"selects and calls"| SAMD21
    HAL -->|"selects and calls"| STM32

    %% Platform to Hardware
    AVR -->|"configures"| ZeroCross
    AVR -->|"configures"| Timer
    AVR -->|"drives"| Triac
    ESP8266 -->|"configures"| ZeroCross
    ESP8266 -->|"configures"| Timer
    ESP8266 -->|"drives"| Triac
    ESP32 -->|"configures"| ZeroCross
    ESP32 -->|"configures"| Timer
    ESP32 -->|"drives"| Triac
    SAM -->|"configures"| ZeroCross
    SAM -->|"configures"| Timer
    SAM -->|"drives"| Triac
    SAMD21 -->|"configures"| ZeroCross
    SAMD21 -->|"configures"| Timer
    SAMD21 -->|"drives"| Triac
    STM32 -->|"configures"| ZeroCross
    STM32 -->|"configures"| Timer
    STM32 -->|"drives"| Triac

    %% Interrupt flow
    ZeroCross -->|interrupt| HAL
    HAL -->|"ISR callback"| Core

    %% Styles
    classDef user fill:#E0F7FA,stroke:#006064,color:#006064;
    classDef core fill:#E8F5E9,stroke:#2E7D32,color:#2E7D32;
    classDef platform fill:#FFF3E0,stroke:#EF6C00,color:#EF6C00;
    classDef hw fill:#ECEFF1,stroke:#546E7A,color:#546E7A;
```
