tinyproto
Arduino API (C++)

Table of Contents

This page demonstrates how to use TinyProto on Arduino boards using the C++ API.

Light Protocol Examples

The Light protocol (Tiny light protocol API functions) provides simple SLIP-like framing with optional CRC and very low memory consumption (~800 bytes of flash). It does not provide acknowledgments or retransmission — use it when simplicity and low overhead are more important than guaranteed delivery.

Initialization

void setup()
{
Serial.begin(115200);
// Bind protocol to Serial port
proto.beginToSerial();
}

Sending and Receiving Data

Raw Buffers

uint8_t rx_buffer[64];
void setup()
{
Serial.begin(115200);
proto.beginToSerial();
}
void loop()
{
// Send raw bytes
uint8_t tx_data[] = {0x01, 0x02, 0x03};
proto.write((char *)tx_data, sizeof(tx_data));
// Read incoming frame (non-blocking)
int len = proto.read((char *)rx_buffer, sizeof(rx_buffer));
if (len > 0) {
// Process received data
}
}

Using Packets

#include "TinyPacket.h"
tinyproto::Packet<64> rx_packet;
void setup()
{
Serial.begin(115200);
proto.beginToSerial();
}
void loop()
{
// Send a message using Packet helper
tinyproto::Packet<64> tx_packet;
tx_packet.put("Hello");
proto.write(tx_packet);
// Receive
int len = proto.read(rx_packet);
if (len > 0) {
const char *msg = (const char *)rx_packet.data();
// Process message
}
}

Stopping Communication

void stopProtocol()
{
proto.end();
}

Full-Duplex Protocol Examples

The Full-Duplex protocol (Tiny Full Duplex API functions) provides reliable communication with automatic retransmission, sliding window, and CRC error detection. It supports both ABM (peer-to-peer) and NRM (primary/secondary) modes.

ABM Mode (Peer-to-Peer)

#include "TinyProtocolFd.h"
// Static 512-byte buffer — no heap allocation
void onReceive(void *udata, uint8_t addr, tinyproto::IPacket &pkt)
{
// Process received message
Serial.write(pkt.data(), pkt.size());
}
void setup()
{
Serial.begin(115200);
proto.setReceiveCallback(onReceive);
proto.setWindowSize(3); // sliding window of 3 frames
proto.enableCrc16(); // FCS-16 error detection
proto.begin();
}
void loop()
{
// Feed incoming serial bytes to the protocol
if (Serial.available()) {
uint8_t byte = Serial.read();
proto.run_rx(&byte, 1);
}
// Get protocol bytes and send to serial
uint8_t tx_byte;
if (proto.run_tx(&tx_byte, 1) == 1) {
Serial.write(tx_byte);
}
}

To send application data:

void sendMessage()
{
const char *msg = "Hello from Arduino!";
proto.write(msg, strlen(msg) + 1);
}

Dynamic Buffer (Powerful MCUs)

On boards with more RAM (Due, Zero, ESP32, Teensy), you can use dynamic allocation:

#include "TinyProtocolFd.h"
// Dynamic allocation — 2048 bytes determined at runtime
tinyproto::FdD proto(2048);
void setup()
{
Serial.begin(115200);
proto.setWindowSize(7);
proto.enableCrc32();
proto.begin();
}

Connection Events

You can monitor connection and disconnection events:

void onConnect(void *udata, uint8_t addr, bool connected)
{
if (connected)
Serial.println("Connected!");
else
Serial.println("Disconnected!");
}
void setup()
{
// ... other init ...
proto.setConnectEventCallback(onConnect);
proto.begin();
}