# EggBot Serial Protocol (USB/UART)

## Scope
This firmware emulates the EggBot/EBB command protocol over serial transport.

- Transport: USB CDC/UART (`115200 8N1`)
- Command separator: `,`
- Command terminator: carriage return (`\r`, ASCII 0x0D)
- Response terminator: carriage return + newline (`\r\n`)

## Handshake and Generic Responses
- Version query: `v\r`
- Version response: `EBBv13_and_above Protocol emulated by Eggduino-Firmware V1.6a\r\n`
- Success response: `OK\r\n`
- Error response: `unknown CMD\r\n`

## Implemented Commands
All commands are case-sensitive and comma-delimited.

- `EM,<mode>[,<ignored>]`
- `SC,<id>,<value>`
- `SP,<penState>[,<delayMs>]`
- `SM,<durationMs>,<penSteps>,<rotSteps>`
- `SE` (ack-only placeholder)
- `TP[,<delayMs>]`
- `PO` (ack-only placeholder)
- `NI`
- `ND`
- `SN,<nodeCount>`
- `QN`
- `SL,<layer>`
- `QL`
- `QP`
- `QB`

## Command Behavior Notes
- `SM`: firmware blocks until previous move completes, sends `OK`, then runs the requested move.
- `SP` and `TP`: support optional delay argument in milliseconds.
- `QP`: returns pen state line (`1` for pen up, `0` for pen down), then `OK`.
- `QB`: returns button state (`0` or `1`), then `OK`; internal button latch is reset after query.
- `QN`, `QL`: return numeric line first, then `OK`.
- `SC` supports these IDs:
  - `4`: set pen-down servo position (EBB value to servo-mapped value)
  - `5`: set pen-up servo position
  - `6`, `7`: accepted, ignored, and acknowledged
  - `11`: set servo rate up
  - `12`: set servo rate down

## Timing/Parsing Requirements for Hosts
- Always terminate each command with `\r`.
- Do not rely on `\n` as a command terminator.
- Read until `OK\r\n` (or `unknown CMD\r\n`) to complete command transactions.
- For commands that return data (`QP`, `QB`, `QN`, `QL`), read one data line plus the final status line.
