zeroclaw/firmware/zeroclaw-arduino/zeroclaw-arduino.ino
ehu shubham shaw de3ec87d16
Ehu shubham shaw contribution --> Hardware support (#306)
* feat: add ZeroClaw firmware for ESP32 and Nucleo

* Introduced new firmware for ZeroClaw on ESP32 and Nucleo-F401RE, enabling JSON-over-serial communication for GPIO control.
* Added `zeroclaw-esp32` with support for commands like `gpio_read` and `gpio_write`, along with capabilities reporting.
* Implemented `zeroclaw-nucleo` firmware with similar functionality for STM32, ensuring compatibility with existing ZeroClaw protocols.
* Updated `.gitignore` to include new firmware targets and added necessary dependencies in `Cargo.toml` for both platforms.
* Created README files for both firmware projects detailing setup, build, and usage instructions.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>

* feat: enhance hardware peripheral support and documentation

- Added `Peripheral` trait implementation in `src/peripherals/` to manage hardware boards (STM32, RPi GPIO).
- Updated `AGENTS.md` to include new extension points for peripherals and their configuration.
- Introduced comprehensive documentation for adding boards and tools, including a quick start guide and supported boards.
- Enhanced `Cargo.toml` to include optional dependencies for PDF extraction and peripheral support.
- Created new datasheets for Arduino Uno, ESP32, and Nucleo-F401RE, detailing pin aliases and GPIO usage.
- Implemented new tools for hardware memory reading and board information retrieval in the agent loop.

This update significantly improves the integration and usability of hardware peripherals within the ZeroClaw framework.

* feat: add ZeroClaw firmware for ESP32 and Nucleo

* Introduced new firmware for ZeroClaw on ESP32 and Nucleo-F401RE, enabling JSON-over-serial communication for GPIO control.
* Added `zeroclaw-esp32` with support for commands like `gpio_read` and `gpio_write`, along with capabilities reporting.
* Implemented `zeroclaw-nucleo` firmware with similar functionality for STM32, ensuring compatibility with existing ZeroClaw protocols.
* Updated `.gitignore` to include new firmware targets and added necessary dependencies in `Cargo.toml` for both platforms.
* Created README files for both firmware projects detailing setup, build, and usage instructions.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>

* feat: enhance hardware peripheral support and documentation

- Added `Peripheral` trait implementation in `src/peripherals/` to manage hardware boards (STM32, RPi GPIO).
- Updated `AGENTS.md` to include new extension points for peripherals and their configuration.
- Introduced comprehensive documentation for adding boards and tools, including a quick start guide and supported boards.
- Enhanced `Cargo.toml` to include optional dependencies for PDF extraction and peripheral support.
- Created new datasheets for Arduino Uno, ESP32, and Nucleo-F401RE, detailing pin aliases and GPIO usage.
- Implemented new tools for hardware memory reading and board information retrieval in the agent loop.

This update significantly improves the integration and usability of hardware peripherals within the ZeroClaw framework.

* feat: Introduce hardware auto-discovery and expanded configuration options for agents, hardware, and security.

* chore: update dependencies and improve probe-rs integration

- Updated `Cargo.lock` to remove specific version constraints for several dependencies, including `zerocopy`, `syn`, and `strsim`, allowing for more flexibility in version resolution.
- Upgraded `bincode` and `bitfield` to their latest versions, enhancing serialization and memory management capabilities.
- Updated `Cargo.toml` to reflect the new version of `probe-rs` from `0.24` to `0.30`, improving hardware probing functionality.
- Refactored code in `src/hardware` and `src/tools` to utilize the new `SessionConfig` for session management in `probe-rs`, ensuring better compatibility and performance.
- Cleaned up documentation in `docs/datasheets/nucleo-f401re.md` by removing unnecessary lines.

* fix: apply cargo fmt

* docs: add hardware architecture diagram.

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 11:40:10 -05:00

143 lines
3.6 KiB
C++

/*
* ZeroClaw Arduino Uno Firmware
*
* Listens for JSON commands on Serial (115200 baud), executes gpio_read/gpio_write,
* responds with JSON. Compatible with ZeroClaw SerialPeripheral protocol.
*
* Protocol (newline-delimited JSON):
* Request: {"id":"1","cmd":"gpio_write","args":{"pin":13,"value":1}}
* Response: {"id":"1","ok":true,"result":"done"}
*
* Arduino Uno: Pin 13 has built-in LED. Digital pins 0-13 supported.
*
* 1. Open in Arduino IDE
* 2. Select Board: Arduino Uno
* 3. Select correct Port (Tools -> Port)
* 4. Upload
*/
#define BAUDRATE 115200
#define MAX_LINE 256
char lineBuf[MAX_LINE];
int lineLen = 0;
// Parse integer from JSON: "pin":13 or "value":1
int parseArg(const char* key, const char* json) {
char search[32];
snprintf(search, sizeof(search), "\"%s\":", key);
const char* p = strstr(json, search);
if (!p) return -1;
p += strlen(search);
return atoi(p);
}
// Extract "id" for response
void copyId(char* out, int outLen, const char* json) {
const char* p = strstr(json, "\"id\":\"");
if (!p) {
out[0] = '0';
out[1] = '\0';
return;
}
p += 6;
int i = 0;
while (i < outLen - 1 && *p && *p != '"') {
out[i++] = *p++;
}
out[i] = '\0';
}
// Check if cmd is present
bool hasCmd(const char* json, const char* cmd) {
char search[64];
snprintf(search, sizeof(search), "\"cmd\":\"%s\"", cmd);
return strstr(json, search) != NULL;
}
void handleLine(const char* line) {
char idBuf[16];
copyId(idBuf, sizeof(idBuf), line);
if (hasCmd(line, "ping")) {
Serial.print("{\"id\":\"");
Serial.print(idBuf);
Serial.println("\",\"ok\":true,\"result\":\"pong\"}");
return;
}
// Phase C: Dynamic discovery — report GPIO pins and LED pin
if (hasCmd(line, "capabilities")) {
Serial.print("{\"id\":\"");
Serial.print(idBuf);
Serial.print("\",\"ok\":true,\"result\":\"{\\\"gpio\\\":[0,1,2,3,4,5,6,7,8,9,10,11,12,13],\\\"led_pin\\\":13}\"}");
Serial.println();
return;
}
if (hasCmd(line, "gpio_read")) {
int pin = parseArg("pin", line);
if (pin < 0 || pin > 13) {
Serial.print("{\"id\":\"");
Serial.print(idBuf);
Serial.print("\",\"ok\":false,\"result\":\"\",\"error\":\"Invalid pin ");
Serial.print(pin);
Serial.println("\"}");
return;
}
pinMode(pin, INPUT);
int val = digitalRead(pin);
Serial.print("{\"id\":\"");
Serial.print(idBuf);
Serial.print("\",\"ok\":true,\"result\":\"");
Serial.print(val);
Serial.println("\"}");
return;
}
if (hasCmd(line, "gpio_write")) {
int pin = parseArg("pin", line);
int value = parseArg("value", line);
if (pin < 0 || pin > 13) {
Serial.print("{\"id\":\"");
Serial.print(idBuf);
Serial.print("\",\"ok\":false,\"result\":\"\",\"error\":\"Invalid pin ");
Serial.print(pin);
Serial.println("\"}");
return;
}
pinMode(pin, OUTPUT);
digitalWrite(pin, value ? HIGH : LOW);
Serial.print("{\"id\":\"");
Serial.print(idBuf);
Serial.println("\",\"ok\":true,\"result\":\"done\"}");
return;
}
// Unknown command
Serial.print("{\"id\":\"");
Serial.print(idBuf);
Serial.println("\",\"ok\":false,\"result\":\"\",\"error\":\"Unknown command\"}");
}
void setup() {
Serial.begin(BAUDRATE);
lineLen = 0;
}
void loop() {
while (Serial.available()) {
char c = Serial.read();
if (c == '\n' || c == '\r') {
if (lineLen > 0) {
lineBuf[lineLen] = '\0';
handleLine(lineBuf);
lineLen = 0;
}
} else if (lineLen < MAX_LINE - 1) {
lineBuf[lineLen++] = c;
} else {
lineLen = 0; // Overflow, discard
}
}
}