# ctaverna/micropython_ir architecture

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

- Interactive diagram: https://gitdiagram.com/ctaverna/micropython_ir
- Repository: https://github.com/ctaverna/micropython_ir

## Overview

##### Main Components
- IR Receiver Module – Represented by the content inside the ir_rx directory. This module handles receiving IR signals, processes hardware interrupts, and decodes signals into meaningful data (address, data bytes, etc.).
- IR Transmitter Module – Represented by the ir_tx directory. This module is responsible for encoding commands (according to the various protocols) and driving the IR LED to emit the appropriate modulated signals.

##### Relationships and Interactions
- Data Flow in the Receiver: Display how an IR signal is captured through a hardware pin interrupt, passed to low-level decoding routines (using protocol-specific logic), and then exposed (for example via a REPL or application) for further processing.
- Data Flow in the Transmitter: Indicate the process where an application (or user input) triggers code that selects a specific protocol routine, encodes the command, and sends the modulated signal to the IR LED.
- Cross-module Dependency: Although the modules are separate for receiving and transmitting, they share similar protocol decoding/encoding logic. A note on the reuse or similarity between these sub-modules can be added as a design detail.

##### Architectural Patterns and Principles
- Modularity and Separation of Concerns: The repository is clearly modular with separate directories for IR receiving and transmitting. Stress that each protocol is isolated in its own file to maintain single responsibility and ease further extension.
- Hardware Abstraction: Note the abstraction from the hardware particulars (carrier frequency dependency, handling soft/hard IRQ differences) so that the same core logic applies across multiple platforms. This architectural decision is key to cross-platform portability.
- Nonblocking and ISR-based design: Emphasize that the drivers are designed not to block execution (and they integrate ideally with uasyncio if desired). At the same time, timing-critical aspects are handled through interrupts where hardware supports them.

##### Incorporate Relevant Technologies and Libraries
- Python-based driver implementation, with specific code files targeting MicroPython environments.
- Mention that additional configuration (such as the package.json files in each driver folder) may indicate some JavaScript or Node.js aspects for package/dependency management, even if their role is minimal in the core driver functionality.
- Hardware references and links in the README suggest that this project integrates with specific electronic components and relies on precise signal timings.

##### Detail Without Overcomplicating
- Although the architecture is relatively straightforward, be detailed in showing the separation between receiving and transmitting channels, along with the protocol-specific routines.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Hardware Components
    IR_SENSOR["IR Sensor (TSOP4838)"]:::hardware
    IR_LED["IR LED (with transistor)"]:::hardware
    PLATFORMS["Supported Microcontrollers<br>(Pyboard, ESP8266, ESP32, Raspberry Pi Pico)"]:::platform
    APP["Application/REPL"]:::external

    %% IR Receiver Module Subgraph
    subgraph "IR Receiver Module"
        IR_RX_DRIVER["IR Receiver Driver"]:::driver
        NEC_RX["NEC Protocol (RX)"]:::protocol
        SONY_RX["Sony Protocol (RX)"]:::protocol
        PHILIPS_RX["Philips Protocol (RX)"]:::protocol
        MCE_RX["MCE Protocol (RX)"]:::protocol
        PKG_RX["Dependency & Config (package.json)"]:::config
    end

    %% IR Transmitter Module Subgraph
    subgraph "IR Transmitter Module"
        IR_TX_DRIVER["IR Transmitter Driver"]:::driver
        NEC_TX["NEC Protocol (TX)"]:::protocol
        SONY_TX["Sony Protocol (TX)"]:::protocol
        PHILIPS_TX["Philips Protocol (TX)"]:::protocol
        MCE_TX["MCE Protocol (TX)"]:::protocol
        PKG_TX["Dependency & Config (package.json)"]:::config
    end

    %% Documentation & Hardware Notes Subgraph
    subgraph "Documentation & Hardware Notes"
        DOC_README["README & Driver Docs"]:::doc
        CIRCUITS["Circuit Schematics (images/circuits.fzz)"]:::doc
    end

    %% Data Flow Connections
    IR_SENSOR -->|"captures"| IR_RX_DRIVER
    IR_RX_DRIVER -->|"decoded_output"| APP
    APP -->|"command_input"| IR_TX_DRIVER
    IR_TX_DRIVER -->|"emit_signal"| IR_LED

    %% Platform Dependency Arrows
    PLATFORMS --- IR_RX_DRIVER
    PLATFORMS --- IR_TX_DRIVER

    %% Documentation References
    DOC_README --- IR_RX_DRIVER
    DOC_README --- IR_TX_DRIVER
    CIRCUITS --- IR_LED

    %% Cross Module Protocol Shared Logic (dotted lines)
    NEC_RX ---|shared_logic| NEC_TX
    SONY_RX ---|shared_logic| SONY_TX
    PHILIPS_RX ---|shared_logic| PHILIPS_TX
    MCE_RX ---|shared_logic| MCE_TX

    %% Click Events for IR Receiver Components
    click IR_RX_DRIVER "https://github.com/ctaverna/micropython_ir/tree/master/ir_rx"
    click NEC_RX "https://github.com/ctaverna/micropython_ir/blob/master/ir_rx/nec.py"
    click SONY_RX "https://github.com/ctaverna/micropython_ir/blob/master/ir_rx/sony.py"
    click PHILIPS_RX "https://github.com/ctaverna/micropython_ir/blob/master/ir_rx/philips.py"
    click MCE_RX "https://github.com/ctaverna/micropython_ir/blob/master/ir_rx/mce.py"
    click PKG_RX "https://github.com/ctaverna/micropython_ir/blob/master/ir_rx/package.json"

    %% Click Events for IR Transmitter Components
    click IR_TX_DRIVER "https://github.com/ctaverna/micropython_ir/tree/master/ir_tx"
    click NEC_TX "https://github.com/ctaverna/micropython_ir/blob/master/ir_tx/nec.py"
    click SONY_TX "https://github.com/ctaverna/micropython_ir/blob/master/ir_tx/sony.py"
    click PHILIPS_TX "https://github.com/ctaverna/micropython_ir/blob/master/ir_tx/philips.py"
    click MCE_TX "https://github.com/ctaverna/micropython_ir/blob/master/ir_tx/mce.py"
    click PKG_TX "https://github.com/ctaverna/micropython_ir/blob/master/ir_tx/package.json"

    %% Click Events for Documentation
    click DOC_README "https://github.com/ctaverna/micropython_ir/blob/master/README.md"
    click CIRCUITS "https://github.com/ctaverna/micropython_ir/blob/master/images/circuits.fzz"

    %% Styles
    classDef driver fill:#bbf,stroke:#333,stroke-width:2px;
    classDef protocol fill:#cfc,stroke:#333,stroke-width:2px;
    classDef hardware fill:#fdd,stroke:#333,stroke-width:2px;
    classDef config fill:#fcf,stroke:#333,stroke-width:2px;
    classDef doc fill:#ffd,stroke:#333,stroke-width:2px;
    classDef platform fill:#ddf,stroke:#333,stroke-width:2px;
    classDef external fill:#efe,stroke:#333,stroke-width:2px;
```
