# 🤖 MashaBot v3.2.0 — Complete User Guide (English & বাংলা)

Welcome to **MashaBot** — an intelligent, expressive **ESP32-S3** desktop companion robot designed specifically for **Bangladeshi Bangla (`bn-BD`)** and **English** users.

---

## 📋 Table of Contents
1. [What MashaBot Can Do (All Features)](#1-what-mashabot-can-do-all-features)
2. [Hardware & Wiring Reference](#2-hardware--wiring-reference)
3. [Starting the System (One-Click)](#3-starting-the-system-one-click)
4. [Account Setup & Pairing Your Bot](#4-account-setup--pairing-your-bot)
5. [Friends & Cross-User Popup Messaging (বন্ধু যোগ করা ও মেসেজ পাঠানো)](#5-friends--cross-user-popup-messaging)
6. [Touch Sensor Gestures & 9-Item OLED Menu](#6-touch-sensor-gestures--9-item-oled-menu)
7. [Voice Commands & Intelligent Bangla AI Brain](#7-voice-commands--intelligent-bangla-ai-brain)
8. [Switching Online (Groq) vs Offline (Local GPU) STT](#8-switching-online-groq-vs-offline-local-gpu-stt)
9. [Native Bangla OLED Display & Long-Message Scrolling](#9-native-bangla-oled-display--long-message-scrolling)
10. [Clock, Weather Desk Mode & Ambient Sound Sensitivity](#10-clock-weather-desk-mode--ambient-sound-sensitivity)
11. [Emotions, Soundboard & Custom Sound Composer](#11-emotions-soundboard--custom-sound-composer)
12. [Version Management & Keeping Backups](#12-version-management--keeping-backups)
13. [Moving to a Cloud Server (VPS) & Troubleshooting](#13-moving-to-a-cloud-server-vps--troubleshooting)

---

## 1. What MashaBot Can Do (All Features)
- **Expressive 60 FPS OLED Face:** 16 animated emotions (`idle`, `happy`, `laugh`, `love`, `curious`, `excited`, `surprised`, `wink`, `sleepy`, `sad`, `angry`, `dizzy`, `cool`, `startled`, `listening`, `speaking`) with squash-and-stretch physics, natural blinking, glancing, and audio-reactive mouth movement.
- **Studio-Quality 44.1 kHz Synthesized Audio:** 19 musical & emotional sound effects plus a **Custom Sound Composer** right in the Web Studio.
- **Crisp Native Bangladeshi Bangla on OLED:** Server-side Windows GDI `Nirmala UI` complex-script shaping renders true Bangla matras and conjuncts (যুক্তবর্ণ) into 1-bit bitmaps with **Down → Up** (slow), **Right → Left**, or **Static** scrolling.
- **Intelligent Bangladeshi Bangla Voice AI:**
  - **Online Mode:** Groq `whisper-large-v3-turbo` STT + `qwen/qwen3.8-27b` LLM Brain (~190 ms response). Understands natural Bangladeshi Bangla, Banglish, and English — asks for time/weather, changes bot settings by voice, chats, and sends messages to friends!
  - **Offline Mode:** Local GPU-accelerated `faster-whisper` (CUDA) + local bilingual keyword parser — 100% free and works without internet.
- **Multi-User Friends & Live Bot-to-Bot Popups:** Connect with other MashaBot users via **Friend Code (`MSH-XXXX`)** or **Email** and send Bangla/English popup messages that appear, chime, and scroll live on their bot's OLED screen!
- **Full Single-Touch Sensor Control & 9-Item OLED Menu:** Control screen mode, mood, volume, brightness, sound sensitivity, scroll mode, voice AI, and network info directly on the bot without touching a phone or PC.
- **Dhaka Clock & Live Weather Desk Mode:** Displays Dhaka 12-hour time, date, animated weather icon, and temperature (auto-synced every 10 minutes via Open-Meteo).
- **Ambient Sound Reactivity:** Startles and reacts playfully to sudden claps or loud sounds (configurable sensitivity 10–100).

---

## 2. Hardware & Wiring Reference

**Board:** `XH-S3E-AI_V1.0` (`ESP32-S3-N16R8` — 16 MB Flash, 8 MB OPI PSRAM)

| Peripheral | Signal | ESP32-S3 GPIO | Notes |
|---|---|---|---|
| **SSD1306 OLED (128×64 I2C)** | `SDA` / `SCL` | `GPIO 41` / `GPIO 42` | 100 kHz I2C, auto-detects `0x3C` or `0x3D` |
| **Onboard Speaker (NS4168 I2S)** | `BCLK` / `LRCK` / `DOUT` / `AMP_EN` | `15` / `16` / `7` / `17` | Onboard, 44.1 kHz stereo |
| **Onboard Mic (I2S MEMS)** | `WS` / `SCK` / `DIN` | `4` / `5` / `6` | Onboard, 16 kHz capture |
| **TTP223B Touch Sensor (Blue)** | `SIG` / `VCC` / `GND` | `GPIO 43 (TXD)` / `GPIO 44 (RXD)` / `GND` | Set `touch_pin=43`, `touch_power_pin=44`, `touch_mode=digital_high` in Studio |
| **BOOT Button** | `BOOT` | `GPIO 0` | Short press = happy boop; Hold 5s = factory reset & Wi-Fi setup portal |

---

## 3. Starting the System (One-Click)

From `i:\Server\htdocs\mashabot`, run:
```powershell
powershell -ExecutionPolicy Bypass -File start-mashabot.ps1
```
This starts:
1. **Python STT & Bangla Bitmap Server** (`stt_server.py`) on `http://127.0.0.1:5055` (CUDA GPU accelerated).
2. **MashaBot Node.js Server** (`server.js`) on `http://0.0.0.0:3000`.

Then open the **Web Studio** in your browser:
- Local PC: `http://localhost:3000`
- Phone / Another PC on same Wi-Fi: `http://<YOUR_PC_LAN_IP>:3000` (e.g., `http://192.168.0.132:3000`)

---

## 4. Account Setup & Pairing Your Bot

### English
1. Open `http://localhost:3000`.
2. Enter your **Email** and **Password** and click **Sign In** (new accounts are created automatically on first sign-in).
3. Power on your MashaBot ESP32-S3. It connects to Wi-Fi and displays a **6-digit Pair Code** on its OLED screen (it also appears under **Discovered on Local Network**).
4. Type the 6-digit code into **Connect & Pair Bot** and click **Pair by Code** (or click the **Pair** button in the discovered list).
5. Your bot chimes, smiles, and syncs all settings immediately!

### বাংলায় ধাপে ধাপে
১. ব্রাউজারে `http://localhost:3000` খুলুন।  
২. আপনার **ইমেইল** ও **পাসওয়ার্ড** দিয়ে **Sign In** করুন (নতুন ইমেইল দিলে অটোমেটিক অ্যাকাউন্ট তৈরি হয়ে যাবে)।  
৩. MashaBot চালু করুন। Wi-Fi কানেক্ট হলে OLED স্ক্রিনে একটি **৬ সংখ্যার Pair Code** দেখাবে।  
৪. Web Studio-তে কোডটি লিখে **Pair by Code** ক্লিক করুন (অথবা *Discovered on Local Network* থেকে সরাসরি **Pair** ক্লিক করুন)।  
৫. বটটি মিষ্টি শব্দ করে আপনার অ্যাকাউন্টের সাথে কানেক্ট হয়ে যাবে!

---

## 5. Friends & Cross-User Popup Messaging
*(বন্ধু যোগ করা ও এক বট থেকে অন্য বটে মেসেজ পাঠানো)*

Every user automatically gets a unique **Friend Code** (e.g., `MSH-6183`) displayed in the **Friends & Cross-User Bot Messaging** card.

### Step-by-Step: How to Add a Friend
1. **Share your code:** Click **📋 Copy** next to `My Friend Code: MSH-XXXX` and send it to your friend (or give them your MashaBot login email).
2. **Add your friend:** In the **Friends** panel, paste your friend's **Friend Code (`MSH-XXXX`)** or **Email** into the box and click **Add Friend**.
3. Your friend appears in your Friends List with a live status dot:
   - 🟢 **Online** — their MashaBot is currently connected and ready for live popups.
   - ⚪ **Offline** — messages will be saved in their **Friend Message Inbox**.

### Step-by-Step: How to Send a Live Popup Message
1. **From Web Studio:**
   - Select your friend from the dropdown list.
   - Choose an **Emotion** (`❤️ Love`, `😊 Happy`, `🎉 Excited`, `😂 Laugh`, `😮 Surprised`, `😉 Wink`).
   - Type any **Bangla or English** message (short or long!).
   - Click **Send Popup** (or press `Enter`).
   - **What happens on your friend's bot:** Their bot immediately plays an alert chime, shows the chosen emotion, and **smoothly scrolls your Bangla/English message** across their OLED screen!
2. **By Voice (with AI Brain enabled):**
   - Just say naturally in Bangla or English:
     - *"রহিমকে মেসেজ পাঠাও আজকে দেখা হবে"*
     - *"Send a message to MSH-1234 saying Good night!"*

### বাংলায় সহজ নির্দেশিকা (বন্ধু ও মেসেজিং)
১. আপনার **My Friend Code** (যেমন `MSH-6183`) কপি করে বন্ধুকে দিন।  
২. বন্ধুর `MSH-XXXX` কোড বা ইমেইল লিখে **Add Friend** বাটনে ক্লিক করুন।  
৩. বন্ধুকে সিলেক্ট করুন, ইমোশন বাছাই করুন, বাংলা বা ইংরেজি মেসেজ লিখুন এবং **Send Popup** ক্লিক করুন।  
৪. বন্ধুর বট অনলাইনে থাকলে সাথে সাথেই তার OLED স্ক্রিনে মিষ্টি সাউন্ডসহ আপনার বাংলা লেখা সুন্দরভাবে স্ক্রল হয়ে ভেসে উঠবে!

---

## 6. Touch Sensor Gestures & 9-Item OLED Menu

MashaBot uses a **single touch sensor** to unlock full control right on your desk:

### Normal Mode Gestures
| Gesture | Action | বাংলা বিবরণ |
|---|---|---|
| **1× Single Tap** | Cute reaction (Happy + Boop) | একবার টাচ: মিষ্টি রিয়্যাকশন |
| **2× Double Tap** | **Voice AI Listen (4 sec)** — speak Bangla/English command | দুবার টাচ: ৪ সেকেন্ড ভয়েস কমান্ড শুনবে |
| **3× Triple Tap** | **Open 9-Item OLED Menu** | তিনবার টাচ: OLED মেনু খুলবে |
| **4× Rapid Tap** | Tickle reaction (Laugh + Tickle sound) | চারবার টাচ: সুড়সুড়ি ও হাসি |
| **Hold ~0.65 sec** | Long-Press Pet (Love + Purr) | একটু ধরে রাখলে: আদর (Love) |
| **Hold ~3.8 sec & keep holding** | **Push-to-Talk Recording** — shows `Recording...` and records until you release (up to 10s) | ৪ সেকেন্ড ধরে রাখলে: ছেড়ে না দেওয়া পর্যন্ত ভয়েস রেকর্ড করবে |

### Inside the 9-Item OLED Menu (Opened via 3× Triple Tap)
- **1× Single Tap:** Move to next item (`Next`)
- **2× Double Tap:** Select / Toggle current item (`OK`)
- **Hold ~0.65 sec:** Exit menu (`Exit`)

**Menu Items:**
1. `Screen:` Switch between `face` → `clock_weather` → `auto_cycle`
2. `Mood:` Cycle default idle mood (`idle`, `happy`, `cool`, `love`, `curious`, `sleepy`)
3. `Volume:` Step speaker volume (`30%` → `50%` → `70%` → `90%` → `100%`)
4. `Bright:` Step OLED brightness (`80` → `150` → `210` → `255`)
5. `SoundAlert:` Toggle ambient sound reactivity (`ON` / `OFF`)
6. `Scroll:` Cycle long-message scroll direction (`up` → `left` → `none`)
7. `Voice AI: Listen` Start a 4-second voice command immediately
8. `Net Info & IP` Show bot's Wi-Fi IP address & firmware version
9. `Exit Menu` Return to face/clock screen

> Every change made in the OLED menu automatically syncs back to the server and Web Studio!

---

## 7. Voice Commands & Intelligent Bangla AI Brain

You can talk to MashaBot in **3 ways**:
1. **Onboard Bot Mic:** Double-tap the touch sensor (4s) OR hold touch sensor ~3.8s until you finish speaking OR click **🤖 Record Bot Mic (4s)** in Web Studio.
2. **Browser Mic:** Click **🎙️ Record Browser Mic** in Web Studio, speak, then click **⏹️ Stop & Save**.
3. **Text Command Box:** Type any Bangla or English sentence in **Voice Recorder → Run Text Cmd** and press `Enter`.

### Example Natural Bangladeshi Bangla & English Commands
- **Time & Weather:**
  - *"এখন কয়টা বাজে?"* / *"What time is it?"*
  - *"ঢাকার আবহাওয়া কেমন?"* / *"How's the weather today?"*
- **Live Settings Control by Voice:**
  - *"ভলিউম একটু বাড়াও"* / *"ভলিউম ৫০ করো"* / *"Make it quieter"*
  - *"ব্রাইটনেস কমাও"* / *"Set brightness to 120"*
  - *"ঘড়ি আর আবহাওয়া দেখাও"* / *"Switch to clock weather mode"*
  - *"ফেস মোডে ফিরে যাও"* / *"Show your face"*
  - *"স্ক্রল ডান থেকে বাম করো"* / *"Scroll down to up"*
  - *"ইংরেজিতে কথা বলো"* / *"বাংলায় কথা বলো"*
  - *"সাউন্ড সেন্সিটিভিটি বন্ধ করো"*
- **Emotions & Chat:**
  - *"মাশা কেমন আছো?"* / *"হাহা একটু হাসো তো"* / *"তোমাকে ভালোবাসি"* / *"ঘুমিয়ে পড়ো"*
- **Send Friend Messages:**
  - *"MSH-1234 কে বলো আমি আসছি"*

---

## 8. Switching Online (Groq) vs Offline (Local GPU) STT

In the **Voice Recorder** card, choose your **Speech-to-Text Engine** and click **Save & Sync All to Bot**:

| Engine | Best For | Speed | Internet Needed? |
|---|---|---|---|
| **Online: Groq Whisper v3 Turbo** | Highest Bangladeshi Bangla accuracy + fast cloud STT | ~250–450 ms | Yes (`GROQ_API_KEY` in `.env`) |
| **Offline: Local faster-whisper (GPU)** | 100% private, zero API quota, CUDA GPU accelerated | ~200–400 ms (GPU) | No (runs locally on port `5055`) |

- **Automatic Fallback:** If Online Groq is unreachable or rate-limited, MashaBot automatically falls back to your local GPU `faster-whisper` server!
- **Silence Gate:** Silent or accidental clips are automatically detected before calling Groq so your API quota is never wasted.
- **AI Brain Toggle:** Uncheck **Enable Groq AI Brain** if you want pure offline keyword-rule matching without LLM calls.

---

## 9. Native Bangla OLED Display & Long-Message Scrolling

In **Server-Synced Bot Settings**, configure:
- **Long Message Scroll (`scroll_mode`):**
  - `Down → Up (Slow)` *(Recommended)* — Smoothly scrolls multi-line Bangla paragraphs upward from bottom to top.
  - `Right → Left` — Single-line marquee scrolling right to left.
  - `Static` — Fixed 128×48 card.
- **Scroll Speed (`scroll_speed_ms`):** Milliseconds per pixel (`20` = fast, `55` = smooth slow default, `120` = very slow).

Short replies (≤ 18 characters) automatically appear in a cute rounded **bottom banner** underneath Masha's animated face. Longer messages use the **full scrollable viewport** with mini peeking eyes in the header!

---

## 10. Clock, Weather Desk Mode & Ambient Sound Sensitivity

- **OLED Screen Mode (`screen_mode`):**
  - `Expressive Eyes & Mouth`: Always shows Masha's animated face.
  - `Dhaka Clock & Weather Desk`: Shows large 12-hour Dhaka time, date, animated sun/cloud/rain icon, and live °C temperature.
  - `Auto Cycle (Face + Weather)`: Cycles 20s Face / 10s Clock & Weather automatically.
- **Weather City:** Change `city` (e.g., `Dhaka`, `Chittagong`, `Sylhet`, `Rajshahi`, `Khulna`) and click **Save & Sync All to Bot**. Server automatically refreshes weather & clock every 10 minutes.
- **Ambient Sound Reactivity:** Enable **React to Sudden Ambient Sounds** and tune **Sound Sensitivity (10–100)**. When a loud clap/sound occurs while idle, Masha jumps (`startled`) and chirps!

---

## 11. Emotions, Soundboard & Custom Sound Composer
- **Quick Bilingual Presets:** One-click mood + sound + localized Bangla/English OLED caption.
- **16 Emotions & 19 Sounds:** Trigger any emotion or sound live from the Studio with adjustable duration.
- **Custom Funny Sound Composer:** Set `Start Hz`, `End Hz`, `Length ms`, and `Wobble Hz` to synthesize custom robot chirps directly on the ESP32-S3's 44.1 kHz audio engine!

---

## 12. Version Management & Keeping Backups

Every release of MashaBot is preserved with clean source code only (no `node_modules/`, no `.env` secrets, no build artifacts):
- **Git Tags:** `v1.0.0` through `v3.2.0` (`git tag -l`)
- **Clean Source Archives:** `versions/mashabot-vX.Y.Z.zip`
- **Changelog:** `CHANGELOG.md`

To cut a new version after making changes:
```powershell
powershell -ExecutionPolicy Bypass -File scripts/release.ps1 -Version "3.3.0" -Message "Your release summary"
```
This automatically updates `package.json` & `MashaBot.ino`, commits, creates an annotated Git tag, and exports `versions/mashabot-v3.3.0.zip`.

---

## 13. Moving to a Cloud Server (VPS) & Troubleshooting

### Moving from Local PC to Cloud VPS (Zero Reflash!)
1. Deploy the Node.js server on your VPS (with HTTPS/WSS via Nginx or Caddy).
2. In Web Studio → **Local → Cloud Server Migration**, enter your domain (e.g., `bot.yourdomain.com`), port (`443`), path (`/ws/device`), check **WSS (TLS)**, and click **Push New Server to Bot**.
3. The ESP32-S3 saves the new server in NVS and reconnects automatically!

### Common Troubleshooting
- **Bot mic recording says "Too Quiet":** Speak ~15–30 cm from the board after the cue beep finishes (0.25s after tapping). Check saved clip playback in Web Studio.
- **OLED stays black:** Check `SDA=41`, `SCL=42`, `3V3`, `GND` seating. From `v3.2.0`, MashaBot still boots and runs audio/Wi-Fi cleanly even if the OLED is unplugged.
- **Touch sensor not responding:** Verify `touch_pin=43`, `touch_power_pin=44`, `touch_mode=digital_high` for the blue TTP223B sensor on the top header pins, then click **Calibrate Baseline Now**.
- **Forgot Wi-Fi or changed router:** Hold the **BOOT (`GPIO 0`)** button for **5 seconds** to open the `MashaBot-Setup` Wi-Fi captive portal.
