Tiny protocol  0.6.4
Tiny communication protocol for microcontrollers
Arduino related API (C++)

Table of Contents

This is Tiny protocol implementation for microcontrollers (Arduino, Stellaris).

Simple Tiny Protocol examples

Simple Tiny Protocol examples section is applicable only when working with SIMPLE_API and ADVANCED_API. If you want to work with Tiny Half Duplex protocol, please refere to Half Duplex Tiny Protocol examples section.

Initialization

Tiny::Proto proto;
void setup()
{
proto.beginToSerial();
}

Sending/Receiving data over protocol

Variant 1

First variant: without using any helpers to work with data

Tiny::Proto proto;
/* The buffer must be defined globally */
uint8_t g_buffer[16];
void loop()
{
if (needToSend)
{
/* define buffer, you want to use */
uint8_t buffer[16];
/* Prepare data you want to send here */
buffer[0] = 10;
buffer[1] = 20;
/* Send 2 bytes to remote side */
proto.write( buffer, 2 );
}
int length;
length = proto.read( g_buffer, sizeof(g_buffer), TINY_FLAG_NO_WAIT );
if ( length > 0 )
{
/* Parse your data received from remote side here. */
}
}

Variant 2

Second variant: with using special helper to pack the data being sent

Tiny::Proto proto;
/* The buffer must be defined globally */
uint8_t g_buffer[16];
Tiny::Packet packet(g_buffer, sizeof(g_buffer));
void loop()
{
if (needToSend)
{
/* define buffer, you want to use */
uint8_t buffer[16];
/* Create helper object to simplify packing of data being sent */
Tiny::Packet packet(buffer, sizeof(buffer));
/* Pack data you want to send here */
packet.put( "message" );
/* Send message to remote side */
proto.write( packet );
}
int length;
length = proto.read( packet, TINY_FLAG_NO_WAIT );
if ( length > 0 )
{
/* Parse your data received from remote side here. */
/* For example, read sent ealier in example above "message" */
char * str = packet.getString();
}
}

Stopping communication

Tiny::Proto proto;
void loop()
{
...
if ( needToStop )
{
proto.end();
}
}

Half Duplex Tiny Protocol examples

Half Duplex Tiny Protocol examples section is applicable when working with HALF_DUPLEX_API, SIMPLE_API and ADVANCED_API.

Initialization

uint8_t g_buffer[64];
void onReceiveData(uint8_t *buffer, int len)
{
}
Tiny::ProtoHd proto(g_buffer, sizeof(g_buffer), onReceiveData);
void setup()
{
proto.beginToSerial();
}

Sending/Receiving data over protocol

Variant 1

First variant: without using any helpers to work with data

uint8_t g_buffer[64];
void onReceiveData(uint8_t *buffer, int len)
{
/* Parse your received data from remote side here. */
}
Tiny::ProtoHd proto(g_buffer, sizeof(g_buffer), onReceiveData);
void loop()
{
if (needToSend)
{
/* define buffer, you want to use */
uint8_t buffer[16];
/* Prepare data you want to send here */
buffer[0] = 10;
buffer[1] = 20;
/* Send 2 bytes to remote side */
proto.write( buffer, 2 );
}
proto.run();
...
}

Variant 2

Second variant: with using special helper to pack the data being sent

uint8_t g_buffer[64];
void onReceiveData(uint8_t *buffer, int len)
{
/* Parse your received data here. */
/* For example, read sent ealier in example above "message" */
Tiny::Packet packet(buffer, len);
char * str = packet.getString();
}
Tiny::ProtoHd proto(g_buffer, sizeof(g_buffer), onReceiveData);
void loop()
{
if (needToSend)
{
/* define buffer, you want to use */
uint8_t buffer[16];
/* Create helper object to simplify packing of data being sent */
Tiny::Packet packet(buffer, sizeof(buffer));
/* Pack data you want to send here */
packet.put( "message" );
/* Send message to remote side */
proto.write( packet );
}
proto.run();
...
}

Stopping communication

Tiny::ProtoHd proto(g_buffer, sizeof(g_buffer), onReceiveData);
void loop()
{
...
if ( needToStop )
{
proto.end();
}
}