2026-06-26 02:40:19 +02:00
2026-06-26 02:40:19 +02:00
2026-06-26 02:40:19 +02:00
2026-06-26 02:40:19 +02:00
2026-06-26 02:40:19 +02:00
2026-06-26 02:40:19 +02:00
2026-06-26 02:40:19 +02:00

calculatorFirmware

ESP32-S3 firmware for a standalone calculator and USB/Bluetooth macro keypad, with a USB RNDIS web dashboard for configuration, lighting, macros, and updates.

Features · Requirements · Getting started · Configuration · Usage · Architecture · Documentation · Credits · License

Features

  • Triple Operational Modes:
    • USB Mode: Wired USB HID Keyboard with ultra-low latency.
    • Bluetooth Mode: Wireless NimBLE keyboard with automatic host reconnection and bond management.
    • Calculator Mode: Standalone desktop calculator with local display output and memory registers.
  • Cascaded 12-Digit LED Display:
    • Twin MAX7219 display controllers with hardware intensity scaling (0–15).
    • Alphanumeric font rendering with right/left text alignment, smooth scrolling, and decimal point handling.
  • High-Rate Matrix Keypad Scanning:
    • Dedicated FreeRTOS polling task with dynamically benchmarked polling rates up to 1000 Hz.
    • Dedicated hardware modifier toggle switch (TGL) with debouncing and state isolation.
  • 19-Key ARGB Backlight (WS2812B):
    • Hardware DMA bitbang rendering via NeoPixelBus.
    • Multiple animation modes: Dynamic Shifting Rainbow (argbRainbow), Synchronized Rainbow (rgbRainbow), Static White (whiteStatic), Single Color (colorStatic), and Individual Per-Key Color (colorStaticPerKey).
    • Speed, brightness, and reactive lighting controls.
  • Integrated Macro Engine:
    • 8 distinct macro slots with up to 32 discrete steps per macro (Press, Release, Millisecond Delay).
    • Integrated key release validation to prevent stuck modifier or alphanumeric keys.
  • Real-Time Telemetry Diagnostics:
    • Live CPU Load (%) calculation.
    • Free Heap memory monitoring (absolute bytes and percentage).
    • System Uptime counter.
    • Battery capacity (%) and charging status detection.
    • Host connection indicators (USB HID Ready, BLE Connected, OS Lock LEDs).
  • Embedded Web Server (Mongoose):
    • Hosted directly on LittleFS flash partition at http://192.168.3.1 or http://calculator.local.
    • High-throughput MessagePack binary serialization over WebSockets.
  • Dual OTA Update Engine:
    • Browser-based drag-and-drop firmware and LittleFS filesystem updater.
    • Automated PlatformIO post-build OTA script (pio_ota.py) deploying over RNDIS.

Requirements

  • PlatformIO Core (CLI) or PlatformIO IDE extension for VS Code.
  • ESP32-S3 development board with native USB connected to the PC.

Getting started

Build firmware and filesystem

Build the firmware and LittleFS filesystem binaries:

# Build firmware
pio run -e esp32-s3-devkitm-1

# Build LittleFS filesystem image
pio run -t buildfs -e esp32-s3-devkitm-1

Note: The built artifacts are automatically copied to image/firmware.bin and image/littlefs.bin.

Flashing via USB

Connect the ESP32-S3 in bootloader mode (hold BOOT, tap RESET, release BOOT):

# Flash application
pio run -t upload -e esp32-s3-devkitm-1

# Flash filesystem
pio run -t uploadfs -e esp32-s3-devkitm-1

Flashing via RNDIS OTA

When the calculator is connected via USB and running the RNDIS stack, you can flash updates over HTTP:

  1. Via Web Dashboard:

    • Open http://192.168.3.1 (or http://calculator.local).
    • Click ⚙️ System Settings.
    • Under OTA Firmware Update, select Firmware (.bin) or LittleFS Filesystem (.bin), choose the file from image/, and click Upload & Update.
  2. Via PlatformIO CLI:

    pio run -e esp32-s3-ota -t upload
    

Configuration

Settings are stored in the /settings.json file on the LittleFS partition:

{
  "keyMapMain": { "0": "0x62", "1": "0x59", "+": "0x57", "e": "0x58" },
  "keyMapModifier": { "0": "0x62", "1": "0x59" },
  "keyMapCalcMain": { "0": "0x0A", "1": "0x01", "+": "0x0D", "e": "0x11" },
  "keyMapCalcModifier": { "0": "0x0C", "+": "0x16" },
  "screenBrightness": 8,
  "keyboardBrightness": 100,
  "rgbSpeed": 100,
  "selectedRGBMode": 0,
  "mdnsName": "calculator",
  "usbManufacturer": "Karol Labus",
  "usbProduct": "ESP32-S3 Calculator & MacroPad",
  "usbSerialNumber": "CALC-2026-S3",
  "deviceIP": "192.168.3.1",
  "rgbEnabled": true,
  "macros": []
}

Usage

The web dashboard is served directly from the ESP32-S3 flash memory over RNDIS USB Ethernet without needing any internet connection, external drivers, or cloud services.

Main dashboard and hardware mirroring

The primary interface provides a pixel-perfect, bidirectional twin of the physical device:

Main Dashboard

  • Virtual 12-Digit Display: Reflects the exact digits, decimals, and status messages shown on the physical MAX7219 LED display in real time.
  • Interactive Numpad: Highlights physical key depressions instantaneously. Key slots indicate current RGB backlighting.
  • WebSocket Terminal: Streams real-time logging, diagnostic events, state dumps, and permits manual command dispatch.
  • Global Device Controls:
    • Mode Selection: Switch between USB, Bluetooth, and Calculator modes, or lock the active mode to prevent auto-switching on USB connection.
    • Screen Brightness: 16-level intensity slider directly controlling MAX7219 hardware intensity.
    • Save & Revert: Synchronizes JSON settings to non-volatile LittleFS storage.

Lighting studio and per-key RGB

The Lighting Studio enables full customization of the 19 individual WS2812B RGB LEDs:

Lighting Studio

  • RGB Modes: Choose between flowing multi-color waves, solid colors, or per-key custom palettes.
  • Effect Speed & Brightness: Fine-tune animation tick speed (0–255) and master keyboard backlight brightness (0–100%).
  • Interactive Paintbrush Tool: Click any key on the virtual keyboard to assign custom hex colors individually when in colorStaticPerKey mode.

Key remapping context menu

Every key on the pad features a four-layer mapping matrix accessible via right-click or tap:

Key Remapping Context Menu

  • Main Map: USB HID keycode emitted on a regular keypress in USB or Bluetooth mode.
  • Modifier Map (TGL): Secondary keycode dispatched when the physical TGL modifier switch is engaged.
  • Calc Main Map: Calculator operation performed on normal keypress in Calculator mode.
  • Calc Modifier Map: Secondary calculator operation performed when the modifier is held.

Macro studio sequence builder

The Macro Studio allows recording and configuring complex key sequences without recompiling firmware:

Macro Studio

  • 8 Programmable Macro Profiles: Activate, name, and bind macros to custom keycodes (0xB0–0xB7).
  • Precision Action Sequencing:
    • Press Key: Emits HID press event.
    • Release Key: Emits HID release event.
    • Delay (ms): Non-blocking millisecond sleep between keystrokes.
  • Safety Validator: Enforces proper release events for all pressed keys before saving to prevent runaway keys on the host OS.

System settings and OTA updates

Configure low-level identity parameters and perform remote firmware upgrades:

System Settings & OTA

  • USB Descriptors: Customize USB Manufacturer, Product String, Serial Number, and Interface Names.
  • Network & Bluetooth: Change device IP address, mDNS hostname (.local), MAC address, and BLE advertising name.
  • BLE Bond Reset: Clear stored Bluetooth bonding tables with a single click.
  • Over-The-Air (OTA) Updates: Upload compiled firmware.bin or littlefs.bin directly with live percentage progress tracking and auto-reboot.

Architecture

Hardware specifications and pinout

Microcontroller

  • Module: ESP32-S3-WROOM-1 / ESP32-S3-DevKitM-1
  • Core: Dual-core Xtensa LX7 @ 240 MHz
  • Memory: 512 KB SRAM + 8 MB Flash
  • USB: Native USB 2.0 Full-Speed OTG (GPIO 19: D-, GPIO 20: D+)

Display module (MAX7219)

The display subsystem employs two cascaded MAX7219 LED driver ICs driving a total of 12 digits:

Signal ESP32-S3 GPIO Description
DIN GPIO 11 SPI Serial Data Input
CLK GPIO 12 SPI Serial Clock
CS GPIO 13 SPI Chip Select (Active LOW)
VCC 5V / VBUS Logic and LED Drive Power
GND GND Common Ground
  • Chip 0: Drives Digits 0 through 7.
  • Chip 1: Drives Digits 8 through 11.

Keyboard matrix (5x4) and modifier

A 5-row by 4-column matrix read by a dedicated FreeRTOS keyboard task:

Matrix Line ESP32-S3 GPIO Direction / Mode
Row 0 GPIO 1 Output (Active Scan)
Row 1 GPIO 2 Output (Active Scan)
Row 2 GPIO 3 Output (Active Scan)
Row 3 GPIO 4 Output (Active Scan)
Row 4 GPIO 5 Output (Active Scan)
Col 0 GPIO 6 Input (Pull-down/up)
Col 1 GPIO 7 Input (Pull-down/up)
Col 2 GPIO 8 Input (Pull-down/up)
Col 3 GPIO 9 Input (Pull-down/up)
Modifier (TGL) GPIO 15 Input (INPUT_PULLUP, Active LOW)
Physical key layout matrix
+---------------+---------------+---------------+---------------+
|  NumLock (n)  |       /       |       *       |       -       |
+---------------+---------------+---------------+---------------+
|       7       |       8       |       9       |    DE (r)     |
+---------------+---------------+---------------+---------------+
|       4       |       5       |       6       |    AC (o)     |
+---------------+---------------+---------------+---------------+
|       1       |       2       |       3       |       +       |
+---------------+---------------+---------------+ (Double Tall) |
|    (Blank)    |       0       |       .       |    Enter (e)  |
+---------------+---------------+---------------+---------------+

Addressable ARGB lighting

  • Controller: GPIO 14
  • Protocol: WS2812B / NeoPixel (800 kHz bitbang DMA)
  • Count: 19 Addressable LEDs mapped beneath individual switches

Software architecture

flowchart TD
    subgraph Core [ESP32-S3 Firmware Core]
        APP[App Logic Engine]
        KB[FreeRTOS Matrix Scanner Task]
        RGB[FreeRTOS ARGB Engine Task]
        MATH[BigNumber Arbitrary Math Engine]
        SET[Settings Manager LittleFS]
    end

    subgraph Connectivity [I/O & Networking]
        TUSB[TinyUSB Composite Driver]
        RNDIS[RNDIS Virtual Ethernet]
        HID[USB HID Keyboard]
        BLE[NimBLE Bluetooth Keyboard]
        MONGOOSE[Mongoose Web & WebSocket Server]
    end

    subgraph Physical [Hardware Components]
        KEYPAD[5x4 Matrix + TGL Switch]
        LEDS[19x WS2812B LEDs]
        DISP[2x MAX7219 12-Digit LED]
    end

    KEYPAD --> KB
    KB --> APP
    APP --> MATH
    APP --> DISP
    APP --> RGB
    RGB --> LEDS
    APP --> HID
    APP --> BLE
    TUSB --> RNDIS
    TUSB --> HID
    RNDIS --> MONGOOSE
    SET <--> APP
    MONGOOSE <--> APP

Operating modes

  1. USB Keyboard Mode (KEYBOARD_MODE_USB):
    • Emits standard USB HID scancodes upon matrix events.
    • Includes automatic NumLock mounting negotiation hack to synchronize OS status.
  2. Bluetooth Keyboard Mode (KEYBOARD_MODE_BLUETOOTH):
    • NimBLE HID keyboard stack with battery service.
    • Automatic background reconnection to bonded hosts.
  3. Calculator Mode (KEYBOARD_MODE_CALCULATOR):
    • Routes keypresses directly into the arbitrary-precision mathematical calculation engine.
    • Outputs intermediate and final results to the MAX7219 7-segment display.

Calculator mathematics engine

  • Engine: Nick Gammon's BigNumber library (BC-style arbitrary-precision arithmetic).
  • Supported Operations:
    • Addition (+), Subtraction (-), Multiplication (*), Division (/).
    • Single-operand: Square Root (sqrt), Power (^), Inversion (1/x), Negation (+/-), Square (x²), Cube Root (∛x).
    • Independent Dual Memory Banks: M1 (Store, Recall, Clear), M2 (Store, Recall, Clear).
    • Accumulator Memory: M Sum (Add, Subtract, Recall, Clear).
    • Trailing zero and fractional precision retention.

WebSockets and protocol

Communication between the web browser and the ESP32-S3 occurs over /ws using binary MessagePack packets for optimal bandwidth and minimal latency:

Event ID Identifier Direction Payload Structure
1 EVT_TERMINAL_WEB_TO_MCU Web → MCU { textToDisplay: string }
2 EVT_KEY_DOWN_WEB Web → MCU { char: string, modifier: bool }
3 EVT_KEY_UP_WEB Web → MCU { char: string }
8 EVT_DISPLAY_ALL MCU → Web { values: uint8_t[12] }
10 EVT_RGB_ALL MCU → Web { colors: uint8_t[57] }
11 EVT_UPDATE_SETTINGS_FROM_WEB Web → MCU { settings: object }
15 EVT_SET_MCU_KEYBOARD_MODE Web → MCU { mode: int, lock: bool }
17 EVT_SYSTEM_STATUS MCU → Web { ble, usb, heap, totalHeap, uptime, battery, charging, pollingRate, cpuLoad }
19 EVT_CLEAR_BLE_BONDS Web → MCU null
20 EVT_OS_LED_STATE MCU → Web { leds: uint8_t }

Documentation

README style guide.

Credits

Author: Karol Labus. The original README and interface walkthrough assets were created with AI assistance.

License

No project-level license file is currently included in this repository. Third-party components retain their own licenses.

S
Description
Advanced ESP32-S3 Smart Calculator, BLE/USB Macro Keypad & RNDIS Web Dashboard
Readme 177 MiB
Languages
C 85.1%
C++ 4.3%
Python 3.3%
Linker Script 2.5%
Assembly 1.5%
Other 3%