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;
uint8_t g_buffer[16];
void loop()
{
if (needToSend)
{
uint8_t buffer[16];
buffer[0] = 10;
buffer[1] = 20;
proto.write( buffer, 2 );
}
int length;
length = proto.read( g_buffer, sizeof(g_buffer), TINY_FLAG_NO_WAIT );
if ( length > 0 )
{
}
}
Variant 2
Second variant: with using special helper to pack the data being sent
Tiny::Proto proto;
uint8_t g_buffer[16];
Tiny::Packet packet(g_buffer, sizeof(g_buffer));
void loop()
{
if (needToSend)
{
uint8_t buffer[16];
Tiny::Packet packet(buffer, sizeof(buffer));
packet.put( "message" );
proto.write( packet );
}
int length;
length = proto.read( packet, TINY_FLAG_NO_WAIT );
if ( length > 0 )
{
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)
{
}
Tiny::ProtoHd proto(g_buffer, sizeof(g_buffer), onReceiveData);
void loop()
{
if (needToSend)
{
uint8_t buffer[16];
buffer[0] = 10;
buffer[1] = 20;
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)
{
Tiny::Packet packet(buffer, len);
char * str = packet.getString();
}
Tiny::ProtoHd proto(g_buffer, sizeof(g_buffer), onReceiveData);
void loop()
{
if (needToSend)
{
uint8_t buffer[16];
Tiny::Packet packet(buffer, sizeof(buffer));
packet.put( "message" );
proto.write( packet );
}
proto.run();
...
}
Stopping communication
Tiny::ProtoHd proto(g_buffer, sizeof(g_buffer), onReceiveData);
void loop()
{
...
if ( needToStop )
{
proto.end();
}
}